Skip to content

Commit

Permalink
fix: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomzemp committed Dec 11, 2023
1 parent 57ce8cf commit 875cf3d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const getRestrictedOrgUnits = (orgUnits, orgUnitType, currentUser) => {

// Try the requested orgUnitType first and use currentUser.organisationUnits as fallback
const availableOrgUnits =
currentUser[orgUnitType].length > 0
currentUser[orgUnitType]?.length > 0
? currentUser[orgUnitType]
: currentUser.organisationUnits

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { getRestrictedOrgUnits } from './getRestrictedOrgUnits'

Check failure on line 1 in src/components/SearchableOrgUnitTree/AsyncAutoComplete/getRestrictedOrgUnits.test.js

View workflow job for this annotation

GitHub Actions / lint

Missing file extension "js" for "./getRestrictedOrgUnits"

describe('getRestrictedOrgUnits', () => {
const orgUnits = [
{ id: 'grapefruit', ancestors: [{ id: 'pomelo' }] },
{ id: 'tangerine', ancestors: [{ id: 'orange' }, { id: 'pomelo' }] },
{ id: 'lemon', ancestors: [] },
]
const orgUnitType = 'fruit'

it('returns all organisation units for super users', () => {
const currentUser = { authorities: ['ALL'] }
const validOrgUnits = getRestrictedOrgUnits(
orgUnits,
orgUnitType,
currentUser
)
expect(validOrgUnits).toEqual(orgUnits)
})
it('filters based on specified type when available', () => {
const currentUser = {
authorities: [],
fruit: [{ id: 'grapefruit' }, { id: 'kiwi' }, { id: 'mango' }],
}
const validOrgUnits = getRestrictedOrgUnits(
orgUnits,
orgUnitType,
currentUser
)
expect(validOrgUnits.map(({ id }) => id)).toEqual(['grapefruit'])
})
it('filters based on default organisationUnits specified type is not available', () => {
const currentUser = {
authorities: [],
organisationUnits: [
{ id: 'tangerine' },
{ id: 'soursop' },
{ id: 'apricot' },
],
}
const validOrgUnits = getRestrictedOrgUnits(
orgUnits,
orgUnitType,
currentUser
)
expect(validOrgUnits.map(({ id }) => id)).toEqual(['tangerine'])
})
it('checks ancestors when filtering', () => {
const currentUser = {
authorities: [],
organisationUnits: [{ id: 'pomelo' }],
}
const validOrgUnits = getRestrictedOrgUnits(
orgUnits,
orgUnitType,
currentUser
)
expect(validOrgUnits.map(({ id }) => id)).toEqual([
'grapefruit',
'tangerine',
])
})
it('returns nothing, if user is not super user and has no org units', () => {
const currentUser = { authorities: [], organisationUnits: [] }
const validOrgUnits = getRestrictedOrgUnits(
orgUnits,
orgUnitType,
currentUser
)
expect(validOrgUnits.length).toEqual(0)
})
})

0 comments on commit 875cf3d

Please sign in to comment.