diff --git a/spec/ClearSubstitute.spec.ts b/spec/ClearSubstitute.spec.ts index c9a93df..899249f 100644 --- a/spec/ClearSubstitute.spec.ts +++ b/spec/ClearSubstitute.spec.ts @@ -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 { @@ -14,51 +14,15 @@ type InstanceReturningSubstitute = SubstituteOf & { [SubstituteNode.instance]: SubstituteNode } -test('clears everything on a substitute', t => { - const calculator = Substitute.for() as InstanceReturningSubstitute - 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() as InstanceReturningSubstitute 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() as InstanceReturningSubstitute - 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) }) \ No newline at end of file diff --git a/src/SubstituteNode.ts b/src/SubstituteNode.ts index 03119ce..dfb0be5 100644 --- a/src/SubstituteNode.ts +++ b/src/SubstituteNode.ts @@ -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' import type { ObjectSubstitute } from './Transformations' +import { didNotReceive, mimick, mimicks, received, rejects, resolves, returns, throws } from './Symbols' const instance = Symbol('Substitute:Instance') const clearTypeToFilterMap: Record> = { @@ -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') } @@ -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`) diff --git a/src/Symbols.ts b/src/Symbols.ts index 068d3ad..2f2bc0a 100644 --- a/src/Symbols.ts +++ b/src/Symbols.ts @@ -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'); diff --git a/src/Transformations.ts b/src/Transformations.ts index 380fc3b..4432320 100644 --- a/src/Transformations.ts +++ b/src/Transformations.ts @@ -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'; type FunctionSubstituteWithOverloads = @@ -96,6 +96,6 @@ export type ObjectSubstitute = ObjectSubstituteTransformation & { [received](amount?: number): TerminatingObject; [didNotReceive](): TerminatingObject; [mimick](instance: OmitProxyMethods): void; - [clearSubstitute](clearType?: ClearType): void; + [clearReceivedCalls](clearType?: ClearType): void; } export type DisabledSubstituteObject = T extends ObjectSubstitute ? K : never; diff --git a/src/Types.ts b/src/Types.ts index 7ff2368..77dd1ab 100644 --- a/src/Types.ts +++ b/src/Types.ts @@ -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 = (item: T) => boolean \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index bd72dc8..ac40401 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 \ No newline at end of file