diff --git a/src/__tests__/defaults.spec.ts b/src/__tests__/defaults.spec.ts new file mode 100644 index 00000000..ff90363a --- /dev/null +++ b/src/__tests__/defaults.spec.ts @@ -0,0 +1,27 @@ +import defaults from '../defaults' +import { OObject } from '../types' + +describe('defaults', (): void => { + test('should merge all objects', (): void => { + const getDefaults = defaults({ a: 1 }) + const b = { b: 2 } + const c = { c: 3 } + const keys = Object.keys(getDefaults(b, c)) + + expect(keys).toHaveLength(3) + }) + + test('should deep merge objects', (): void => { + const getDefaults = defaults({ a: 1, b: { c: 2 } }) + const b = { b: { c: 3 } } + + expect(getDefaults(b).b.c).toBe(3) + }) + + test('should throw TypeError if follow argument is invalid', (): void => { + const invalidObj: unknown = 'testing' + + expect((): OObject => defaults(invalidObj as OObject)) + .toThrow(new TypeError('Expected Object, got string testing')) + }) +}) diff --git a/src/__tests__/shallowDefaults.spec.ts b/src/__tests__/shallowDefaults.spec.ts new file mode 100644 index 00000000..a8fe1519 --- /dev/null +++ b/src/__tests__/shallowDefaults.spec.ts @@ -0,0 +1,30 @@ +import shallowDefaults from '../shallowDefaults' +import { OObject } from '../types' + +describe('defaults', (): void => { + test('should merge all objects', (): void => { + const getDefaults = shallowDefaults({ a: 1 }) + const b = { b: 2 } + const c = { c: 3 } + const keys = Object.keys(getDefaults(b, c)) + + expect(keys).toHaveLength(3) + }) + + test('should shallow merge objects', (): void => { + const getDefaults = shallowDefaults({ a: 1, b: { c: 2, d: 3 } }) + const b = { a: 2, b: { c: 3 } } + const result = getDefaults(b) + + expect(result.a).toBe(2) + expect(result.b.c).toBe(3) + expect(result.b.d).toBeUndefined() + }) + + test('should throw TypeError if follow argument is invalid', (): void => { + const invalidObj: unknown = 'testing' + + expect((): OObject => shallowDefaults(invalidObj as OObject)) + .toThrow(new TypeError('Expected Object, got string testing')) + }) +})