Skip to content

Commit

Permalink
Merge pull request #166 from DEFRA/BAU-yes-no-model
Browse files Browse the repository at this point in the history
BAU: set up re-usable yes/no button factory
  • Loading branch information
hughfdjackson authored Jan 28, 2025
2 parents f92af86 + 111975e commit 0c5df9d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { RadioButtonAnswer } from '../radio-button/radio-button.js'
/** @import {RadioButtonConfig} from '../radio-button/radio-button.js' */

/**
* export @typedef {'yes' | 'no'} YesNoRadioButtonData
* @typedef {{ yesOrNo: 'yes' | 'no' }} YesNoRadioButtonPayload
*/

export const yesNoRadioButtonFactory = ({ emptyOptionText }) => {
/** @type {RadioButtonConfig} */
const yesNoRadio = {
payloadKey: 'yesOrNo',
options: {
yes: { label: 'Yes' },
no: { label: 'No' }
},
errors: {
emptyOptionText
}
}

/** @augments {RadioButtonAnswer<YesNoRadioButtonPayload>} */
class YesNoAnswer extends RadioButtonAnswer {
get config() {
return yesNoRadio
}

static get config() {
return yesNoRadio
}
}

return YesNoAnswer
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { RadioButtonAnswer } from '../radio-button/radio-button.js'
import { yesNoRadioButtonFactory } from './yes-no-radio-button.js'
/** @import {YesNoRadioButtonPayload} from './yes-no-radio-button.js' */

/** @type {YesNoRadioButtonPayload} */
const payload = {
yesOrNo: 'yes'
}
const emptyOptionText = 'Select if you are a test case?'

describe('YesNoRadioButton', () => {
const YesNoRadioButton = yesNoRadioButtonFactory({ emptyOptionText })

it('should be an instance of radio button', () => {
expect(new YesNoRadioButton(payload)).toBeInstanceOf(RadioButtonAnswer)
})

it('should have same static config & instance config', () => {
expect(new YesNoRadioButton(payload).config).toEqual(
YesNoRadioButton.config
)
})

it('should have the right payload key', () => {
expect(YesNoRadioButton.config.payloadKey).toBe('yesOrNo')
})

it('should define the right empty input message', () => {
expect(YesNoRadioButton.config.errors.emptyOptionText).toBe(emptyOptionText)
})

it('should have the expected options to select from', () => {
expect(Object.keys(YesNoRadioButton.config.options)).toHaveLength(2)
expect(YesNoRadioButton.config.options.yes.label).toBe('Yes')
expect(YesNoRadioButton.config.options.no.label).toBe('No')
})
})

0 comments on commit 0c5df9d

Please sign in to comment.