@@ -12,6 +12,10 @@ const invalidProperties = Array.from(implementedProperties).filter(
12
12
prop => ! allowedProperties . includes ( parsers . dashedToCamelCase ( prop ) )
13
13
) ;
14
14
15
+ if ( typeof BigInt === 'undefined' ) {
16
+ var BigInt = Number ;
17
+ }
18
+
15
19
describe ( 'CSSStyleDeclaration' , ( ) => {
16
20
test ( 'has only valid properties implemented' , ( ) => {
17
21
expect ( invalidProperties . length ) . toEqual ( 0 ) ;
@@ -352,6 +356,56 @@ describe('CSSStyleDeclaration', () => {
352
356
expect ( style . fillOpacity ) . toEqual ( '0' ) ;
353
357
} ) ;
354
358
359
+ test ( 'setting a property with a value that can not be converted to string should throw an error' , ( ) => {
360
+ const style = new CSSStyleDeclaration ( ) ;
361
+
362
+ expect ( ( ) => ( style . opacity = Symbol ( '0' ) ) ) . toThrow ( 'Cannot convert symbol to string' ) ;
363
+ expect ( ( ) => ( style . opacity = { toString : ( ) => [ 0 ] } ) ) . toThrow (
364
+ 'Cannot convert object to primitive value'
365
+ ) ;
366
+ } ) ;
367
+
368
+ test ( 'setting a property with a value that can be converted to string should work' , ( ) => {
369
+ const style = new CSSStyleDeclaration ( ) ;
370
+
371
+ // Property with custom setter
372
+ style . borderStyle = { toString : ( ) => 'solid' } ;
373
+ expect ( style . borderStyle ) . toEqual ( 'solid' ) ;
374
+
375
+ // Property with default setter
376
+ style . opacity = 1 ;
377
+ expect ( style . opacity ) . toBe ( '1' ) ;
378
+ style . opacity = { toString : ( ) => '0' } ;
379
+ expect ( style . opacity ) . toBe ( '0' ) ;
380
+
381
+ style . opacity = { toString : ( ) => 1 } ;
382
+ expect ( style . opacity ) . toEqual ( '1' ) ;
383
+
384
+ style . opacity = BigInt ( 0 ) ;
385
+ expect ( style . opacity ) . toBe ( '0' ) ;
386
+ style . opacity = { toString : ( ) => BigInt ( 1 ) } ;
387
+ expect ( style . opacity ) . toEqual ( '1' ) ;
388
+
389
+ style . setProperty ( '--custom' , [ 0 ] ) ;
390
+ expect ( style . getPropertyValue ( '--custom' ) ) . toEqual ( '0' ) ;
391
+
392
+ style . setProperty ( '--custom' , null ) ;
393
+ expect ( style . getPropertyValue ( '--custom' ) ) . toBe ( '' ) ;
394
+ style . setProperty ( '--custom' , { toString : ( ) => null } ) ;
395
+ expect ( style . getPropertyValue ( '--custom' ) ) . toBe ( 'null' ) ;
396
+
397
+ style . setProperty ( '--custom' , undefined ) ;
398
+ expect ( style . getPropertyValue ( '--custom' ) ) . toBe ( 'undefined' ) ;
399
+ style . setProperty ( '--custom' , null ) ;
400
+ style . setProperty ( '--custom' , { toString : ( ) => undefined } ) ;
401
+ expect ( style . getPropertyValue ( '--custom' ) ) . toBe ( 'undefined' ) ;
402
+
403
+ style . setProperty ( '--custom' , false ) ;
404
+ expect ( style . getPropertyValue ( '--custom' ) ) . toBe ( 'false' ) ;
405
+ style . setProperty ( '--custom' , { toString : ( ) => true } ) ;
406
+ expect ( style . getPropertyValue ( '--custom' ) ) . toBe ( 'true' ) ;
407
+ } ) ;
408
+
355
409
test ( 'onchange callback should be called when the csstext changes' , ( ) => {
356
410
var style = new CSSStyleDeclaration ( function ( cssText ) {
357
411
expect ( cssText ) . toEqual ( 'opacity: 0;' ) ;
0 commit comments