|
| 1 | +import { expect } from '@vaadin/chai-plugins'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import { registerCSSProperty } from '../src/css-utils.js'; |
| 4 | + |
| 5 | +describe('registerCSSProperty', () => { |
| 6 | + let warn; |
| 7 | + let counter = 0; |
| 8 | + |
| 9 | + // CSS property registration is global and cannot be undone within a page, |
| 10 | + // so use a unique name per test to keep tests order-independent. |
| 11 | + function uniqueName() { |
| 12 | + counter += 1; |
| 13 | + return `--test-register-style-property-${counter}`; |
| 14 | + } |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + warn = sinon.stub(console, 'warn'); |
| 18 | + }); |
| 19 | + |
| 20 | + afterEach(() => { |
| 21 | + warn.restore(); |
| 22 | + }); |
| 23 | + |
| 24 | + it('should register a property once without warning', () => { |
| 25 | + const name = uniqueName(); |
| 26 | + registerCSSProperty({ name, syntax: '<number>', inherits: false, initialValue: '0' }); |
| 27 | + |
| 28 | + expect(warn.called).to.be.false; |
| 29 | + }); |
| 30 | + |
| 31 | + it('should not throw when the same property is registered twice', () => { |
| 32 | + const name = uniqueName(); |
| 33 | + const definition = { name, syntax: '<number>', inherits: false, initialValue: '0' }; |
| 34 | + |
| 35 | + registerCSSProperty(definition); |
| 36 | + expect(() => registerCSSProperty(definition)).to.not.throw(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should warn when the same property is registered twice', () => { |
| 40 | + const name = uniqueName(); |
| 41 | + const definition = { name, syntax: '<number>', inherits: false, initialValue: '0' }; |
| 42 | + |
| 43 | + registerCSSProperty(definition); |
| 44 | + registerCSSProperty(definition); |
| 45 | + |
| 46 | + expect(warn.calledOnce).to.be.true; |
| 47 | + expect(warn.firstCall.args[0]).to.include(name); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should rethrow errors other than InvalidModificationError', () => { |
| 51 | + expect(() => registerCSSProperty({ name: uniqueName(), syntax: '<not-a-syntax>' })).to.throw(); |
| 52 | + }); |
| 53 | +}); |
0 commit comments