|
1 |
| -import {expect, fixture, html} from '@open-wc/testing' |
2 |
| -import {restore, fake} from 'sinon' |
3 | 1 | import type {CustomElement} from '../src/custom-element.js'
|
4 |
| -import {createAbility, attachShadowCallback, attachInternalsCallback} from '../src/ability.js' |
| 2 | +import {expect, fixture, html} from '@open-wc/testing' |
| 3 | +import {restore} from 'sinon' |
| 4 | +import {createAbility} from '../src/ability.js' |
5 | 5 |
|
6 | 6 | describe('ability', () => {
|
7 |
| - let calls: string[] = [] |
| 7 | + const calls: string[] = [] |
8 | 8 | const fakeable = createAbility(
|
9 | 9 | Class =>
|
10 | 10 | class extends Class {
|
@@ -91,189 +91,4 @@ describe('ability', () => {
|
91 | 91 | instance.connectedCallback!()
|
92 | 92 | expect(calls).to.eql(['fakeable connectedCallback', 'fakeable connectedCallback'])
|
93 | 93 | })
|
94 |
| - |
95 |
| - describe('subclass behaviour', () => { |
96 |
| - const CoreTest = otherfakeable(fakeable(Element)) |
97 |
| - customElements.define('core-test', CoreTest) |
98 |
| - |
99 |
| - let instance: CustomElement & typeof CoreTest |
100 |
| - beforeEach(async () => { |
101 |
| - instance = await fixture(html`<core-test />`) |
102 |
| - }) |
103 |
| - |
104 |
| - it('applies keys from delegate onto subclass upon instantiation', () => { |
105 |
| - expect(instance).to.have.property('foo') |
106 |
| - expect((instance as unknown as Record<string, () => void>).foo()).to.equal('foo!') |
107 |
| - expect(instance).to.have.property('bar') |
108 |
| - expect((instance as unknown as Record<string, () => void>).bar()).to.equal('bar!') |
109 |
| - }) |
110 |
| - |
111 |
| - for (const method of ['connectedCallback', 'disconnectedCallback', 'adoptedCallback', 'attributeChangedCallback']) { |
112 |
| - it(`delegates to other ${method}s before class ${method}`, () => { |
113 |
| - calls = [] |
114 |
| - ;(instance as unknown as Record<string, () => void>)[method]() |
115 |
| - expect(calls).to.eql([`otherfakeable ${method}`, `fakeable ${method}`]) |
116 |
| - }) |
117 |
| - } |
118 |
| - }) |
119 |
| - |
120 |
| - describe('ability extension behaviour', () => { |
121 |
| - describe('attachShadowCallback', () => { |
122 |
| - let attachShadowFake: (shadow: ShadowRoot) => void |
123 |
| - let shadow: ShadowRoot | null |
124 |
| - beforeEach(() => { |
125 |
| - shadow = null |
126 |
| - attachShadowFake = fake() |
127 |
| - }) |
128 |
| - |
129 |
| - const declarable = createAbility( |
130 |
| - Class => |
131 |
| - class extends Class { |
132 |
| - [attachShadowCallback](...args: [ShadowRoot]) { |
133 |
| - super[attachShadowCallback]!(...args) |
134 |
| - return attachShadowFake.apply(this, args) |
135 |
| - } |
136 |
| - } |
137 |
| - ) |
138 |
| - customElements.define( |
139 |
| - 'declarative-shadow-ability', |
140 |
| - declarable( |
141 |
| - class extends HTMLElement { |
142 |
| - constructor() { |
143 |
| - super() |
144 |
| - // Declarative shadows run before constructor() is available, but |
145 |
| - // abilities run after element constructor |
146 |
| - shadow = HTMLElement.prototype.attachShadow.call(this, {mode: 'closed'}) |
147 |
| - } |
148 |
| - } |
149 |
| - ) |
150 |
| - ) |
151 |
| - customElements.define( |
152 |
| - 'closed-shadow-ability', |
153 |
| - declarable( |
154 |
| - class extends HTMLElement { |
155 |
| - constructor() { |
156 |
| - super() |
157 |
| - shadow = this.attachShadow({mode: 'closed'}) |
158 |
| - } |
159 |
| - } |
160 |
| - ) |
161 |
| - ) |
162 |
| - customElements.define( |
163 |
| - 'connected-shadow-ability', |
164 |
| - declarable( |
165 |
| - class extends HTMLElement { |
166 |
| - connectedCallback() { |
167 |
| - shadow = this.attachShadow({mode: 'closed'}) |
168 |
| - } |
169 |
| - } |
170 |
| - ) |
171 |
| - ) |
172 |
| - customElements.define('manual-shadow-ability', declarable(class extends HTMLElement {})) |
173 |
| - |
174 |
| - customElements.define( |
175 |
| - 'disallowed-shadow-ability', |
176 |
| - declarable( |
177 |
| - class extends HTMLElement { |
178 |
| - static disabledFeatures = ['shadow'] |
179 |
| - } |
180 |
| - ) |
181 |
| - ) |
182 |
| - |
183 |
| - it('is called with shadowRoot of declarative ShadowDOM', async () => { |
184 |
| - const instance = await fixture(html`<declarative-shadow-ability></declarative-shadow-ability>`) |
185 |
| - expect(shadow).to.exist.and.be.instanceof(ShadowRoot) |
186 |
| - expect(attachShadowFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(shadow) |
187 |
| - }) |
188 |
| - |
189 |
| - it('is called with shadowRoot from attachShadow call', async () => { |
190 |
| - const instance = await fixture(html`<manual-shadow-ability></manual-shadow-ability>`) |
191 |
| - shadow = instance.attachShadow({mode: 'closed'}) |
192 |
| - expect(shadow).to.exist.and.be.instanceof(ShadowRoot) |
193 |
| - expect(attachShadowFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(shadow) |
194 |
| - }) |
195 |
| - |
196 |
| - it('is called with shadowRoot from attachInternals call', async () => { |
197 |
| - const instance = await fixture(html`<closed-shadow-ability></closed-shadow-ability>`) |
198 |
| - expect(shadow).to.exist.and.be.instanceof(ShadowRoot) |
199 |
| - expect(attachShadowFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(shadow) |
200 |
| - }) |
201 |
| - |
202 |
| - it('is called with shadowRoot from connectedCallback', async () => { |
203 |
| - const instance = await fixture(html`<connected-shadow-ability></connected-shadow-ability>`) |
204 |
| - expect(shadow).to.exist.and.be.instanceof(ShadowRoot) |
205 |
| - expect(attachShadowFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(shadow) |
206 |
| - }) |
207 |
| - |
208 |
| - it('does not error if shadowdom is disabled', async () => { |
209 |
| - await fixture(html`<disabled-shadow-ability></disabled-shadow-ability>`) |
210 |
| - expect(attachShadowFake).to.be.have.callCount(0) |
211 |
| - }) |
212 |
| - }) |
213 |
| - |
214 |
| - describe('attachInternalsCallback', () => { |
215 |
| - let attachInternalsFake: (internals: ElementInternals) => void |
216 |
| - let internals: ElementInternals | null |
217 |
| - beforeEach(() => { |
218 |
| - internals = null |
219 |
| - attachInternalsFake = fake() |
220 |
| - }) |
221 |
| - |
222 |
| - const internable = createAbility( |
223 |
| - Class => |
224 |
| - class extends Class { |
225 |
| - [attachInternalsCallback](...args: [ElementInternals]) { |
226 |
| - super[attachInternalsCallback]!(...args) |
227 |
| - return attachInternalsFake.apply(this, args) |
228 |
| - } |
229 |
| - } |
230 |
| - ) |
231 |
| - customElements.define( |
232 |
| - 'internals-ability', |
233 |
| - internable( |
234 |
| - class extends HTMLElement { |
235 |
| - constructor() { |
236 |
| - super() |
237 |
| - internals = this.attachInternals() |
238 |
| - } |
239 |
| - } |
240 |
| - ) |
241 |
| - ) |
242 |
| - customElements.define('manual-internals-ability', internable(class extends HTMLElement {})) |
243 |
| - |
244 |
| - customElements.define( |
245 |
| - 'disallowed-internals-ability', |
246 |
| - internable( |
247 |
| - class extends HTMLElement { |
248 |
| - static disabledFeatures = ['internals'] |
249 |
| - } |
250 |
| - ) |
251 |
| - ) |
252 |
| - |
253 |
| - it('is called on constructor', async () => { |
254 |
| - const instance = await fixture(html`<manual-internals-ability></manual-internals-ability>`) |
255 |
| - expect(attachInternalsFake).to.be.calledOnce.calledOn(instance) |
256 |
| - }) |
257 |
| - |
258 |
| - it('does not prevent attachInternals being called by userland class', async () => { |
259 |
| - const instance = await fixture(html`<internals-ability></internals-ability>`) |
260 |
| - expect(internals).to.exist.and.be.instanceof(ElementInternals) |
261 |
| - expect(attachInternalsFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(internals) |
262 |
| - }) |
263 |
| - |
264 |
| - it('errors if userland calls attachInternals more than once', async () => { |
265 |
| - const instance = await fixture<CustomElement>(html`<manual-internals-ability></manual-internals-ability>`) |
266 |
| - internals = instance.attachInternals() |
267 |
| - expect(internals).to.exist.and.be.instanceof(ElementInternals) |
268 |
| - expect(attachInternalsFake).to.be.calledOnce.calledOn(instance).and.calledWithExactly(internals) |
269 |
| - |
270 |
| - expect(() => instance.attachInternals()).to.throw(DOMException) |
271 |
| - }) |
272 |
| - |
273 |
| - it('does not error if element internals are disabled', async () => { |
274 |
| - await fixture(html`<disallowed-internals-ability></disallowed-internals-ability>`) |
275 |
| - expect(attachInternalsFake).to.have.callCount(0) |
276 |
| - }) |
277 |
| - }) |
278 |
| - }) |
279 | 94 | })
|
0 commit comments