-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add more jest tests #333
Open
Mohammer5
wants to merge
8
commits into
master
Choose a base branch
from
DHIS2-14384_DHIS2-14383_DHIS2-14382_DHIS2-14381
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
00c6423
test(build validation result): cover with tests (DHIS2-14384)
Mohammer5 a5ee073
refactor(use data value set): extract mapDataValuesToFormInitialValue…
Mohammer5 ad27add
test(map data values to form initial values): cover with tests (DHIS2…
Mohammer5 3755c0b
refactor(use is valid selection): simplify missing categoryCombo & da…
Mohammer5 2254297
test(use is valid selection): cover with tests (DHIS2-14382)
Mohammer5 f6739d3
refactor(update lock status from backend): move to its own file
Mohammer5 3b00623
refactor(use check lock status): change org unit checker fn name to r…
Mohammer5 7bb59c2
test(use check lock status): cover with tests (DHIS2-14381j)
Mohammer5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
export { | ||
useCheckLockStatus, | ||
updateLockStatusFromBackend, | ||
} from './use-check-lock-status.js' | ||
export { LockedContext } from './locked-info-context.js' | ||
export { LockedProvider } from './locked-info-provider.js' | ||
export { LockedStates } from './locked-states.js' | ||
export { useCheckLockStatus } from './use-check-lock-status.js' | ||
export { useLockedContext } from './use-locked-context.js' | ||
export { updateLockStatusFromBackend } from './update-lock-status-from-backend.js' |
32 changes: 32 additions & 0 deletions
32
src/shared/locked-status/update-lock-status-from-backend.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { LockedStates } from './locked-states.js' | ||
|
||
export const updateLockStatusFromBackend = ( | ||
frontEndLockStatus, | ||
backEndLockStatus, | ||
setLockStatus | ||
) => { | ||
// if the lock status is APPROVED, set to approved | ||
if (backEndLockStatus === 'APPROVED') { | ||
setLockStatus(LockedStates.LOCKED_APPROVED) | ||
return | ||
} | ||
|
||
// if the lock status is LOCKED, this is locked due to expiry days | ||
if (backEndLockStatus === 'LOCKED') { | ||
setLockStatus(LockedStates.LOCKED_EXPIRY_DAYS) | ||
return | ||
} | ||
|
||
// a lock status of 'OPEN' from the backend could mean either that the form is open OR | ||
// that the form should be locked due to data input period, OR | ||
// that the form should be locked because an organisation unit is out of range, SO | ||
// set to OPEN unless frontend check has identified that data input period as out-of-bounds | ||
if ( | ||
![ | ||
LockedStates.LOCKED_DATA_INPUT_PERIOD, | ||
LockedStates.LOCKED_ORGANISATION_UNIT, | ||
].includes(frontEndLockStatus) | ||
) { | ||
setLockStatus(LockedStates.OPEN) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import { renderHook } from '@testing-library/react-hooks' | ||
import { useClientServerDate } from '../date/index.js' | ||
import { useMetadata } from '../metadata/index.js' | ||
import { | ||
usePeriodId, | ||
useDataSetId, | ||
useOrgUnitId, | ||
} from '../use-context-selection/index.js' | ||
import { useDataValueSet } from '../use-data-value-set/use-data-value-set.js' | ||
import { useOrgUnit } from '../use-org-unit/use-organisation-unit.js' | ||
import { LockedStates } from './locked-states.js' | ||
import { useCheckLockStatus } from './use-check-lock-status.js' | ||
import { useLockedContext } from './use-locked-context.js' | ||
|
||
jest.mock('../date/use-client-server-date.js', () => ({ | ||
__esModule: true, | ||
default: jest.fn(), | ||
})) | ||
|
||
jest.mock('../metadata/use-metadata.js', () => ({ | ||
useMetadata: jest.fn(), | ||
})) | ||
|
||
jest.mock('../use-context-selection/use-context-selection.js', () => ({ | ||
useDataSetId: jest.fn(), | ||
useOrgUnitId: jest.fn(), | ||
usePeriodId: jest.fn(), | ||
})) | ||
|
||
jest.mock('./use-locked-context.js', () => ({ | ||
useLockedContext: jest.fn(), | ||
})) | ||
|
||
jest.mock('../use-data-value-set/use-data-value-set.js', () => ({ | ||
useDataValueSet: jest.fn(), | ||
})) | ||
|
||
jest.mock('../use-org-unit/use-organisation-unit.js', () => ({ | ||
useOrgUnit: jest.fn(), | ||
})) | ||
|
||
describe('useCheckLockStatus', () => { | ||
useDataSetId.mockImplementation(() => ['dataSet1']) | ||
useOrgUnitId.mockImplementation(() => ['orgUnit1']) | ||
usePeriodId.mockImplementation(() => ['202301']) | ||
useOrgUnit.mockImplementation(() => ({ | ||
data: { | ||
id: 'orgUnit1', | ||
displayName: 'Org unit 1', | ||
path: '/orgUnit1', | ||
openingDate: '2015-01-01', | ||
closedDate: '2023-01-18', | ||
}, | ||
})) | ||
useMetadata.mockImplementation(() => ({ | ||
data: { | ||
dataSets: { | ||
dataSet1: { | ||
id: 'dataSet1', | ||
dataInputPeriods: [ | ||
{ | ||
period: { id: '202301', name: '202301' }, | ||
// This defines in which time frame you're allowed | ||
// to enter data for this period | ||
openingDate: '2022-07-01T00:00:00.000', | ||
closingDate: '2023-01-28T00:00:00.000', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
})) | ||
useDataValueSet.mockImplementation(() => ({ | ||
loading: false, | ||
error: null, | ||
data: null, | ||
})) | ||
|
||
const setLockStatus = jest.fn() | ||
useLockedContext.mockImplementation(() => ({ setLockStatus })) | ||
|
||
afterEach(() => { | ||
setLockStatus.mockClear() | ||
}) | ||
|
||
it('should set the lock status to LOCKED_DATA_INPUT_PERIOD', () => { | ||
useClientServerDate.mockImplementation(() => ({ | ||
serverDate: new Date('2023-01-30'), | ||
clientDate: new Date('2023-01-30'), | ||
})) | ||
|
||
renderHook(useCheckLockStatus) | ||
|
||
expect(setLockStatus).toHaveBeenCalledWith( | ||
LockedStates.LOCKED_DATA_INPUT_PERIOD | ||
) | ||
}) | ||
|
||
it('should set the lock status to LOCKED_ORGANISATION_UNIT', () => { | ||
useClientServerDate.mockImplementation(() => ({ | ||
serverDate: new Date('2023-01-22'), | ||
clientDate: new Date('2023-01-22'), | ||
})) | ||
|
||
renderHook(useCheckLockStatus) | ||
|
||
expect(setLockStatus).toHaveBeenCalledWith( | ||
LockedStates.LOCKED_ORGANISATION_UNIT | ||
) | ||
}) | ||
|
||
it('should set the lock status to OPEN', () => { | ||
usePeriodId.mockImplementation(() => ['202301']) | ||
|
||
useOrgUnit.mockImplementation(() => ({ | ||
data: { | ||
id: 'orgUnit1', | ||
displayName: 'Org unit 1', | ||
path: '/orgUnit1', | ||
openingDate: '2015-01-01', | ||
closedDate: '2023-02-01', | ||
}, | ||
})) | ||
|
||
useMetadata.mockImplementation(() => ({ | ||
data: { | ||
dataSets: { | ||
dataSet1: { | ||
id: 'dataSet1', | ||
dataInputPeriods: [ | ||
{ | ||
period: { id: '202301', name: '202301' }, | ||
openingDate: '2022-07-01T00:00:00.000', | ||
closingDate: '2023-01-31T00:00:00.000', | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
})) | ||
|
||
useClientServerDate.mockImplementation(() => ({ | ||
serverDate: new Date('2022-12-10'), | ||
clientDate: new Date('2022-12-10'), | ||
})) | ||
|
||
renderHook(useCheckLockStatus) | ||
|
||
expect(setLockStatus).toHaveBeenCalledWith(LockedStates.OPEN) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why isn't
isOrgUnitLocked
good enough? This starts to be a bit too long imo. If it's used for other things other thanlockStatus
its fine, but if its only for lockstatus, I think we should keep it. It is very descriptive of what it does though.isOrgunitLocked
describes what it's used for, not the implementation details of what makes it locked, which could in theory change, and you would need another function.