|
| 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 | +}>; |
| 18 | + |
| 19 | +type TestLabelControlInputs = LabelControlInputs & Required<LabelControlOptionalInputs>; |
| 20 | + |
| 21 | +// This is a helper type to make all properties of LabelControlInputs writable signals. |
| 22 | +type WritableLabelControlInputs = { |
| 23 | + [K in keyof TestLabelControlInputs]: WritableSignal< |
| 24 | + TestLabelControlInputs[K] extends {(): infer T} ? T : never |
| 25 | + >; |
| 26 | +}; |
| 27 | + |
| 28 | +function getLabelControl(initialValues: TestInputs = {}): { |
| 29 | + control: LabelControl; |
| 30 | + inputs: WritableLabelControlInputs; |
| 31 | +} { |
| 32 | + const inputs: WritableLabelControlInputs = { |
| 33 | + defaultLabelledBy: signal(initialValues.defaultLabelledBy ?? []), |
| 34 | + label: signal(initialValues.label), |
| 35 | + labelledBy: signal(initialValues.labelledBy ?? []), |
| 36 | + }; |
| 37 | + |
| 38 | + const control = new LabelControl(inputs); |
| 39 | + |
| 40 | + return {control, inputs}; |
| 41 | +} |
| 42 | + |
| 43 | +describe('LabelControl', () => { |
| 44 | + describe('#label', () => { |
| 45 | + it('should return the user-provided label', () => { |
| 46 | + const {control} = getLabelControl({label: 'My Label'}); |
| 47 | + expect(control.label()).toBe('My Label'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return undefined if no label is provided', () => { |
| 51 | + const {control} = getLabelControl(); |
| 52 | + expect(control.label()).toBeUndefined(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should update when the input signal changes', () => { |
| 56 | + const {control, inputs} = getLabelControl({label: 'Initial Label'}); |
| 57 | + expect(control.label()).toBe('Initial Label'); |
| 58 | + |
| 59 | + inputs.label.set('Updated Label'); |
| 60 | + expect(control.label()).toBe('Updated Label'); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('#labelledBy', () => { |
| 65 | + it('should return user-provided labelledBy even if a label is provided', () => { |
| 66 | + const {control} = getLabelControl({ |
| 67 | + label: 'My Label', |
| 68 | + defaultLabelledBy: ['default-id'], |
| 69 | + labelledBy: ['user-id'], |
| 70 | + }); |
| 71 | + expect(control.labelledBy()).toEqual(['user-id']); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should return defaultLabelledBy if no user-provided labelledBy exists', () => { |
| 75 | + const {control} = getLabelControl({defaultLabelledBy: ['default-id']}); |
| 76 | + expect(control.labelledBy()).toEqual(['default-id']); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should update when label changes from undefined to a string', () => { |
| 80 | + const {control, inputs} = getLabelControl({ |
| 81 | + defaultLabelledBy: ['default-id'], |
| 82 | + }); |
| 83 | + expect(control.labelledBy()).toEqual(['default-id']); |
| 84 | + inputs.label.set('A wild label appears'); |
| 85 | + expect(control.labelledBy()).toEqual([]); |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments