Skip to content

Commit ffaae9d

Browse files
committed
test: improve test consistency and refactor async handling
- Replaced `flushEffects` with `tick` for better control in `derivedAsync` tests. - Added `autoDetectChanges` in `autoEffect` tests to streamline change detection. - Fixed incorrect configuration in `inject-text-selection` tests by replacing `declarations` with `imports`.
1 parent 48873e8 commit ffaae9d

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

libs/ngxtension/auto-effect/src/auto-effect.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ describe(injectAutoEffect.name, () => {
2828

2929
function setup() {
3030
const fixture = TestBed.createComponent(Test);
31+
fixture.autoDetectChanges();
32+
3133
const component = fixture.componentInstance;
3234
fixture.detectChanges();
3335
return [

libs/ngxtension/derived-async/src/derived-async.spec.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe(derivedAsync.name, () => {
1515
const result = derivedAsync(() => value());
1616
expect(result()).toEqual(undefined); // initial value
1717
value.set(null);
18-
TestBed.flushEffects();
18+
tick();
1919
expect(result()).toEqual(null);
2020
});
2121
}));
@@ -29,10 +29,10 @@ describe(derivedAsync.name, () => {
2929
});
3030

3131
expect(result()).toEqual(undefined); // initial value
32-
TestBed.flushEffects();
32+
tick();
3333
expect(result()).toEqual([1, 2, 3]);
3434
value.set(1);
35-
TestBed.flushEffects();
35+
tick();
3636
expect(result()).toEqual([2, 3, 4]);
3737
});
3838
}));
@@ -49,10 +49,10 @@ describe(derivedAsync.name, () => {
4949
);
5050

5151
expect(result()).toEqual([]); // initial value
52-
TestBed.flushEffects();
52+
tick();
5353
expect(result()).toEqual([1, 2, 3]);
5454
value.set(1);
55-
TestBed.flushEffects();
55+
tick();
5656
expect(result()).toEqual([2, 3, 4]);
5757
});
5858
}));
@@ -110,27 +110,32 @@ describe(derivedAsync.name, () => {
110110
const value = signal(1);
111111
const s = derivedAsync(() => of(value()));
112112
expect(s()).toEqual(undefined); // initial value
113+
tick();
114+
expect(s()).toEqual(1);
113115
});
114116
}));
115117
it('returns correct value and doesnt throw error if enabled', fakeAsync(() => {
116118
TestBed.runInInjectionContext(() => {
117119
const value = signal(1);
118120
const s = derivedAsync(() => of(value()), { requireSync: true });
121+
tick();
119122
expect(s()).toEqual(1); // initial value
120123
});
121124
}));
122-
it('returns correct value and doesnt throw error with initial value provided', () => {
125+
it('returns correct value and doesnt throw error with initial value provided', fakeAsync(() => {
123126
TestBed.runInInjectionContext(() => {
124127
const s = derivedAsync(() => of(1), { initialValue: 2 });
125128
expect(s()).toEqual(2); // initial value
126-
TestBed.flushEffects();
129+
tick();
127130
expect(s()).toEqual(1);
128131
});
129-
});
132+
}));
130133
it('returns correct value and doesnt throw error with normal variable when enabled', fakeAsync(() => {
131134
TestBed.runInInjectionContext(() => {
132135
const s = derivedAsync(() => 1, { requireSync: true });
133136
expect(s()).toEqual(1); // initial value
137+
tick();
138+
expect(s()).toEqual(1); // initial value
134139
});
135140
}));
136141
it('throws error for promises', () => {

libs/ngxtension/inject-text-selection/src/inject-text-selection.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe(injectTextSelection.name, () => {
5151

5252
// Configure the testing module with our test component.
5353
TestBed.configureTestingModule({
54-
declarations: [TestSelectionComponent],
54+
imports: [TestSelectionComponent],
5555
});
5656

5757
// Create the component and trigger initial change detection.

0 commit comments

Comments
 (0)