-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathcontroller.ts
More file actions
141 lines (123 loc) · 4.52 KB
/
Copy pathcontroller.ts
File metadata and controls
141 lines (123 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import {expect, fixture, html} from '@open-wc/testing'
import {replace, fake} from 'sinon'
import {controller} from '../src/controller.js'
import {attr} from '../src/attr.js'
describe('controller', () => {
let instance
it('calls register', async () => {
@controller
class ControllerRegisterElement extends HTMLElement {}
instance = await fixture(html`<controller-register />`)
expect(instance).to.be.instanceof(ControllerRegisterElement)
})
it('adds data-catalyst to elements', async () => {
@controller
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class ControllerDataAttrElement extends HTMLElement {}
instance = await fixture(html`<controller-data-attr />`)
expect(instance.hasAttribute('data-catalyst')).to.equal(true)
expect(instance.getAttribute('data-catalyst')).to.equal('')
})
it('binds controllers before custom connectedCallback behaviour', async () => {
@controller
class ControllerBindOrderElement extends HTMLElement {
foo = fake()
}
@controller
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class ControllerBindOrderSubElement extends HTMLElement {
connectedCallback() {
this.dispatchEvent(new CustomEvent('loaded'))
}
}
instance = await fixture<ControllerBindOrderElement>(html`
<controller-bind-order>
<controller-bind-order-sub data-action="loaded:controller-bind-order#foo" />
</controller-bind-order>
`)
expect(instance.foo).to.have.callCount(1)
})
it('binds shadowRoots after connectedCallback behaviour', async () => {
@controller
class ControllerBindShadowElement extends HTMLElement {
connectedCallback() {
this.attachShadow({mode: 'open'})
const button = document.createElement('button')
button.setAttribute('data-action', 'click:controller-bind-shadow#foo')
this.shadowRoot!.appendChild(button)
}
foo() {
return 'foo'
}
}
instance = await fixture<ControllerBindShadowElement>(html`<controller-bind-shadow />`)
replace(instance, 'foo', fake(instance.foo))
instance.shadowRoot!.querySelector('button')!.click()
expect(instance.foo).to.have.callCount(1)
})
it('binds auto shadowRoots', async () => {
@controller
class ControllerBindAutoShadowElement extends HTMLElement {
foo() {
return 'foo'
}
}
instance = await fixture<ControllerBindAutoShadowElement>(html`
<controller-bind-auto-shadow>
<template data-shadowroot="open">
<button data-action="click:controller-bind-auto-shadow#foo" />
</template>
</controller-bind-auto-shadow>
`)
replace(instance, 'foo', fake(instance.foo))
expect(instance.shadowRoot).to.exist
expect(instance).to.have.property('shadowRoot').not.equal(null)
expect(instance.shadowRoot!.children).to.have.lengthOf(1)
instance.shadowRoot!.querySelector('button')!.click()
expect(instance.foo).to.have.callCount(1)
})
it('upgrades child decendants when connected', async () => {
@controller
// eslint-disable-next-line @typescript-eslint/no-unused-vars
class ChildElementElement extends HTMLElement {}
@controller
class ParentElementElement extends HTMLElement {
connectedCallback() {
const child = this.querySelector('child-element')!
expect(child.matches(':defined')).to.equal(true)
}
}
instance = await fixture<ParentElementElement>(html`
<parent-element>
<child-element />
</parent-element>
`)
})
describe('attrs', () => {
let attrValues: string[] = []
@controller
class AttributeTestElement extends HTMLElement {
foo = 'baz'
attributeChangedCallback() {
attrValues.push(this.getAttribute('data-foo')!)
attrValues.push(this.foo)
}
}
attr(AttributeTestElement.prototype, 'foo')
beforeEach(() => {
attrValues = []
})
it('initializes attrs as attributes in attributeChangedCallback', async () => {
instance = await fixture<AttributeTestElement>(html`<attribute-test></attribute-test>`)
instance.foo = 'bar'
instance.attributeChangedCallback()
expect(attrValues).to.eql(['bar', 'bar'])
})
it('initializes attributes as attrs in attributeChangedCallback', async () => {
instance = await fixture<AttributeTestElement>(html`<attribute-test />`)
instance.setAttribute('data-foo', 'bar')
instance.attributeChangedCallback()
expect(attrValues).to.eql(['bar', 'bar'])
})
})
})