Skip to content

Commit 1b6abde

Browse files
committed
refactor test files to use fixture helpers
This makes the tests a little more readable.
1 parent b74432c commit 1b6abde

File tree

5 files changed

+189
-298
lines changed

5 files changed

+189
-298
lines changed

test/attr.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
import {expect} from '@open-wc/testing'
1+
import {expect, fixture, html} from '@open-wc/testing'
22
import {initializeAttrs, defineObservedAttributes, attr} from '../src/attr.js'
33

44
describe('initializeAttrs', () => {
55
class InitializeAttrTestElement extends HTMLElement {}
66
window.customElements.define('initialize-attr-test-element', InitializeAttrTestElement)
77

8+
let instance
9+
beforeEach(async () => {
10+
instance = await fixture(html`<initialize-attr-test-element />`)
11+
})
12+
813
it('creates a getter/setter pair for each given attr name', () => {
9-
const instance = document.createElement('initialize-attr-test-element')
1014
expect(instance).to.not.have.ownPropertyDescriptor('foo')
1115
initializeAttrs(instance, ['foo'])
1216
expect(instance).to.have.ownPropertyDescriptor('foo')
1317
})
1418

1519
it('reflects the `data-*` attribute name of the given key', () => {
16-
const instance = document.createElement('initialize-attr-test-element')
1720
initializeAttrs(instance, ['foo'])
1821
expect(instance.foo).to.equal('')
1922
instance.foo = 'bar'
@@ -24,7 +27,6 @@ describe('initializeAttrs', () => {
2427
})
2528

2629
it('sets the attribute to a previously defined value on the key', () => {
27-
const instance = document.createElement('initialize-attr-test-element')
2830
instance.foo = 'hello'
2931
initializeAttrs(instance, ['foo'])
3032
expect(instance.foo).to.equal('hello')
@@ -33,7 +35,6 @@ describe('initializeAttrs', () => {
3335
})
3436

3537
it('prioritises the value in the attribute over the property', () => {
36-
const instance = document.createElement('initialize-attr-test-element')
3738
instance.foo = 'goodbye'
3839
instance.setAttribute('data-foo', 'hello')
3940
initializeAttrs(instance, ['foo'])
@@ -44,7 +45,6 @@ describe('initializeAttrs', () => {
4445

4546
describe('types', () => {
4647
it('infers number types from property and casts as number always', () => {
47-
const instance = document.createElement('initialize-attr-test-element')
4848
instance.foo = 1
4949
initializeAttrs(instance, ['foo'])
5050
expect(instance.foo).to.equal(1)
@@ -63,7 +63,6 @@ describe('initializeAttrs', () => {
6363
})
6464

6565
it('infers boolean types from property and uses has/toggleAttribute', () => {
66-
const instance = document.createElement('initialize-attr-test-element')
6766
instance.foo = false
6867
initializeAttrs(instance, ['foo'])
6968
expect(instance.foo).to.equal(false)
@@ -86,7 +85,6 @@ describe('initializeAttrs', () => {
8685
})
8786

8887
it('defaults to inferring string type for non-boolean non-number types', () => {
89-
const instance = document.createElement('initialize-attr-test-element')
9088
instance.foo = /^a regexp$/
9189
initializeAttrs(instance, ['foo'])
9290
expect(instance.foo).to.equal('/^a regexp$/')
@@ -97,23 +95,20 @@ describe('initializeAttrs', () => {
9795

9896
describe('naming', () => {
9997
it('converts camel cased property names to their HTML dasherized equivalents', () => {
100-
const instance = document.createElement('initialize-attr-test-element')
10198
initializeAttrs(instance, ['fooBarBazBing'])
10299
expect(instance.fooBarBazBing).to.equal('')
103100
instance.fooBarBazBing = 'bar'
104101
expect(instance.getAttributeNames()).to.eql(['data-foo-bar-baz-bing'])
105102
})
106103

107104
it('will intuitively dasherize acryonyms', () => {
108-
const instance = document.createElement('initialize-attr-test-element')
109105
initializeAttrs(instance, ['URLBar'])
110106
expect(instance.URLBar).to.equal('')
111107
instance.URLBar = 'bar'
112108
expect(instance.getAttributeNames()).to.eql(['data-url-bar'])
113109
})
114110

115111
it('dasherizes cap suffixed names correctly', () => {
116-
const instance = document.createElement('initialize-attr-test-element')
117112
initializeAttrs(instance, ['ClipX'])
118113
expect(instance.ClipX).to.equal('')
119114
instance.ClipX = 'bar'
@@ -127,8 +122,11 @@ describe('initializeAttrs', () => {
127122
}
128123
customElements.define('class-field-attr-test-element', ClassFieldAttrTestElement)
129124

125+
beforeEach(async () => {
126+
instance = await fixture(html`<class-field-attr-test-element />`)
127+
})
128+
130129
it('overrides any getters assigned in constructor (like class fields)', () => {
131-
const instance = document.createElement('class-field-attr-test-element')
132130
initializeAttrs(instance, ['foo'])
133131
instance.foo = 2
134132
expect(instance.foo).to.equal(2)
@@ -138,14 +136,12 @@ describe('initializeAttrs', () => {
138136
})
139137

140138
it('defaults to class field value attribute not present', () => {
141-
const instance = document.createElement('class-field-attr-test-element')
142139
initializeAttrs(instance, ['foo'])
143140
expect(instance.foo).to.equal(1)
144141
expect(instance.getAttribute('data-foo')).to.equal('1')
145142
})
146143

147144
it('ignores class field value if element has attribute already', () => {
148-
const instance = document.createElement('class-field-attr-test-element')
149145
instance.setAttribute('data-foo', '2')
150146
initializeAttrs(instance, ['foo'])
151147
expect(instance.foo).to.equal(2)
@@ -166,8 +162,12 @@ describe('attr', () => {
166162
}
167163
window.customElements.define('extended-attr-test-element', ExtendedAttrTestElement)
168164

165+
let instance
166+
beforeEach(async () => {
167+
instance = await fixture(html`<attr-test-element />`)
168+
})
169+
169170
it('populates the "default" list for initializeAttrs', () => {
170-
const instance = document.createElement('attr-test-element')
171171
instance.foo = 'hello'
172172
initializeAttrs(instance)
173173
expect(instance).to.have.property('foo', 'hello')
@@ -177,8 +177,8 @@ describe('attr', () => {
177177
expect(instance.getAttribute('data-bar')).to.equal('')
178178
})
179179

180-
it('includes attrs from extended elements', () => {
181-
const instance = document.createElement('extended-attr-test-element')
180+
it('includes attrs from extended elements', async () => {
181+
instance = await fixture(html`<extended-attr-test-element />`)
182182
instance.bar = 'hello'
183183
instance.baz = 'world'
184184
initializeAttrs(instance)
@@ -191,8 +191,8 @@ describe('attr', () => {
191191
expect(instance.getAttribute('data-baz')).to.equal('world')
192192
})
193193

194-
it('can be initialized multiple times without error', () => {
195-
const instance = document.createElement('initialize-attr-test-element')
194+
it('can be initialized multiple times without error', async () => {
195+
instance = await fixture(html`<initialize-attr-test-element />`)
196196
expect(instance).to.not.have.ownPropertyDescriptor('foo')
197197
initializeAttrs(instance, ['foo'])
198198
expect(instance).to.have.ownPropertyDescriptor('foo')

test/auto-shadow-root.ts

Lines changed: 29 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,63 @@
1-
import {expect} from '@open-wc/testing'
1+
import {expect, fixture, html} from '@open-wc/testing'
22
import {replace, fake} from 'sinon'
33
import {autoShadowRoot} from '../src/auto-shadow-root.js'
44

55
describe('autoShadowRoot', () => {
6-
window.customElements.define('autoshadowroot-test-element', class extends HTMLElement {})
6+
window.customElements.define('shadowroot-test-element', class extends HTMLElement {})
77

8-
let root
9-
10-
beforeEach(() => {
11-
root = document.createElement('div')
12-
document.body.appendChild(root)
13-
})
14-
15-
afterEach(() => {
16-
root.remove()
8+
let instance
9+
beforeEach(async () => {
10+
instance = await fixture(html`<shadowroot-test-element />`)
1711
})
1812

19-
it('automatically declares shadowroot for elements with `template[data-shadowroot]` children', () => {
20-
const instance = document.createElement('shadowroot-test-element')
21-
const template = document.createElement('template')
22-
template.innerHTML = 'Hello World'
23-
template.setAttribute('data-shadowroot', 'open')
24-
instance.appendChild(template)
25-
13+
it('automatically declares shadowroot for elements with `template[data-shadowroot]` children', async () => {
14+
instance = await fixture(html`<shadowroot-test-element>
15+
<template data-shadowroot="open">Hello World</template>
16+
</shadowroot-test-element>`)
2617
autoShadowRoot(instance)
2718

2819
expect(instance).to.have.property('shadowRoot').not.equal(null)
2920
expect(instance.shadowRoot.textContent).to.equal('Hello World')
3021
})
3122

32-
it('does not attach shadowroot without a template`data-shadowroot` child', () => {
33-
const instance = document.createElement('shadowroot-test-element')
34-
const template = document.createElement('template')
35-
template.setAttribute('data-notshadowroot', 'open')
36-
const otherTemplate = document.createElement('div')
37-
otherTemplate.setAttribute('data-shadowroot', 'open')
38-
instance.appendChild(template, otherTemplate)
23+
it('does not attach shadowroot without a template`data-shadowroot` child', async () => {
24+
instance = await fixture(html`<shadowroot-test-element>
25+
<template data-notshadowroot="open">Hello</template>
26+
<div data-shadowroot="open">World</div>
27+
</shadowroot-test-element>`)
3928

4029
autoShadowRoot(instance)
4130

4231
expect(instance).to.have.property('shadowRoot').equal(null)
4332
})
4433

45-
it('does not attach shadowroots which are not direct children of the element', () => {
46-
const instance = document.createElement('shadowroot-test-element')
47-
const div = document.createElement('div')
48-
const template = document.createElement('template')
49-
template.setAttribute('data-notshadowroot', 'open')
50-
div.appendChild(template)
51-
instance.appendChild(div)
34+
it('does not attach shadowroots which are not direct children of the element', async () => {
35+
instance = await fixture(html`<shadowroot-test-element>
36+
<div>
37+
<template data-shadowroot="open">Hello World</template>
38+
</div>
39+
</shadowroot-test-element>`)
5240

5341
autoShadowRoot(instance)
5442

5543
expect(instance).to.have.property('shadowRoot').equal(null)
5644
})
5745

58-
it('attaches shadowRoot nodes open by default', () => {
59-
const instance = document.createElement('shadowroot-test-element')
60-
const template = document.createElement('template')
61-
template.innerHTML = 'Hello World'
62-
template.setAttribute('data-shadowroot', '')
63-
instance.appendChild(template)
46+
it('attaches shadowRoot nodes open by default', async () => {
47+
instance = await fixture(html`<shadowroot-test-element>
48+
<template data-shadowroot>Hello World</template>
49+
</shadowroot-test-element>`)
6450

6551
autoShadowRoot(instance)
6652

6753
expect(instance).to.have.property('shadowRoot').not.equal(null)
6854
expect(instance.shadowRoot.textContent).to.equal('Hello World')
6955
})
7056

71-
it('attaches shadowRoot nodes closed if `data-shadowroot` is `closed`', () => {
72-
const instance = document.createElement('shadowroot-test-element')
73-
const template = document.createElement('template')
74-
template.innerHTML = 'Hello World'
75-
template.setAttribute('data-shadowroot', 'closed')
76-
instance.appendChild(template)
77-
57+
it('attaches shadowRoot nodes closed if `data-shadowroot` is `closed`', async () => {
58+
instance = await fixture(html`<shadowroot-test-element>
59+
<template data-shadowroot="closed">Hello World</template>
60+
</shadowroot-test-element>`)
7861
let shadowRoot = null
7962
replace(
8063
instance,
@@ -89,6 +72,6 @@ describe('autoShadowRoot', () => {
8972

9073
expect(instance).to.have.property('shadowRoot').equal(null)
9174
expect(instance.attachShadow).to.have.been.calledOnceWith({mode: 'closed'})
92-
expect(shadowRoot.textContent).to.equal('Hello World')
75+
expect(shadowRoot!.textContent).to.equal('Hello World')
9376
})
9477
})

0 commit comments

Comments
 (0)