diff --git a/apps/rxjs-pipe-bug/project.json b/apps/rxjs-pipe-bug/project.json index 90b7abe..f863344 100644 --- a/apps/rxjs-pipe-bug/project.json +++ b/apps/rxjs-pipe-bug/project.json @@ -20,7 +20,8 @@ "apps/rxjs-pipe-bug/src/assets" ], "styles": ["apps/rxjs-pipe-bug/src/styles.scss"], - "scripts": [] + "scripts": [], + "allowedCommonJsDependencies": ["seedrandom"] }, "configurations": { "production": { diff --git a/apps/rxjs-pipe-bug/src/app/localDB.service.ts b/apps/rxjs-pipe-bug/src/app/localDB.service.ts index 710c62f..88d1e1d 100644 --- a/apps/rxjs-pipe-bug/src/app/localDB.service.ts +++ b/apps/rxjs-pipe-bug/src/app/localDB.service.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/member-ordering */ +import { randomError } from '@angular-challenges/shared/utils'; import { Injectable } from '@angular/core'; -import { randNumber } from '@ngneat/falso'; import { ComponentStore } from '@ngrx/component-store'; import { of } from 'rxjs'; @@ -47,12 +47,12 @@ export class LocalDBService extends ComponentStore { }) ); - deleteOneTopic = (id: number) => { - const randomNumber = randNumber({ min: 0.1, max: 1, fraction: 2 }); - if (randomNumber > 0.5) { - this.deleteOne(id); - return of(true); - } - return of(false); - }; + deleteOneTopic = (id: number) => + randomError({ + success: () => { + this.deleteOne(id); + return of(true); + }, + error: () => of(false), + }); } diff --git a/libs/shared/utils/.eslintrc.json b/libs/shared/utils/.eslintrc.json new file mode 100644 index 0000000..7b4ae9b --- /dev/null +++ b/libs/shared/utils/.eslintrc.json @@ -0,0 +1,36 @@ +{ + "extends": ["../../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts"], + "rules": { + "@angular-eslint/directive-selector": [ + "error", + { + "type": "attribute", + "prefix": "angularChallenges", + "style": "camelCase" + } + ], + "@angular-eslint/component-selector": [ + "error", + { + "type": "element", + "prefix": "angular-challenges", + "style": "kebab-case" + } + ] + }, + "extends": [ + "plugin:@nrwl/nx/angular", + "plugin:@angular-eslint/template/process-inline-templates" + ] + }, + { + "files": ["*.html"], + "extends": ["plugin:@nrwl/nx/angular-template"], + "rules": {} + } + ] +} diff --git a/libs/shared/utils/README.md b/libs/shared/utils/README.md new file mode 100644 index 0000000..146a8b8 --- /dev/null +++ b/libs/shared/utils/README.md @@ -0,0 +1,7 @@ +# shared-utils + +This library was generated with [Nx](https://nx.dev). + +## Running unit tests + +Run `nx test shared-utils` to execute the unit tests. diff --git a/libs/shared/utils/jest.config.ts b/libs/shared/utils/jest.config.ts new file mode 100644 index 0000000..96edea3 --- /dev/null +++ b/libs/shared/utils/jest.config.ts @@ -0,0 +1,22 @@ +/* eslint-disable */ +export default { + displayName: 'shared-utils', + preset: '../../../jest.preset.js', + setupFilesAfterEnv: ['/src/test-setup.ts'], + globals: { + 'ts-jest': { + tsconfig: '/tsconfig.spec.json', + stringifyContentPathRegex: '\\.(html|svg)$', + }, + }, + coverageDirectory: '../../../coverage/libs/shared/utils', + 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/libs/shared/utils/project.json b/libs/shared/utils/project.json new file mode 100644 index 0000000..917309a --- /dev/null +++ b/libs/shared/utils/project.json @@ -0,0 +1,28 @@ +{ + "name": "shared-utils", + "$schema": "../../../node_modules/nx/schemas/project-schema.json", + "projectType": "library", + "sourceRoot": "libs/shared/utils/src", + "prefix": "angular-challenges", + "targets": { + "test": { + "executor": "@nrwl/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], + "options": { + "jestConfig": "libs/shared/utils/jest.config.ts", + "passWithNoTests": true + } + }, + "lint": { + "executor": "@nrwl/linter:eslint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": [ + "libs/shared/utils/**/*.ts", + "libs/shared/utils/**/*.html" + ] + } + } + }, + "tags": [] +} diff --git a/libs/shared/utils/src/index.ts b/libs/shared/utils/src/index.ts new file mode 100644 index 0000000..2b1a80f --- /dev/null +++ b/libs/shared/utils/src/index.ts @@ -0,0 +1 @@ +export * from './lib/random-http-error.utils'; diff --git a/libs/shared/utils/src/lib/random-http-error.utils.ts b/libs/shared/utils/src/lib/random-http-error.utils.ts new file mode 100644 index 0000000..7ed0f97 --- /dev/null +++ b/libs/shared/utils/src/lib/random-http-error.utils.ts @@ -0,0 +1,24 @@ +import { randNumber } from '@ngneat/falso'; + +/** + * Execute success function if value is above threshold and error function otherwise + * Success and error function should return the same ReturnType + * @param success + * @param error + * @param threashold default to 0.5 + */ +export const randomError = ({ + success, + error, + threashold, +}: { + success: () => T; + error: () => T; + threashold?: number; +}): T => { + const randomNumber = randNumber({ min: 0.1, max: 1, fraction: 2 }); + if (randomNumber > (threashold ?? 0.5)) { + return success(); + } + return error(); +}; diff --git a/libs/shared/utils/src/test-setup.ts b/libs/shared/utils/src/test-setup.ts new file mode 100644 index 0000000..1100b3e --- /dev/null +++ b/libs/shared/utils/src/test-setup.ts @@ -0,0 +1 @@ +import 'jest-preset-angular/setup-jest'; diff --git a/libs/shared/utils/tsconfig.json b/libs/shared/utils/tsconfig.json new file mode 100644 index 0000000..5cf0a16 --- /dev/null +++ b/libs/shared/utils/tsconfig.json @@ -0,0 +1,29 @@ +{ + "compilerOptions": { + "target": "es2022", + "useDefineForClassFields": false, + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ], + "extends": "../../../tsconfig.base.json", + "angularCompilerOptions": { + "enableI18nLegacyMessageIdFormat": false, + "strictInjectionParameters": true, + "strictInputAccessModifiers": true, + "strictTemplates": true + } +} diff --git a/libs/shared/utils/tsconfig.lib.json b/libs/shared/utils/tsconfig.lib.json new file mode 100644 index 0000000..4026c77 --- /dev/null +++ b/libs/shared/utils/tsconfig.lib.json @@ -0,0 +1,17 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../dist/out-tsc", + "declaration": true, + "declarationMap": true, + "inlineSources": true, + "types": [] + }, + "exclude": [ + "src/test-setup.ts", + "src/**/*.spec.ts", + "jest.config.ts", + "src/**/*.test.ts" + ], + "include": ["src/**/*.ts"] +} diff --git a/libs/shared/utils/tsconfig.spec.json b/libs/shared/utils/tsconfig.spec.json new file mode 100644 index 0000000..ca14cc6 --- /dev/null +++ b/libs/shared/utils/tsconfig.spec.json @@ -0,0 +1,15 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "files": ["src/test-setup.ts"], + "include": [ + "jest.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.d.ts" + ] +} diff --git a/tsconfig.base.json b/tsconfig.base.json index a033bbb..aa02329 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -21,6 +21,7 @@ "@angular-challenges/ngrx-notification/model": [ "libs/ngrx-notification/model/src/index.ts" ], + "@angular-challenges/shared/utils": ["libs/shared/utils/src/index.ts"], "@tomalaforge/ngrx-callstate-store": [ "libs/shared/ngrx-callstate-store/src/index.ts" ]