|
1 | | -import { Component, Prop, Watch, h } from '@stencil/core'; |
| 1 | +import { Component, Element, Host, Prop, Watch, h } from '@stencil/core'; |
2 | 2 | import { newSpecPage } from '@stencil/core/testing'; |
3 | 3 |
|
4 | 4 |
|
@@ -193,4 +193,93 @@ describe('lifecycle sync', () => { |
193 | 193 | expect(rootInstance.renders).toBe(2); |
194 | 194 | }); |
195 | 195 |
|
| 196 | + describe('childrens', () => { |
| 197 | + it ('sync', async () => { |
| 198 | + |
| 199 | + const log: string[] = []; |
| 200 | + @Component({ |
| 201 | + tag: 'cmp-a' |
| 202 | + }) |
| 203 | + class CmpA { |
| 204 | + @Prop() prop: string; |
| 205 | + componentWillLoad() { |
| 206 | + log.push('componentWillLoad a'); |
| 207 | + } |
| 208 | + componentDidLoad() { |
| 209 | + log.push('componentDidLoad a'); |
| 210 | + } |
| 211 | + componentWillUpdate() { |
| 212 | + log.push('componentWillUpdate a'); |
| 213 | + } |
| 214 | + componentDidUpdate() { |
| 215 | + log.push('componentDidUpdate a'); |
| 216 | + } |
| 217 | + render() { |
| 218 | + return ( |
| 219 | + <Host> |
| 220 | + <cmp-b id='b1' prop={this.prop}> |
| 221 | + <cmp-b id='b2' prop={this.prop}> |
| 222 | + <cmp-b id='b3' prop={this.prop}></cmp-b> |
| 223 | + </cmp-b> |
| 224 | + </cmp-b> |
| 225 | + </Host> |
| 226 | + ); |
| 227 | + } |
| 228 | + } |
| 229 | + @Component({ |
| 230 | + tag: 'cmp-b' |
| 231 | + }) |
| 232 | + class CmpB { |
| 233 | + @Element() el: HTMLElement; |
| 234 | + @Prop() prop: string; |
| 235 | + componentWillLoad() { |
| 236 | + log.push(`componentWillLoad ${this.el.id}`); |
| 237 | + } |
| 238 | + componentDidLoad() { |
| 239 | + log.push(`componentDidLoad ${this.el.id}`); |
| 240 | + } |
| 241 | + componentWillUpdate() { |
| 242 | + log.push(`componentWillUpdate ${this.el.id}`); |
| 243 | + } |
| 244 | + componentDidUpdate() { |
| 245 | + log.push(`componentDidUpdate ${this.el.id}`); |
| 246 | + } |
| 247 | + } |
| 248 | + const {root, waitForChanges} = await newSpecPage({ |
| 249 | + components: [CmpA, CmpB], |
| 250 | + template: () => <cmp-a></cmp-a> |
| 251 | + }); |
| 252 | + expect(log).toEqual([ |
| 253 | + 'componentWillLoad a', |
| 254 | + 'componentWillLoad b1', |
| 255 | + 'componentWillLoad b2', |
| 256 | + 'componentWillLoad b3', |
| 257 | + 'componentDidLoad b3', |
| 258 | + 'componentDidLoad b2', |
| 259 | + 'componentDidLoad b1', |
| 260 | + 'componentDidLoad a', |
| 261 | + ]); |
| 262 | + log.length = 0; |
| 263 | + root.forceUpdate(); |
| 264 | + await waitForChanges(); |
| 265 | + expect(log).toEqual([ |
| 266 | + 'componentWillUpdate a', |
| 267 | + 'componentDidUpdate a', |
| 268 | + ]); |
| 269 | + |
| 270 | + log.length = 0; |
| 271 | + root.prop = 'something else'; |
| 272 | + await waitForChanges(); |
| 273 | + expect(log).toEqual([ |
| 274 | + 'componentWillUpdate a', |
| 275 | + 'componentWillUpdate b1', |
| 276 | + 'componentWillUpdate b2', |
| 277 | + 'componentWillUpdate b3', |
| 278 | + 'componentDidUpdate b3', |
| 279 | + 'componentDidUpdate b2', |
| 280 | + 'componentDidUpdate b1', |
| 281 | + 'componentDidUpdate a', |
| 282 | + ]); |
| 283 | + }); |
| 284 | + }); |
196 | 285 | }); |
0 commit comments