Skip to content

Commit

Permalink
test(defaults): add defaults and shallowDefaults
Browse files Browse the repository at this point in the history
Add tests for new defaults and shallowDefaults methods
  • Loading branch information
Sean Hamilton committed Nov 20, 2019
1 parent 6e27a72 commit 93f77b4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/__tests__/defaults.spec.ts
Original file line number Diff line number Diff line change
@@ -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'))
})
})
30 changes: 30 additions & 0 deletions src/__tests__/shallowDefaults.spec.ts
Original file line number Diff line number Diff line change
@@ -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'))
})
})

0 comments on commit 93f77b4

Please # to comment.