Skip to content

Commit

Permalink
test: add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Aug 29, 2021
1 parent f9844c0 commit b92b99d
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion tests/deepmerge-custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,58 @@ test("custom don't merge arrays", (t) => {
DeepMergeArraysURI: "DeepMergeLeafURI";
}>({
mergeArrays: false,
} as const);
});

const merged = customizedDeepmerge(v, x, y, z);

t.deepEqual(merged, expected);
});

declare module "../src/types" {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface DeepMergeMergeFunctionURItoKind<
T1,
T2,
MF extends DeepMergeMergeFunctionsURIs
> {
readonly MergeDates1: T1 extends Date
? T2 extends Date
? [T1, T2]
: T2 extends Readonly<ReadonlyArray<Date>>
? [T1, ...T2]
: T2
: T1 extends Readonly<ReadonlyArray<Date>>
? [...T1, T2]
: T2;
}
}

test("custom merge dates", (t) => {
const x = { foo: new Date("2020-01-01") };
const y = { foo: new Date("2021-02-02") };
const z = { foo: new Date("2022-03-03") };

const expected = { foo: [x.foo, y.foo, z.foo] } as const;

const customizedDeepmerge = deepmergeCustom<{
DeepMergeOthersURI: "MergeDates1";
}>({
mergeOthers: (val1, val2) => {
if (val1 instanceof Date && val2 instanceof Date) {
return [val1, val2];
}
if (
Array.isArray(val1) &&
val1.every((val) => val instanceof Date) &&
val2 instanceof Date
) {
return [...val1, val2];
}
return val2;
},
});

const merged = customizedDeepmerge(x, y, z);

t.deepEqual(merged, expected);
});

0 comments on commit b92b99d

Please sign in to comment.