Skip to content

Commit

Permalink
fix: handle sections without data elements [DHIS2-15883] (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp authored Oct 5, 2023
1 parent f80d67c commit 83c062e
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { IndicatorTableCell } from './indicator-table-cell.js'
export const IndicatorsTableBody = ({
indicators,
maxColumnsInSection,
renderColumnTotals,
renderRowTotals,
filterText,
globalFilterText,
}) => {
Expand All @@ -28,8 +28,8 @@ export const IndicatorsTableBody = ({
)
})
const hiddenItemsCount = indicators.length - filteredIndicators.length
const nrOfPadColumns = maxColumnsInSection - (renderColumnTotals ? 0 : 1)
const padColumns = Array(nrOfPadColumns).fill(0)
const nrOfPadColumns = maxColumnsInSection - (renderRowTotals ? 0 : 1)
const padColumns = nrOfPadColumns > 0 ? Array(nrOfPadColumns).fill(0) : []

return (
<TableBody>
Expand All @@ -50,7 +50,10 @@ export const IndicatorsTableBody = ({

{filteredIndicators.map((indicator) => {
return (
<TableRow key={indicator.id}>
<TableRow
key={indicator.id}
dataTest="dhis2-dataentry-indicatorstablerow"
>
<TableCell className={cx(styles.dataElementName)}>
{indicator.displayFormName}
</TableCell>
Expand All @@ -64,6 +67,7 @@ export const IndicatorsTableBody = ({
<TableCell
key={i}
className={styles.paddingCell}
dataTest="dhis2-dataentry-indicatorspaddingcell"
></TableCell>
))}
</TableRow>
Expand All @@ -88,5 +92,5 @@ IndicatorsTableBody.propTypes = {
filterText: PropTypes.string,
globalFilterText: PropTypes.string,
maxColumnsInSection: PropTypes.number,
renderColumnTotals: PropTypes.bool,
renderRowTotals: PropTypes.bool,
}
127 changes: 127 additions & 0 deletions src/data-workspace/indicators-table-body/indicators-table-body.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { getAllByTestId, queryAllByTestId } from '@testing-library/react'
import React from 'react'
import { render } from '../../test-utils/index.js'
import { FinalFormWrapper } from '../final-form-wrapper.js'
import { IndicatorsTableBody } from './indicators-table-body.js'

const tableIndicators = [
{
name: 'indieGator',
displayFormName: "I'm an indicator!",
decimals: 2,
indicatorType: {
factor: 1,
},
numerator: '#{fakeUID1}',
denominator: '#{fakeUID2}',
id: 'indicator1',
},
]

describe('<IndicatorsTableBody />', () => {
it('should not render padding cells if maxColumnsInSection=0', () => {
const result = render(
<IndicatorsTableBody
indicators={tableIndicators}
maxColumnsInSection={0}
/>,
{
wrapper: ({ children }) => (
<FinalFormWrapper dataValueSet={{}}>
{children}
</FinalFormWrapper>
),
}
)

const inputRows = result.getAllByTestId(
'dhis2-dataentry-indicatorstablerow'
)

const cellsInFirstRow = queryAllByTestId(
inputRows[0],
'dhis2-dataentry-indicatorspaddingcell'
)
expect(cellsInFirstRow.length).toBe(0)
})

it('should not render padding cells if maxColumnsInSection=-Infinity', () => {
const result = render(
<IndicatorsTableBody
indicators={tableIndicators}
maxColumnsInSection={-Infinity}
/>,
{
wrapper: ({ children }) => (
<FinalFormWrapper dataValueSet={{}}>
{children}
</FinalFormWrapper>
),
}
)

const inputRows = result.getAllByTestId(
'dhis2-dataentry-indicatorstablerow'
)
expect(inputRows.length).toBe(1)

const cellsInFirstRow = queryAllByTestId(
inputRows[0],
'dhis2-dataentry-indicatorspaddingcell'
)
expect(cellsInFirstRow.length).toBe(0)
})

it('should render 3 padding cells if 1 indicator and maxColumnsInSection=4', () => {
const result = render(
<IndicatorsTableBody
indicators={tableIndicators}
maxColumnsInSection={4}
/>,
{
wrapper: ({ children }) => (
<FinalFormWrapper dataValueSet={{}}>
{children}
</FinalFormWrapper>
),
}
)

const inputRows = result.getAllByTestId(
'dhis2-dataentry-indicatorstablerow'
)

const cellsInFirstRow = getAllByTestId(
inputRows[0],
'dhis2-dataentry-indicatorspaddingcell'
)
expect(cellsInFirstRow.length).toBe(3)
})

it('should render 4 padding cells if 1 indicator, maxColumnsInSection=4, and row totals displayed', () => {
const result = render(
<IndicatorsTableBody
indicators={tableIndicators}
maxColumnsInSection={4}
renderRowTotals={true}
/>,
{
wrapper: ({ children }) => (
<FinalFormWrapper dataValueSet={{}}>
{children}
</FinalFormWrapper>
),
}
)

const inputRows = result.getAllByTestId(
'dhis2-dataentry-indicatorstablerow'
)

const cellsInFirstRow = getAllByTestId(
inputRows[0],
'dhis2-dataentry-indicatorspaddingcell'
)
expect(cellsInFirstRow.length).toBe(4)
})
})
5 changes: 4 additions & 1 deletion src/data-workspace/section-form/section.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ export function SectionFormSection({ section, dataSetId, globalFilterText }) {
: selectors.getGroupedDataElementsByCatCombo(data, dataElements)

const maxColumnsInSection = useMemo(() => {
if (groupedDataElements.length === 0) {
return 0
}
const groupedTotalColumns = groupedDataElements.map((grp) =>
selectors.getNrOfColumnsInCategoryCombo(data, grp.categoryCombo.id)
)
Expand Down Expand Up @@ -117,7 +120,7 @@ export function SectionFormSection({ section, dataSetId, globalFilterText }) {
{indicators.length > 0 && (
<IndicatorsTableBody
indicators={indicators}
renderColumnTotals={section.showColumnTotals}
renderRowTotals={section.showRowTotals}
maxColumnsInSection={maxColumnsInSection}
filterText={filterText}
globalFilterText={globalFilterText}
Expand Down

1 comment on commit 83c062e

@dhis2-bot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.