feat(crud): add jest test config

This commit is contained in:
thomas laforge
2022-12-11 20:42:13 +01:00
parent a9a3b7a7d8
commit 97f1ede245
6 changed files with 102 additions and 0 deletions

22
apps/crud/jest.config.ts Normal file
View 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',
],
};

View File

@@ -79,6 +79,14 @@
"options": { "options": {
"lintFilePatterns": ["apps/crud/**/*.ts", "apps/crud/**/*.html"] "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": [] "tags": []

View 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),
(_) => _
)
)
)
)
);
}

View File

@@ -0,0 +1 @@
import 'jest-preset-angular/setup-jest';

View File

@@ -6,6 +6,9 @@
{ {
"path": "./tsconfig.app.json" "path": "./tsconfig.app.json"
}, },
{
"path": "./tsconfig.spec.json"
},
{ {
"path": "./tsconfig.editor.json" "path": "./tsconfig.editor.json"
} }

View 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"]
}