Skip to content

Commit eabe879

Browse files
committed
test(query): 🥳 add initial tests
1 parent 3b4b403 commit eabe879

9 files changed

+228
-1
lines changed

package-lock.json

+11-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
"eslint": "~8.46.0",
6969
"eslint-config-prettier": "^9.0.0",
7070
"eslint-plugin-playwright": "^0.15.3",
71+
"expect-type": "^0.17.3",
7172
"git-cz": "^4.9.0",
7273
"husky": "^8.0.3",
7374
"jest": "^29.4.1",
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('InfiniteQuery', () => {
2+
it('should work', () => {});
3+
4+
it('should work with signals', () => {});
5+
});

query/src/tests/is-fetching.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('IsFetching', () => {
2+
it('should work', () => {});
3+
4+
it('should work with signals', () => {});
5+
});

query/src/tests/is-mutating.spec.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('IsMutating', () => {
2+
it('should work', () => {});
3+
4+
it('should work with signals', () => {});
5+
});

query/src/tests/mutation.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
describe('mutation', () => {
2+
it('should work', () => {});
3+
4+
it('should work with signals', () => {});
5+
6+
it('should be typed', () => {});
7+
8+
it('should work with callback handlers', () => {});
9+
});

query/src/tests/operators.spec.ts

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
describe('Operators', () => {
2+
describe('mapResultData', () => {
3+
it('should map the data of a successful result', () => {
4+
// Write your test here
5+
});
6+
7+
// Additional tests for mapResultData
8+
});
9+
10+
describe('filterSuccessResult', () => {
11+
it('should filter only success results', () => {
12+
// Write your test here
13+
});
14+
15+
// Additional tests for filterSuccessResult
16+
});
17+
18+
describe('filterErrorResult', () => {
19+
it('should filter only error results', () => {
20+
// Write your test here
21+
});
22+
23+
// Additional tests for filterErrorResult
24+
});
25+
26+
describe('tapSuccessResult', () => {
27+
it('should tap into a success result', () => {
28+
// Write your test here
29+
});
30+
31+
// Additional tests for tapSuccessResult
32+
});
33+
34+
describe('tapErrorResult', () => {
35+
it('should tap into an error result', () => {
36+
// Write your test here
37+
});
38+
39+
// Additional tests for tapErrorResult
40+
});
41+
42+
describe('takeUntilResultFinalize', () => {
43+
it('should take values until result finalizes', () => {
44+
// Write your test here
45+
});
46+
47+
// Additional tests for takeUntilResultFinalize
48+
});
49+
50+
describe('takeUntilResultSuccess', () => {
51+
it('should take values until success result', () => {
52+
// Write your test here
53+
});
54+
55+
// Additional tests for takeUntilResultSuccess
56+
});
57+
58+
describe('takeUntilResultError', () => {
59+
it('should take values until error result', () => {
60+
// Write your test here
61+
});
62+
63+
// Additional tests for takeUntilResultError
64+
});
65+
66+
describe('startWithQueryResult', () => {
67+
it('should start with a default query result', () => {
68+
// Write your test here
69+
});
70+
71+
// Additional tests for startWithQueryResult
72+
});
73+
74+
describe('intersectResults$', () => {
75+
it('should intersect and map multiple query results', () => {
76+
// Write your test here
77+
});
78+
79+
// Additional tests for intersectResults$
80+
});
81+
});

query/src/tests/query.spec.ts

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import {
2+
Injectable,
3+
Injector,
4+
effect,
5+
runInInjectionContext,
6+
} from '@angular/core';
7+
import { injectQuery } from '../lib/query';
8+
import { injectQueryClient } from '../lib/query-client';
9+
import { queryOptions } from '../lib/query-options';
10+
import { expectTypeOf } from 'expect-type';
11+
import { map, timer } from 'rxjs';
12+
import { fakeAsync, flush, TestBed, tick } from '@angular/core/testing';
13+
14+
interface Todo {
15+
id: number;
16+
}
17+
18+
@Injectable({ providedIn: 'root' })
19+
class TodosService {
20+
#client = injectQueryClient();
21+
#query = injectQuery();
22+
23+
#getTodosOptions = queryOptions({
24+
queryKey: ['todos'] as const,
25+
queryFn: () => {
26+
return timer(1000).pipe(
27+
map(() => {
28+
return [
29+
{
30+
id: 1,
31+
},
32+
{ id: 2 },
33+
] as Todo[];
34+
}),
35+
);
36+
},
37+
});
38+
39+
getTodos() {
40+
return this.#query(this.#getTodosOptions);
41+
}
42+
43+
getCachedTodos() {
44+
return this.#client.getQueryData(this.#getTodosOptions.queryKey);
45+
}
46+
}
47+
48+
describe('query', () => {
49+
let service: TodosService;
50+
51+
beforeEach(() => {
52+
TestBed.configureTestingModule({
53+
providers: [TodosService],
54+
});
55+
service = TestBed.inject(TodosService);
56+
});
57+
58+
it('should work', fakeAsync(() => {
59+
const spy = jest.fn();
60+
61+
const sub = service.getTodos().result$.subscribe((v) => {
62+
spy(v.status);
63+
expectTypeOf(v.data).toEqualTypeOf<Todo[] | undefined>();
64+
});
65+
66+
expect(spy).toHaveBeenCalledTimes(1);
67+
expect(spy).toHaveBeenCalledWith('pending');
68+
tick(1000);
69+
flush();
70+
71+
expect(spy).toHaveBeenCalledTimes(2);
72+
expect(spy).toHaveBeenCalledWith('success');
73+
74+
sub.unsubscribe();
75+
flush();
76+
}));
77+
78+
it('should work with signals', fakeAsync(() => {
79+
const spy = jest.fn();
80+
81+
runInInjectionContext(TestBed.inject(Injector), () => {
82+
const result = service.getTodos().result;
83+
expectTypeOf(result().data).toEqualTypeOf<Todo[] | undefined>();
84+
85+
effect(() => {
86+
spy(result().status);
87+
});
88+
});
89+
90+
TestBed.flushEffects();
91+
92+
expect(spy).toHaveBeenCalledTimes(1);
93+
expect(spy).toHaveBeenCalledWith('pending');
94+
tick(1000);
95+
flush();
96+
TestBed.flushEffects();
97+
expect(spy).toHaveBeenCalledTimes(2);
98+
expect(spy).toHaveBeenCalledWith('success');
99+
}));
100+
101+
it('should be typed', () => {
102+
expectTypeOf(service.getCachedTodos()).toEqualTypeOf<Todo[] | undefined>();
103+
});
104+
});

query/src/tests/signals.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
describe('intersectResults', () => {
2+
it('should correctly merge and map multiple successful signal queries', () => {});
3+
4+
it('should handle error results correctly', () => {});
5+
6+
// Additional tests for different scenarios, such as pending, loading states, and mixed results
7+
});

0 commit comments

Comments
 (0)