@@ -37,41 +37,21 @@ describe('attributeValueToTypedAttributeValue', () => {
3737 } ) ;
3838
3939 describe ( 'arrays' , ( ) => {
40- it ( 'converts an array of strings to a typed attribute value' , ( ) => {
41- const result = attributeValueToTypedAttributeValue ( [ 'foo' , 'bar' ] ) ;
42- expect ( result ) . toStrictEqual ( {
43- value : [ 'foo' , 'bar' ] ,
44- type : 'string[]' ,
45- } ) ;
46- } ) ;
47-
48- it ( 'converts an array of integer numbers to a typed attribute value' , ( ) => {
49- const result = attributeValueToTypedAttributeValue ( [ 1 , 2 , 3 ] ) ;
50- expect ( result ) . toStrictEqual ( {
51- value : [ 1 , 2 , 3 ] ,
52- type : 'integer[]' ,
53- } ) ;
54- } ) ;
55-
56- it ( 'converts an array of double numbers to a typed attribute value' , ( ) => {
57- const result = attributeValueToTypedAttributeValue ( [ 1.1 , 2.2 , 3.3 ] ) ;
58- expect ( result ) . toStrictEqual ( {
59- value : [ 1.1 , 2.2 , 3.3 ] ,
60- type : 'double[]' ,
61- } ) ;
62- } ) ;
63-
64- it ( 'converts an array of booleans to a typed attribute value' , ( ) => {
65- const result = attributeValueToTypedAttributeValue ( [ true , false , true ] ) ;
66- expect ( result ) . toStrictEqual ( {
67- value : [ true , false , true ] ,
68- type : 'boolean[]' ,
69- } ) ;
40+ it . each ( [
41+ [ 'foo' , 'bar' ] ,
42+ [ 1 , 2 , 3 ] ,
43+ [ true , false , true ] ,
44+ [ 1 , 'foo' , true ] ,
45+ { foo : 'bar' } ,
46+ ( ) => 'test' ,
47+ Symbol ( 'test' ) ,
48+ ] ) ( 'returns undefined for none-primitive values (%s)' , value => {
49+ const result = attributeValueToTypedAttributeValue ( value ) ;
50+ expect ( result ) . toBeUndefined ( ) ;
7051 } ) ;
7152 } ) ;
7253
7354 describe ( 'attribute objects without units' , ( ) => {
74- // Note: These tests only test exemplar type and fallback behaviour (see above for more cases)
7555 it ( 'converts a primitive value to a typed attribute value' , ( ) => {
7656 const result = attributeValueToTypedAttributeValue ( { value : 123.45 } ) ;
7757 expect ( result ) . toStrictEqual ( {
@@ -80,20 +60,17 @@ describe('attributeValueToTypedAttributeValue', () => {
8060 } ) ;
8161 } ) ;
8262
83- it ( 'converts an array of primitive values to a typed attribute value' , ( ) => {
84- const result = attributeValueToTypedAttributeValue ( { value : [ true , false ] } ) ;
85- expect ( result ) . toStrictEqual ( {
86- value : [ true , false ] ,
87- type : 'boolean[]' ,
88- } ) ;
89- } ) ;
90-
91- it ( 'converts an unsupported object value to a string attribute value' , ( ) => {
92- const result = attributeValueToTypedAttributeValue ( { value : { foo : 'bar' } } ) ;
93- expect ( result ) . toStrictEqual ( {
94- value : '{"foo":"bar"}' ,
95- type : 'string' ,
96- } ) ;
63+ it . each ( [
64+ [ 'foo' , 'bar' ] ,
65+ [ 1 , 2 , 3 ] ,
66+ [ true , false , true ] ,
67+ [ 1 , 'foo' , true ] ,
68+ { foo : 'bar' } ,
69+ ( ) => 'test' ,
70+ Symbol ( 'test' ) ,
71+ ] ) ( 'returns undefined for none-primitive values (%s)' , value => {
72+ const result = attributeValueToTypedAttributeValue ( { value } ) ;
73+ expect ( result ) . toBeUndefined ( ) ;
9774 } ) ;
9875 } ) ;
9976
@@ -108,24 +85,6 @@ describe('attributeValueToTypedAttributeValue', () => {
10885 } ) ;
10986 } ) ;
11087
111- it ( 'converts an array of primitive values to a typed attribute value' , ( ) => {
112- const result = attributeValueToTypedAttributeValue ( { value : [ true , false ] , unit : 'count' } ) ;
113- expect ( result ) . toStrictEqual ( {
114- value : [ true , false ] ,
115- type : 'boolean[]' ,
116- unit : 'count' ,
117- } ) ;
118- } ) ;
119-
120- it ( 'converts an unsupported object value to a string attribute value' , ( ) => {
121- const result = attributeValueToTypedAttributeValue ( { value : { foo : 'bar' } , unit : 'bytes' } ) ;
122- expect ( result ) . toStrictEqual ( {
123- value : '{"foo":"bar"}' ,
124- type : 'string' ,
125- unit : 'bytes' ,
126- } ) ;
127- } ) ;
128-
12988 it ( 'extracts the value property of an object with a value property' , ( ) => {
13089 // and ignores other properties.
13190 // For now we're fine with this but we may reconsider in the future.
@@ -138,114 +97,6 @@ describe('attributeValueToTypedAttributeValue', () => {
13897 } ) ;
13998 } ) ;
14099
141- describe ( 'unsupported value types' , ( ) => {
142- it ( 'stringifies mixed float and integer numbers to a string attribute value' , ( ) => {
143- const result = attributeValueToTypedAttributeValue ( [ 1 , 2.2 , 3 ] ) ;
144- expect ( result ) . toStrictEqual ( {
145- value : '[1,2.2,3]' ,
146- type : 'string' ,
147- } ) ;
148- } ) ;
149-
150- it ( 'stringifies an array of allowed but incoherent types to a string attribute value' , ( ) => {
151- const result = attributeValueToTypedAttributeValue ( [ 1 , 'foo' , true ] ) ;
152- expect ( result ) . toStrictEqual ( {
153- value : '[1,"foo",true]' ,
154- type : 'string' ,
155- } ) ;
156- } ) ;
157-
158- it ( 'stringifies an array of disallowed and incoherent types to a string attribute value' , ( ) => {
159- const result = attributeValueToTypedAttributeValue ( [ null , undefined , NaN ] ) ;
160- expect ( result ) . toStrictEqual ( {
161- value : '[null,null,null]' ,
162- type : 'string' ,
163- } ) ;
164- } ) ;
165-
166- it ( 'stringifies an object value to a string attribute value' , ( ) => {
167- const result = attributeValueToTypedAttributeValue ( { foo : 'bar' } ) ;
168- expect ( result ) . toStrictEqual ( {
169- value : '{"foo":"bar"}' ,
170- type : 'string' ,
171- } ) ;
172- } ) ;
173-
174- it ( 'stringifies a null value to a string attribute value' , ( ) => {
175- const result = attributeValueToTypedAttributeValue ( null ) ;
176- expect ( result ) . toStrictEqual ( {
177- value : 'null' ,
178- type : 'string' ,
179- } ) ;
180- } ) ;
181-
182- it ( 'stringifies an undefined value to a string attribute value' , ( ) => {
183- const result = attributeValueToTypedAttributeValue ( undefined ) ;
184- expect ( result ) . toStrictEqual ( {
185- value : 'undefined' ,
186- type : 'string' ,
187- } ) ;
188- } ) ;
189-
190- it ( 'stringifies an NaN number value to a string attribute value' , ( ) => {
191- const result = attributeValueToTypedAttributeValue ( NaN ) ;
192- expect ( result ) . toStrictEqual ( {
193- value : 'null' ,
194- type : 'string' ,
195- } ) ;
196- } ) ;
197-
198- it ( 'converts an object toString if stringification fails' , ( ) => {
199- const result = attributeValueToTypedAttributeValue ( {
200- value : {
201- toJson : ( ) => {
202- throw new Error ( 'test' ) ;
203- } ,
204- } ,
205- } ) ;
206- expect ( result ) . toStrictEqual ( {
207- value : '{}' ,
208- type : 'string' ,
209- } ) ;
210- } ) ;
211-
212- it ( 'falls back to an empty string if stringification and toString fails' , ( ) => {
213- const result = attributeValueToTypedAttributeValue ( {
214- value : {
215- toJSON : ( ) => {
216- throw new Error ( 'test' ) ;
217- } ,
218- toString : ( ) => {
219- throw new Error ( 'test' ) ;
220- } ,
221- } ,
222- } ) ;
223- expect ( result ) . toStrictEqual ( {
224- value : '' ,
225- type : 'string' ,
226- } ) ;
227- } ) ;
228-
229- it ( 'converts a function toString ' , ( ) => {
230- const result = attributeValueToTypedAttributeValue ( ( ) => {
231- return 'test' ;
232- } ) ;
233-
234- expect ( result ) . toStrictEqual ( {
235- value : '() => {\n return "test";\n }' ,
236- type : 'string' ,
237- } ) ;
238- } ) ;
239-
240- it ( 'converts a symbol toString' , ( ) => {
241- const result = attributeValueToTypedAttributeValue ( Symbol ( 'test' ) ) ;
242- expect ( result ) . toStrictEqual ( {
243- value : 'Symbol(test)' ,
244- type : 'string' ,
245- } ) ;
246- } ) ;
247- } ) ;
248-
249100 it . each ( [ 1 , true , null , undefined , NaN , Symbol ( 'test' ) , { foo : 'bar' } ] ) (
250101 'ignores invalid (non-string) units (%s)' ,
251102 unit => {
0 commit comments