|
| 1 | +import { UmbContextToken } from '../token/context-token.js'; |
| 2 | +import type { UmbContextMinimal } from '../types.js'; |
| 3 | +import { UmbContextProvider } from '../provide/context-provider.js'; |
| 4 | +import { consumeContext } from './context-consume.decorator.js'; |
| 5 | +import { aTimeout, elementUpdated, expect, fixture } from '@open-wc/testing'; |
| 6 | +import { UmbLitElement } from '@umbraco-cms/backoffice/lit-element'; |
| 7 | +import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api'; |
| 8 | +import { html, state } from '@umbraco-cms/backoffice/external/lit'; |
| 9 | + |
| 10 | +class UmbTestContextConsumerClass implements UmbContextMinimal { |
| 11 | + public prop: string = 'value from provider'; |
| 12 | + getHostElement() { |
| 13 | + return undefined as any; |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +const testToken = new UmbContextToken<UmbTestContextConsumerClass>('my-test-context'); |
| 18 | + |
| 19 | +class MyTestElement extends UmbLitElement { |
| 20 | + @consumeContext({ |
| 21 | + context: testToken, |
| 22 | + }) |
| 23 | + @state() |
| 24 | + contextValue?: UmbTestContextConsumerClass; |
| 25 | + |
| 26 | + override render() { |
| 27 | + return html`<div>${this.contextValue?.prop ?? 'no context'}</div>`; |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +customElements.define('my-consume-test-element', MyTestElement); |
| 32 | + |
| 33 | +describe('@consume decorator', () => { |
| 34 | + let provider: UmbContextProvider; |
| 35 | + let element: MyTestElement; |
| 36 | + |
| 37 | + beforeEach(async () => { |
| 38 | + provider = new UmbContextProvider(document.body, testToken, new UmbTestContextConsumerClass()); |
| 39 | + provider.hostConnected(); |
| 40 | + |
| 41 | + element = await fixture<MyTestElement>(`<my-consume-test-element></my-consume-test-element>`); |
| 42 | + }); |
| 43 | + |
| 44 | + afterEach(() => { |
| 45 | + provider.destroy(); |
| 46 | + (provider as any) = undefined; |
| 47 | + }); |
| 48 | + |
| 49 | + it('should receive a context value when provided on the host', () => { |
| 50 | + expect(element.contextValue).to.equal(provider.providerInstance()); |
| 51 | + expect(element.contextValue?.prop).to.equal('value from provider'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should render the value from the context', async () => { |
| 55 | + expect(element).shadowDom.to.equal('<div>value from provider</div>'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should work when the decorator is used in a controller', async () => { |
| 59 | + class MyController extends UmbControllerBase { |
| 60 | + @consumeContext({ context: testToken }) |
| 61 | + contextValue?: UmbTestContextConsumerClass; |
| 62 | + } |
| 63 | + |
| 64 | + const controller = new MyController(element); |
| 65 | + |
| 66 | + await elementUpdated(element); |
| 67 | + |
| 68 | + expect(element.contextValue).to.equal(provider.providerInstance()); |
| 69 | + expect(controller.contextValue).to.equal(provider.providerInstance()); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should have called the callback first', async () => { |
| 73 | + let callbackCalled = false; |
| 74 | + |
| 75 | + class MyCallbackTestElement extends UmbLitElement { |
| 76 | + @consumeContext({ |
| 77 | + context: testToken, |
| 78 | + callback: () => { |
| 79 | + callbackCalled = true; |
| 80 | + }, |
| 81 | + }) |
| 82 | + contextValue?: UmbTestContextConsumerClass; |
| 83 | + } |
| 84 | + |
| 85 | + customElements.define('my-callback-consume-test-element', MyCallbackTestElement); |
| 86 | + |
| 87 | + const callbackElement = await fixture<MyCallbackTestElement>( |
| 88 | + `<my-callback-consume-test-element></my-callback-consume-test-element>`, |
| 89 | + ); |
| 90 | + |
| 91 | + await elementUpdated(callbackElement); |
| 92 | + |
| 93 | + expect(callbackCalled).to.be.true; |
| 94 | + expect(callbackElement.contextValue).to.equal(provider.providerInstance()); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should update the context value when the provider instance changes', async () => { |
| 98 | + const newProviderInstance = new UmbTestContextConsumerClass(); |
| 99 | + newProviderInstance.prop = 'new value from provider'; |
| 100 | + |
| 101 | + const newProvider = new UmbContextProvider(element, testToken, newProviderInstance); |
| 102 | + newProvider.hostConnected(); |
| 103 | + |
| 104 | + await elementUpdated(element); |
| 105 | + |
| 106 | + expect(element.contextValue).to.equal(newProvider.providerInstance()); |
| 107 | + expect(element.contextValue?.prop).to.equal(newProviderInstance.prop); |
| 108 | + }); |
| 109 | + |
| 110 | + it('should be able to consume without subscribing', async () => { |
| 111 | + class MyNoSubscribeTestController extends UmbControllerBase { |
| 112 | + @consumeContext({ context: testToken, subscribe: false }) |
| 113 | + contextValue?: UmbTestContextConsumerClass; |
| 114 | + } |
| 115 | + |
| 116 | + const controller = new MyNoSubscribeTestController(element); |
| 117 | + await aTimeout(0); // Wait a tick for promise to resolve |
| 118 | + |
| 119 | + expect(controller.contextValue).to.equal(provider.providerInstance()); |
| 120 | + |
| 121 | + const newProviderInstance = new UmbTestContextConsumerClass(); |
| 122 | + newProviderInstance.prop = 'new value from provider'; |
| 123 | + |
| 124 | + const newProvider = new UmbContextProvider(element, testToken, newProviderInstance); |
| 125 | + newProvider.hostConnected(); |
| 126 | + |
| 127 | + await aTimeout(0); // Wait a tick for promise to resolve |
| 128 | + |
| 129 | + // Should still be the old value |
| 130 | + expect(controller.contextValue).to.not.equal(newProvider.providerInstance()); |
| 131 | + expect(controller.contextValue?.prop).to.equal('value from provider'); |
| 132 | + }); |
| 133 | +}); |
0 commit comments