From f58c708af214f13147e5c95ede8545fc5c03bc57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Henrik=20=C3=98verland?= Date: Fri, 22 Nov 2024 14:38:23 +0100 Subject: [PATCH] fix: enable ou tree and levels/groups with user orgunits (DHIS2-18066) (#1702) * fix: do not disable tree or levels/groups for user ou * fix: Grab name from 'items' when necessary * test: add unit test for getOuLevelAndGroupText * test: add org unit dimension component test * test: remove unnecessary mocks from OrgUnitDimension test --- .../OrgUnitDimension/OrgUnitDimension.js | 16 +--- .../__tests__/OrgUnitDimension.spec.js | 59 ++++++++++++ .../OrgUnitDimension.spec.js.snap | 94 +++++++++++++++++++ .../PivotTable/PivotTableTitleRows.js | 1 + .../__tests__/getOuLevelAndGroupText.spec.js | 11 +++ src/modules/getOuLevelAndGroupText.js | 4 +- 6 files changed, 170 insertions(+), 15 deletions(-) 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/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, })} > diff --git a/src/components/OrgUnitDimension/__tests__/OrgUnitDimension.spec.js b/src/components/OrgUnitDimension/__tests__/OrgUnitDimension.spec.js new file mode 100644 index 000000000..9f85b035b --- /dev/null +++ b/src/components/OrgUnitDimension/__tests__/OrgUnitDimension.spec.js @@ -0,0 +1,59 @@ +import { shallow } from 'enzyme' +import React from 'react' +import OrgUnitDimension from '../OrgUnitDimension.js' + +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 + +
+ +
+
+