From 5a00ac15fedd219a3f678d2119ebf41dff75283b Mon Sep 17 00:00:00 2001 From: Sean Hamilton Date: Tue, 2 Jul 2019 15:53:16 +0100 Subject: [PATCH] test(set): add set function tests --- src/__tests__/set.spec.ts | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/__tests__/set.spec.ts diff --git a/src/__tests__/set.spec.ts b/src/__tests__/set.spec.ts new file mode 100644 index 00000000..87b3f1b0 --- /dev/null +++ b/src/__tests__/set.spec.ts @@ -0,0 +1,35 @@ +import set from '../set'; + +describe('set', () => { + test('should return a new object not a reference', () => { + const obj = {}; + const result = set(obj, 'a', 1); + + expect(result.a).toBe(1); + expect(Object.keys(obj)).toHaveLength(0); + }); + + test('should set the specified value on the object', () => { + const obj = {}; + const result = set(obj, 'a', 1); + + expect(result.a).toBe(1); + }); + + test('should use dot notation for the path', () => { + const obj = {}; + const result = set(obj, 'a.b', 1); + + expect(result.a.b).toBe(1); + }); + + test('should throw TypeError for invalid arguments', () => { + const invalidObj: unknown = 'testing'; + const invalidPath: unknown = 1; + + expect(() => set(invalidObj as OObject, 'test', 'test')) + .toThrow(new TypeError('Expected Object, got string testing')); + expect(() => set({}, invalidPath as string, 'test')) + .toThrow(new TypeError('Expected String, got number 1')); + }); +});