diff --git a/legalconsenthub/test/unit/composables/useFormElementValueClearing.spec.ts b/legalconsenthub/test/unit/composables/useFormElementValueClearing.spec.ts new file mode 100644 index 0000000..0382ea4 --- /dev/null +++ b/legalconsenthub/test/unit/composables/useFormElementValueClearing.spec.ts @@ -0,0 +1,268 @@ +import { describe, it, expect } from 'vitest' +import type { + FormElementDto, + FormElementSectionDto, + FormElementSubSectionDto, + FormOptionDto +} from '../../../.api-client' +import { useFormElementValueClearing } from '../../../app/composables/useFormElementValueClearing' + +describe('useFormElementValueClearing', () => { + const { clearHiddenFormElementValues } = useFormElementValueClearing() + + // Visibility state constants + const VISIBLE = true + const HIDDEN = false + + // Helper functions for creating test data + function createFormOption(overrides: Partial = {}): FormOptionDto { + return { + value: '', + label: 'Option', + ...overrides + } as FormOptionDto + } + + function createFormElement(type: string, overrides: Partial = {}): FormElementDto { + return { + id: 'element1', + reference: 'ref_element1', + title: 'Element', + // eslint-disable-next-line @typescript-eslint/no-explicit-any + type: type as any, + options: [createFormOption()], + ...overrides + } + } + + function createSubSection(formElements: FormElementDto[] = []): FormElementSubSectionDto { + return { + id: 'subsection1', + title: 'SubSection', + formElements: formElements.length > 0 ? formElements : [createFormElement('TEXTFIELD')] + } + } + + function createSection(subsections: FormElementSubSectionDto[] = []): FormElementSectionDto { + return { + id: 'section1', + title: 'Section', + formElementSubSections: subsections.length > 0 ? subsections : [createSubSection()] + } + } + + describe('clearHiddenFormElementValues', () => { + it('should return sections unchanged when no elements become newly hidden', () => { + const sections = [createSection()] + const previousMap = new Map([['element1', VISIBLE]]) + const currentMap = new Map([['element1', VISIBLE]]) + + const result = clearHiddenFormElementValues(sections, previousMap, currentMap) + + expect(result).toEqual(sections) + }) + + it('should clear value of newly hidden TEXTFIELD element', () => { + const element = createFormElement('TEXTFIELD', { + id: 'element1', + options: [createFormOption({ value: 'some text' })] + }) + const subsection = createSubSection([element]) + const section = createSection([subsection]) + + const previousMap = new Map([['element1', VISIBLE]]) + const currentMap = new Map([['element1', HIDDEN]]) + + const result = clearHiddenFormElementValues([section], previousMap, currentMap) + + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[0]!.value).toBe('') + }) + + it('should clear value of newly hidden SELECT element', () => { + const element = createFormElement('SELECT', { + id: 'element1', + options: [createFormOption({ value: 'option1' }), createFormOption({ value: 'option2' })] + }) + const subsection = createSubSection([element]) + const section = createSection([subsection]) + + const previousMap = new Map([['element1', VISIBLE]]) + const currentMap = new Map([['element1', HIDDEN]]) + + const result = clearHiddenFormElementValues([section], previousMap, currentMap) + + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[0]!.value).toBe('false') + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[1]!.value).toBe('false') + }) + + it('should clear value of newly hidden CHECKBOX element', () => { + const element = createFormElement('CHECKBOX', { + id: 'element1', + options: [createFormOption({ value: 'true' }), createFormOption({ value: 'true' })] + }) + const subsection = createSubSection([element]) + const section = createSection([subsection]) + + const previousMap = new Map([['element1', VISIBLE]]) + const currentMap = new Map([['element1', HIDDEN]]) + + const result = clearHiddenFormElementValues([section], previousMap, currentMap) + + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[0]!.value).toBe('false') + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[1]!.value).toBe('false') + }) + + it('should clear value of newly hidden RADIOBUTTON element', () => { + const element = createFormElement('RADIOBUTTON', { + id: 'element1', + options: [createFormOption({ value: 'option1' }), createFormOption({ value: 'option2' })] + }) + const subsection = createSubSection([element]) + const section = createSection([subsection]) + + const previousMap = new Map([['element1', VISIBLE]]) + const currentMap = new Map([['element1', HIDDEN]]) + + const result = clearHiddenFormElementValues([section], previousMap, currentMap) + + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[0]!.value).toBe('false') + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[1]!.value).toBe('false') + }) + + it('should not clear visible elements', () => { + const originalValue = 'original value' + const element = createFormElement('TEXTFIELD', { + id: 'element1', + options: [createFormOption({ value: originalValue })] + }) + const subsection = createSubSection([element]) + const section = createSection([subsection]) + + const previousMap = new Map([['element1', VISIBLE]]) + const currentMap = new Map([['element1', VISIBLE]]) + + const result = clearHiddenFormElementValues([section], previousMap, currentMap) + + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[0]!.value).toBe(originalValue) + }) + + it('should not clear elements that were already hidden (previousMap false, currentMap false)', () => { + const originalValue = 'original value' + const element = createFormElement('TEXTFIELD', { + id: 'element1', + options: [createFormOption({ value: originalValue })] + }) + const subsection = createSubSection([element]) + const section = createSection([subsection]) + + const previousMap = new Map([['element1', HIDDEN]]) + const currentMap = new Map([['element1', HIDDEN]]) + + const result = clearHiddenFormElementValues([section], previousMap, currentMap) + + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[0]!.value).toBe(originalValue) + }) + + it('should handle elements identified by id', () => { + const element = createFormElement('TEXTFIELD', { + id: 'element1', + reference: undefined, + options: [createFormOption({ value: 'text' })] + }) + const subsection = createSubSection([element]) + const section = createSection([subsection]) + + const previousMap = new Map([['element1', VISIBLE]]) + const currentMap = new Map([['element1', HIDDEN]]) + + const result = clearHiddenFormElementValues([section], previousMap, currentMap) + + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[0]!.value).toBe('') + }) + + it('should handle elements identified by reference (when id is undefined)', () => { + const element = createFormElement('TEXTFIELD', { + id: undefined, + reference: 'ref_element1', + options: [createFormOption({ value: 'text' })] + }) + const subsection = createSubSection([element]) + const section = createSection([subsection]) + + const previousMap = new Map([['ref_element1', VISIBLE]]) + const currentMap = new Map([['ref_element1', HIDDEN]]) + + const result = clearHiddenFormElementValues([section], previousMap, currentMap) + + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[0]!.value).toBe('') + }) + + it('should handle multiple sections with multiple subsections', () => { + const element1 = createFormElement('TEXTFIELD', { + id: 'element1', + options: [createFormOption({ value: 'text1' })] + }) + const element2 = createFormElement('SELECT', { + id: 'element2', + options: [createFormOption({ value: 'option' })] + }) + + const subsection1 = createSubSection([element1]) + const subsection2 = createSubSection([element2]) + const section1 = createSection([subsection1, subsection2]) + + const element3 = createFormElement('CHECKBOX', { + id: 'element3', + options: [createFormOption({ value: 'true' })] + }) + const subsection3 = createSubSection([element3]) + const section2 = createSection([subsection3]) + + const previousMap = new Map([ + ['element1', VISIBLE], + ['element2', VISIBLE], + ['element3', VISIBLE] + ]) + const currentMap = new Map([ + ['element1', HIDDEN], + ['element2', VISIBLE], + ['element3', HIDDEN] + ]) + + const result = clearHiddenFormElementValues([section1, section2], previousMap, currentMap) + + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[0]!.value).toBe('') + expect(result[0]!.formElementSubSections[1]!.formElements[0]!.options[0]!.value).toBe('option') + expect(result[1]!.formElementSubSections[0]!.formElements[0]!.options[0]!.value).toBe('false') + }) + + it('should handle mixed TEXTFIELD and SELECT in same section', () => { + const textElement = createFormElement('TEXTFIELD', { + id: 'text1', + options: [createFormOption({ value: 'text content' })] + }) + const selectElement = createFormElement('SELECT', { + id: 'select1', + options: [createFormOption({ value: 'val1' }), createFormOption({ value: 'val2' })] + }) + + const subsection = createSubSection([textElement, selectElement]) + const section = createSection([subsection]) + + const previousMap = new Map([ + ['text1', VISIBLE], + ['select1', VISIBLE] + ]) + const currentMap = new Map([ + ['text1', HIDDEN], + ['select1', HIDDEN] + ]) + + const result = clearHiddenFormElementValues([section], previousMap, currentMap) + + expect(result[0]!.formElementSubSections[0]!.formElements[0]!.options[0]!.value).toBe('') + expect(result[0]!.formElementSubSections[0]!.formElements[1]!.options[0]!.value).toBe('false') + expect(result[0]!.formElementSubSections[0]!.formElements[1]!.options[1]!.value).toBe('false') + }) + }) +})