Skip to content

Commit

Permalink
Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
CeEv committed Jul 26, 2024
1 parent 134d0fe commit c9292fa
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 13 deletions.
114 changes: 111 additions & 3 deletions apps/server/src/shared/common/guards/type.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ describe('TypeGuard', () => {
});

it('should throw an error', () => {
expect(() => TypeGuard.checkArray(1)).toThrowError('Type is not an array with elements.');
expect(() => TypeGuard.checkArrayWithElements(1)).toThrowError('Type is not an array with elements.');
});
});
});
Expand Down Expand Up @@ -411,9 +411,117 @@ describe('TypeGuard', () => {
});
});

// getValueFromObjectKey
describe('getValueFromObjectKey', () => {
describe('when passing value is an object that has the requested key', () => {
it('should be return the key value', () => {
expect(TypeGuard.getValueFromObjectKey({ xyz: 'abc' }, 'xyz')).toEqual('abc');
});
});

describe('when passing value and key do not match', () => {
it('should be return undefined', () => {
expect(TypeGuard.getValueFromObjectKey({ xyz: 'abc' }, 'zzz')).toBeUndefined();
});

it('should be return undefined', () => {
expect(TypeGuard.getValueFromObjectKey([], 'zzz')).toBeUndefined();
});

it('should be return undefined', () => {
expect(TypeGuard.getValueFromObjectKey('string', 'zzz')).toBeUndefined();
});

it('should be return undefined', () => {
expect(TypeGuard.getValueFromObjectKey(1, 'zzz')).toBeUndefined();
});

it('should be return undefined', () => {
expect(TypeGuard.getValueFromObjectKey(null, 'zzz')).toBeUndefined();
});

it('should be return undefined', () => {
expect(TypeGuard.getValueFromObjectKey(undefined, 'zzz')).toBeUndefined();
});
});

describe('when param contract is not fullfilled', () => {
it('should be throw an error', () => {
// @ts-expect-error test-case
expect(() => TypeGuard.getValueFromObjectKey({ xyz: 'abc' }, undefined)).toThrowError('Type is not a string');
});

it('should be throw an error', () => {
// @ts-expect-error test-case
expect(() => TypeGuard.getValueFromObjectKey({ xyz: 'abc' }, null)).toThrowError('Type is not a string');
});

it('should be throw an error', () => {
// @ts-expect-error test-case
expect(() => TypeGuard.getValueFromObjectKey({ xyz: 'abc' }, 1)).toThrowError('Type is not a string');
});
});
});

describe('getValueFromDeepObjectKey', () => {
describe('when passing value is an object that has the requested key path', () => {
it('should be return the key value', () => {
expect(TypeGuard.getValueFromDeepObjectKey({ x1: { x2: 'abc' } }, ['x1', 'x2'])).toEqual('abc');
});
});

describe('when passing value and key path do not match', () => {
it('should be return undefined', () => {
expect(TypeGuard.getValueFromDeepObjectKey({ xyz: 'abc' }, ['x1', 'x2'])).toBeUndefined();
});

it('should be return undefined', () => {
expect(TypeGuard.getValueFromDeepObjectKey({ x1: { zzz: 'abc' } }, ['x1', 'x2'])).toBeUndefined();
});

it('should be return undefined', () => {
expect(TypeGuard.getValueFromDeepObjectKey([], ['x1', 'x2'])).toBeUndefined();
});

it('should be return undefined', () => {
expect(TypeGuard.getValueFromDeepObjectKey('string', ['x1', 'x2'])).toBeUndefined();
});

it('should be return undefined', () => {
expect(TypeGuard.getValueFromDeepObjectKey(1, ['x1', 'x2'])).toBeUndefined();
});

// getValueFromDeepObjectKey
it('should be return undefined', () => {
expect(TypeGuard.getValueFromDeepObjectKey(null, ['x1', 'x2'])).toBeUndefined();
});

it('should be return undefined', () => {
expect(TypeGuard.getValueFromDeepObjectKey(undefined, ['x1', 'x2'])).toBeUndefined();
});
});

describe('when param contract is not fullfilled', () => {
it('should be throw an error', () => {
// @ts-expect-error test-case
expect(() => TypeGuard.getValueFromDeepObjectKey({ x1: { x2: 'abc' } }, undefined)).toThrowError(
'Type is not an array with elements.'
);
});

it('should be throw an error', () => {
// @ts-expect-error test-case
expect(() => TypeGuard.getValueFromDeepObjectKey({ x1: { x2: 'abc' } }, null)).toThrowError(
'Type is not an array with elements.'
);
});

it('should be throw an error', () => {
// @ts-expect-error test-case
expect(() => TypeGuard.getValueFromDeepObjectKey({ x1: { x2: 'abc' } }, 1)).toThrowError(
'Type is not an array with elements.'
);
});
});
});

// checkKeyInObject

Expand Down
24 changes: 14 additions & 10 deletions apps/server/src/shared/common/guards/type.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export class TypeGuard {
return isUndefined;
}

static isNumber(value: unknown): value is number {
const isNumber = typeof value === 'number';

return isNumber;
}

static checkNumber(value: unknown): number {
if (!TypeGuard.isNumber(value)) {
throw new Error('Type is not a number');
Expand All @@ -25,10 +31,10 @@ export class TypeGuard {
return value;
}

static isNumber(value: unknown): value is number {
const isNumber = typeof value === 'number';
static isString(value: unknown): value is string {
const isString = typeof value === 'string';

return isNumber;
return isString;
}

static checkString(value: unknown): string {
Expand All @@ -39,12 +45,6 @@ export class TypeGuard {
return value;
}

static isString(value: unknown): value is string {
const isString = typeof value === 'string';

return isString;
}

static isArray(value: unknown): value is [] {
const isArray = Array.isArray(value);

Expand All @@ -53,7 +53,7 @@ export class TypeGuard {

static checkArray(value: unknown): [] {
if (!TypeGuard.isArray(value)) {
throw new Error('Type is not an array');
throw new Error('Type is not an array.');
}

return value;
Expand Down Expand Up @@ -89,12 +89,16 @@ export class TypeGuard {

/** @return undefined if no object or key do not exists, otherwise the value of the key. */
static getValueFromObjectKey(value: unknown, key: string): unknown {
TypeGuard.checkString(key);

const result: unknown = TypeGuard.isDefinedObject(value) ? value[key] : undefined;

return result;
}

static getValueFromDeepObjectKey(value: unknown, keyPath: string[]): unknown {
TypeGuard.checkArrayWithElements(keyPath);

let result: unknown = value;

keyPath.forEach((key) => {
Expand Down

0 comments on commit c9292fa

Please sign in to comment.