Skip to content

Commit 8514d14

Browse files
refactor core types
1 parent 6e3011a commit 8514d14

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

src/Transformations.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { AllArguments } from './Arguments';
1+
import type { AllArguments } from './Arguments';
2+
import type { ClearType, FirstLevelMethod } from './Types';
23

34
type FunctionSubstituteWithOverloads<TFunc, Terminating = false> =
45
TFunc extends {
@@ -66,13 +67,6 @@ type MockObjectMixin<TArguments extends any[], TReturnType> = BaseMockObjectMixi
6667
mimicks: OneArgumentRequiredFunction<(...args: TArguments) => TReturnType, void>;
6768
}
6869

69-
export type ObjectSubstitute<T extends Object, K extends Object = T> = ObjectSubstituteTransformation<T> & {
70-
received(amount?: number): TerminatingObject<K>;
71-
didNotReceive(): TerminatingObject<K>;
72-
mimick(instance: T): void;
73-
clearSubstitute(clearType?: ClearType): void;
74-
}
75-
7670
type TerminatingFunction<TArguments extends any[]> = ((...args: TArguments) => void) & ((arg: AllArguments<TArguments>) => void)
7771

7872
type TerminatingObject<T> = {
@@ -83,16 +77,19 @@ type TerminatingObject<T> = {
8377
T[P];
8478
}
8579

86-
type ObjectSubstituteTransformation<T extends Object> = {
80+
type ObjectSubstituteTransformation<K, T = OmitProxyMethods<K>> = {
8781
[P in keyof T]:
8882
T[P] extends (...args: infer F) => infer R ?
8983
F extends [] ? NoArgumentFunctionSubstitute<R> :
9084
FunctionSubstituteWithOverloads<T[P]> :
9185
PropertySubstitute<T[P]>;
9286
}
9387

94-
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
95-
96-
export type ClearType = 'all' | 'receivedCalls' | 'substituteValues';
97-
export type OmitProxyMethods<T extends any> = Omit<T, 'mimick' | 'received' | 'didNotReceive' | 'clearSubstitute'>;
98-
export type DisabledSubstituteObject<T> = T extends ObjectSubstitute<OmitProxyMethods<infer K>, infer K> ? K : never;
88+
export type OmitProxyMethods<T> = Omit<T, FirstLevelMethod>;
89+
export type ObjectSubstitute<T> = ObjectSubstituteTransformation<T> & {
90+
received(amount?: number): TerminatingObject<T>;
91+
didNotReceive(): TerminatingObject<T>;
92+
mimick(instance: OmitProxyMethods<T>): void;
93+
clearSubstitute(clearType?: ClearType): void;
94+
}
95+
export type DisabledSubstituteObject<T> = T extends ObjectSubstitute<infer K> ? K : never;

src/Types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export type PropertyType = 'method' | 'property'
2+
export type AssertionMethod = 'received' | 'didNotReceive'
3+
export type ConfigurationMethod = 'clearSubstitute' | 'mimick'
4+
export type SubstitutionMethod = 'mimicks' | 'throws' | 'returns' | 'resolves' | 'rejects'
5+
6+
export type FirstLevelMethod = AssertionMethod | ConfigurationMethod
7+
export type SubstituteMethod = FirstLevelMethod | SubstitutionMethod
8+
export type SubstituteContext = SubstituteMethod | 'none'
9+
10+
export type ClearType = 'all' | 'receivedCalls' | 'substituteValues'
11+
12+
export type FilterFunction<T> = (item: T) => boolean

src/Utilities.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import { inspect } from 'util'
22
import { RecordedArguments } from './RecordedArguments'
3+
import type { AssertionMethod, ConfigurationMethod, SubstituteMethod, SubstitutionMethod } from './Types'
34

4-
export enum PropertyType {
5-
method = 'method',
6-
property = 'property'
7-
}
5+
export const PropertyType = {
6+
Method: 'method',
7+
Property: 'property'
8+
} as const
89

9-
export type AssertionMethod = 'received' | 'didNotReceive'
1010
export const isAssertionMethod = (property: PropertyKey): property is AssertionMethod =>
1111
property === 'received' || property === 'didNotReceive'
1212

13-
export type ConfigurationMethod = 'clearSubstitute'
1413
export const isConfigurationMethod = (property: PropertyKey): property is ConfigurationMethod => property === 'clearSubstitute'
1514

16-
export type SubstitutionMethod = 'mimicks' | 'throws' | 'returns' | 'resolves' | 'rejects'
1715
export const isSubstitutionMethod = (property: PropertyKey): property is SubstitutionMethod =>
1816
property === 'mimicks' || property === 'returns' || property === 'throws' || property === 'resolves' || property === 'rejects'
1917

20-
export const isSubstituteMethod = (property: PropertyKey): property is SubstitutionMethod | ConfigurationMethod | AssertionMethod =>
18+
export const isSubstituteMethod = (property: PropertyKey): property is SubstituteMethod =>
2119
isSubstitutionMethod(property) || isConfigurationMethod(property) || isAssertionMethod(property)
2220

21+
export const ClearType = {
22+
All: 'all',
23+
ReceivedCalls: 'receivedCalls',
24+
SubstituteValues: 'substituteValues'
25+
} as const
26+
2327
export const stringifyArguments = (args: RecordedArguments) => textModifier.faint(
2428
args.hasNoArguments
2529
? 'no arguments'

0 commit comments

Comments
 (0)