|
9 | 9 | import {TemplateRef, ViewContainerRef} from '../../src/core'; |
10 | 10 | import {getOrCreateNodeInjectorForNode, getOrCreateTemplateRef} from '../../src/render3/di'; |
11 | 11 | import {defineComponent, defineDirective, injectTemplateRef, injectViewContainerRef} from '../../src/render3/index'; |
12 | | -import {bind, container, elementEnd, elementProperty, elementStart, interpolation1, load, loadDirective, text, textBinding} from '../../src/render3/instructions'; |
| 12 | +import {bind, container, containerRefreshEnd, containerRefreshStart, elementEnd, elementProperty, elementStart, embeddedViewEnd, embeddedViewStart, interpolation1, load, loadDirective, text, textBinding} from '../../src/render3/instructions'; |
13 | 13 |
|
14 | 14 | import {ComponentFixture} from './render_util'; |
15 | 15 |
|
@@ -190,4 +190,161 @@ describe('ViewContainerRef', () => { |
190 | 190 | expect(fixture.html).toEqual('before||after'); |
191 | 191 | }); |
192 | 192 |
|
| 193 | + it('should add embedded views at the right position in the DOM tree (ng-template next to other ng-template)', |
| 194 | + () => { |
| 195 | + let directiveInstances: TestDirective[] = []; |
| 196 | + |
| 197 | + class TestDirective { |
| 198 | + static ngDirectiveDef = defineDirective({ |
| 199 | + type: TestDirective, |
| 200 | + selectors: [['', 'testdir', '']], |
| 201 | + factory: () => { |
| 202 | + const instance = new TestDirective(injectViewContainerRef(), injectTemplateRef()); |
| 203 | + |
| 204 | + directiveInstances.push(instance); |
| 205 | + |
| 206 | + return instance; |
| 207 | + } |
| 208 | + }); |
| 209 | + |
| 210 | + constructor(private _vcRef: ViewContainerRef, private _tplRef: TemplateRef<{}>) {} |
| 211 | + |
| 212 | + insertTpl(ctx: {}) { this._vcRef.createEmbeddedView(this._tplRef, ctx); } |
| 213 | + |
| 214 | + remove(index?: number) { this._vcRef.remove(index); } |
| 215 | + } |
| 216 | + |
| 217 | + function EmbeddedTemplateA(ctx: any, cm: boolean) { |
| 218 | + if (cm) { |
| 219 | + text(0, 'A'); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + function EmbeddedTemplateB(ctx: any, cm: boolean) { |
| 224 | + if (cm) { |
| 225 | + text(0, 'B'); |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * before| |
| 231 | + * <ng-template directive>A<ng-template> |
| 232 | + * <ng-template directive>B<ng-template> |
| 233 | + * |after |
| 234 | + */ |
| 235 | + class TestComponent { |
| 236 | + testDir: TestDirective; |
| 237 | + static ngComponentDef = defineComponent({ |
| 238 | + type: TestComponent, |
| 239 | + selectors: [['test-cmp']], |
| 240 | + factory: () => new TestComponent(), |
| 241 | + template: (cmp: TestComponent, cm: boolean) => { |
| 242 | + if (cm) { |
| 243 | + text(0, 'before|'); |
| 244 | + container(1, EmbeddedTemplateA, undefined, ['testdir', '']); |
| 245 | + container(2, EmbeddedTemplateB, undefined, ['testdir', '']); |
| 246 | + text(3, '|after'); |
| 247 | + } |
| 248 | + }, |
| 249 | + directives: [TestDirective] |
| 250 | + }); |
| 251 | + } |
| 252 | + |
| 253 | + const fixture = new ComponentFixture(TestComponent); |
| 254 | + expect(fixture.html).toEqual('before||after'); |
| 255 | + |
| 256 | + directiveInstances ![1].insertTpl({}); |
| 257 | + expect(fixture.html).toEqual('before|B|after'); |
| 258 | + |
| 259 | + directiveInstances ![0].insertTpl({}); |
| 260 | + expect(fixture.html).toEqual('before|AB|after'); |
| 261 | + }); |
| 262 | + |
| 263 | + |
| 264 | + it('should add embedded views at the right position in the DOM tree (ng-template next to a JS block)', |
| 265 | + () => { |
| 266 | + let directiveInstance: TestDirective; |
| 267 | + |
| 268 | + class TestDirective { |
| 269 | + static ngDirectiveDef = defineDirective({ |
| 270 | + type: TestDirective, |
| 271 | + selectors: [['', 'testdir', '']], |
| 272 | + factory: () => directiveInstance = |
| 273 | + new TestDirective(injectViewContainerRef(), injectTemplateRef()) |
| 274 | + }); |
| 275 | + |
| 276 | + constructor(private _vcRef: ViewContainerRef, private _tplRef: TemplateRef<{}>) {} |
| 277 | + |
| 278 | + insertTpl(ctx: {}) { this._vcRef.createEmbeddedView(this._tplRef, ctx); } |
| 279 | + |
| 280 | + remove(index?: number) { this._vcRef.remove(index); } |
| 281 | + } |
| 282 | + |
| 283 | + function EmbeddedTemplateA(ctx: any, cm: boolean) { |
| 284 | + if (cm) { |
| 285 | + text(0, 'A'); |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + /** |
| 290 | + * before| |
| 291 | + * <ng-template directive>A<ng-template> |
| 292 | + * % if (condition) { |
| 293 | + * B |
| 294 | + * } |
| 295 | + * |after |
| 296 | + */ |
| 297 | + class TestComponent { |
| 298 | + condition = false; |
| 299 | + testDir: TestDirective; |
| 300 | + static ngComponentDef = defineComponent({ |
| 301 | + type: TestComponent, |
| 302 | + selectors: [['test-cmp']], |
| 303 | + factory: () => new TestComponent(), |
| 304 | + template: (cmp: TestComponent, cm: boolean) => { |
| 305 | + if (cm) { |
| 306 | + text(0, 'before|'); |
| 307 | + container(1, EmbeddedTemplateA, undefined, ['testdir', '']); |
| 308 | + container(2); |
| 309 | + text(3, '|after'); |
| 310 | + } |
| 311 | + containerRefreshStart(2); |
| 312 | + { |
| 313 | + if (cmp.condition) { |
| 314 | + let cm1 = embeddedViewStart(0); |
| 315 | + { |
| 316 | + if (cm1) { |
| 317 | + text(0, 'B'); |
| 318 | + } |
| 319 | + } |
| 320 | + embeddedViewEnd(); |
| 321 | + } |
| 322 | + } |
| 323 | + containerRefreshEnd(); |
| 324 | + }, |
| 325 | + directives: [TestDirective] |
| 326 | + }); |
| 327 | + } |
| 328 | + |
| 329 | + const fixture = new ComponentFixture(TestComponent); |
| 330 | + expect(fixture.html).toEqual('before||after'); |
| 331 | + |
| 332 | + fixture.component.condition = true; |
| 333 | + fixture.update(); |
| 334 | + expect(fixture.html).toEqual('before|B|after'); |
| 335 | + |
| 336 | + directiveInstance !.insertTpl({}); |
| 337 | + expect(fixture.html).toEqual('before|AB|after'); |
| 338 | + |
| 339 | + fixture.component.condition = false; |
| 340 | + fixture.update(); |
| 341 | + expect(fixture.html).toEqual('before|A|after'); |
| 342 | + |
| 343 | + directiveInstance !.insertTpl({}); |
| 344 | + expect(fixture.html).toEqual('before|AA|after'); |
| 345 | + |
| 346 | + fixture.component.condition = true; |
| 347 | + fixture.update(); |
| 348 | + expect(fixture.html).toEqual('before|AAB|after'); |
| 349 | + }); |
193 | 350 | }); |
0 commit comments