Skip to content

Commit

Permalink
Merge pull request #71 from DEFRA/DSFAAP-583-section-pages-updates
Browse files Browse the repository at this point in the history
DSFAAP-583: section pages updates
  • Loading branch information
joseluisgraa authored Dec 13, 2024
2 parents 9cbe48b + 083027f commit 914a9eb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
24 changes: 10 additions & 14 deletions src/server/common/model/section/origin.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { SectionModel } from '../section/section-model/index.js'
import { CphNumber } from '../answer/cph-number.js'
import { Address } from '../answer/address.js'

import { OnOffFarm } from '../answer/on-off-farm.js'
import { OnOffFarmPage } from '../../../origin/on-off-farm/index.js'
import { CphNumberPage } from '../../../origin/cph-number/index.js'
import { OriginAddressPage } from '../../../origin/address/index.js'
import { onOffFarmPage } from '../../../origin/on-off-farm/index.js'
import { cphNumberPage } from '../../../origin/cph-number/index.js'
import { originAddressPage } from '../../../origin/address/index.js'

/**
* export @typedef {{
Expand All @@ -19,7 +15,7 @@ import { OriginAddressPage } from '../../../origin/address/index.js'
*/

export class Origin extends SectionModel {
firstPage = new OnOffFarmPage()
firstPage = onOffFarmPage

get onOffFarm() {
return this._data?.onOffFarm.answer
Expand All @@ -40,16 +36,16 @@ export class Origin extends SectionModel {
static fromState(state) {
return new Origin({
onOffFarm: {
page: new OnOffFarmPage(),
answer: OnOffFarm.fromState(state?.onOffFarm)
page: onOffFarmPage,
answer: onOffFarmPage.Answer.fromState(state?.onOffFarm)
},
cphNumber: {
page: new CphNumberPage(),
answer: CphNumber.fromState(state?.cphNumber)
page: cphNumberPage,
answer: cphNumberPage.Answer.fromState(state?.cphNumber)
},
address: {
page: new OriginAddressPage(),
answer: Address.fromState(state?.address)
page: originAddressPage,
answer: originAddressPage.Answer.fromState(state?.address)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,25 @@ export class SectionModelUpdated {
return this._data
}

/**
* returns {QuestionPage[]}
*/
get pages() {
const pages = []

/** @type {QuestionPage} */
/** @type {Page} */
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))
if (!currPage.answer.validate().isValid) {
break
}

page = page.nextPage(currPage.answer)
}

return pages
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 { CphNumber } from '../../answer/cph-number.js'

/** @import {OnOffFarmData} from '~/src/server/common/model/answer/on-off-farm.js' */

Expand Down Expand Up @@ -29,7 +30,7 @@ const exitState = {
address: validAddress // this is unreachable in the journey, because we will have exited already
}

describe('SectionModel.value', () => {
describe('SectionModel.pages', () => {
it('should short-circuit on an exit page', () => {
const origin = Origin.fromState(exitState)
const pages = origin.pages
Expand All @@ -40,6 +41,22 @@ describe('SectionModel.value', () => {
OnOffFarm
)
})

it('should short-circuit on a page with an invalid answer', () => {
const origin = Origin.fromState(invalidState)
const pages = origin.pages

expect(pages).toHaveLength(2)
expect(pages.at(0)).toBeInstanceOf(OnOffFarmPage)
expect(origin[pages.at(0)?.questionKey ?? 'invalid']).toBeInstanceOf(
OnOffFarm
)

expect(pages.at(1)).toBeInstanceOf(CphNumberPage)
expect(origin[pages.at(1)?.questionKey ?? 'invalid']).toBeInstanceOf(
CphNumber
)
})
})

describe('SectionModel.validate', () => {
Expand Down

0 comments on commit 914a9eb

Please sign in to comment.