|
| 1 | +// @vitest-environment jsdom |
| 2 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 3 | + |
| 4 | +import { Form } from '@bpmn-io/form-js-viewer'; |
| 5 | +import CustomFormFieldsModule from './index'; |
| 6 | +import { NONE_OF_THESE_VALUE } from './ChecklistWithNone'; |
| 7 | +import { normalizeArrayFieldData } from '@/utils/arrayFieldData'; |
| 8 | + |
| 9 | +let form: Form | undefined; |
| 10 | + |
| 11 | +afterEach(() => { |
| 12 | + form?.destroy(); |
| 13 | + form = undefined; |
| 14 | + document.body.innerHTML = ''; |
| 15 | +}); |
| 16 | + |
| 17 | +describe('ChecklistWithNone', () => { |
| 18 | + it.each([ |
| 19 | + { |
| 20 | + source: 'input data', |
| 21 | + options: { valuesKey: 'benefitOptions' }, |
| 22 | + data: { benefitOptions: [{ label: 'Medicaid', value: 'Medicaid' }] }, |
| 23 | + }, |
| 24 | + { |
| 25 | + source: 'expression', |
| 26 | + options: { valuesExpression: '=[{"label":"Medicaid","value":"Medicaid"}]' }, |
| 27 | + data: {}, |
| 28 | + }, |
| 29 | + ])('places None last when options come from $source', async ({ options, data }) => { |
| 30 | + const schema = { |
| 31 | + components: [{ |
| 32 | + type: 'checklist_none', |
| 33 | + id: 'Field_enrollments', |
| 34 | + key: 'enrollments', |
| 35 | + label: 'Benefits', |
| 36 | + description: 'Choose all that apply.', |
| 37 | + ...options, |
| 38 | + }], |
| 39 | + type: 'default', |
| 40 | + id: 'Form_test', |
| 41 | + schemaVersion: 18, |
| 42 | + }; |
| 43 | + const container = document.body.appendChild(document.createElement('div')); |
| 44 | + form = new Form({ container, additionalModules: [CustomFormFieldsModule] }); |
| 45 | + await form.importSchema(schema, data); |
| 46 | + |
| 47 | + const optionRows = Array.from(container.querySelectorAll('.fjs-form-field-checklist > .fjs-inline-label')); |
| 48 | + expect(optionRows.map((row) => row.textContent?.trim())).toEqual(['Medicaid', 'None of these']); |
| 49 | + expect(optionRows[1].nextElementSibling?.textContent).toContain('Choose all that apply.'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('makes None exclusive and preserves the answer across reimport', async () => { |
| 53 | + const schema = { |
| 54 | + components: [{ |
| 55 | + type: 'checklist_none', |
| 56 | + id: 'Field_enrollments', |
| 57 | + key: 'enrollments', |
| 58 | + label: 'Benefits', |
| 59 | + description: 'Choose all that apply.', |
| 60 | + values: [{ label: 'SNAP', value: 'SNAP' }], |
| 61 | + }], |
| 62 | + type: 'default', |
| 63 | + id: 'Form_test', |
| 64 | + schemaVersion: 18, |
| 65 | + }; |
| 66 | + const container = document.body.appendChild(document.createElement('div')); |
| 67 | + form = new Form({ container, additionalModules: [CustomFormFieldsModule] }); |
| 68 | + await form.importSchema(schema, {}); |
| 69 | + |
| 70 | + const checkboxes = () => Array.from(container.querySelectorAll<HTMLInputElement>('input[type="checkbox"]')); |
| 71 | + expect(checkboxes()).toHaveLength(2); |
| 72 | + const optionRows = Array.from(container.querySelectorAll('.fjs-form-field-checklist > .fjs-inline-label')); |
| 73 | + expect(optionRows.map((row) => row.textContent?.trim())).toEqual(['SNAP', 'None of these']); |
| 74 | + expect(optionRows[1].nextElementSibling?.textContent).toContain('Choose all that apply.'); |
| 75 | + |
| 76 | + checkboxes()[1].click(); |
| 77 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 78 | + expect(form._getState().data.enrollments).toEqual([NONE_OF_THESE_VALUE]); |
| 79 | + |
| 80 | + await form.importSchema(schema, form._getState().data); |
| 81 | + expect(checkboxes()[1].checked).toBe(true); |
| 82 | + |
| 83 | + checkboxes()[0].click(); |
| 84 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 85 | + expect(form._getState().data.enrollments).toEqual(['SNAP']); |
| 86 | + expect(checkboxes()[1].checked).toBe(false); |
| 87 | + |
| 88 | + checkboxes()[1].click(); |
| 89 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 90 | + expect(form._getState().data.enrollments).toEqual([NONE_OF_THESE_VALUE]); |
| 91 | + expect(checkboxes()[0].checked).toBe(false); |
| 92 | + |
| 93 | + expect(normalizeArrayFieldData(schema, form._getState().data).enrollments).toEqual([]); |
| 94 | + checkboxes()[1].click(); |
| 95 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 96 | + expect(normalizeArrayFieldData(schema, form._getState().data).enrollments).toBeNull(); |
| 97 | + }); |
| 98 | +}); |
0 commit comments