|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {signal, WritableSignal} from '@angular/core'; |
| 10 | +import {LabelControl, LabelControlInputs, LabelControlOptionalInputs} from './label'; |
| 11 | + |
| 12 | +// This is a helper type for the initial values passed to the setup function. |
| 13 | +type TestInputs = Partial<{ |
| 14 | + label: string | undefined; |
| 15 | + defaultLabelledBy: string[]; |
| 16 | + labelledBy: string[]; |
| 17 | + labelledByAppend: boolean; |
| 18 | + defaultDescribedBy: string[]; |
| 19 | + describedBy: string[]; |
| 20 | + describedByAppend: boolean; |
| 21 | +}>; |
| 22 | + |
| 23 | +type TestLabelControlInputs = LabelControlInputs & Required<LabelControlOptionalInputs>; |
| 24 | + |
| 25 | +// This is a helper type to make all properties of LabelControlInputs writable signals. |
| 26 | +type WritableLabelControlInputs = { |
| 27 | + [K in keyof TestLabelControlInputs]: WritableSignal< |
| 28 | + TestLabelControlInputs[K] extends {(): infer T} ? T : never |
| 29 | + >; |
| 30 | +}; |
| 31 | + |
| 32 | +function getLabelControl(initialValues: TestInputs = {}): { |
| 33 | + control: LabelControl; |
| 34 | + inputs: WritableLabelControlInputs; |
| 35 | +} { |
| 36 | + const inputs: WritableLabelControlInputs = { |
| 37 | + defaultLabelledBy: signal(initialValues.defaultLabelledBy ?? []), |
| 38 | + defaultDescribedBy: signal(initialValues.defaultDescribedBy ?? []), |
| 39 | + label: signal(initialValues.label), |
| 40 | + labelledBy: signal(initialValues.labelledBy ?? []), |
| 41 | + labelledByAppend: signal(initialValues.labelledByAppend ?? false), |
| 42 | + describedBy: signal(initialValues.describedBy ?? []), |
| 43 | + describedByAppend: signal(initialValues.describedByAppend ?? false), |
| 44 | + }; |
| 45 | + |
| 46 | + const control = new LabelControl(inputs); |
| 47 | + |
| 48 | + return {control, inputs}; |
| 49 | +} |
| 50 | + |
| 51 | +describe('LabelControl', () => { |
| 52 | + describe('#label', () => { |
| 53 | + it('should return the user-provided label', () => { |
| 54 | + const {control} = getLabelControl({label: 'My Label'}); |
| 55 | + expect(control.label()).toBe('My Label'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should return undefined if no label is provided', () => { |
| 59 | + const {control} = getLabelControl(); |
| 60 | + expect(control.label()).toBeUndefined(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should update when the input signal changes', () => { |
| 64 | + const {control, inputs} = getLabelControl({label: 'Initial Label'}); |
| 65 | + expect(control.label()).toBe('Initial Label'); |
| 66 | + |
| 67 | + inputs.label.set('Updated Label'); |
| 68 | + expect(control.label()).toBe('Updated Label'); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('#labelledBy', () => { |
| 73 | + it('should return an empty array if a label is provided', () => { |
| 74 | + const {control} = getLabelControl({ |
| 75 | + label: 'My Label', |
| 76 | + defaultLabelledBy: ['default-id'], |
| 77 | + labelledBy: ['user-id'], |
| 78 | + }); |
| 79 | + expect(control.labelledBy()).toEqual([]); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should return defaultLabelledBy if no user-provided labelledBy exists', () => { |
| 83 | + const {control} = getLabelControl({defaultLabelledBy: ['default-id']}); |
| 84 | + expect(control.labelledBy()).toEqual(['default-id']); |
| 85 | + }); |
| 86 | + |
| 87 | + it('should return only user-provided labelledBy if labelledByAppend is false', () => { |
| 88 | + const {control} = getLabelControl({ |
| 89 | + defaultLabelledBy: ['default-id'], |
| 90 | + labelledBy: ['user-id'], |
| 91 | + labelledByAppend: false, |
| 92 | + }); |
| 93 | + expect(control.labelledBy()).toEqual(['user-id']); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should return default and user-provided labelledBy if labelledByAppend is true', () => { |
| 97 | + const {control} = getLabelControl({ |
| 98 | + defaultLabelledBy: ['default-id'], |
| 99 | + labelledBy: ['user-id'], |
| 100 | + labelledByAppend: true, |
| 101 | + }); |
| 102 | + expect(control.labelledBy()).toEqual(['default-id', 'user-id']); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should update when label changes from undefined to a string', () => { |
| 106 | + const {control, inputs} = getLabelControl({ |
| 107 | + defaultLabelledBy: ['default-id'], |
| 108 | + }); |
| 109 | + expect(control.labelledBy()).toEqual(['default-id']); |
| 110 | + inputs.label.set('A wild label appears'); |
| 111 | + expect(control.labelledBy()).toEqual([]); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe('#describedBy', () => { |
| 116 | + it('should return defaultDescribedBy if no user-provided describedBy exists', () => { |
| 117 | + const {control} = getLabelControl({defaultDescribedBy: ['default-id']}); |
| 118 | + expect(control.describedBy()).toEqual(['default-id']); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should return only user-provided describedBy if describedByAppend is false', () => { |
| 122 | + const {control} = getLabelControl({ |
| 123 | + defaultDescribedBy: ['default-id'], |
| 124 | + describedBy: ['user-id'], |
| 125 | + describedByAppend: false, |
| 126 | + }); |
| 127 | + expect(control.describedBy()).toEqual(['user-id']); |
| 128 | + }); |
| 129 | + |
| 130 | + it('should return default and user-provided describedBy if describedByAppend is true', () => { |
| 131 | + const {control} = getLabelControl({ |
| 132 | + defaultDescribedBy: ['default-id'], |
| 133 | + describedBy: ['user-id'], |
| 134 | + describedByAppend: true, |
| 135 | + }); |
| 136 | + expect(control.describedBy()).toEqual(['default-id', 'user-id']); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should update when describedByAppend changes', () => { |
| 140 | + const {control, inputs} = getLabelControl({ |
| 141 | + defaultDescribedBy: ['default-id'], |
| 142 | + describedBy: ['user-id'], |
| 143 | + describedByAppend: false, |
| 144 | + }); |
| 145 | + expect(control.describedBy()).toEqual(['user-id']); |
| 146 | + inputs.describedByAppend.set(true); |
| 147 | + expect(control.describedBy()).toEqual(['default-id', 'user-id']); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should not be affected by the label property', () => { |
| 151 | + const {control, inputs} = getLabelControl({ |
| 152 | + defaultDescribedBy: ['default-id'], |
| 153 | + }); |
| 154 | + expect(control.describedBy()).toEqual(['default-id']); |
| 155 | + inputs.label.set('A wild label appears'); |
| 156 | + expect(control.describedBy()).toEqual(['default-id']); |
| 157 | + }); |
| 158 | + }); |
| 159 | +}); |
0 commit comments