diff --git a/apps/crud/jest.config.ts b/apps/crud/jest.config.ts new file mode 100644 index 0000000..0c36a73 --- /dev/null +++ b/apps/crud/jest.config.ts @@ -0,0 +1,22 @@ +/* eslint-disable */ +export default { + displayName: 'crud', + preset: '../../jest.preset.js', + setupFilesAfterEnv: ['/src/test-setup.ts'], + globals: { + 'ts-jest': { + tsconfig: '/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', + ], +}; diff --git a/apps/crud/project.json b/apps/crud/project.json index 675e401..d0fcadf 100644 --- a/apps/crud/project.json +++ b/apps/crud/project.json @@ -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": [] diff --git a/apps/crud/src/app/app.store.ts b/apps/crud/src/app/app.store.ts new file mode 100644 index 0000000..c875718 --- /dev/null +++ b/apps/crud/src/app/app.store.ts @@ -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( + pipe( + tap(() => this.patchState({ loading: true })), + switchMap(() => + this.todoService.getAllTodo().pipe( + tapResponse( + (todos) => this.patchState({ todos, loading: false }), + (_) => _ + ) + ) + ) + ) + ); + + readonly updateTodo = this.effect( + pipe( + tap(() => this.patchState({ loading: true })), + switchMap((id) => + this.todoService.update(id).pipe( + tapResponse( + (todo) => this.updateTodos(todo), + (_) => _ + ) + ) + ) + ) + ); +} diff --git a/apps/crud/src/test-setup.ts b/apps/crud/src/test-setup.ts new file mode 100644 index 0000000..1100b3e --- /dev/null +++ b/apps/crud/src/test-setup.ts @@ -0,0 +1 @@ +import 'jest-preset-angular/setup-jest'; diff --git a/apps/crud/tsconfig.json b/apps/crud/tsconfig.json index 0b7a078..c756d84 100644 --- a/apps/crud/tsconfig.json +++ b/apps/crud/tsconfig.json @@ -6,6 +6,9 @@ { "path": "./tsconfig.app.json" }, + { + "path": "./tsconfig.spec.json" + }, { "path": "./tsconfig.editor.json" } diff --git a/apps/crud/tsconfig.spec.json b/apps/crud/tsconfig.spec.json new file mode 100644 index 0000000..c5db027 --- /dev/null +++ b/apps/crud/tsconfig.spec.json @@ -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"] +}