|
1 |
| -import { TestBed } from '@angular/core/testing'; |
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
2 | 2 | import { TemplateIdDirective } from './template-id.directive';
|
3 |
| -import { TemplateRef } from '@angular/core'; |
| 3 | +import { Component, computed, DebugElement, TemplateRef, viewChild } from '@angular/core'; |
| 4 | +import { By } from '@angular/platform-browser'; |
| 5 | +import { NgTemplateOutlet } from '@angular/common'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + imports: [TemplateIdDirective, NgTemplateOutlet], |
| 9 | + template: ` |
| 10 | + <ng-template cTemplateId="test">Inner Text</ng-template> |
| 11 | + <div class="test"> |
| 12 | + <ng-container *ngTemplateOutlet="id()" /> |
| 13 | + </div> |
| 14 | + ` |
| 15 | +}) |
| 16 | +class TestComponent { |
| 17 | + readonly templateId = viewChild(TemplateIdDirective); |
| 18 | + |
| 19 | + readonly id = computed(() => this.templateId()?.templateRef); |
| 20 | +} |
4 | 21 |
|
5 | 22 | describe('TemplateIdDirective', () => {
|
| 23 | + let fixture: ComponentFixture<TestComponent>; |
| 24 | + let component: TestComponent; |
| 25 | + let debugElement: DebugElement; |
| 26 | + |
6 | 27 | beforeEach(() => {
|
7 | 28 | TestBed.configureTestingModule({
|
8 |
| - providers: [TemplateRef], |
9 |
| - }); |
| 29 | + imports: [TestComponent], |
| 30 | + providers: [TemplateRef] |
| 31 | + }).compileComponents(); |
| 32 | + |
| 33 | + fixture = TestBed.createComponent(TestComponent); |
| 34 | + debugElement = fixture.debugElement.query(By.css('.test')); |
| 35 | + component = fixture.componentInstance; |
| 36 | + |
| 37 | + fixture.detectChanges(); |
10 | 38 | });
|
11 | 39 | it('should create an instance', () => {
|
12 | 40 | TestBed.runInInjectionContext(() => {
|
13 | 41 | const directive = new TemplateIdDirective();
|
14 | 42 | expect(directive).toBeTruthy();
|
15 | 43 | });
|
16 | 44 | });
|
| 45 | + |
| 46 | + it('should create a template with innerText', () => { |
| 47 | + expect(debugElement.nativeElement.innerText).toBe('Inner Text'); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should get the template id', () => { |
| 51 | + expect(component.templateId()?.id).toBe('test'); |
| 52 | + expect(component.templateId()?.templateRef).toEqual(jasmine.any(TemplateRef)); |
| 53 | + }); |
17 | 54 | });
|
0 commit comments