Skip to content

Commit

Permalink
test(query): 🥳 add initial tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NetanelBasal committed Nov 23, 2023
1 parent 3b4b403 commit eabe879
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 1 deletion.
12 changes: 11 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"eslint": "~8.46.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-playwright": "^0.15.3",
"expect-type": "^0.17.3",
"git-cz": "^4.9.0",
"husky": "^8.0.3",
"jest": "^29.4.1",
Expand Down
5 changes: 5 additions & 0 deletions query/src/tests/infinite-query.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('InfiniteQuery', () => {
it('should work', () => {});

it('should work with signals', () => {});
});
5 changes: 5 additions & 0 deletions query/src/tests/is-fetching.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('IsFetching', () => {
it('should work', () => {});

it('should work with signals', () => {});
});
5 changes: 5 additions & 0 deletions query/src/tests/is-mutating.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe('IsMutating', () => {
it('should work', () => {});

it('should work with signals', () => {});
});
9 changes: 9 additions & 0 deletions query/src/tests/mutation.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('mutation', () => {
it('should work', () => {});

it('should work with signals', () => {});

it('should be typed', () => {});

it('should work with callback handlers', () => {});
});
81 changes: 81 additions & 0 deletions query/src/tests/operators.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
describe('Operators', () => {
describe('mapResultData', () => {
it('should map the data of a successful result', () => {
// Write your test here
});

// Additional tests for mapResultData
});

describe('filterSuccessResult', () => {
it('should filter only success results', () => {
// Write your test here
});

// Additional tests for filterSuccessResult
});

describe('filterErrorResult', () => {
it('should filter only error results', () => {
// Write your test here
});

// Additional tests for filterErrorResult
});

describe('tapSuccessResult', () => {
it('should tap into a success result', () => {
// Write your test here
});

// Additional tests for tapSuccessResult
});

describe('tapErrorResult', () => {
it('should tap into an error result', () => {
// Write your test here
});

// Additional tests for tapErrorResult
});

describe('takeUntilResultFinalize', () => {
it('should take values until result finalizes', () => {
// Write your test here
});

// Additional tests for takeUntilResultFinalize
});

describe('takeUntilResultSuccess', () => {
it('should take values until success result', () => {
// Write your test here
});

// Additional tests for takeUntilResultSuccess
});

describe('takeUntilResultError', () => {
it('should take values until error result', () => {
// Write your test here
});

// Additional tests for takeUntilResultError
});

describe('startWithQueryResult', () => {
it('should start with a default query result', () => {
// Write your test here
});

// Additional tests for startWithQueryResult
});

describe('intersectResults$', () => {
it('should intersect and map multiple query results', () => {
// Write your test here
});

// Additional tests for intersectResults$
});
});
104 changes: 104 additions & 0 deletions query/src/tests/query.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import {
Injectable,
Injector,
effect,
runInInjectionContext,
} from '@angular/core';
import { injectQuery } from '../lib/query';
import { injectQueryClient } from '../lib/query-client';
import { queryOptions } from '../lib/query-options';
import { expectTypeOf } from 'expect-type';
import { map, timer } from 'rxjs';
import { fakeAsync, flush, TestBed, tick } from '@angular/core/testing';

interface Todo {
id: number;
}

@Injectable({ providedIn: 'root' })
class TodosService {
#client = injectQueryClient();
#query = injectQuery();

#getTodosOptions = queryOptions({
queryKey: ['todos'] as const,
queryFn: () => {
return timer(1000).pipe(
map(() => {
return [
{
id: 1,
},
{ id: 2 },
] as Todo[];
}),
);
},
});

getTodos() {
return this.#query(this.#getTodosOptions);
}

getCachedTodos() {
return this.#client.getQueryData(this.#getTodosOptions.queryKey);
}
}

describe('query', () => {
let service: TodosService;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [TodosService],
});
service = TestBed.inject(TodosService);
});

it('should work', fakeAsync(() => {
const spy = jest.fn();

const sub = service.getTodos().result$.subscribe((v) => {
spy(v.status);
expectTypeOf(v.data).toEqualTypeOf<Todo[] | undefined>();
});

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('pending');
tick(1000);
flush();

expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledWith('success');

sub.unsubscribe();
flush();
}));

it('should work with signals', fakeAsync(() => {
const spy = jest.fn();

runInInjectionContext(TestBed.inject(Injector), () => {
const result = service.getTodos().result;
expectTypeOf(result().data).toEqualTypeOf<Todo[] | undefined>();

effect(() => {
spy(result().status);
});
});

TestBed.flushEffects();

expect(spy).toHaveBeenCalledTimes(1);
expect(spy).toHaveBeenCalledWith('pending');
tick(1000);
flush();
TestBed.flushEffects();
expect(spy).toHaveBeenCalledTimes(2);
expect(spy).toHaveBeenCalledWith('success');
}));

it('should be typed', () => {
expectTypeOf(service.getCachedTodos()).toEqualTypeOf<Todo[] | undefined>();
});
});
7 changes: 7 additions & 0 deletions query/src/tests/signals.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe('intersectResults', () => {
it('should correctly merge and map multiple successful signal queries', () => {});

it('should handle error results correctly', () => {});

// Additional tests for different scenarios, such as pending, loading states, and mixed results
});

0 comments on commit eabe879

Please sign in to comment.