Skip to content

Commit

Permalink
fix: progress on new syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
ffMathy committed Mar 10, 2024
1 parent 374196d commit 2e93eef
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 55 deletions.
42 changes: 3 additions & 39 deletions spec/ClearSubstitute.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'

import { Substitute, SubstituteOf, clearSubstitute, received } from '../src'
import { Substitute, SubstituteOf, clearReceivedCalls, received } from '../src'
import { SubstituteNode } from '../src/SubstituteNode'

interface Calculator {
Expand All @@ -14,51 +14,15 @@ type InstanceReturningSubstitute<T> = SubstituteOf<T> & {
[SubstituteNode.instance]: SubstituteNode
}

test('clears everything on a substitute', t => {
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
calculator.add(1, 1)
calculator[received]().add(1, 1)
calculator[clearSubstitute]()

t.is(calculator[SubstituteNode.instance].recorder.records.size, 0)
t.is(calculator[SubstituteNode.instance].recorder.indexedRecords.size, 0)

t.throws(() => calculator[received]().add(1, 1))

// explicitly using 'all'
calculator.add(1, 1)
calculator.received().add(1, 1)
calculator.clearSubstitute('all')

t.is(calculator[SubstituteNode.instance].recorder.records.size, 0)
t.is(calculator[SubstituteNode.instance].recorder.indexedRecords.size, 0)

t.throws(() => calculator.received().add(1, 1))
})

test('clears received calls on a substitute', t => {
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
calculator.add(1, 1)
calculator.add(1, 1).returns(2)
calculator.clearSubstitute('receivedCalls')
calculator[clearReceivedCalls]();

t.is(calculator[SubstituteNode.instance].recorder.records.size, 2)
t.is(calculator[SubstituteNode.instance].recorder.indexedRecords.size, 2)

t.throws(() => calculator.received().add(1, 1))
t.throws(() => calculator[received]().add(1, 1))
t.is(2, calculator.add(1, 1))
})

test('clears return values on a substitute', t => {
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
calculator.add(1, 1)
calculator.add(1, 1).returns(2)
calculator.clearSubstitute('substituteValues')

t.is(calculator[SubstituteNode.instance].recorder.records.size, 2)
t.is(calculator[SubstituteNode.instance].recorder.indexedRecords.size, 2)

t.notThrows(() => calculator.received().add(1, 1))
// @ts-expect-error
t.true(calculator.add(1, 1)[SubstituteNode.instance] instanceof SubstituteNode)
})
17 changes: 9 additions & 8 deletions src/SubstituteNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ClearType as ClearTypeMap, PropertyType as PropertyTypeMap, isAssertion
import { SubstituteException } from './SubstituteException'
import type { FilterFunction, SubstituteContext, SubstitutionMethod, ClearType, PropertyType } from './Types'

Check failure on line 7 in src/SubstituteNode.ts

View workflow job for this annotation

GitHub Actions / build (16)

Module '"./Types"' has no exported member 'ClearType'.

Check failure on line 7 in src/SubstituteNode.ts

View workflow job for this annotation

GitHub Actions / build (18)

Module '"./Types"' has no exported member 'ClearType'.

Check failure on line 7 in src/SubstituteNode.ts

View workflow job for this annotation

GitHub Actions / build (20)

Module '"./Types"' has no exported member 'ClearType'.
import type { ObjectSubstitute } from './Transformations'
import { didNotReceive, mimick, mimicks, received, rejects, resolves, returns, throws } from './Symbols'

const instance = Symbol('Substitute:Instance')
const clearTypeToFilterMap: Record<ClearType, FilterFunction<SubstituteNode>> = {
Expand Down Expand Up @@ -114,17 +115,17 @@ export class SubstituteNode extends SubstituteNodeBase implements ObjectSubstitu
return this._recordedArguments
}

public received(amount?: number): SubstituteNode {
public [received](amount?: number): SubstituteNode {
this.handleMethod([amount])
return this.proxy
}

public didNotReceive(): SubstituteNode {
public [didNotReceive](): SubstituteNode {
this.handleMethod([0])
return this.proxy
}

public mimick() {
public [mimick]() {
throw new Error('Mimick is not implemented yet')
}

Expand Down Expand Up @@ -165,17 +166,17 @@ export class SubstituteNode extends SubstituteNodeBase implements ObjectSubstitu
? this.child.recordedArguments.value?.shift()
: this.child.recordedArguments.value[0]
switch (substitutionMethod) {
case 'throws':
case throws:
throw substitutionValue
case 'mimicks':
case mimicks:
if (this.propertyType === PropertyTypeMap.Property) return substitutionValue()
if (!contextArguments.hasArguments()) throw new TypeError('Context arguments cannot be undefined')
return substitutionValue(...contextArguments.value)
case 'resolves':
case resolves:
return Promise.resolve(substitutionValue)
case 'rejects':
case rejects:
return Promise.reject(substitutionValue)
case 'returns':
case returns:
return substitutionValue
default:
throw SubstituteException.generic(`Substitution method '${substitutionMethod}' not implemented`)
Expand Down
2 changes: 1 addition & 1 deletion src/Symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export const received = Symbol('received');
export const didNotReceive = Symbol('didNotReceive');
export const mimick = Symbol('mimick');
export const clearSubstitute = Symbol('clearSubstitute');
export const clearReceivedCalls = Symbol('clearReceivedCalls');

export const mimicks = Symbol('mimicks');
export const throws = Symbol('throws');
Expand Down
4 changes: 2 additions & 2 deletions src/Transformations.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AllArguments } from './Arguments';
import { clearSubstitute, didNotReceive, mimick, received } from './Symbols';
import { clearReceivedCalls, didNotReceive, mimick, received } from './Symbols';
import type { ClearType, FirstLevelMethod } from './Types';

Check failure on line 3 in src/Transformations.ts

View workflow job for this annotation

GitHub Actions / build (16)

Module '"./Types"' has no exported member 'ClearType'.

Check failure on line 3 in src/Transformations.ts

View workflow job for this annotation

GitHub Actions / build (18)

Module '"./Types"' has no exported member 'ClearType'.

Check failure on line 3 in src/Transformations.ts

View workflow job for this annotation

GitHub Actions / build (20)

Module '"./Types"' has no exported member 'ClearType'.

type FunctionSubstituteWithOverloads<TFunc, Terminating = false> =
Expand Down Expand Up @@ -96,6 +96,6 @@ export type ObjectSubstitute<T> = ObjectSubstituteTransformation<T> & {
[received](amount?: number): TerminatingObject<T>;
[didNotReceive](): TerminatingObject<T>;
[mimick](instance: OmitProxyMethods<T>): void;
[clearSubstitute](clearType?: ClearType): void;
[clearReceivedCalls](clearType?: ClearType): void;
}
export type DisabledSubstituteObject<T> = T extends ObjectSubstitute<infer K> ? K : never;
6 changes: 2 additions & 4 deletions src/Types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import type { clearSubstitute, didNotReceive, mimick, mimicks, received, rejects, resolves, returns, throws } from "./Symbols"
import type { clearReceivedCalls, didNotReceive, mimick, mimicks, received, rejects, resolves, returns, throws } from "./Symbols"

export type PropertyType = 'method' | 'property'
export type AssertionMethod = typeof received | typeof didNotReceive
export type ConfigurationMethod = typeof clearSubstitute | typeof mimick
export type ConfigurationMethod = typeof clearReceivedCalls | typeof mimick
export type SubstitutionMethod = typeof mimicks | typeof throws | typeof returns | typeof resolves | typeof 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
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Substitute, SubstituteOf } from './Substitute'
export { Arg } from './Arguments'
export { Substitute, SubstituteOf }
export { ClearType } from './Utilities'
export { clearSubstitute, didNotReceive, mimick, received } from './Symbols'
export { clearReceivedCalls, didNotReceive, mimick, received } from './Symbols'

export default Substitute

0 comments on commit 2e93eef

Please sign in to comment.