Skip to content

Commit

Permalink
test(core): added tests to optional file
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Mar 1, 2022
1 parent c651a64 commit e72baf4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/v2/core/optional.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
/**
* Return the defaultValue whether the value is undefined, otherwise, return the value.
*
* @example```typescript
* const value1 = getDefaultIfUndefined(undefined, true);
* const value2 = getDefaultIfUndefined(false, true);
*
* console.log(value1);
* // true
* console.log(value2);
* // false
* ```
*
* @param value The value to be checked
* @param defaultValue The default value when value is undefined
*/
Expand Down
32 changes: 32 additions & 0 deletions test/core/optional.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { getDefaultIfUndefined } from '../../src/v2/core';

describe('getDefaultIfUndefined', () => {
it('should return the value when value is not undefined', () => {
const options: [testValue: any, defaultValue: any, expectedValue: any][] = [
['batata', 'potato', 'batata'],
[true, false, true],
[false, true, false],
];

for (const [testValue, defaultValue, expectedValue] of options) {
expect(getDefaultIfUndefined(testValue, defaultValue)).toBe(
expectedValue,
);
}
});

it('should return the default value when value is undefined', () => {
const options: [testValue: any, defaultValue: any, expectedValue: any][] = [
[undefined, true, true],
[undefined, 'text', 'text'],
[void 0, true, true],
[void 0, 'text', 'text'],
];

for (const [testValue, defaultValue, expectedValue] of options) {
expect(getDefaultIfUndefined(testValue, defaultValue)).toBe(
expectedValue,
);
}
});
});

0 comments on commit e72baf4

Please sign in to comment.