mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-11 21:33:02 -05:00
feat(utils): add utils library with a fake ramdon generator
This commit is contained in:
1
libs/shared/utils/src/index.ts
Normal file
1
libs/shared/utils/src/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './lib/random-http-error.utils';
|
||||
24
libs/shared/utils/src/lib/random-http-error.utils.ts
Normal file
24
libs/shared/utils/src/lib/random-http-error.utils.ts
Normal file
@@ -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 = <T>({
|
||||
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();
|
||||
};
|
||||
1
libs/shared/utils/src/test-setup.ts
Normal file
1
libs/shared/utils/src/test-setup.ts
Normal file
@@ -0,0 +1 @@
|
||||
import 'jest-preset-angular/setup-jest';
|
||||
Reference in New Issue
Block a user