Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle sections without data elements [DHIS2-15883] #359

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading