77 */
88
99import { signal , WritableSignalLike } from '../signal-like/signal-like' ;
10- import { LabelControl , LabelControlInputs , LabelControlOptionalInputs } from './label' ;
10+ import { LabelControl , LabelControlInputs } from './label' ;
1111
1212// This is a helper type for the initial values passed to the setup function.
1313type TestInputs = Partial < {
1414 label : string | undefined ;
15- defaultLabelledBy : string [ ] ;
15+ defaultLabelledBy : string ;
1616 labelledBy : string [ ] ;
1717} > ;
1818
19- type TestLabelControlInputs = LabelControlInputs & Required < LabelControlOptionalInputs > ;
20-
2119// This is a helper type to make all properties of LabelControlInputs writable signals.
2220type WritableLabelControlInputs = {
23- [ K in keyof TestLabelControlInputs ] : WritableSignalLike <
24- TestLabelControlInputs [ K ] extends { ( ) : infer T } ? T : never
21+ [ K in keyof LabelControlInputs ] : WritableSignalLike <
22+ LabelControlInputs [ K ] extends { ( ) : infer T } ? T : never
2523 > ;
2624} ;
2725
@@ -30,7 +28,7 @@ function getLabelControl(initialValues: TestInputs = {}): {
3028 inputs : WritableLabelControlInputs ;
3129} {
3230 const inputs : WritableLabelControlInputs = {
33- defaultLabelledBy : signal ( initialValues . defaultLabelledBy ?? [ ] ) ,
31+ defaultLabelledBy : signal ( initialValues . defaultLabelledBy ) ,
3432 label : signal ( initialValues . label ) ,
3533 labelledBy : signal ( initialValues . labelledBy ?? [ ] ) ,
3634 } ;
@@ -47,6 +45,11 @@ describe('LabelControl', () => {
4745 expect ( control . label ( ) ) . toBe ( 'My Label' ) ;
4846 } ) ;
4947
48+ it ( 'should return the user-provided label even ifdefaultLabelledBy is provided ' , ( ) => {
49+ const { control} = getLabelControl ( { defaultLabelledBy : 'default-id' , label : 'My Label' } ) ;
50+ expect ( control . label ( ) ) . toBe ( 'My Label' ) ;
51+ } ) ;
52+
5053 it ( 'should return undefined if no label is provided' , ( ) => {
5154 const { control} = getLabelControl ( ) ;
5255 expect ( control . label ( ) ) . toBeUndefined ( ) ;
@@ -62,27 +65,27 @@ describe('LabelControl', () => {
6265 } ) ;
6366
6467 describe ( '#labelledBy' , ( ) => {
65- it ( 'should return user-provided labelledBy even if a label is provided' , ( ) => {
68+ it ( 'should return user-provided labelledBy ids even if a label is provided' , ( ) => {
6669 const { control} = getLabelControl ( {
6770 label : 'My Label' ,
68- defaultLabelledBy : [ 'default-id' ] ,
69- labelledBy : [ 'user-id' ] ,
71+ defaultLabelledBy : 'default-id' ,
72+ labelledBy : [ 'user-id' , 'user-id-2' ] ,
7073 } ) ;
71- expect ( control . labelledBy ( ) ) . toEqual ( [ 'user-id' ] ) ;
74+ expect ( control . labelledBy ( ) ) . toEqual ( 'user-id user-id-2' ) ;
7275 } ) ;
7376
7477 it ( 'should return defaultLabelledBy if no user-provided labelledBy exists' , ( ) => {
75- const { control} = getLabelControl ( { defaultLabelledBy : [ 'default-id' ] } ) ;
76- expect ( control . labelledBy ( ) ) . toEqual ( [ 'default-id' ] ) ;
78+ const { control} = getLabelControl ( { defaultLabelledBy : 'default-id' } ) ;
79+ expect ( control . labelledBy ( ) ) . toEqual ( 'default-id' ) ;
7780 } ) ;
7881
7982 it ( 'should update when label changes from undefined to a string' , ( ) => {
8083 const { control, inputs} = getLabelControl ( {
81- defaultLabelledBy : [ 'default-id' ] ,
84+ defaultLabelledBy : 'default-id' ,
8285 } ) ;
83- expect ( control . labelledBy ( ) ) . toEqual ( [ 'default-id' ] ) ;
86+ expect ( control . labelledBy ( ) ) . toEqual ( 'default-id' ) ;
8487 inputs . label . set ( 'A wild label appears' ) ;
85- expect ( control . labelledBy ( ) ) . toEqual ( [ ] ) ;
88+ expect ( control . labelledBy ( ) ) . toEqual ( undefined ) ;
8689 } ) ;
8790 } ) ;
8891} ) ;
0 commit comments