-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Converting machine states to version 5 of xstate
- Loading branch information
Showing
4 changed files
with
198 additions
and
97 deletions.
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
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,71 +1,124 @@ | ||
import { interpret } from 'xstate'; | ||
import { createActor } from 'xstate'; | ||
import validationConstants from '../validationConstants'; | ||
import { validationMachine, initialContext } from '../ValidationMachine'; | ||
|
||
// Create a date that will be valid for all tests | ||
const today = new Date(); | ||
const lastAllowedDate = new Date(today.getFullYear(), today.getMonth(), today.getDate()); | ||
const firstAllowedDate = new Date(2022, 0, 1); | ||
|
||
const currentContext = { | ||
startDate: '2022-01-09', | ||
endDate: '2022-01-10', | ||
lastAllowedDate: new Date(), | ||
firstAllowedDate: new Date(2022, 0, 1), | ||
lastAllowedDate, | ||
firstAllowedDate, | ||
}; | ||
|
||
describe('Validation Machine', () => { | ||
let validateService; | ||
beforeAll(() => { | ||
validateService = interpret( | ||
validationMachine.withContext({ ...initialContext, ...currentContext }) | ||
); | ||
validateService.start(); | ||
let validateActor; | ||
|
||
beforeEach(() => { | ||
// Initialize with null dates first | ||
validateActor = createActor(validationMachine, { | ||
input: { | ||
...initialContext, | ||
lastAllowedDate, | ||
firstAllowedDate | ||
} | ||
}).start(); | ||
|
||
// Then send the actual dates | ||
validateActor.send({ | ||
type: 'REVALIDATE', | ||
startDate: currentContext.startDate, | ||
endDate: currentContext.endDate | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
validateActor.stop(); | ||
}); | ||
|
||
it('validation machine should be in success state when given correct props', async () => { | ||
expect(validateService._state.value).toEqual('success'); | ||
it('validation machine should be in success state when given correct props', () => { | ||
const snapshot = validateActor.getSnapshot(); | ||
expect(snapshot.value).toEqual('success'); | ||
}); | ||
|
||
it('returns startDateInvalid error message when start date is malformed', async () => { | ||
validateService.send('REVALIDATE', { startDate: 'aaaaaaa' }); | ||
expect(validateService._state.value).toEqual('failure'); | ||
expect(validateService._state.context.startDateInvalid).toEqual(validationConstants.MALFORMED); | ||
expect(validateService._state.context.endDateInvalid).toBeFalsy(); | ||
it('returns startDateInvalid error message when start date is malformed', () => { | ||
validateActor.send({ | ||
type: 'REVALIDATE', | ||
startDate: 'aaaaaaa', | ||
endDate: currentContext.endDate | ||
}); | ||
const snapshot = validateActor.getSnapshot(); | ||
expect(snapshot.value).toEqual('failure'); | ||
expect(snapshot.context.startDateInvalid).toEqual(validationConstants.MALFORMED); | ||
expect(snapshot.context.endDateInvalid).toBeFalsy(); | ||
}); | ||
|
||
it('returns endDateInvalid error message when end date is malformed', async () => { | ||
validateService.send('REVALIDATE', { startDate: '2022-01-09', endDate: 'aaaaaaa' }); | ||
expect(validateService._state.value).toEqual('failure'); | ||
expect(validateService._state.context.endDateInvalid).toEqual(validationConstants.MALFORMED); | ||
expect(validateService._state.context.startDateInvalid).toBeFalsy(); | ||
it('returns endDateInvalid error message when end date is malformed', () => { | ||
validateActor.send({ | ||
type: 'REVALIDATE', | ||
startDate: currentContext.startDate, | ||
endDate: 'aaaaaaa' | ||
}); | ||
const snapshot = validateActor.getSnapshot(); | ||
expect(snapshot.value).toEqual('failure'); | ||
expect(snapshot.context.endDateInvalid).toEqual(validationConstants.MALFORMED); | ||
expect(snapshot.context.startDateInvalid).toBeFalsy(); | ||
}); | ||
|
||
it('returns startDateInvalid error message when end date is before start date', async () => { | ||
validateService.send('REVALIDATE', { startDate: '2022-01-09', endDate: '2022-01-06' }); | ||
expect(validateService._state.value).toEqual('failure'); | ||
expect(validateService._state.context.startDateInvalid).toEqual( | ||
it('returns startDateInvalid error message when end date is before start date', () => { | ||
validateActor.send({ | ||
type: 'REVALIDATE', | ||
startDate: '2022-01-09', | ||
endDate: '2022-01-06' | ||
}); | ||
const snapshot = validateActor.getSnapshot(); | ||
expect(snapshot.value).toEqual('failure'); | ||
expect(snapshot.context.startDateInvalid).toEqual( | ||
validationConstants.START_DATE_AFTER_END_DATE | ||
); | ||
expect(validateService._state.context.endDateInvalid).toBeFalsy(); | ||
expect(snapshot.context.endDateInvalid).toBeFalsy(); | ||
}); | ||
|
||
it('returns startDateInvalid error message when start date is before the first allowed date and endDateInvalid error message when end date is malformed', async () => { | ||
validateService.send('REVALIDATE', { startDate: '2019-01-12', endDate: 'aaaaaa' }); | ||
expect(validateService._state.value).toEqual('failure'); | ||
expect(validateService._state.context.startDateInvalid).toEqual( | ||
it('returns startDateInvalid error message when start date is before the first allowed date and endDateInvalid error message when end date is malformed', () => { | ||
validateActor.send({ | ||
type: 'REVALIDATE', | ||
startDate: '2019-01-12', | ||
endDate: 'aaaaaa' | ||
}); | ||
const snapshot = validateActor.getSnapshot(); | ||
expect(snapshot.value).toEqual('failure'); | ||
expect(snapshot.context.startDateInvalid).toEqual( | ||
validationConstants.DATE_BEFORE_FIRST_ALLOWED | ||
); | ||
expect(validateService._state.context.endDateInvalid).toEqual(validationConstants.MALFORMED); | ||
expect(snapshot.context.endDateInvalid).toEqual(validationConstants.MALFORMED); | ||
}); | ||
|
||
it('returns endDateInvalid error message when end date is before first allowed and startDateInvalid error message when start date is malformed', async () => { | ||
validateService.send('REVALIDATE', { startDate: 'invalid', endDate: '2019-01-06' }); | ||
expect(validateService._state.value).toEqual('failure'); | ||
expect(validateService._state.context.startDateInvalid).toEqual(validationConstants.MALFORMED); | ||
expect(validateService._state.context.endDateInvalid).toEqual( | ||
it('returns endDateInvalid error message when end date is before first allowed and startDateInvalid error message when start date is malformed', () => { | ||
validateActor.send({ | ||
type: 'REVALIDATE', | ||
startDate: 'invalid', | ||
endDate: '2019-01-06' | ||
}); | ||
const snapshot = validateActor.getSnapshot(); | ||
expect(snapshot.value).toEqual('failure'); | ||
expect(snapshot.context.startDateInvalid).toEqual(validationConstants.MALFORMED); | ||
expect(snapshot.context.endDateInvalid).toEqual( | ||
validationConstants.DATE_BEFORE_FIRST_ALLOWED | ||
); | ||
}); | ||
|
||
it('validation in success state after revalidating with correct props', async () => { | ||
validateService.send('REVALIDATE', currentContext); | ||
expect(validateService._state.value).toEqual('success'); | ||
expect(validateService._state.context.startDateInvalid).toBeFalsy(); | ||
expect(validateService._state.context.endDateInvalid).toBeFalsy(); | ||
it('validation in success state after revalidating with correct props', () => { | ||
validateActor.send({ | ||
type: 'REVALIDATE', | ||
startDate: currentContext.startDate, | ||
endDate: currentContext.endDate | ||
}); | ||
const snapshot = validateActor.getSnapshot(); | ||
expect(snapshot.value).toEqual('success'); | ||
expect(snapshot.context.startDateInvalid).toBeFalsy(); | ||
expect(snapshot.context.endDateInvalid).toBeFalsy(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.