Skip to content

Commit

Permalink
fix(merge): ensure undefined is assigned when merging partial mock (#748
Browse files Browse the repository at this point in the history
)

Co-authored-by: Vittorio Guerriero <[email protected]>
  • Loading branch information
Pmyl and uittorio authored May 22, 2021
1 parent 66de8a0 commit 31c5ff5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/merge/merge.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import { mergeWith, isObject } from 'lodash-es';
import { mergeWith, isObject, set } from 'lodash-es';
import { ɵMarker as Marker } from 'ts-auto-mock/extension';
import { PartialDeep } from '../partial/partial';

export class Merge {
public static merge<T>(a: T, b: PartialDeep<T>): T {
return mergeWith(a, b, (objValue: unknown, srcValue: unknown) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (isObject(srcValue) && srcValue[Marker.instance.get()]) {
return srcValue;
return mergeWith(
a,
b,
(
objValue: unknown,
srcValue: unknown,
oneLevelPath: string,
objContainer: object,
srcContainer: object
) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (isObject(srcValue) && srcValue[Marker.instance.get()]) {
return srcValue;
}

if (
srcContainer.hasOwnProperty(oneLevelPath) &&
srcValue === undefined
) {
set(objContainer, [oneLevelPath], undefined);
}
}
});
);
}

public static mergeIterator<T>(
Expand Down
22 changes: 22 additions & 0 deletions test/frameworkContext/create-mock-values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,26 @@ describe('create-mock-values', () => {

expect().nothing();
});

it('should replace values when providing undefined', () => {
interface InterfaceWithValues {
property: {
a: 123;
c: () => void;
};
method(): void;
}

const properties: InterfaceWithValues = createMock<InterfaceWithValues>({
property: {
a: undefined,
},
});

properties.method();
properties.property.c();
expect(properties.method).toHaveBeenCalled();
expect(properties.property.a).toBeUndefined();
expect(properties.property.c).toHaveBeenCalled();
});
});

0 comments on commit 31c5ff5

Please sign in to comment.