Skip to content

Commit

Permalink
test: upgrade and fix test code
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Jan 23, 2021
1 parent 0933e04 commit fb69b46
Show file tree
Hide file tree
Showing 6 changed files with 473 additions and 483 deletions.
53 changes: 27 additions & 26 deletions test/custom-array-merge.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import type { Options } from "deepmerge"
import { deepmerge as merge } from "deepmerge"
import test from "tape"

test('custom merge array', function(t) {
var mergeFunctionCalled = false
function overwriteMerge(target, source, options) {
test(`custom merge array`, (t) => {
let mergeFunctionCalled = false

const overwriteMerge: Options[`arrayMerge`] = (target, source, options) => {
mergeFunctionCalled = true
t.equal(options.arrayMerge, overwriteMerge)

return source
}

const destination = {
someArray: [ 1, 2 ],
someObject: { what: 'yes' },
someObject: { what: `yes` },
}
const source = {
someArray: [ 1, 2, 3 ],
Expand All @@ -20,46 +23,44 @@ test('custom merge array', function(t) {
const actual = merge(destination, source, { arrayMerge: overwriteMerge })
const expected = {
someArray: [ 1, 2, 3 ],
someObject: { what: 'yes' },
someObject: { what: `yes` },
}

t.ok(mergeFunctionCalled)
t.deepEqual(actual, expected)
t.end()
})

test('merge top-level arrays', function(t) {
function overwriteMerge(a, b) {
return b
}
var actual = merge([ 1, 2 ], [ 1, 2 ], { arrayMerge: overwriteMerge })
var expected = [ 1, 2 ]
test(`merge top-level arrays`, (t) => {
const overwriteMerge: Options[`arrayMerge`] = (a, b) => b

const actual = merge([ 1, 2 ], [ 1, 2 ], { arrayMerge: overwriteMerge })
const expected = [ 1, 2 ]

t.deepEqual(actual, expected)
t.end()
})

test('cloner function is available for merge functions to use', function(t) {
var customMergeWasCalled = false
function cloneMerge(target, source, options) {
test(`cloner function is available for merge functions to use`, (t) => {
let customMergeWasCalled = false

const cloneMerge: Options[`arrayMerge`] = (target, source, options) => {
customMergeWasCalled = true
t.ok(options.cloneUnlessOtherwiseSpecified, 'cloner function is available')
return target.concat(source).map(function(element) {
return options.cloneUnlessOtherwiseSpecified(element, options)
})
t.ok(options.cloneUnlessOtherwiseSpecified, `cloner function is available`)
return [ ...target, ...source ].map((element) => options.cloneUnlessOtherwiseSpecified(element, options))
}

var src = {
key1: [ 'one', 'three' ],
key2: [ 'four' ],
const src = {
key1: [ `one`, `three` ],
key2: [ `four` ],
}
var target = {
key1: [ 'one', 'two' ],
const target = {
key1: [ `one`, `two` ],
}

var expected = {
key1: [ 'one', 'two', 'one', 'three' ],
key2: [ 'four' ],
const expected = {
key1: [ `one`, `two`, `one`, `three` ],
key2: [ `four` ],
}

t.deepEqual(merge(target, src, { arrayMerge: cloneMerge }), expected)
Expand Down
39 changes: 19 additions & 20 deletions test/custom-is-mergeable-object.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
import type { Options } from "deepmerge"
import { deepmerge as merge } from "deepmerge"
import test from "tape"

test('isMergeable function copying object over object', function(t) {
var src = { key: { isMergeable: false }, baz: 'yes' }
var target = { key: { foo: 'wat' }, baz: 'whatever' }
test(`isMergeable function copying object over object`, (t) => {
const src = { key: { isMergeable: false }, baz: `yes` }
const target = { key: { foo: `wat` }, baz: `whatever` }

function customIsMergeable(object) {
return object && typeof object === 'object' && object.isMergeable !== false
}
const customIsMergeable: Options[`isMergeable`] = (object) =>
object && typeof object === `object` && object.isMergeable !== false

var res = merge(target, src, {
isMergeable: customIsMergeable
const res = merge(target, src, {
isMergeable: customIsMergeable,
})

t.deepEqual(res, { key: { isMergeable: false }, baz: 'yes' })
t.equal(res.key, src.key, 'Object has the same identity and was not cloned')
t.deepEqual(res, { key: { isMergeable: false }, baz: `yes` })
t.equal(res.key, src.key, `Object has the same identity and was not cloned`)
t.end()
})

test('isMergeable function copying object over nothing', function(t) {
var src = { key: { isMergeable: false, foo: 'bar' }, baz: 'yes' }
var target = { baz: 'whatever' }
test(`isMergeable function copying object over nothing`, (t) => {
const src = { key: { isMergeable: false, foo: `bar` }, baz: `yes` }
const target = { baz: `whatever` }

function customIsMergeable(object) {
return object && typeof object === 'object' && object.isMergeable !== false
}
const customIsMergeable: Options[`isMergeable`] = (object) =>
object && typeof object === `object` && object.isMergeable !== false

var res = merge(target, src, {
isMergeable: customIsMergeable
const res = merge(target, src, {
isMergeable: customIsMergeable,
})

t.deepEqual(res, { key: { isMergeable: false, foo: 'bar' }, baz: 'yes' })
t.equal(res.key, src.key, 'Object has the same identity and was not cloned')
t.deepEqual(res, { key: { isMergeable: false, foo: `bar` }, baz: `yes` })
t.equal(res.key, src.key, `Object has the same identity and was not cloned`)
t.end()
})
72 changes: 36 additions & 36 deletions test/merge-all.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
import { deepmergeAll as mergeAll } from "deepmerge"
import test from "tape"

test('throw error if first argument is not an array', function(t) {
// @ts-expect-error
t.throws(mergeAll.bind(null, { example: true }, { another: '2' }), Error)
test(`throw error if first argument is not an array`, (t) => {
// @ts-expect-error -- Calling mergeAll without passing in an array.
t.throws(mergeAll.bind(null, { example: true }, { another: `2` }), Error)
t.end()
})

test('return an empty object if first argument is an array with no elements', function(t) {
test(`return an empty object if first argument is an array with no elements`, (t) => {
t.deepEqual(mergeAll([]), {})
t.end()
})

test('Work just fine if first argument is an array with least than two elements', function(t) {
var actual = mergeAll([{ example: true }])
var expected = { example: true }
test(`Work just fine if first argument is an array with least than two elements`, (t) => {
const actual = mergeAll([{ example: true }])
const expected = { example: true }
t.deepEqual(actual, expected)
t.end()
})

test('execute correctly if options object were not passed', function(t) {
var arrayToMerge = [{ example: true }, { another: '123' }]
test(`execute correctly if options object were not passed`, (t) => {
const arrayToMerge = [{ example: true }, { another: `123` }]
t.doesNotThrow(mergeAll.bind(null, arrayToMerge))
t.end()
})

test('execute correctly if options object were passed', function(t) {
var arrayToMerge = [{ example: true }, { another: '123' }]
test(`execute correctly if options object were passed`, (t) => {
const arrayToMerge = [{ example: true }, { another: `123` }]
t.doesNotThrow(mergeAll.bind(null, arrayToMerge, { clone: true }))
t.end()
})

test('invoke merge on every item in array should result with all props', function(t) {
var firstObject = { first: true }
var secondObject = { second: false }
var thirdObject = { third: 123 }
var fourthObject = { fourth: 'some string' }
test(`invoke merge on every item in array should result with all props`, (t) => {
const firstObject = { first: true }
const secondObject = { second: false }
const thirdObject = { third: 123 }
const fourthObject = { fourth: `some string` }

var mergedObject = mergeAll([ firstObject, secondObject, thirdObject, fourthObject ])
const mergedObject = mergeAll([ firstObject, secondObject, thirdObject, fourthObject ])

t.ok(mergedObject.first === true)
t.ok(mergedObject.second === false)
t.ok(mergedObject.third === 123)
t.ok(mergedObject.fourth === 'some string')
t.ok(mergedObject.fourth === `some string`)
t.end()
})

test('invoke merge on every item in array with clone should clone all elements', function(t) {
var firstObject = { a: { d: 123 } }
var secondObject = { b: { e: true } }
var thirdObject = { c: { f: 'string' } }
test(`invoke merge on every item in array with clone should clone all elements`, (t) => {
const firstObject = { a: { d: 123 } }
const secondObject = { b: { e: true } }
const thirdObject = { c: { f: `string` } }

var mergedWithClone = mergeAll([ firstObject, secondObject, thirdObject ], { clone: true })
const mergedWithClone = mergeAll([ firstObject, secondObject, thirdObject ], { clone: true })

t.notEqual(mergedWithClone.a, firstObject.a)
t.notEqual(mergedWithClone.b, secondObject.b)
Expand All @@ -60,12 +60,12 @@ test('invoke merge on every item in array with clone should clone all elements',
t.end()
})

test('invoke merge on every item in array clone=false should not clone all elements', function(t) {
var firstObject = { a: { d: 123 } }
var secondObject = { b: { e: true } }
var thirdObject = { c: { f: 'string' } }
test(`invoke merge on every item in array clone=false should not clone all elements`, (t) => {
const firstObject = { a: { d: 123 } }
const secondObject = { b: { e: true } }
const thirdObject = { c: { f: `string` } }

var mergedWithoutClone = mergeAll([ firstObject, secondObject, thirdObject ], { clone: false })
const mergedWithoutClone = mergeAll([ firstObject, secondObject, thirdObject ], { clone: false })

t.equal(mergedWithoutClone.a, firstObject.a)
t.equal(mergedWithoutClone.b, secondObject.b)
Expand All @@ -75,12 +75,12 @@ test('invoke merge on every item in array clone=false should not clone all eleme
})


test('invoke merge on every item in array without clone should clone all elements', function(t) {
var firstObject = { a: { d: 123 } }
var secondObject = { b: { e: true } }
var thirdObject = { c: { f: 'string' } }
test(`invoke merge on every item in array without clone should clone all elements`, (t) => {
const firstObject = { a: { d: 123 } }
const secondObject = { b: { e: true } }
const thirdObject = { c: { f: `string` } }

var mergedWithoutClone = mergeAll([ firstObject, secondObject, thirdObject ])
const mergedWithoutClone = mergeAll([ firstObject, secondObject, thirdObject ])

t.notEqual(mergedWithoutClone.a, firstObject.a)
t.notEqual(mergedWithoutClone.b, secondObject.b)
Expand All @@ -89,12 +89,12 @@ test('invoke merge on every item in array without clone should clone all element
t.end()
})

test('With clone: false, mergeAll should not clone the target root', t => {
test(`With clone: false, mergeAll should not clone the target root`, (t) => {
const destination = {}
const output = mergeAll([
destination, {
sup: true
}
sup: true,
},
], { clone: false })

t.equal(destination, output)
Expand Down
8 changes: 4 additions & 4 deletions test/merge-plain-objects.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { deepmerge as merge } from "deepmerge"
import test from "tape"

test('plain objects are merged by default', function(t) {
test(`plain objects are merged by default`, (t) => {
const input = {
newObject: new Object(),
objectLiteral: { a: 123 }
objectLiteral: { a: 123 },
}
const output = merge({}, input)

Expand All @@ -16,11 +16,11 @@ test('plain objects are merged by default', function(t) {
t.end()
})

test('instantiated objects are copied by reference', function(t) {
test(`instantiated objects are copied by reference`, (t) => {
const input = {
date: new Date(),
error: new Error(),
regex: /regex/
regex: /regex/,
}
const output = merge({}, input)

Expand Down
Loading

0 comments on commit fb69b46

Please sign in to comment.