11import { expect , fixture , html } from '@open-wc/testing'
22import { fake } from 'sinon'
33import { provide , consume , providable , ContextEvent } from '../src/providable.js'
4+ import { target , targetable } from '../src/targetable.js'
5+ import { attr , attrable } from '../src/attrable.js'
46
57describe ( '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
@@ -40,6 +46,8 @@ describe('Providable', () => {
4046 @consume set qux ( value : number ) {
4147 this . count += 1
4248 }
49+ @consume target ! : HTMLElement
50+ @consume testAttribute = ''
4351 connectedCallback ( ) {
4452 this . textContent = `${ this . foo } ${ this . bar } `
4553 }
@@ -74,7 +82,7 @@ describe('Providable', () => {
7482 } )
7583
7684 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 )
7886 const fooEvent = events . getCall ( 0 ) . args [ 0 ]
7987 expect ( fooEvent ) . to . be . instanceof ( ContextEvent )
8088 expect ( fooEvent ) . to . have . nested . property ( 'context.name' , 'foo' )
@@ -106,13 +114,27 @@ describe('Providable', () => {
106114 const quxEvent = events . getCall ( 4 ) . args [ 0 ]
107115 expect ( quxEvent ) . to . be . instanceof ( ContextEvent )
108116 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 )
110118 expect ( quxEvent ) . to . have . property ( 'multiple' , true )
111119 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 )
112134 } )
113135
114136 it ( 'changes value based on callback new value' , async ( ) => {
115- expect ( events ) . to . have . callCount ( 5 )
137+ expect ( events ) . to . have . callCount ( 7 )
116138 const fooCallback = events . getCall ( 0 ) . args [ 0 ] . callback
117139 fooCallback ( 'hello' )
118140 expect ( instance ) . to . have . property ( 'foo' , 'hello' )
@@ -123,7 +145,7 @@ describe('Providable', () => {
123145 it ( 'disposes of past callbacks when given new ones' , async ( ) => {
124146 const dispose1 = fake ( )
125147 const dispose2 = fake ( )
126- expect ( events ) . to . have . callCount ( 5 )
148+ expect ( events ) . to . have . callCount ( 7 )
127149 const fooCallback = events . getCall ( 0 ) . args [ 0 ] . callback
128150 fooCallback ( 'hello' , dispose1 )
129151 expect ( dispose1 ) . to . have . callCount ( 0 )
@@ -144,10 +166,11 @@ describe('Providable', () => {
144166 let provider : ProvidableProviderTest
145167 beforeEach ( async ( ) => {
146168 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 > `
151174 )
152175 } )
153176
@@ -193,7 +216,7 @@ describe('Providable', () => {
193216 let provider : ProvidableProviderTest
194217 let consumer : ProvidableConsumerTest
195218 beforeEach ( async ( ) => {
196- provider = await fixture ( html `< providable-provider-test >
219+ provider = await fixture ( html `< providable-provider-test test-attribute =" x " >
197220 < main >
198221 < article >
199222 < section >
@@ -203,6 +226,7 @@ describe('Providable', () => {
203226 </ section >
204227 </ article >
205228 </ main >
229+ < small data-target ="providable-provider-test.target "> </ small >
206230 </ providable-provider-test > ` )
207231 consumer = provider . querySelector < ProvidableConsumerTest > ( 'providable-consumer-test' ) !
208232 } )
@@ -212,7 +236,9 @@ describe('Providable', () => {
212236 expect ( consumer ) . to . have . property ( 'bar' , 'world' )
213237 expect ( consumer ) . to . have . property ( 'baz' , 3 )
214238 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' )
216242 } )
217243
218244 it ( 'updates values provided if they change' , ( ) => {
@@ -222,6 +248,20 @@ describe('Providable', () => {
222248 expect ( consumer ) . to . have . property ( 'foo' , 'greetings' )
223249 } )
224250
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+
225265 it ( 'calls consumer set callbacks when the value is updated' , ( ) => {
226266 expect ( consumer ) . to . have . property ( 'qux' , 8 )
227267 expect ( consumer ) . to . have . property ( 'count' , 1 )
0 commit comments