-
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 #166 from DEFRA/BAU-yes-no-model
BAU: set up re-usable yes/no button factory
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
src/server/common/model/answer/yes-no-radio-button/yes-no-radio-button.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,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 | ||
} |
37 changes: 37 additions & 0 deletions
37
src/server/common/model/answer/yes-no-radio-button/yes-no-radio-button.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,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') | ||
}) | ||
}) |