1
1
import { expect , fixture , html } from '@open-wc/testing'
2
2
import { fake } from 'sinon'
3
3
import { provide , consume , providable , ContextEvent } from '../src/providable.js'
4
+ import { target , targetable } from '../src/targetable.js'
5
+ import { attr , attrable } from '../src/attrable.js'
4
6
5
7
describe ( 'Providable' , ( ) => {
6
8
const sym = Symbol ( 'bing' )
9
+ @attrable
10
+ @targetable
7
11
@providable
8
12
class ProvidableProviderTest extends HTMLElement {
9
13
@provide foo = 'hello'
@@ -13,6 +17,8 @@ describe('Providable', () => {
13
17
}
14
18
@provide [ sym ] = { provided : true }
15
19
@provide qux = 8
20
+ @provide @attr testAttribute = ''
21
+ @provide @target target ! : HTMLElement
16
22
}
17
23
window . customElements . define ( 'providable-provider-test' , ProvidableProviderTest )
18
24
@@ -40,6 +46,8 @@ describe('Providable', () => {
40
46
@consume set qux ( value : number ) {
41
47
this . count += 1
42
48
}
49
+ @consume target ! : HTMLElement
50
+ @consume testAttribute = ''
43
51
connectedCallback ( ) {
44
52
this . textContent = `${ this . foo } ${ this . bar } `
45
53
}
@@ -74,7 +82,7 @@ describe('Providable', () => {
74
82
} )
75
83
76
84
it ( 'emits the `context-request` event when connected, for each field' , async ( ) => {
77
- expect ( events ) . to . have . callCount ( 5 )
85
+ expect ( events ) . to . have . callCount ( 7 )
78
86
const fooEvent = events . getCall ( 0 ) . args [ 0 ]
79
87
expect ( fooEvent ) . to . be . instanceof ( ContextEvent )
80
88
expect ( fooEvent ) . to . have . nested . property ( 'context.name' , 'foo' )
@@ -106,13 +114,27 @@ describe('Providable', () => {
106
114
const quxEvent = events . getCall ( 4 ) . args [ 0 ]
107
115
expect ( quxEvent ) . to . be . instanceof ( ContextEvent )
108
116
expect ( quxEvent ) . to . have . nested . property ( 'context.name' , 'qux' )
109
- expect ( quxEvent ) . to . have . nested . property ( 'context.initialValue' ) . eql ( 0 )
117
+ expect ( quxEvent ) . to . have . nested . property ( 'context.initialValue' , 0 )
110
118
expect ( quxEvent ) . to . have . property ( 'multiple' , true )
111
119
expect ( quxEvent ) . to . have . property ( 'bubbles' , true )
120
+
121
+ const targetEvent = events . getCall ( 5 ) . args [ 0 ]
122
+ expect ( targetEvent ) . to . be . instanceof ( ContextEvent )
123
+ expect ( targetEvent ) . to . have . nested . property ( 'context.name' , 'target' )
124
+ expect ( targetEvent ) . to . have . nested . property ( 'context.initialValue' , undefined )
125
+ expect ( targetEvent ) . to . have . property ( 'multiple' , true )
126
+ expect ( targetEvent ) . to . have . property ( 'bubbles' , true )
127
+
128
+ const attrEvent = events . getCall ( 6 ) . args [ 0 ]
129
+ expect ( attrEvent ) . to . be . instanceof ( ContextEvent )
130
+ expect ( attrEvent ) . to . have . nested . property ( 'context.name' , 'testAttribute' )
131
+ expect ( attrEvent ) . to . have . nested . property ( 'context.initialValue' , '' )
132
+ expect ( attrEvent ) . to . have . property ( 'multiple' , true )
133
+ expect ( attrEvent ) . to . have . property ( 'bubbles' , true )
112
134
} )
113
135
114
136
it ( 'changes value based on callback new value' , async ( ) => {
115
- expect ( events ) . to . have . callCount ( 5 )
137
+ expect ( events ) . to . have . callCount ( 7 )
116
138
const fooCallback = events . getCall ( 0 ) . args [ 0 ] . callback
117
139
fooCallback ( 'hello' )
118
140
expect ( instance ) . to . have . property ( 'foo' , 'hello' )
@@ -123,7 +145,7 @@ describe('Providable', () => {
123
145
it ( 'disposes of past callbacks when given new ones' , async ( ) => {
124
146
const dispose1 = fake ( )
125
147
const dispose2 = fake ( )
126
- expect ( events ) . to . have . callCount ( 5 )
148
+ expect ( events ) . to . have . callCount ( 7 )
127
149
const fooCallback = events . getCall ( 0 ) . args [ 0 ] . callback
128
150
fooCallback ( 'hello' , dispose1 )
129
151
expect ( dispose1 ) . to . have . callCount ( 0 )
@@ -144,10 +166,11 @@ describe('Providable', () => {
144
166
let provider : ProvidableProviderTest
145
167
beforeEach ( async ( ) => {
146
168
provider = await fixture (
147
- html `< providable-provider-test
148
- > < div >
149
- < span > < strong > </ strong > </ span > </ div
150
- > </ providable-provider-test > `
169
+ html `< providable-provider-test >
170
+ < div >
171
+ < span > < strong > </ strong > </ span >
172
+ </ div >
173
+ </ providable-provider-test > `
151
174
)
152
175
} )
153
176
@@ -193,7 +216,7 @@ describe('Providable', () => {
193
216
let provider : ProvidableProviderTest
194
217
let consumer : ProvidableConsumerTest
195
218
beforeEach ( async ( ) => {
196
- provider = await fixture ( html `< providable-provider-test >
219
+ provider = await fixture ( html `< providable-provider-test test-attribute =" x " >
197
220
< main >
198
221
< article >
199
222
< section >
@@ -203,6 +226,7 @@ describe('Providable', () => {
203
226
</ section >
204
227
</ article >
205
228
</ main >
229
+ < small data-target ="providable-provider-test.target "> </ small >
206
230
</ providable-provider-test > ` )
207
231
consumer = provider . querySelector < ProvidableConsumerTest > ( 'providable-consumer-test' ) !
208
232
} )
@@ -212,7 +236,9 @@ describe('Providable', () => {
212
236
expect ( consumer ) . to . have . property ( 'bar' , 'world' )
213
237
expect ( consumer ) . to . have . property ( 'baz' , 3 )
214
238
expect ( consumer ) . to . have . property ( sym ) . eql ( { provided : true } )
215
- expect ( consumer ) . to . have . property ( 'qux' ) . eql ( 8 )
239
+ expect ( consumer ) . to . have . property ( 'qux' , 8 )
240
+ expect ( consumer ) . to . have . property ( 'target' , provider . querySelector ( 'small' ) ! )
241
+ expect ( consumer ) . to . have . property ( 'testAttribute' , 'x' )
216
242
} )
217
243
218
244
it ( 'updates values provided if they change' , ( ) => {
@@ -222,6 +248,20 @@ describe('Providable', () => {
222
248
expect ( consumer ) . to . have . property ( 'foo' , 'greetings' )
223
249
} )
224
250
251
+ it ( 'updates @provide @attr values if they change' , async ( ) => {
252
+ provider . setAttribute ( 'test-attribute' , 'y' )
253
+ await Promise . resolve ( )
254
+ expect ( consumer ) . to . have . property ( 'testAttribute' , 'y' )
255
+ } )
256
+
257
+ it ( 'updates @provide @target values if they change' , async ( ) => {
258
+ const big = document . createElement ( 'big' )
259
+ big . setAttribute ( 'data-target' , 'providable-provider-test.target' )
260
+ provider . prepend ( big )
261
+ await Promise . resolve ( )
262
+ expect ( consumer ) . to . have . property ( 'target' , big )
263
+ } )
264
+
225
265
it ( 'calls consumer set callbacks when the value is updated' , ( ) => {
226
266
expect ( consumer ) . to . have . property ( 'qux' , 8 )
227
267
expect ( consumer ) . to . have . property ( 'count' , 1 )
0 commit comments