@@ -76,28 +76,39 @@ describe('attributeValueToTypedAttributeValue', () => {
7676 ) ;
7777 } ) ;
7878
79+ describe ( 'homogeneous primitive arrays' , ( ) => {
80+ it . each ( [
81+ [ [ 'foo' , 'bar' ] ] ,
82+ [ [ 1 , 2 , 3 ] ] ,
83+ [ [ true , false , true ] ] ,
84+ [ [ ] as unknown [ ] ] ,
85+ ] ) ( 'emits a typed array attribute for raw value %j' , value => {
86+ const result = attributeValueToTypedAttributeValue ( value ) ;
87+ expect ( result ) . toStrictEqual ( { value, type : 'array' } ) ;
88+ } ) ;
89+
90+ it ( 'emits a typed array attribute for attribute object values' , ( ) => {
91+ const result = attributeValueToTypedAttributeValue ( { value : [ 'foo' , 'bar' ] } ) ;
92+ expect ( result ) . toStrictEqual ( { value : [ 'foo' , 'bar' ] , type : 'array' } ) ;
93+ } ) ;
94+ } ) ;
95+
7996 describe ( 'invalid values (non-primitives)' , ( ) => {
8097 it . each ( [
81- [ 'foo' , 'bar' ] ,
82- [ 1 , 2 , 3 ] ,
83- [ true , false , true ] ,
84- [ 1 , 'foo' , true ] ,
85- { foo : 'bar' } ,
86- ( ) => 'test' ,
87- Symbol ( 'test' ) ,
98+ [ [ 1 , 'foo' , true ] ] ,
99+ [ { foo : 'bar' } ] ,
100+ [ ( ) => 'test' ] ,
101+ [ Symbol ( 'test' ) ] ,
88102 ] ) ( 'returns undefined for non-primitive raw values (%s)' , value => {
89103 const result = attributeValueToTypedAttributeValue ( value ) ;
90104 expect ( result ) . toBeUndefined ( ) ;
91105 } ) ;
92106
93107 it . each ( [
94- [ 'foo' , 'bar' ] ,
95- [ 1 , 2 , 3 ] ,
96- [ true , false , true ] ,
97- [ 1 , 'foo' , true ] ,
98- { foo : 'bar' } ,
99- ( ) => 'test' ,
100- Symbol ( 'test' ) ,
108+ [ [ 1 , 'foo' , true ] ] ,
109+ [ { foo : 'bar' } ] ,
110+ [ ( ) => 'test' ] ,
111+ [ Symbol ( 'test' ) ] ,
101112 ] ) ( 'returns undefined for non-primitive attribute object values (%s)' , value => {
102113 const result = attributeValueToTypedAttributeValue ( { value } ) ;
103114 expect ( result ) . toBeUndefined ( ) ;
@@ -189,26 +200,10 @@ describe('attributeValueToTypedAttributeValue', () => {
189200 } ) ;
190201
191202 describe ( 'invalid values (non-primitives) - stringified fallback' , ( ) => {
192- it ( 'stringifies string arrays' , ( ) => {
193- const result = attributeValueToTypedAttributeValue ( [ 'foo' , 'bar' ] , true ) ;
194- expect ( result ) . toStrictEqual ( {
195- value : '["foo","bar"]' ,
196- type : 'string' ,
197- } ) ;
198- } ) ;
199-
200- it ( 'stringifies number arrays' , ( ) => {
201- const result = attributeValueToTypedAttributeValue ( [ 1 , 2 , 3 ] , true ) ;
203+ it ( 'stringifies mixed-type arrays (not homogeneous)' , ( ) => {
204+ const result = attributeValueToTypedAttributeValue ( [ 'foo' , 1 , true ] , true ) ;
202205 expect ( result ) . toStrictEqual ( {
203- value : '[1,2,3]' ,
204- type : 'string' ,
205- } ) ;
206- } ) ;
207-
208- it ( 'stringifies boolean arrays' , ( ) => {
209- const result = attributeValueToTypedAttributeValue ( [ true , false , true ] , true ) ;
210- expect ( result ) . toStrictEqual ( {
211- value : '[true,false,true]' ,
206+ value : '["foo",1,true]' ,
212207 type : 'string' ,
213208 } ) ;
214209 } ) ;
@@ -425,15 +420,17 @@ describe('serializeAttributes', () => {
425420 describe ( 'invalid (non-primitive) values' , ( ) => {
426421 it ( "doesn't fall back to stringification by default" , ( ) => {
427422 const result = serializeAttributes ( { foo : { some : 'object' } , bar : [ 1 , 2 , 3 ] , baz : ( ) => { } } ) ;
428- expect ( result ) . toStrictEqual ( { } ) ;
423+ expect ( result ) . toStrictEqual ( {
424+ bar : { type : 'array' , value : [ 1 , 2 , 3 ] } ,
425+ } ) ;
429426 } ) ;
430427
431428 it ( 'falls back to stringification of unsupported non-primitive values if fallback is true' , ( ) => {
432429 const result = serializeAttributes ( { foo : { some : 'object' } , bar : [ 1 , 2 , 3 ] , baz : ( ) => { } } , true ) ;
433430 expect ( result ) . toStrictEqual ( {
434431 bar : {
435- type : 'string ' ,
436- value : ' [1,2,3]' ,
432+ type : 'array ' ,
433+ value : [ 1 , 2 , 3 ] ,
437434 } ,
438435 baz : {
439436 type : 'string' ,
@@ -445,5 +442,12 @@ describe('serializeAttributes', () => {
445442 } ,
446443 } ) ;
447444 } ) ;
445+
446+ it ( 'drops mixed-type arrays by default and stringifies them with fallback' , ( ) => {
447+ expect ( serializeAttributes ( { mixed : [ 'a' , 1 ] } ) ) . toStrictEqual ( { } ) ;
448+ expect ( serializeAttributes ( { mixed : [ 'a' , 1 ] } , true ) ) . toStrictEqual ( {
449+ mixed : { type : 'string' , value : '["a",1]' } ,
450+ } ) ;
451+ } ) ;
448452 } ) ;
449453} ) ;
0 commit comments