Repository navigation
Expand file tree
/
Copy pathnext-render.test.js
More file actions
195 lines (164 loc) · 7.39 KB
/
Copy pathnext-render.test.js
File metadata and controls
195 lines (164 loc) · 7.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Tests for nextRender() and whenMounted()
*/
import { describe, assert } from './test-runner.js';
import {
defineComponent, Component, html, when, nextRender, reactive
} from '../../lib/framework.js';
describe('nextRender()', function(it) {
it('resolves after a text binding is committed to the DOM', async () => {
class NrText extends Component {
constructor(props) { super(props); this.state = { count: 0 }; }
template() { return html`<span id="c">${this.state.count}</span>`; }
}
defineComponent('nr-text', NrText);
const el = document.createElement('nr-text');
document.body.appendChild(el);
await el.nextRender();
assert.equal(el.querySelector('#c').textContent, '0', 'initial render present');
el.state.count = 7;
await el.nextRender();
assert.equal(el.querySelector('#c').textContent, '7', 'DOM updated after nextRender');
document.body.removeChild(el);
});
it('resolves after a newly mounted conditional branch is present', async () => {
class NrBranch extends Component {
constructor(props) { super(props); this.state = { show: false }; }
template() {
return html`<div>${when(this.state.show,
() => html`<p id="panel">panel</p>`,
() => html`<span id="empty">empty</span>`)}</div>`;
}
}
defineComponent('nr-branch', NrBranch);
const el = document.createElement('nr-branch');
document.body.appendChild(el);
await el.nextRender();
assert.ok(el.querySelector('#empty'), 'empty branch initially');
assert.ok(!el.querySelector('#panel'), 'panel absent initially');
el.state.show = true;
await el.nextRender();
assert.ok(el.querySelector('#panel'), 'panel branch mounted after nextRender');
assert.ok(!el.querySelector('#empty'), 'empty branch removed');
document.body.removeChild(el);
});
it('works with no pending flush (schedules one)', async () => {
// Nothing mutated - nextRender must still resolve.
let resolved = false;
const p = nextRender().then(() => { resolved = true; });
await p;
assert.ok(resolved, 'nextRender resolves even with no pending work');
});
it('standalone nextRender sees mutations to plain reactive state', async () => {
const state = reactive({ n: 0 });
let observed = -1;
// An effect-like read via a component keeps this simple; here we just
// assert the promise resolves after a microtask turn.
state.n = 5;
await nextRender();
observed = state.n;
assert.equal(observed, 5, 'state readable after nextRender');
});
});
describe('whenMounted()', function(it) {
it('resolves a child that mounts after a state change', async () => {
defineComponent('wm-child', class extends Component {
template() { return html`<span>child</span>`; }
});
class WmParent extends Component {
constructor(props) { super(props); this.state = { show: false }; }
template() {
return html`<div>${when(this.state.show,
() => html`<wm-child id="kid"></wm-child>`)}</div>`;
}
}
defineComponent('wm-parent', WmParent);
const el = document.createElement('wm-parent');
document.body.appendChild(el);
await el.nextRender();
el.state.show = true;
const child = await el.whenMounted('#kid');
assert.isNotNull(child, 'child resolved');
assert.equal(child.tagName.toLowerCase(), 'wm-child', 'correct element');
assert.ok(child._isMounted, 'child is fully mounted');
document.body.removeChild(el);
});
it('resolves child whose mounted() has completed', async () => {
let mountedRan = false;
defineComponent('wm-child-mounted', {
mounted() { mountedRan = true; },
template() { return html`<span>x</span>`; }
});
class WmParent2 extends Component {
template() { return html`<wm-child-mounted id="k2"></wm-child-mounted>`; }
}
defineComponent('wm-parent2', WmParent2);
const el = document.createElement('wm-parent2');
document.body.appendChild(el);
const child = await el.whenMounted('#k2');
assert.isNotNull(child, 'child resolved');
assert.ok(mountedRan, 'mounted() ran before whenMounted resolved');
document.body.removeChild(el);
});
it('resolves null if the waiter unmounts while waiting', async () => {
class WmNever extends Component {
constructor(props) { super(props); this.state = { show: false }; }
template() {
return html`<div>${when(this.state.show,
() => html`<span id="never">x</span>`)}</div>`;
}
}
defineComponent('wm-never', WmNever);
const el = document.createElement('wm-never');
document.body.appendChild(el);
await el.nextRender();
// Never set show=true; instead unmount the waiter while it polls.
const p = el.whenMounted('#never');
document.body.removeChild(el);
const result = await p;
assert.isNull(result, 'resolves null on waiter unmount');
});
it('accepts an explicit element and waits for its readiness', async () => {
defineComponent('wm-explicit-child', class extends Component {
template() { return html`<span>e</span>`; }
});
class WmExplicit extends Component {
template() { return html`<wm-explicit-child id="ec"></wm-explicit-child>`; }
}
defineComponent('wm-explicit', WmExplicit);
const el = document.createElement('wm-explicit');
document.body.appendChild(el);
await el.nextRender();
const target = el.querySelector('#ec');
const resolved = await el.whenMounted(target);
assert.equal(resolved, target, 'resolves the same element');
document.body.removeChild(el);
});
});
describe('nextRender() - mounted() hook cascades', function(it) {
it('resolves only after a newly-mounted child\'s mounted() state writes rendered', async () => {
defineComponent('nr-cascade-child', {
data() { return { msg: 'pending' }; },
mounted() { this.state.msg = 'ready'; },
template() { return html`<span class="msg">${this.state.msg}</span>`; }
});
defineComponent('nr-cascade-parent', {
data() { return { show: false }; },
template() {
return html`<div>${when(this.state.show, () => html`<nr-cascade-child></nr-cascade-child>`)}</div>`;
}
});
const container = document.createElement('div');
document.body.appendChild(container);
container.innerHTML = '<nr-cascade-parent></nr-cascade-parent>';
await new Promise(r => setTimeout(r, 60));
const parent = container.querySelector('nr-cascade-parent');
parent.state.show = true;
await parent.nextRender();
const msg = parent.querySelector('.msg');
assert.ok(msg, 'child branch mounted when nextRender resolves');
assert.equal(msg.textContent, 'ready',
"the child's mounted() state write is rendered before nextRender resolves");
container.remove();
});
});