Skip to content

Commit

Permalink
test: functions
Browse files Browse the repository at this point in the history
  • Loading branch information
oscar-guerin committed Aug 2, 2024
1 parent e7cb681 commit 6ddb3da
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/functions.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {get, intersection, set} from './functions';

describe('#get', () => {
const simpleObject: unknown = {a: {b: 2}};
const complexObject: unknown = {a: [{bar: {c: 3}}]};
const falsyObject: unknown = {a: null, b: undefined, c: 0};

it('should return 2 for simpleObject and path "a.b"', () => {
expect(get(simpleObject, 'a.b')).toBe(2);
});

it('should return 3 for complexObject and path "a[0].bar.c"', () => {
expect(get(complexObject, 'a[0].bar.c')).toBe(3);
});

it('should return 3 for complexObject and path array ["a", "0", "bar", "c"]', () => {
expect(get(complexObject, ['a', '0', 'bar', 'c'])).toBe(3);
});

it('should return "default" for simpleObject and invalid path "a.bar.c"', () => {
expect(get(simpleObject, 'a.bar.c', 'default')).toBe('default');
});

it('should return "default" for complexObject and invalid path "a.bar.c"', () => {
expect(get(complexObject, 'a.bar.c', 'default')).toBe('default');
});

it('should return undefined for complexObject and null path', () => {
expect(get(complexObject, null)).toBeUndefined();
});

it('should return null for falsyObject and path "a"', () => {
expect(get(falsyObject, 'a', 'default')).toBeNull();
});

it('should return undefined for falsyObject and path "b"', () => {
expect(get(falsyObject, 'b')).toBeUndefined();
});

it('should return 0 for falsyObject and path "c"', () => {
expect(get(falsyObject, 'c', 'default')).toBe(0);
});
});

describe('#set', () => {
const object: any = {a: [{bar: {c: 3}}]};

it('should set object.a[0].bar.c to 4', () => {
set(object, 'a[0].bar.c', 4);
expect(object.a[0].bar.c).toBe(4);
});

it('should set object.x[0].y.z to 5', () => {
set(object, ['x', '0', 'y', 'z'], 5);
expect(object.x[0].y.z).toBe(5);
});
});

describe('#intersection', () => {
it('should return [2] for intersection([2, 1], [2, 3])', () => {
expect(intersection([2, 1], [2, 3])).toEqual([2]);
});
});


0 comments on commit 6ddb3da

Please sign in to comment.