From 8ea9a96ad053c478be811a454504485876af2670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Henrik=20=C3=98verland?= Date: Mon, 23 Sep 2024 12:00:14 +0200 Subject: [PATCH 1/5] fix: do not disable tree or levels/groups for user ou --- .../OrgUnitDimension/OrgUnitDimension.js | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/src/components/OrgUnitDimension/OrgUnitDimension.js b/src/components/OrgUnitDimension/OrgUnitDimension.js index 5d2d7115a..9ed1078de 100644 --- a/src/components/OrgUnitDimension/OrgUnitDimension.js +++ b/src/components/OrgUnitDimension/OrgUnitDimension.js @@ -51,10 +51,7 @@ const OrgUnitDimension = ({ let result = [...selected] if (checked && DYNAMIC_ORG_UNITS.includes(id)) { - result = [ - ...result.filter((item) => DYNAMIC_ORG_UNITS.includes(item.id)), - { id, displayName }, - ] + result = [...result, { id, displayName }] } else if (checked) { result.push({ id, path, name: displayName }) } else { @@ -236,13 +233,7 @@ const OrgUnitDimension = ({ /> )} -
- DYNAMIC_ORG_UNITS.includes(item.id) - ), - })} - > +
- DYNAMIC_ORG_UNITS.includes(item.id) - ), hidden: hideLevelSelect && hideGroupSelect, })} > From dfc00b2aa04842b1400b62a7da245c55aca65c8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Henrik=20=C3=98verland?= Date: Mon, 23 Sep 2024 12:32:15 +0200 Subject: [PATCH 2/5] fix: Grab name from 'items' when necessary --- src/components/PivotTable/PivotTableTitleRows.js | 1 + src/modules/getOuLevelAndGroupText.js | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/components/PivotTable/PivotTableTitleRows.js b/src/components/PivotTable/PivotTableTitleRows.js index 8a3439c9d..b29439261 100644 --- a/src/components/PivotTable/PivotTableTitleRows.js +++ b/src/components/PivotTable/PivotTableTitleRows.js @@ -6,6 +6,7 @@ import { PivotTableTitleRow } from './PivotTableTitleRow.js' export const PivotTableTitleRows = ({ clippingResult, width }) => { const engine = usePivotTableEngine() + return ( <> {engine.options.title ? ( diff --git a/src/modules/getOuLevelAndGroupText.js b/src/modules/getOuLevelAndGroupText.js index f92e612c2..a7622e28f 100644 --- a/src/modules/getOuLevelAndGroupText.js +++ b/src/modules/getOuLevelAndGroupText.js @@ -28,7 +28,9 @@ export const getOuLevelAndGroupText = (filter, metaData) => { const getLevelAndGroupText = (items, metaData, isLevel) => { const getNameFromMetadata = (id) => - metaData.items[id] ? metaData.items[id].name : id + metaData.items[id] + ? metaData.items[id].name + : items.find((item) => item.id === id)?.name || id const dynamicOuItems = items.filter((item) => isLevel From 6e6215213dbaabdb0860798f4f3f837f9099ef0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Henrik=20=C3=98verland?= Date: Fri, 22 Nov 2024 10:53:28 +0100 Subject: [PATCH 3/5] test: add unit test for getOuLevelAndGroupText --- src/modules/__tests__/getOuLevelAndGroupText.spec.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/modules/__tests__/getOuLevelAndGroupText.spec.js b/src/modules/__tests__/getOuLevelAndGroupText.spec.js index a57a07539..21cb7319b 100644 --- a/src/modules/__tests__/getOuLevelAndGroupText.spec.js +++ b/src/modules/__tests__/getOuLevelAndGroupText.spec.js @@ -61,4 +61,15 @@ describe('getOuLevelAndGroupText', () => { 'Fruit and Veggies groups - Second floor levels' ) }) + + it('grabs name for user orgunits from items when not present in metaData', () => { + filter.items = [ + { id: 'USER_ORGUNIT', name: 'User organisation unit' }, + { id: 'LEVEL-2nd-floor' }, + ] + + expect(getOuLevelAndGroupText(filter, metaData)).toEqual( + 'Second floor levels in User organisation unit' + ) + }) }) From aacb0f72eda64adc488b44e26776a723f327399f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Henrik=20=C3=98verland?= Date: Fri, 22 Nov 2024 11:34:52 +0100 Subject: [PATCH 4/5] test: add org unit dimension component test --- .../__tests__/OrgUnitDimension.spec.js | 76 +++++++++++++++ .../OrgUnitDimension.spec.js.snap | 94 +++++++++++++++++++ 2 files changed, 170 insertions(+) create mode 100644 src/components/OrgUnitDimension/__tests__/OrgUnitDimension.spec.js create mode 100644 src/components/OrgUnitDimension/__tests__/__snapshots__/OrgUnitDimension.spec.js.snap diff --git a/src/components/OrgUnitDimension/__tests__/OrgUnitDimension.spec.js b/src/components/OrgUnitDimension/__tests__/OrgUnitDimension.spec.js new file mode 100644 index 000000000..de54b1d68 --- /dev/null +++ b/src/components/OrgUnitDimension/__tests__/OrgUnitDimension.spec.js @@ -0,0 +1,76 @@ +import { shallow } from 'enzyme' +import React from 'react' +import OrgUnitDimension from '../OrgUnitDimension.js' + +jest.mock('@dhis2/app-runtime', () => ({ + useDataEngine: () => ({ + query: jest.fn(), + }), +})) + +jest.mock('../../../api/organisationUnits.js', () => ({ + apiFetchOrganisationUnitGroups: jest.fn().mockResolvedValue([]), + apiFetchOrganisationUnitLevels: jest.fn().mockResolvedValue([]), +})) + +jest.mock('../../../locales/index.js', () => ({ + t: (key) => key, +})) + +afterEach(jest.clearAllMocks) + +describe('The OrgUnitDimension component', () => { + let props + let shallowOrgUnitDimension + + const getWrapper = () => { + if (!shallowOrgUnitDimension) { + shallowOrgUnitDimension = shallow() + } + return shallowOrgUnitDimension + } + + beforeEach(() => { + props = { + roots: [], + selected: [], + onSelect: jest.fn(), + hideGroupSelect: false, + hideLevelSelect: false, + hideUserOrgUnits: false, + warning: '', + } + shallowOrgUnitDimension = undefined + }) + + it('matches the snapshot', () => { + const wrapper = getWrapper() + expect(wrapper).toMatchSnapshot() + }) + + it('calls onSelect when an organisation unit is selected', () => { + const wrapper = getWrapper() + const orgUnitTree = wrapper.find('OrganisationUnitTree') + const testOrgUnit = { + id: 'testId', + path: '/testPath', + displayName: 'Test Org Unit', + checked: true, + } + orgUnitTree.props().onChange(testOrgUnit) + expect(props.onSelect).toHaveBeenCalledWith({ + dimensionId: 'ou', + items: [{ id: 'testId', path: '/testPath', name: 'Test Org Unit' }], + }) + }) + + it('calls onSelect with an empty array when selection is cleared', () => { + const wrapper = getWrapper() + const deselectButton = wrapper.find('Button[onClick]') + deselectButton.simulate('click') + expect(props.onSelect).toHaveBeenCalledWith({ + dimensionId: 'ou', + items: [], + }) + }) +}) diff --git a/src/components/OrgUnitDimension/__tests__/__snapshots__/OrgUnitDimension.spec.js.snap b/src/components/OrgUnitDimension/__tests__/__snapshots__/OrgUnitDimension.spec.js.snap new file mode 100644 index 000000000..1faa60b95 --- /dev/null +++ b/src/components/OrgUnitDimension/__tests__/__snapshots__/OrgUnitDimension.spec.js.snap @@ -0,0 +1,94 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`The OrgUnitDimension component matches the snapshot 1`] = ` +
+
+ + + +
+
+ +
+
+ + +
+
+ + Nothing selected + +
+ +
+
+