Skip to content

Commit b01539a

Browse files
committed
test: conversion of value to DOM String
1 parent 90f87f2 commit b01539a

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
node: true,
1212
},
1313
globals: {
14+
BigInt: true,
1415
exports: true,
1516
module: true,
1617
require: true,

lib/CSSStyleDeclaration.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ const invalidProperties = Array.from(implementedProperties).filter(
1212
prop => !allowedProperties.includes(parsers.dashedToCamelCase(prop))
1313
);
1414

15+
if (typeof BigInt === 'undefined') {
16+
var BigInt = Number;
17+
}
18+
1519
describe('CSSStyleDeclaration', () => {
1620
test('has only valid properties implemented', () => {
1721
expect(invalidProperties.length).toEqual(0);
@@ -352,6 +356,56 @@ describe('CSSStyleDeclaration', () => {
352356
expect(style.fillOpacity).toEqual('0');
353357
});
354358

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+
355409
test('onchange callback should be called when the csstext changes', () => {
356410
var style = new CSSStyleDeclaration(function(cssText) {
357411
expect(cssText).toEqual('opacity: 0;');

0 commit comments

Comments
 (0)