mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 12:53:03 -05:00
feat(crud): add jest test config
This commit is contained in:
22
apps/crud/jest.config.ts
Normal file
22
apps/crud/jest.config.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/* eslint-disable */
|
||||
export default {
|
||||
displayName: 'crud',
|
||||
preset: '../../jest.preset.js',
|
||||
setupFilesAfterEnv: ['<rootDir>/src/test-setup.ts'],
|
||||
globals: {
|
||||
'ts-jest': {
|
||||
tsconfig: '<rootDir>/tsconfig.spec.json',
|
||||
stringifyContentPathRegex: '\\.(html|svg)$',
|
||||
},
|
||||
},
|
||||
coverageDirectory: '../../coverage/apps/crud',
|
||||
transform: {
|
||||
'^.+\\.(ts|mjs|js|html)$': 'jest-preset-angular',
|
||||
},
|
||||
transformIgnorePatterns: ['node_modules/(?!.*\\.mjs$)'],
|
||||
snapshotSerializers: [
|
||||
'jest-preset-angular/build/serializers/no-ng-attributes',
|
||||
'jest-preset-angular/build/serializers/ng-snapshot',
|
||||
'jest-preset-angular/build/serializers/html-comment',
|
||||
],
|
||||
};
|
||||
@@ -79,6 +79,14 @@
|
||||
"options": {
|
||||
"lintFilePatterns": ["apps/crud/**/*.ts", "apps/crud/**/*.html"]
|
||||
}
|
||||
},
|
||||
"test": {
|
||||
"executor": "@nrwl/jest:jest",
|
||||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
||||
"options": {
|
||||
"jestConfig": "apps/crud/jest.config.ts",
|
||||
"passWithNoTests": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"tags": []
|
||||
|
||||
58
apps/crud/src/app/app.store.ts
Normal file
58
apps/crud/src/app/app.store.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ComponentStore, tapResponse } from '@ngrx/component-store';
|
||||
import { pipe, switchMap, tap } from 'rxjs';
|
||||
import { Todo } from './todo.model';
|
||||
import { TodoService } from './todo.service';
|
||||
|
||||
@Injectable()
|
||||
export class AppStore extends ComponentStore<{
|
||||
todos: Todo[];
|
||||
loading: boolean;
|
||||
}> {
|
||||
readonly todos$ = this.select((state) => state.todos);
|
||||
readonly loading$ = this.select((state) => state.loading);
|
||||
|
||||
readonly person$;
|
||||
|
||||
readonly vm$ = this.select({
|
||||
todos: this.todos$,
|
||||
loading: this.loading$,
|
||||
});
|
||||
|
||||
constructor(private todoService: TodoService) {
|
||||
super({ todos: [], loading: false });
|
||||
}
|
||||
|
||||
private readonly updateTodos = this.updater((state, todo: Todo) => ({
|
||||
loading: false,
|
||||
todos: state.todos.map((t) => (t.id === todo.id ? { ...todo } : t)),
|
||||
}));
|
||||
|
||||
readonly fetchTodo = this.effect<void>(
|
||||
pipe(
|
||||
tap(() => this.patchState({ loading: true })),
|
||||
switchMap(() =>
|
||||
this.todoService.getAllTodo().pipe(
|
||||
tapResponse(
|
||||
(todos) => this.patchState({ todos, loading: false }),
|
||||
(_) => _
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
readonly updateTodo = this.effect<number>(
|
||||
pipe(
|
||||
tap(() => this.patchState({ loading: true })),
|
||||
switchMap((id) =>
|
||||
this.todoService.update(id).pipe(
|
||||
tapResponse(
|
||||
(todo) => this.updateTodos(todo),
|
||||
(_) => _
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
1
apps/crud/src/test-setup.ts
Normal file
1
apps/crud/src/test-setup.ts
Normal file
@@ -0,0 +1 @@
|
||||
import 'jest-preset-angular/setup-jest';
|
||||
@@ -6,6 +6,9 @@
|
||||
{
|
||||
"path": "./tsconfig.app.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.spec.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.editor.json"
|
||||
}
|
||||
|
||||
10
apps/crud/tsconfig.spec.json
Normal file
10
apps/crud/tsconfig.spec.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "../../dist/out-tsc",
|
||||
"module": "commonjs",
|
||||
"types": ["jest", "node"]
|
||||
},
|
||||
"files": ["src/test-setup.ts"],
|
||||
"include": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts", "**/*.d.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user