Skip to content

Commit

Permalink
refactor core types
Browse files Browse the repository at this point in the history
  • Loading branch information
notanengineercom committed Oct 7, 2022
1 parent 6e3011a commit 8514d14
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 22 deletions.
25 changes: 11 additions & 14 deletions src/Transformations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AllArguments } from './Arguments';
import type { AllArguments } from './Arguments';
import type { ClearType, FirstLevelMethod } from './Types';

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

export type ObjectSubstitute<T extends Object, K extends Object = T> = ObjectSubstituteTransformation<T> & {
received(amount?: number): TerminatingObject<K>;
didNotReceive(): TerminatingObject<K>;
mimick(instance: T): void;
clearSubstitute(clearType?: ClearType): void;
}

type TerminatingFunction<TArguments extends any[]> = ((...args: TArguments) => void) & ((arg: AllArguments<TArguments>) => void)

type TerminatingObject<T> = {
Expand All @@ -83,16 +77,19 @@ type TerminatingObject<T> = {
T[P];
}

type ObjectSubstituteTransformation<T extends Object> = {
type ObjectSubstituteTransformation<K, T = OmitProxyMethods<K>> = {
[P in keyof T]:
T[P] extends (...args: infer F) => infer R ?
F extends [] ? NoArgumentFunctionSubstitute<R> :
FunctionSubstituteWithOverloads<T[P]> :
PropertySubstitute<T[P]>;
}

type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;

export type ClearType = 'all' | 'receivedCalls' | 'substituteValues';
export type OmitProxyMethods<T extends any> = Omit<T, 'mimick' | 'received' | 'didNotReceive' | 'clearSubstitute'>;
export type DisabledSubstituteObject<T> = T extends ObjectSubstitute<OmitProxyMethods<infer K>, infer K> ? K : never;
export type OmitProxyMethods<T> = Omit<T, FirstLevelMethod>;
export type ObjectSubstitute<T> = ObjectSubstituteTransformation<T> & {
received(amount?: number): TerminatingObject<T>;
didNotReceive(): TerminatingObject<T>;
mimick(instance: OmitProxyMethods<T>): void;
clearSubstitute(clearType?: ClearType): void;
}
export type DisabledSubstituteObject<T> = T extends ObjectSubstitute<infer K> ? K : never;
12 changes: 12 additions & 0 deletions src/Types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type PropertyType = 'method' | 'property'
export type AssertionMethod = 'received' | 'didNotReceive'
export type ConfigurationMethod = 'clearSubstitute' | 'mimick'
export type SubstitutionMethod = 'mimicks' | 'throws' | 'returns' | 'resolves' | 'rejects'

export type FirstLevelMethod = AssertionMethod | ConfigurationMethod
export type SubstituteMethod = FirstLevelMethod | SubstitutionMethod
export type SubstituteContext = SubstituteMethod | 'none'

export type ClearType = 'all' | 'receivedCalls' | 'substituteValues'

export type FilterFunction<T> = (item: T) => boolean
20 changes: 12 additions & 8 deletions src/Utilities.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { inspect } from 'util'
import { RecordedArguments } from './RecordedArguments'
import type { AssertionMethod, ConfigurationMethod, SubstituteMethod, SubstitutionMethod } from './Types'

export enum PropertyType {
method = 'method',
property = 'property'
}
export const PropertyType = {
Method: 'method',
Property: 'property'
} as const

export type AssertionMethod = 'received' | 'didNotReceive'
export const isAssertionMethod = (property: PropertyKey): property is AssertionMethod =>
property === 'received' || property === 'didNotReceive'

export type ConfigurationMethod = 'clearSubstitute'
export const isConfigurationMethod = (property: PropertyKey): property is ConfigurationMethod => property === 'clearSubstitute'

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

export const isSubstituteMethod = (property: PropertyKey): property is SubstitutionMethod | ConfigurationMethod | AssertionMethod =>
export const isSubstituteMethod = (property: PropertyKey): property is SubstituteMethod =>
isSubstitutionMethod(property) || isConfigurationMethod(property) || isAssertionMethod(property)

export const ClearType = {
All: 'all',
ReceivedCalls: 'receivedCalls',
SubstituteValues: 'substituteValues'
} as const

export const stringifyArguments = (args: RecordedArguments) => textModifier.faint(
args.hasNoArguments
? 'no arguments'
Expand Down

0 comments on commit 8514d14

Please sign in to comment.