|
| 1 | +import {Component, DebugElement, Directive, effect, inject, signal} from '@angular/core'; |
| 2 | +import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing'; |
| 3 | +import {DeferredContent, DeferredContentAware} from './deferred-content'; |
| 4 | +import {By} from '@angular/platform-browser'; |
| 5 | + |
| 6 | +describe('DeferredContent', () => { |
| 7 | + let fixture: ComponentFixture<TestComponent>; |
| 8 | + let collapsible: DebugElement; |
| 9 | + |
| 10 | + beforeEach(waitForAsync(() => { |
| 11 | + TestBed.configureTestingModule({ |
| 12 | + imports: [TestComponent], |
| 13 | + }); |
| 14 | + })); |
| 15 | + |
| 16 | + beforeEach(() => { |
| 17 | + fixture = TestBed.createComponent(TestComponent); |
| 18 | + collapsible = fixture.debugElement.query(By.directive(Collapsible)); |
| 19 | + }); |
| 20 | + |
| 21 | + it('removes the content when hidden.', async () => { |
| 22 | + collapsible.injector.get(Collapsible).contentVisible.set(false); |
| 23 | + await fixture.whenStable(); |
| 24 | + expect(collapsible.nativeElement.innerText).toBe(''); |
| 25 | + }); |
| 26 | + |
| 27 | + it('creates the content when visible.', async () => { |
| 28 | + collapsible.injector.get(Collapsible).contentVisible.set(true); |
| 29 | + await fixture.whenStable(); |
| 30 | + expect(collapsible.nativeElement.innerText).toBe('Lazy Content'); |
| 31 | + }); |
| 32 | + |
| 33 | + describe('with preserveContent', () => { |
| 34 | + let component: TestComponent; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + component = fixture.componentInstance; |
| 38 | + component.preserveContent.set(true); |
| 39 | + }); |
| 40 | + |
| 41 | + it('creates the content when hidden.', async () => { |
| 42 | + collapsible.injector.get(Collapsible).contentVisible.set(false); |
| 43 | + await fixture.whenStable(); |
| 44 | + expect(collapsible.nativeElement.innerText).toBe('Lazy Content'); |
| 45 | + }); |
| 46 | + |
| 47 | + it('creates the content when visible.', async () => { |
| 48 | + collapsible.injector.get(Collapsible).contentVisible.set(true); |
| 49 | + await fixture.whenStable(); |
| 50 | + expect(collapsible.nativeElement.innerText).toBe('Lazy Content'); |
| 51 | + }); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +@Directive({ |
| 56 | + selector: '[collapsible]', |
| 57 | + hostDirectives: [{directive: DeferredContentAware, inputs: ['preserveContent']}], |
| 58 | +}) |
| 59 | +class Collapsible { |
| 60 | + private readonly _deferredContentAware = inject(DeferredContentAware); |
| 61 | + |
| 62 | + contentVisible = signal(true); |
| 63 | + |
| 64 | + constructor() { |
| 65 | + effect(() => this._deferredContentAware.contentVisible.set(this.contentVisible())); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +@Directive({ |
| 70 | + selector: 'ng-template[collapsibleContent]', |
| 71 | + hostDirectives: [DeferredContent], |
| 72 | +}) |
| 73 | +class CollapsibleContent {} |
| 74 | + |
| 75 | +@Component({ |
| 76 | + template: ` |
| 77 | + <div collapsible [preserveContent]="preserveContent()"> |
| 78 | + <ng-template collapsibleContent> |
| 79 | + Lazy Content |
| 80 | + </ng-template> |
| 81 | + </div> |
| 82 | + `, |
| 83 | + imports: [Collapsible, CollapsibleContent], |
| 84 | +}) |
| 85 | +class TestComponent { |
| 86 | + preserveContent = signal(false); |
| 87 | +} |
0 commit comments