|
1 |
| -import { component, html, createContext, useContext, useState } from '../src/haunted.js'; |
| 1 | +import { component, html, createContext, useContext, useState, useProvideContext } from '../src/haunted.js'; |
2 | 2 | import { fixture, expect, nextFrame } from '@open-wc/testing';
|
3 | 3 |
|
4 | 4 | describe('context', function() {
|
@@ -37,16 +37,25 @@ describe('context', function() {
|
37 | 37 | component(ProviderWithSlots)
|
38 | 38 | );
|
39 | 39 |
|
| 40 | + function CustomProvider(host) { |
| 41 | + const {value} = host; |
| 42 | + useProvideContext(Context, value, [value]); |
| 43 | + } |
| 44 | + |
| 45 | + customElements.define('custom-provider', component(CustomProvider)); |
| 46 | + |
40 | 47 | let withProviderValue, withProviderUpdate;
|
41 | 48 | let rootProviderValue, rootProviderUpdate;
|
42 | 49 | let nestedProviderValue, nestedProviderUpdate;
|
43 | 50 | let genericConsumerValue, genericConsumerUpdate;
|
| 51 | + let customProviderValue, customProviderUpdate; |
44 | 52 |
|
45 | 53 | function Tests() {
|
46 | 54 | [withProviderValue, withProviderUpdate] = useState();
|
47 | 55 | [rootProviderValue, rootProviderUpdate] = useState('root');
|
48 | 56 | [nestedProviderValue, nestedProviderUpdate] = useState('nested');
|
49 | 57 | [genericConsumerValue, genericConsumerUpdate] = useState('generic');
|
| 58 | + [customProviderValue, customProviderUpdate] = useState('custom'); |
50 | 59 |
|
51 | 60 | return html`
|
52 | 61 | <div id="without-provider">
|
@@ -81,6 +90,12 @@ describe('context', function() {
|
81 | 90 | </slotted-context-provider>
|
82 | 91 | </context-provider>
|
83 | 92 | </div>
|
| 93 | +
|
| 94 | + <div id="custom-provider"> |
| 95 | + <custom-provider .value=${customProviderValue}> |
| 96 | + <context-consumer></context-consumer> |
| 97 | + </custom-provider> |
| 98 | + </div> |
84 | 99 | `;
|
85 | 100 | }
|
86 | 101 |
|
@@ -122,6 +137,10 @@ describe('context', function() {
|
122 | 137 | expect(getResults('#with-slotted-provider slotted-context-provider context-consumer')[0]).to.equal('slotted');
|
123 | 138 | });
|
124 | 139 |
|
| 140 | + it('uses custom value when custom provider is found', async () => { |
| 141 | + expect(getResults('#custom-provider context-consumer')[0]).to.equal('custom'); |
| 142 | + }); |
| 143 | + |
125 | 144 | describe('with generic consumer component', function () {
|
126 | 145 | it('should render template with context value', async () => {
|
127 | 146 | expect(getContentResults('#generic-consumer generic-consumer')).to.deep.equal(['generic-value']);
|
|
0 commit comments