-
Notifications
You must be signed in to change notification settings - Fork 3
DF-944: Implement TrackingField component #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
211 changes: 211 additions & 0 deletions
211
src/server/plugins/engine/components/TrackingField.test.ts
This file contains hidden or 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,211 @@ | ||
| import { ComponentType, type TrackingFieldComponent } from '@defra/forms-model' | ||
|
|
||
| import { ComponentCollection } from '~/src/server/plugins/engine/components/ComponentCollection.js' | ||
| import { | ||
| getAnswer, | ||
| type Field | ||
| } from '~/src/server/plugins/engine/components/helpers/components.js' | ||
| import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js' | ||
| import { stubTranslator } from '~/src/server/plugins/engine/pageControllers/__stubs__/translator.js' | ||
| import definition from '~/test/form/definitions/blank.js' | ||
| import { getFormData, getFormState } from '~/test/helpers/component-helpers.js' | ||
|
|
||
| const translator = new FormModel(definition, { | ||
| basePath: '/' | ||
| }).createTranslator() | ||
|
|
||
| describe('TrackingField', () => { | ||
| let model: FormModel | ||
|
|
||
| beforeEach(() => { | ||
| model = new FormModel(definition, { | ||
| basePath: 'test' | ||
| }) | ||
| }) | ||
|
|
||
| describe('Defaults', () => { | ||
| let def: TrackingFieldComponent | ||
| let collection: ComponentCollection | ||
| let field: Field | ||
|
|
||
| beforeEach(() => { | ||
| def = { | ||
| title: 'Tracking field', | ||
| name: 'myComponent', | ||
| type: ComponentType.TrackingField, | ||
| options: {} | ||
| } satisfies TrackingFieldComponent | ||
|
|
||
| collection = new ComponentCollection([def], { model }) | ||
| field = collection.fields[0] | ||
| }) | ||
|
|
||
| describe('Schema', () => { | ||
| it('uses component title as label as default', () => { | ||
| const { formSchema } = collection | ||
| const { keys } = formSchema.describe() | ||
|
|
||
| expect(keys).toHaveProperty( | ||
| 'myComponent', | ||
| expect.objectContaining({ | ||
| flags: expect.objectContaining({ | ||
| label: 'Tracking field' | ||
| }) | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('uses component name as keys', () => { | ||
| const { formSchema } = collection | ||
| const { keys } = formSchema.describe() | ||
|
|
||
| expect(field.keys).toEqual(['myComponent']) | ||
| expect(field.collection).toBeUndefined() | ||
|
|
||
| for (const key of field.keys) { | ||
| expect(keys).toHaveProperty(key) | ||
| } | ||
| }) | ||
|
|
||
| it('is required by default', () => { | ||
| const { formSchema } = collection | ||
| const { keys } = formSchema.describe() | ||
|
|
||
| expect(keys).toHaveProperty( | ||
| 'myComponent', | ||
| expect.objectContaining({ | ||
| flags: expect.objectContaining({ | ||
| presence: 'required' | ||
| }) | ||
| }) | ||
| ) | ||
| }) | ||
|
|
||
| it('accepts valid values', () => { | ||
| const result1 = collection.validate(getFormData(true)) | ||
|
|
||
| expect(result1.errors).toBeUndefined() | ||
| }) | ||
|
|
||
| it('adds errors for false value', () => { | ||
| const result = collection.validate(getFormData(false)) | ||
|
|
||
| expect(result.errors).toEqual([ | ||
| expect.objectContaining({ | ||
| text: 'Select tracking field' | ||
| }) | ||
| ]) | ||
| }) | ||
|
|
||
| it('adds errors for empty value', () => { | ||
| const result = collection.validate(getFormData('')) | ||
|
|
||
| expect(result.errors).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| text: 'Select tracking field' | ||
| }), | ||
| expect.objectContaining({ | ||
| text: 'Tracking field must be a boolean' | ||
| }) | ||
| ]) | ||
| ) | ||
| }) | ||
|
|
||
| it('adds errors for invalid values', () => { | ||
| const result1 = collection.validate(getFormData(['invalid'])) | ||
| const result2 = collection.validate( | ||
| // @ts-expect-error - Allow invalid param for test | ||
| getFormData({ unknown: 'invalid' }) | ||
| ) | ||
|
|
||
| expect(result1.errors).toBeTruthy() | ||
| expect(result2.errors).toBeTruthy() | ||
| }) | ||
| }) | ||
|
|
||
| describe('State', () => { | ||
| it('returns text from state', () => { | ||
| const state1 = getFormState(true) | ||
| const state2 = getFormState(null) | ||
|
|
||
| const answer1 = getAnswer(field, state1, translator) | ||
| const answer2 = getAnswer(field, state2, translator) | ||
|
|
||
| expect(answer1).toBe('true') | ||
| expect(answer2).toBe('') | ||
| }) | ||
|
|
||
| it('returns payload from state', () => { | ||
| const state1 = getFormState(true) | ||
| const state2 = getFormState(null) | ||
|
|
||
| const payload1 = field.getFormDataFromState(state1) | ||
| const payload2 = field.getFormDataFromState(state2) | ||
|
|
||
| expect(payload1).toEqual(getFormData(true)) | ||
| expect(payload2).toEqual(getFormData()) | ||
| }) | ||
|
|
||
| it('returns value from state', () => { | ||
| const state1 = getFormState(true) | ||
| const state2 = getFormState(null) | ||
|
|
||
| const value1 = field.getFormValueFromState(state1) | ||
| const value2 = field.getFormValueFromState(state2) | ||
|
|
||
| expect(value1).toBe(true) | ||
| expect(value2).toBeUndefined() | ||
| }) | ||
|
|
||
| it('returns context for conditions and form submission', () => { | ||
| const state1 = getFormState(true) | ||
| const state2 = getFormState(null) | ||
|
|
||
| const value1 = field.getContextValueFromState(state1) | ||
| const value2 = field.getContextValueFromState(state2) | ||
|
|
||
| expect(value1).toBe(true) | ||
| expect(value2).toBeNull() | ||
| }) | ||
|
|
||
| it('returns state from payload', () => { | ||
| const payload1 = getFormData(true) | ||
| const payload2 = getFormData() | ||
|
|
||
| const value1 = field.getStateFromValidForm(payload1) | ||
| const value2 = field.getStateFromValidForm(payload2) | ||
|
|
||
| expect(value1).toEqual(getFormState(true)) | ||
| expect(value2).toEqual(getFormState(null)) | ||
| }) | ||
| }) | ||
|
|
||
| describe('View model', () => { | ||
| it('sets Nunjucks component defaults', () => { | ||
| const viewModel = field.getViewModel({ | ||
| payload: getFormData(true), | ||
| errors: undefined, | ||
| translator: stubTranslator | ||
| }) | ||
|
|
||
| expect(viewModel).toEqual( | ||
| expect.objectContaining({ | ||
| label: { text: def.title }, | ||
| name: 'myComponent', | ||
| id: 'myComponent', | ||
| value: true | ||
| }) | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('AllPossibleErrors', () => { | ||
| it('should return errors', () => { | ||
| const errors = field.getAllPossibleErrors() | ||
| expect(errors.baseErrors).not.toBeEmpty() | ||
| expect(errors.advancedSettingsErrors).toBeEmpty() | ||
| }) | ||
| }) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'...when the user reaches a certain point in the form...' - suggest '...when the user reaches the page the tracking field is on..'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I nearly went with this but then reverted. It's only once the user passes then page (through a POST) that it's set to true (i.e. not on the GET).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about 'They are hidden fields that are set to
truewhen the user passes the page in the form.'