|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright (c) 2018 The Polymer Project Authors. All rights reserved. |
| 4 | + * This code may only be used under the BSD style license found at |
| 5 | + * http://polymer.github.io/LICENSE.txt |
| 6 | + * The complete set of authors may be found at |
| 7 | + * http://polymer.github.io/AUTHORS.txt |
| 8 | + * The complete set of contributors may be found at |
| 9 | + * http://polymer.github.io/CONTRIBUTORS.txt |
| 10 | + * Code distributed by Google as part of the polymer project is also |
| 11 | + * subject to an additional IP rights grant found at |
| 12 | + * http://polymer.github.io/PATENTS.txt |
| 13 | + */ |
| 14 | + |
| 15 | +// import * as ReactModule from 'react'; |
| 16 | +import 'react/umd/react.development.js'; |
| 17 | +import 'react-dom/umd/react-dom.development.js'; |
| 18 | +import {useController} from '../use-controller.js'; |
| 19 | +import {assert} from '@esm-bundle/chai'; |
| 20 | +import { |
| 21 | + ReactiveController, |
| 22 | + ReactiveControllerHost, |
| 23 | +} from '@lit/reactive-element'; |
| 24 | + |
| 25 | +const {React, ReactDOM} = window; |
| 26 | + |
| 27 | +suite('useController', () => { |
| 28 | + let container: HTMLElement; |
| 29 | + let ctorCallCount = 0; |
| 30 | + |
| 31 | + setup(() => { |
| 32 | + container = document.createElement('div'); |
| 33 | + document.body.appendChild(container); |
| 34 | + ctorCallCount = 0; |
| 35 | + }); |
| 36 | + |
| 37 | + teardown(() => { |
| 38 | + if (container && container.parentNode) { |
| 39 | + container.parentNode.removeChild(container); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + class TestController implements ReactiveController { |
| 44 | + host: ReactiveControllerHost; |
| 45 | + a: string; |
| 46 | + |
| 47 | + log: Array<string> = []; |
| 48 | + |
| 49 | + constructor(host: ReactiveControllerHost, a: string) { |
| 50 | + this.host = host; |
| 51 | + this.a = a; |
| 52 | + host.addController(this); |
| 53 | + ctorCallCount++; |
| 54 | + } |
| 55 | + |
| 56 | + hostConnected() { |
| 57 | + this.log.push('connected'); |
| 58 | + } |
| 59 | + |
| 60 | + hostDisconnected() { |
| 61 | + this.log.push('disconnected'); |
| 62 | + } |
| 63 | + |
| 64 | + hostUpdate() { |
| 65 | + this.log.push('update'); |
| 66 | + } |
| 67 | + |
| 68 | + hostUpdated() { |
| 69 | + this.log.push('updated'); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + const useTest = (a: string) => { |
| 74 | + return useController( |
| 75 | + React, |
| 76 | + (host: ReactiveControllerHost) => new TestController(host, a) |
| 77 | + ); |
| 78 | + }; |
| 79 | + |
| 80 | + test('basic lifecycle', () => { |
| 81 | + let testController!: TestController; |
| 82 | + |
| 83 | + const TestComponent = ({x}: {x: number}) => { |
| 84 | + testController = useTest('a'); |
| 85 | + return React.createElement('div', {className: 'foo'}, [ |
| 86 | + `x:${x}, a:${testController.a}`, |
| 87 | + ]); |
| 88 | + }; |
| 89 | + |
| 90 | + const render = (props: any) => { |
| 91 | + ReactDOM.render(React.createElement(TestComponent, props), container); |
| 92 | + }; |
| 93 | + |
| 94 | + render({x: 1}); |
| 95 | + assert.equal(ctorCallCount, 1); |
| 96 | + assert.equal(container.innerHTML, `<div class="foo">x:1, a:a</div>`); |
| 97 | + assert.deepEqual(testController.log, ['connected', 'update', 'updated']); |
| 98 | + const firstTestController = testController; |
| 99 | + |
| 100 | + testController.log.length = 0; |
| 101 | + render({x: 2}); |
| 102 | + assert.equal(ctorCallCount, 1); |
| 103 | + assert.equal(container.innerHTML, `<div class="foo">x:2, a:a</div>`); |
| 104 | + assert.deepEqual(testController.log, ['update', 'updated']); |
| 105 | + assert.strictEqual(testController, firstTestController); |
| 106 | + }); |
| 107 | + |
| 108 | + test('requestUpdate', async () => { |
| 109 | + let testController!: TestController; |
| 110 | + |
| 111 | + const TestComponent = ({x}: {x: number}) => { |
| 112 | + testController = useTest('a'); |
| 113 | + return React.createElement('div', {className: 'foo'}, [ |
| 114 | + `x:${x}, a:${testController.a}`, |
| 115 | + ]); |
| 116 | + }; |
| 117 | + |
| 118 | + const render = (props: any) => { |
| 119 | + ReactDOM.render(React.createElement(TestComponent, props), container); |
| 120 | + }; |
| 121 | + |
| 122 | + render({x: 1}); |
| 123 | + assert.deepEqual(testController.log, ['connected', 'update', 'updated']); |
| 124 | + testController.log.length = 0; |
| 125 | + testController.a = 'b'; |
| 126 | + testController.host.requestUpdate(); |
| 127 | + |
| 128 | + await new Promise((r) => setTimeout(r, 0)); |
| 129 | + |
| 130 | + assert.equal(container.innerHTML, `<div class="foo">x:1, a:b</div>`); |
| 131 | + assert.deepEqual(testController.log, ['update', 'updated']); |
| 132 | + }); |
| 133 | + |
| 134 | + test('disconnect', () => { |
| 135 | + let testController!: TestController; |
| 136 | + |
| 137 | + const TestComponent = ({x}: {x: number}) => { |
| 138 | + testController = useTest('a'); |
| 139 | + return React.createElement('div', {className: 'foo'}, [ |
| 140 | + `x:${x}, a:${testController.a}`, |
| 141 | + ]); |
| 142 | + }; |
| 143 | + |
| 144 | + ReactDOM.render(React.createElement(TestComponent, {x: 1}), container); |
| 145 | + assert.deepEqual(testController.log, ['connected', 'update', 'updated']); |
| 146 | + testController.log.length = 0; |
| 147 | + |
| 148 | + ReactDOM.render(React.createElement('div'), container); |
| 149 | + assert.equal(container.innerHTML, `<div></div>`); |
| 150 | + assert.deepEqual(testController.log, ['disconnected']); |
| 151 | + }); |
| 152 | + |
| 153 | + test('updateComplete', async () => { |
| 154 | + let testController!: TestController; |
| 155 | + let updateCompleteCount = 0; |
| 156 | + let lastNestedUpdate: boolean | undefined; |
| 157 | + let rerender = false; |
| 158 | + |
| 159 | + const TestComponent = ({x}: {x: number}) => { |
| 160 | + testController = useTest('a'); |
| 161 | + testController.host.updateComplete.then((nestedUpdate) => { |
| 162 | + updateCompleteCount++; |
| 163 | + lastNestedUpdate = nestedUpdate; |
| 164 | + }); |
| 165 | + if (rerender) { |
| 166 | + // prevent an infinite loop |
| 167 | + rerender = false; |
| 168 | + testController.host.requestUpdate(); |
| 169 | + } |
| 170 | + return React.createElement('div', {className: 'foo'}, [ |
| 171 | + `x:${x}, a:${testController.a}`, |
| 172 | + ]); |
| 173 | + }; |
| 174 | + |
| 175 | + const render = (props: any) => { |
| 176 | + ReactDOM.render(React.createElement(TestComponent, props), container); |
| 177 | + }; |
| 178 | + |
| 179 | + render({x: 1}); |
| 180 | + assert.deepEqual(testController.log, ['connected', 'update', 'updated']); |
| 181 | + testController.log.length = 0; |
| 182 | + await 0; |
| 183 | + assert.equal(updateCompleteCount, 1); |
| 184 | + assert.strictEqual(lastNestedUpdate, false); |
| 185 | + |
| 186 | + // cause a requestUpdate() during render |
| 187 | + rerender = true; |
| 188 | + render({x: 2}); |
| 189 | + await 0; |
| 190 | + // Expect only one more renders since requestUpdate() is called |
| 191 | + // during render |
| 192 | + assert.equal(updateCompleteCount, 2); |
| 193 | + assert.strictEqual(lastNestedUpdate, false); |
| 194 | + |
| 195 | + await 0; |
| 196 | + |
| 197 | + assert.equal(updateCompleteCount, 2); |
| 198 | + }); |
| 199 | +}); |
0 commit comments