Skip to content

Commit

Permalink
test: add org unit dimension component test
Browse files Browse the repository at this point in the history
  • Loading branch information
janhenrikoverland committed Nov 22, 2024
1 parent 6e62152 commit aacb0f7
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/components/OrgUnitDimension/__tests__/OrgUnitDimension.spec.js
Original file line number Diff line number Diff line change
@@ -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(<OrgUnitDimension {...props} />)
}
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: [],
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`The OrgUnitDimension component matches the snapshot 1`] = `
<div
className="container"
>
<div
className="userOrgUnitsWrapper"
>
<Checkbox
checked={false}
dataTest="dhis2-uicore-checkbox"
dense={true}
indeterminate={false}
label="User organisation unit"
onChange={[Function]}
/>
<Checkbox
checked={false}
dataTest="dhis2-uicore-checkbox"
dense={true}
indeterminate={false}
label="User sub-units"
onChange={[Function]}
/>
<Checkbox
checked={false}
dataTest="dhis2-uicore-checkbox"
dense={true}
indeterminate={false}
label="User sub-x2-units"
onChange={[Function]}
/>
</div>
<div
className="orgUnitTreeWrapper"
>
<OrganisationUnitTree
dataTest="org-unit-tree"
filter={Array []}
highlighted={Array []}
initiallyExpanded={Array []}
onChange={[Function]}
renderNodeLabel={[Function]}
roots={Array []}
selected={Array []}
/>
</div>
<div
className="selectsWrapper"
>
<MultiSelect
dataTest="org-unit-level-select"
dense={true}
loading={true}
onChange={[Function]}
placeholder="Select a level"
selected={Array []}
/>
<MultiSelect
dataTest="org-unit-group-select"
dense={true}
loading={true}
onChange={[Function]}
placeholder="Select a group"
selected={Array []}
/>
</div>
<div
className="summaryWrapper"
>
<span
className="summaryText"
>
Nothing selected
</span>
<div
className="deselectButton"
>
<Button
dataTest="dhis2-uicore-button"
disabled={true}
onClick={[Function]}
secondary={true}
small={true}
type="button"
>
Deselect all
</Button>
</div>
</div>
<style />
</div>
`;

0 comments on commit aacb0f7

Please sign in to comment.