Skip to content

Commit

Permalink
test(@toss/utils): Improve tests for get-set (toss#395)
Browse files Browse the repository at this point in the history
* test: improve test code

* fix: remove useless test code

* test: separate get function test case
  • Loading branch information
po4tion authored Dec 16, 2023
1 parent 0fd0b65 commit 970bc9f
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions packages/common/utils/src/get-set.spec.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
import { get, set } from './get-set';

describe('get function', () => {
it('should correctly retrieve a value for a single-level property', () => {
expect(get({ a: { b: { c: 'd' } } }, 'a')).toStrictEqual({ b: { c: 'd' } });
describe('Returns a valid value.', () => {
it('should correctly retrieve a value for a single-level property', () => {
expect(get({ a: { b: { c: 'd' } } }, 'a')).toStrictEqual({ b: { c: 'd' } });
});
it('should correctly retrieve a value for a multi-level property', () => {
expect(get({ a: { b: { c: 'd' } } }, 'a.b')).toStrictEqual({ c: 'd' });
expect(get({ a: { b: { c: 'd' } } }, 'a.b.c')).toStrictEqual('d');
});
it('should return a default value when the specified path does not exist', () => {
expect(get({ a: { b: { c: 'd' } } }, 'a.c', 'default')).toStrictEqual('default');
});
it('should ignore consecutive dots in path and retrieve the correct value', () => {
expect(get({ a: { b: { c: 'd' } } }, 'a..b')).toStrictEqual({ c: 'd' });
});
});
it('should correctly retrieve a value for a multi-level property', () => {
expect(get({ a: { b: { c: 'd' } } }, 'a.b')).toStrictEqual({ c: 'd' });
expect(get({ a: { b: { c: 'd' } } }, 'a.b.c')).toStrictEqual('d');
});
it('should return a default value when the specified path does not exist', () => {
expect(get({ a: { b: { c: 'd' } } }, 'a.c', 'default')).toStrictEqual('default');
});
it('should ignore consecutive dots in path and retrieve the correct value', () => {
expect(get({ a: { b: { c: 'd' } } }, 'a..b')).toStrictEqual({ c: 'd' });
});
it('should work correctly with empty objects', () => {
expect(get({}, 'a')).toBeUndefined();

describe('Returns null or undefined', () => {
it('should work correctly with empty objects', () => {
expect(get({}, 'a')).toBeUndefined();
});
it('should handle paths leading to undefined or null values inside nested objects', () => {
const obj = { a: { b: undefined, c: null } };
expect(get(obj, 'a.b')).toBeUndefined();
expect(get(obj, 'a.c')).toBeNull();
expect(get(obj, 'a.b.d')).toBeUndefined();
});
});
});

Expand Down

0 comments on commit 970bc9f

Please sign in to comment.