Skip to content

Commit

Permalink
fix(createMock): PartialDeep to ignore object prototype properties
Browse files Browse the repository at this point in the history
* fix(createMock): PartialDeep to ignore object prototype properties

* fix(eslint): restore new-lines in eslint

* fix(partialDeep): suppress object prototype overrides when using partial deep of object

* fix(partialDeep): restore type conditions order

* fix(partialDeep): last fix of the day

Co-authored-by: Vittorio Guerriero <vittorio.gue@gmail.com>
  • Loading branch information
Pmyl and uittorio committed Aug 29, 2020
1 parent 5980fee commit 37cb34d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 3 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@
"use-isnan": "error",
"valid-typeof": "off",
"brace-style": "error",
"prettier/prettier": "error"
"prettier/prettier": ["error", {
"endOfLine":"auto"
}]
}
}
14 changes: 11 additions & 3 deletions src/partial/partial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export type PartialDeep<T> = T extends Primitive
: T extends ReadonlyMap<infer KeyType, infer ValueType>
? PartialReadonlyMapDeep<KeyType, ValueType>
: T extends ReadonlySet<infer ItemType>
? PartialReadonlySetDeep<ItemType> // eslint-disable-next-line @typescript-eslint/no-explicit-any
: T extends (...args: any[]) => unknown
? PartialReadonlySetDeep<ItemType>
: T extends (...args: any[]) => unknown // eslint-disable-line @typescript-eslint/no-explicit-any
? T | undefined
: T extends object
? PartialObjectDeep<T>
Expand Down Expand Up @@ -42,5 +42,13 @@ interface PartialReadonlySetDeep<T> extends ReadonlySet<PartialDeep<T>> {}
Same as `PartialDeep`, but accepts only `object`s as inputs. Internal helper for `PartialDeep`.
*/
type PartialObjectDeep<ObjectType extends object> = {
[KeyType in keyof ObjectType]?: PartialDeep<ObjectType[KeyType]>;
[KeyType in keyof SuppressObjectPrototypeOverrides<ObjectType>]?: PartialDeep<
SuppressObjectPrototypeOverrides<ObjectType>[KeyType]
>;
};

type SuppressObjectPrototypeOverrides<ObjectType> = Pick<
ObjectType,
Exclude<keyof ObjectType, keyof Object>
> &
Pick<Object, Extract<keyof Object, keyof ObjectType>>;
17 changes: 17 additions & 0 deletions test/frameworkContext/create-mock-values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,21 @@ describe('create-mock-values', () => {
expect(properties.property.a).toBe(2);
expect(properties.property.c).toHaveBeenCalled();
});

it('should not clash with object prototype properties', () => {
interface OverridePrototype {
valueOf(): number;
props: Interface;
}

createMock<OverridePrototype>({
props: {
property: {
a: 2,
},
},
});

expect().nothing();
});
});

0 comments on commit 37cb34d

Please sign in to comment.