Skip to content

Commit 01348fa

Browse files
committed
add composition tests for providable
1 parent bdb1240 commit 01348fa

File tree

1 file changed

+50
-10
lines changed

1 file changed

+50
-10
lines changed

test/providable.ts

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import {expect, fixture, html} from '@open-wc/testing'
22
import {fake} from 'sinon'
33
import {provide, provideAsync, consume, providable, ContextEvent} from '../src/providable.js'
4+
import {target, targetable} from '../src/targetable.js'
5+
import {attr, attrable} from '../src/attrable.js'
46

57
describe('Providable', () => {
68
const sym = Symbol('bing')
9+
@attrable
10+
@targetable
711
@providable
812
class ProvidableProviderTest extends HTMLElement {
913
@provide foo = 'hello'
@@ -13,6 +17,8 @@ describe('Providable', () => {
1317
}
1418
@provide [sym] = {provided: true}
1519
@provide qux = 8
20+
@provide @attr testAttribute = ''
21+
@provide @target target!: HTMLElement
1622
}
1723
window.customElements.define('providable-provider-test', ProvidableProviderTest)
1824

@@ -52,6 +58,8 @@ describe('Providable', () => {
5258
@consume set qux(value: number) {
5359
this.count += 1
5460
}
61+
@consume target!: HTMLElement
62+
@consume testAttribute = ''
5563
connectedCallback() {
5664
this.textContent = `${this.foo} ${this.bar}`
5765
}
@@ -86,7 +94,7 @@ describe('Providable', () => {
8694
})
8795

8896
it('emits the `context-request` event when connected, for each field', async () => {
89-
expect(events).to.have.callCount(5)
97+
expect(events).to.have.callCount(7)
9098
const fooEvent = events.getCall(0).args[0]
9199
expect(fooEvent).to.be.instanceof(ContextEvent)
92100
expect(fooEvent).to.have.nested.property('context.name', 'foo')
@@ -118,13 +126,27 @@ describe('Providable', () => {
118126
const quxEvent = events.getCall(4).args[0]
119127
expect(quxEvent).to.be.instanceof(ContextEvent)
120128
expect(quxEvent).to.have.nested.property('context.name', 'qux')
121-
expect(quxEvent).to.have.nested.property('context.initialValue').eql(0)
129+
expect(quxEvent).to.have.nested.property('context.initialValue', 0)
122130
expect(quxEvent).to.have.property('multiple', true)
123131
expect(quxEvent).to.have.property('bubbles', true)
132+
133+
const targetEvent = events.getCall(5).args[0]
134+
expect(targetEvent).to.be.instanceof(ContextEvent)
135+
expect(targetEvent).to.have.nested.property('context.name', 'target')
136+
expect(targetEvent).to.have.nested.property('context.initialValue', undefined)
137+
expect(targetEvent).to.have.property('multiple', true)
138+
expect(targetEvent).to.have.property('bubbles', true)
139+
140+
const attrEvent = events.getCall(6).args[0]
141+
expect(attrEvent).to.be.instanceof(ContextEvent)
142+
expect(attrEvent).to.have.nested.property('context.name', 'testAttribute')
143+
expect(attrEvent).to.have.nested.property('context.initialValue', '')
144+
expect(attrEvent).to.have.property('multiple', true)
145+
expect(attrEvent).to.have.property('bubbles', true)
124146
})
125147

126148
it('changes value based on callback new value', async () => {
127-
expect(events).to.have.callCount(5)
149+
expect(events).to.have.callCount(7)
128150
const fooCallback = events.getCall(0).args[0].callback
129151
fooCallback('hello')
130152
expect(instance).to.have.property('foo', 'hello')
@@ -135,7 +157,7 @@ describe('Providable', () => {
135157
it('disposes of past callbacks when given new ones', async () => {
136158
const dispose1 = fake()
137159
const dispose2 = fake()
138-
expect(events).to.have.callCount(5)
160+
expect(events).to.have.callCount(7)
139161
const fooCallback = events.getCall(0).args[0].callback
140162
fooCallback('hello', dispose1)
141163
expect(dispose1).to.have.callCount(0)
@@ -156,10 +178,11 @@ describe('Providable', () => {
156178
let provider: ProvidableProviderTest
157179
beforeEach(async () => {
158180
provider = await fixture(
159-
html`<providable-provider-test
160-
><div>
161-
<span><strong></strong></span></div
162-
></providable-provider-test>`
181+
html`<providable-provider-test>
182+
<div>
183+
<span><strong></strong></span>
184+
</div>
185+
</providable-provider-test>`
163186
)
164187
})
165188

@@ -205,7 +228,7 @@ describe('Providable', () => {
205228
let provider: ProvidableProviderTest
206229
let consumer: ProvidableConsumerTest
207230
beforeEach(async () => {
208-
provider = await fixture(html`<providable-provider-test>
231+
provider = await fixture(html`<providable-provider-test test-attribute="x">
209232
<main>
210233
<article>
211234
<section>
@@ -215,6 +238,7 @@ describe('Providable', () => {
215238
</section>
216239
</article>
217240
</main>
241+
<small data-target="providable-provider-test.target"></small>
218242
</providable-provider-test>`)
219243
consumer = provider.querySelector<ProvidableConsumerTest>('providable-consumer-test')!
220244
})
@@ -224,7 +248,9 @@ describe('Providable', () => {
224248
expect(consumer).to.have.property('bar', 'world')
225249
expect(consumer).to.have.property('baz', 3)
226250
expect(consumer).to.have.property(sym).eql({provided: true})
227-
expect(consumer).to.have.property('qux').eql(8)
251+
expect(consumer).to.have.property('qux', 8)
252+
expect(consumer).to.have.property('target', provider.querySelector('small')!)
253+
expect(consumer).to.have.property('testAttribute', 'x')
228254
})
229255

230256
it('updates values provided if they change', () => {
@@ -234,6 +260,20 @@ describe('Providable', () => {
234260
expect(consumer).to.have.property('foo', 'greetings')
235261
})
236262

263+
it('updates @provide @attr values if they change', async () => {
264+
provider.setAttribute('test-attribute', 'y')
265+
await Promise.resolve()
266+
expect(consumer).to.have.property('testAttribute', 'y')
267+
})
268+
269+
it('updates @provide @target values if they change', async () => {
270+
const big = document.createElement('big')
271+
big.setAttribute('data-target', 'providable-provider-test.target')
272+
provider.prepend(big)
273+
await Promise.resolve()
274+
expect(consumer).to.have.property('target', big)
275+
})
276+
237277
it('calls consumer set callbacks when the value is updated', () => {
238278
expect(consumer).to.have.property('qux', 8)
239279
expect(consumer).to.have.property('count', 1)

0 commit comments

Comments
 (0)