-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from DEFRA/583-3
[583] new section validation
- Loading branch information
Showing
17 changed files
with
257 additions
and
54 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
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
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
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
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,3 @@ | ||
import { SectionModelUpdated } from './section-model-updated.js' | ||
|
||
export const SectionModel = SectionModelUpdated |
78 changes: 78 additions & 0 deletions
78
src/server/common/model/section/section-model/section-model-updated.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,78 @@ | ||
import { NotImplementedError } from '../../../helpers/not-implemented-error.js' | ||
import { QuestionPage } from '../../page/question-page-model.js' | ||
|
||
/** | ||
* @import { Page } from '../../page/page-model.js' | ||
* @import {AnswerModel} from '../../answer/answer-model.js' | ||
*/ | ||
|
||
/** | ||
* @typedef {{ page: QuestionPage, answer: AnswerModel }} PageAnswer | ||
* @typedef {{[key: string]: PageAnswer}} SectionPayload | ||
*/ | ||
|
||
/** | ||
* @typedef {{ isValid: boolean, firstInvalidPage?: QuestionPage }} SectionValidation | ||
*/ | ||
|
||
export class SectionModelUpdated { | ||
/** @type {SectionPayload} */ | ||
_data | ||
|
||
/** @type {QuestionPage} */ | ||
firstPage | ||
|
||
constructor(data) { | ||
this._data = data | ||
} | ||
|
||
get value() { | ||
return this._data | ||
} | ||
|
||
get pages() { | ||
const pages = [] | ||
|
||
/** @type {QuestionPage} */ | ||
let page = this._data[this.firstPage.questionKey].page | ||
|
||
while (page instanceof QuestionPage) { | ||
const currPage = this._data[page.questionKey] | ||
|
||
pages.push(page) | ||
|
||
page = /** @type {QuestionPage} */ (page.nextPage(currPage.answer)) | ||
} | ||
|
||
return pages | ||
} | ||
|
||
/** @returns {SectionValidation} */ | ||
validate() { | ||
const pages = this.pages | ||
|
||
if (pages.length === 0) { | ||
return { isValid: false, firstInvalidPage: this.firstPage } | ||
} | ||
|
||
for (const visitedPage of pages) { | ||
const { page, answer } = this._data[visitedPage.questionKey] | ||
if (!answer.validate().isValid) { | ||
return { isValid: false, firstInvalidPage: page } | ||
} | ||
} | ||
|
||
return { isValid: true } | ||
} | ||
|
||
/** | ||
* @param {object} _data | ||
* @returns {SectionModelUpdated} | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
static fromState(_data) { | ||
throw new NotImplementedError() | ||
} | ||
|
||
/* eslint-enable @typescript-eslint/no-unused-vars */ | ||
} |
72 changes: 72 additions & 0 deletions
72
src/server/common/model/section/section-model/section-model-updated.test.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,72 @@ | ||
import { CphNumberPage } from '~/src/server/origin/cph-number/index.js' | ||
import { OnOffFarmPage } from '~/src/server/origin/on-off-farm/index.js' | ||
import { OnOffFarm } from '~/src/server/common/model/answer/on-off-farm.js' | ||
import { Origin } from '../origin.js' | ||
|
||
/** @import {OnOffFarmData} from '~/src/server/common/model/answer/on-off-farm.js' */ | ||
|
||
const validAddress = { | ||
addressLine1: 'Starfleet Headquarters', | ||
addressTown: 'San Francisco', | ||
addressPostcode: 'RG24 8RR' | ||
} | ||
|
||
const validState = { | ||
onOffFarm: /** @type {OnOffFarmData} */ ('off'), | ||
cphNumber: '12/345/6789', | ||
address: validAddress | ||
} | ||
|
||
const invalidState = { | ||
onOffFarm: /** @type {OnOffFarmData} */ ('off'), | ||
cphNumber: 'not-a-cph', | ||
address: validAddress // this is unreachable in the journey, since we've got an invalid question ahead of it | ||
} | ||
|
||
const exitState = { | ||
onOffFarm: /** @type {OnOffFarmData} */ ('on'), | ||
cphNumber: 'not-a-cph', // this is unreachable in the journey, because we will have exited already | ||
address: validAddress // this is unreachable in the journey, because we will have exited already | ||
} | ||
|
||
describe('SectionModel.value', () => { | ||
it('should short-circuit on an exit page', () => { | ||
const origin = Origin.fromState(exitState) | ||
const pages = origin.pages | ||
|
||
expect(pages).toHaveLength(1) | ||
expect(pages.at(0)).toBeInstanceOf(OnOffFarmPage) | ||
expect(origin[pages.at(0)?.questionKey ?? 'invalid']).toBeInstanceOf( | ||
OnOffFarm | ||
) | ||
}) | ||
}) | ||
|
||
describe('SectionModel.validate', () => { | ||
it('should return valid if all questions in journey are validly answered', () => { | ||
const origin = Origin.fromState(validState) | ||
|
||
expect(origin.validate()).toEqual({ isValid: true }) | ||
}) | ||
|
||
// Reason: We have not finalised how exit pages will behave | ||
it.skip('should return ... invalid ? ... if the section hits an exit condition before its complete', () => { | ||
const origin = Origin.fromState(exitState) | ||
|
||
expect(origin.validate().isValid).toBe(true) | ||
}) | ||
|
||
it('should return invalid if the section hits a page with an invalid answer', () => { | ||
const origin = Origin.fromState(invalidState) | ||
const { isValid, firstInvalidPage } = origin.validate() | ||
|
||
expect(isValid).toBe(false) | ||
expect(firstInvalidPage).toBeInstanceOf(CphNumberPage) | ||
}) | ||
}) | ||
|
||
describe('SectionModel.fromState', () => { | ||
it('should return an instance of the class that produced it', () => { | ||
expect(Origin.fromState(validState)).toBeInstanceOf(Origin) | ||
}) | ||
}) |
6 changes: 3 additions & 3 deletions
6
...ver/common/model/section/section-model.js → ...el/section/section-model/section-model.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
2 changes: 1 addition & 1 deletion
2
...ommon/model/section/section.model.test.js → ...ction/section-model/section.model.test.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
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.