diff --git a/test/mergeAndCompare.test.ts b/test/mergeAndCompare.test.ts index 5e5e6f5..e5e47e0 100644 --- a/test/mergeAndCompare.test.ts +++ b/test/mergeAndCompare.test.ts @@ -1,5 +1,5 @@ import { test, expect } from 'vitest' -import { isDate, isString, isArray, isObject } from 'is-what' +import { isDate, isString, isArray } from 'is-what' import { mergeAndCompare } from '../src/index' test('conversion based on original val', () => { @@ -60,3 +60,57 @@ test('Extend with custom concat arrays', () => { // const res2 = mergeAndCompare(concatArr, ['a'], ['b']) // expect(res2).toEqual( ['a', 'b']) }) + +test('undefined object', () => { + const origin = { + pages: { + 'aa': 'ttt', + }, + } + + const newData = { + pages: { + 'aa': '1111', + 'bb': '2222', + } + } + + function convertTimestamps(originVal: any, targetVal: any, key: any) { + if (originVal !== undefined) + return targetVal + } + + const res = mergeAndCompare(convertTimestamps, origin, newData) + expect(res as any).toEqual({ pages: { aa: "1111", bb: undefined } }) +}) + +test('undefined array', () => { + function convertTimestamps(originVal: any, targetVal: any, key: any) { + if (originVal !== undefined) + return targetVal + } + const origin = { + date: [ + { + 'new': 'aaa', + } + ] + } + const target = { + date: [ + { + 'new': 'aaa', + 'old': 'bbb', + } + ] + } + const res = mergeAndCompare(convertTimestamps, origin, target) + expect(res as any).toEqual({ + date: [ + { + 'new': 'aaa', + 'old': undefined, + } + ] + }) +}) \ No newline at end of file