-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathdeferred-content.spec.ts
87 lines (73 loc) · 2.56 KB
/
deferred-content.spec.ts
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
import {Component, DebugElement, Directive, effect, inject, signal} from '@angular/core';
import {ComponentFixture, TestBed, waitForAsync} from '@angular/core/testing';
import {DeferredContent, DeferredContentAware} from './deferred-content';
import {By} from '@angular/platform-browser';
describe('DeferredContent', () => {
let fixture: ComponentFixture<TestComponent>;
let collapsible: DebugElement;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [TestComponent],
});
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
collapsible = fixture.debugElement.query(By.directive(Collapsible));
});
it('removes the content when hidden.', async () => {
collapsible.injector.get(Collapsible).contentVisible.set(false);
await fixture.whenStable();
expect(collapsible.nativeElement.innerText).toBe('');
});
it('creates the content when visible.', async () => {
collapsible.injector.get(Collapsible).contentVisible.set(true);
await fixture.whenStable();
expect(collapsible.nativeElement.innerText).toBe('Lazy Content');
});
describe('with preserveContent', () => {
let component: TestComponent;
beforeEach(() => {
component = fixture.componentInstance;
component.preserveContent.set(true);
});
it('creates the content when hidden.', async () => {
collapsible.injector.get(Collapsible).contentVisible.set(false);
await fixture.whenStable();
expect(collapsible.nativeElement.innerText).toBe('Lazy Content');
});
it('creates the content when visible.', async () => {
collapsible.injector.get(Collapsible).contentVisible.set(true);
await fixture.whenStable();
expect(collapsible.nativeElement.innerText).toBe('Lazy Content');
});
});
});
@Directive({
selector: '[collapsible]',
hostDirectives: [{directive: DeferredContentAware, inputs: ['preserveContent']}],
})
class Collapsible {
private readonly _deferredContentAware = inject(DeferredContentAware);
contentVisible = signal(true);
constructor() {
effect(() => this._deferredContentAware.contentVisible.set(this.contentVisible()));
}
}
@Directive({
selector: 'ng-template[collapsibleContent]',
hostDirectives: [DeferredContent],
})
class CollapsibleContent {}
@Component({
template: `
<div collapsible [preserveContent]="preserveContent()">
<ng-template collapsibleContent>
Lazy Content
</ng-template>
</div>
`,
imports: [Collapsible, CollapsibleContent],
})
class TestComponent {
preserveContent = signal(false);
}