|
| 1 | +import {initializeAttrs, defineObservedAttributes, attr} from '../lib/attr.js' |
| 2 | + |
| 3 | +describe('initializeAttrs', () => { |
| 4 | + class InitializeAttrTestElement extends HTMLElement {} |
| 5 | + window.customElements.define('initialize-attr-test-element', InitializeAttrTestElement) |
| 6 | + |
| 7 | + it('creates a getter/setter pair for each given attr name', () => { |
| 8 | + const instance = document.createElement('initialize-attr-test-element') |
| 9 | + expect(instance).to.not.have.ownPropertyDescriptor('foo') |
| 10 | + initializeAttrs(instance, ['foo']) |
| 11 | + expect(instance).to.have.ownPropertyDescriptor('foo') |
| 12 | + }) |
| 13 | + |
| 14 | + it('reflects the `data-*` attribute name of the given key', () => { |
| 15 | + const instance = document.createElement('initialize-attr-test-element') |
| 16 | + initializeAttrs(instance, ['foo']) |
| 17 | + expect(instance.foo).to.equal('') |
| 18 | + instance.foo = 'bar' |
| 19 | + expect(instance.getAttributeNames()).to.eql(['data-foo']) |
| 20 | + expect(instance.getAttribute('data-foo')).to.equal('bar') |
| 21 | + instance.setAttribute('data-foo', 'baz') |
| 22 | + expect(instance.foo).to.equal('baz') |
| 23 | + }) |
| 24 | + |
| 25 | + it('sets the attribute to a previously defined value on the key', () => { |
| 26 | + const instance = document.createElement('initialize-attr-test-element') |
| 27 | + instance.foo = 'hello' |
| 28 | + initializeAttrs(instance, ['foo']) |
| 29 | + expect(instance.foo).to.equal('hello') |
| 30 | + expect(instance.getAttributeNames()).to.eql(['data-foo']) |
| 31 | + expect(instance.getAttribute('data-foo')).to.equal('hello') |
| 32 | + }) |
| 33 | + |
| 34 | + it('prioritises the value in the attribute over the property', () => { |
| 35 | + const instance = document.createElement('initialize-attr-test-element') |
| 36 | + instance.foo = 'goodbye' |
| 37 | + instance.setAttribute('data-foo', 'hello') |
| 38 | + initializeAttrs(instance, ['foo']) |
| 39 | + expect(instance.foo).to.equal('hello') |
| 40 | + expect(instance.getAttributeNames()).to.eql(['data-foo']) |
| 41 | + expect(instance.getAttribute('data-foo')).to.equal('hello') |
| 42 | + }) |
| 43 | + |
| 44 | + describe('types', () => { |
| 45 | + it('infers number types from property and casts as number always', () => { |
| 46 | + const instance = document.createElement('initialize-attr-test-element') |
| 47 | + instance.foo = 1 |
| 48 | + initializeAttrs(instance, ['foo']) |
| 49 | + expect(instance.foo).to.equal(1) |
| 50 | + expect(instance.getAttributeNames()).to.eql(['data-foo']) |
| 51 | + expect(instance.getAttribute('data-foo')).to.equal('1') |
| 52 | + instance.setAttribute('data-foo', '7') |
| 53 | + expect(instance.foo).to.equal(7) |
| 54 | + instance.setAttribute('data-foo', '-3.14') |
| 55 | + expect(instance.foo).to.equal(-3.14) |
| 56 | + instance.setAttribute('data-foo', 'Not a Number') |
| 57 | + expect(Number.isNaN(instance.foo)).to.equal(true) |
| 58 | + instance.removeAttribute('data-foo') |
| 59 | + expect(instance.foo).to.equal(0) |
| 60 | + instance.foo = 3.14 |
| 61 | + expect(instance.getAttribute('data-foo')).to.equal('3.14') |
| 62 | + }) |
| 63 | + |
| 64 | + it('infers boolean types from property and uses has/toggleAttribute', () => { |
| 65 | + const instance = document.createElement('initialize-attr-test-element') |
| 66 | + instance.foo = false |
| 67 | + initializeAttrs(instance, ['foo']) |
| 68 | + expect(instance.foo).to.equal(false) |
| 69 | + expect(instance.getAttributeNames()).to.eql([]) |
| 70 | + expect(instance.getAttribute('data-foo')).to.equal(null) |
| 71 | + instance.setAttribute('data-foo', '7') |
| 72 | + expect(instance.foo).to.equal(true) |
| 73 | + instance.setAttribute('data-foo', 'hello') |
| 74 | + expect(instance.foo).to.equal(true) |
| 75 | + instance.setAttribute('data-foo', 'false') |
| 76 | + expect(instance.foo).to.equal(true) |
| 77 | + instance.removeAttribute('data-foo') |
| 78 | + expect(instance.foo).to.equal(false) |
| 79 | + instance.foo = '1' |
| 80 | + expect(instance.foo).to.equal(true) |
| 81 | + expect(instance.getAttributeNames()).to.eql(['data-foo']) |
| 82 | + expect(instance.getAttribute('data-foo')).to.equal('') |
| 83 | + instance.foo = false |
| 84 | + expect(instance.getAttributeNames()).to.eql([]) |
| 85 | + }) |
| 86 | + |
| 87 | + it('defaults to inferring string type for non-boolean non-number types', () => { |
| 88 | + const instance = document.createElement('initialize-attr-test-element') |
| 89 | + instance.foo = /^a regexp$/ |
| 90 | + initializeAttrs(instance, ['foo']) |
| 91 | + expect(instance.foo).to.equal('/^a regexp$/') |
| 92 | + expect(instance.getAttributeNames()).to.eql(['data-foo']) |
| 93 | + expect(instance.getAttribute('data-foo')).to.equal('/^a regexp$/') |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + describe('naming', () => { |
| 98 | + it('converts camel cased property names to their HTML dasherized equivalents', () => { |
| 99 | + const instance = document.createElement('initialize-attr-test-element') |
| 100 | + initializeAttrs(instance, ['fooBarBazBing']) |
| 101 | + expect(instance.fooBarBazBing).to.equal('') |
| 102 | + instance.fooBarBazBing = 'bar' |
| 103 | + expect(instance.getAttributeNames()).to.eql(['data-foo-bar-baz-bing']) |
| 104 | + }) |
| 105 | + |
| 106 | + it('will intuitively dasherize acryonyms', () => { |
| 107 | + const instance = document.createElement('initialize-attr-test-element') |
| 108 | + initializeAttrs(instance, ['URLBar']) |
| 109 | + expect(instance.URLBar).to.equal('') |
| 110 | + instance.URLBar = 'bar' |
| 111 | + expect(instance.getAttributeNames()).to.eql(['data-url-bar']) |
| 112 | + }) |
| 113 | + |
| 114 | + it('dasherizes cap suffixed names correctly', () => { |
| 115 | + const instance = document.createElement('initialize-attr-test-element') |
| 116 | + initializeAttrs(instance, ['ClipX']) |
| 117 | + expect(instance.ClipX).to.equal('') |
| 118 | + instance.ClipX = 'bar' |
| 119 | + expect(instance.getAttributeNames()).to.eql(['data-clip-x']) |
| 120 | + }) |
| 121 | + }) |
| 122 | + |
| 123 | + describe('class fields', () => { |
| 124 | + class ClassFieldAttrTestElement extends HTMLElement { |
| 125 | + foo = 1 |
| 126 | + } |
| 127 | + customElements.define('class-field-attr-test-element', ClassFieldAttrTestElement) |
| 128 | + |
| 129 | + it('overrides any getters assigned in constructor (like class fields)', () => { |
| 130 | + const instance = document.createElement('class-field-attr-test-element') |
| 131 | + initializeAttrs(instance, ['foo']) |
| 132 | + instance.foo = 2 |
| 133 | + expect(instance.foo).to.equal(2) |
| 134 | + expect(instance.getAttribute('data-foo')).to.equal('2') |
| 135 | + instance.setAttribute('data-foo', '3') |
| 136 | + expect(instance.foo).to.equal(3) |
| 137 | + }) |
| 138 | + |
| 139 | + it('defaults to class field value attribute not present', () => { |
| 140 | + const instance = document.createElement('class-field-attr-test-element') |
| 141 | + initializeAttrs(instance, ['foo']) |
| 142 | + expect(instance.foo).to.equal(1) |
| 143 | + expect(instance.getAttribute('data-foo')).to.equal('1') |
| 144 | + }) |
| 145 | + |
| 146 | + it('ignores class field value if element has attribute already', () => { |
| 147 | + const instance = document.createElement('class-field-attr-test-element') |
| 148 | + instance.setAttribute('data-foo', '2') |
| 149 | + initializeAttrs(instance, ['foo']) |
| 150 | + expect(instance.foo).to.equal(2) |
| 151 | + expect(instance.getAttribute('data-foo')).to.equal('2') |
| 152 | + }) |
| 153 | + }) |
| 154 | +}) |
| 155 | + |
| 156 | +describe('attr', () => { |
| 157 | + class AttrTestElement extends HTMLElement {} |
| 158 | + window.customElements.define('attr-test-element', AttrTestElement) |
| 159 | + |
| 160 | + it('populates the "default" list for initializeAttrs', () => { |
| 161 | + attr(AttrTestElement.prototype, 'foo') |
| 162 | + attr(AttrTestElement.prototype, 'bar') |
| 163 | + const instance = document.createElement('attr-test-element') |
| 164 | + instance.foo = 'hello' |
| 165 | + initializeAttrs(instance) |
| 166 | + expect(instance).to.have.property('foo', 'hello') |
| 167 | + expect(instance).to.have.property('bar', '') |
| 168 | + expect(instance.getAttributeNames()).to.eql(['data-foo', 'data-bar']) |
| 169 | + expect(instance.getAttribute('data-foo')).to.equal('hello') |
| 170 | + expect(instance.getAttribute('data-bar')).to.equal('') |
| 171 | + }) |
| 172 | + |
| 173 | +}) |
| 174 | + |
| 175 | +describe('defineObservedAttributes', () => { |
| 176 | + it('defines `observedAttributes` getter/setter on class', () => { |
| 177 | + class TestElement extends HTMLElement {} |
| 178 | + defineObservedAttributes(TestElement) |
| 179 | + expect(TestElement).to.have.ownPropertyDescriptor('observedAttributes') |
| 180 | + expect(TestElement.observedAttributes).to.eql([]) |
| 181 | + }) |
| 182 | + |
| 183 | + it('can be set after definition', () => { |
| 184 | + class TestElement extends HTMLElement {} |
| 185 | + defineObservedAttributes(TestElement) |
| 186 | + TestElement.observedAttributes = ['a', 'b', 'c'] |
| 187 | + expect(TestElement.observedAttributes).to.eql(['a', 'b', 'c']) |
| 188 | + }) |
| 189 | + |
| 190 | + it('will reflect values from attr calls', () => { |
| 191 | + class TestElement extends HTMLElement {} |
| 192 | + defineObservedAttributes(TestElement) |
| 193 | + attr(TestElement.prototype, 'foo') |
| 194 | + expect(TestElement.observedAttributes).to.eql(['data-foo']) |
| 195 | + }) |
| 196 | + |
| 197 | + it('will reflect values even if set after definition', () => { |
| 198 | + class TestElement extends HTMLElement {} |
| 199 | + defineObservedAttributes(TestElement) |
| 200 | + attr(TestElement.prototype, 'foo') |
| 201 | + TestElement.observedAttributes = ['a', 'b', 'c'] |
| 202 | + expect(TestElement.observedAttributes).to.eql(['data-foo', 'a', 'b', 'c']) |
| 203 | + }) |
| 204 | +}) |
0 commit comments