Skip to content

Commit 6212118

Browse files
committed
test(core): add tests for parent
1 parent 6161cea commit 6212118

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import {
2+
ChangeDetectionStrategy,
3+
Component,
4+
CUSTOM_ELEMENTS_SCHEMA,
5+
effect,
6+
ElementRef,
7+
signal,
8+
viewChild,
9+
} from '@angular/core';
10+
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
11+
import { By } from '@angular/platform-browser';
12+
import { Object3D } from 'three';
13+
import { NgtParent } from './parent';
14+
15+
describe('parent', () => {
16+
it('should not render if value parent that is not resolved', () => {
17+
const fixture = createTestFixture(`<div *parent="null"></div>`);
18+
fixture.detectChanges();
19+
expect(fixture.debugElement.queryAll(By.css('div')).length).toEqual(0);
20+
});
21+
22+
it('should not render with signal parent that is not resolved', () => {
23+
const fixture = createTestFixture(`<div *parent="nullParent"></div>`);
24+
fixture.detectChanges();
25+
expect(fixture.debugElement.queryAll(By.css('div')).length).toEqual(0);
26+
});
27+
28+
it('should render with element ref parent', () => {
29+
const fixture = createTestFixture(`
30+
<div #parent></div>
31+
<div *parent="parent"></div>
32+
`);
33+
fixture.detectChanges();
34+
35+
expect(fixture.debugElement.queryAll(By.css('div')).length).toEqual(2);
36+
});
37+
38+
it('should render with signal element ref parent', () => {
39+
const fixture = createTestFixture(`
40+
<div #parent></div>
41+
<div *parent="parentRef"></div>
42+
`);
43+
fixture.detectChanges();
44+
45+
expect(fixture.debugElement.queryAll(By.css('div')).length).toEqual(2);
46+
});
47+
48+
it('should render with signal parent that resolves later', fakeAsync(() => {
49+
const fixture = createTestFixture(`<div *parent="signalParent"></div>`);
50+
fixture.detectChanges();
51+
52+
tick(101);
53+
fixture.detectChanges();
54+
55+
expect(fixture.debugElement.queryAll(By.css('div')).length).toEqual(1);
56+
}));
57+
58+
it('should only able to access value once ', () => {
59+
const fixture = createTestFixture(`
60+
<div #parent></div>
61+
<div *parent="parentRef"></div>
62+
`);
63+
fixture.detectChanges();
64+
65+
expect(fixture.debugElement.queryAll(By.css('div')).length).toEqual(2);
66+
const parent = fixture.componentInstance.parent();
67+
expect(parent?.value).toEqual(fixture.componentInstance.parentRef());
68+
expect(parent?.value).toEqual(null);
69+
});
70+
});
71+
72+
@Component({
73+
template: '',
74+
schemas: [CUSTOM_ELEMENTS_SCHEMA],
75+
imports: [NgtParent],
76+
changeDetection: ChangeDetectionStrategy.OnPush,
77+
})
78+
class Test {
79+
parent = viewChild(NgtParent);
80+
parentRef = viewChild<ElementRef<Object3D>>('parent');
81+
82+
nullParent = () => null;
83+
signalParent = signal<Object3D | null>(null);
84+
85+
constructor() {
86+
effect(() => {
87+
setTimeout(() => {
88+
this.signalParent.set(new Object3D());
89+
}, 100);
90+
});
91+
}
92+
}
93+
94+
function createTestFixture(template: string) {
95+
return TestBed.overrideComponent(Test, { set: { template } }).createComponent(Test);
96+
}

0 commit comments

Comments
 (0)