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 + +
+ +
+
+