Skip to content

Commit 2e93eef

Browse files
committed
fix: progress on new syntax
1 parent 374196d commit 2e93eef

File tree

6 files changed

+18
-55
lines changed

6 files changed

+18
-55
lines changed

spec/ClearSubstitute.spec.ts

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import test from 'ava'
22

3-
import { Substitute, SubstituteOf, clearSubstitute, received } from '../src'
3+
import { Substitute, SubstituteOf, clearReceivedCalls, received } from '../src'
44
import { SubstituteNode } from '../src/SubstituteNode'
55

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

17-
test('clears everything on a substitute', t => {
18-
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
19-
calculator.add(1, 1)
20-
calculator[received]().add(1, 1)
21-
calculator[clearSubstitute]()
22-
23-
t.is(calculator[SubstituteNode.instance].recorder.records.size, 0)
24-
t.is(calculator[SubstituteNode.instance].recorder.indexedRecords.size, 0)
25-
26-
t.throws(() => calculator[received]().add(1, 1))
27-
28-
// explicitly using 'all'
29-
calculator.add(1, 1)
30-
calculator.received().add(1, 1)
31-
calculator.clearSubstitute('all')
32-
33-
t.is(calculator[SubstituteNode.instance].recorder.records.size, 0)
34-
t.is(calculator[SubstituteNode.instance].recorder.indexedRecords.size, 0)
35-
36-
t.throws(() => calculator.received().add(1, 1))
37-
})
38-
3917
test('clears received calls on a substitute', t => {
4018
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
4119
calculator.add(1, 1)
4220
calculator.add(1, 1).returns(2)
43-
calculator.clearSubstitute('receivedCalls')
21+
calculator[clearReceivedCalls]();
4422

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

48-
t.throws(() => calculator.received().add(1, 1))
26+
t.throws(() => calculator[received]().add(1, 1))
4927
t.is(2, calculator.add(1, 1))
50-
})
51-
52-
test('clears return values on a substitute', t => {
53-
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
54-
calculator.add(1, 1)
55-
calculator.add(1, 1).returns(2)
56-
calculator.clearSubstitute('substituteValues')
57-
58-
t.is(calculator[SubstituteNode.instance].recorder.records.size, 2)
59-
t.is(calculator[SubstituteNode.instance].recorder.indexedRecords.size, 2)
60-
61-
t.notThrows(() => calculator.received().add(1, 1))
62-
// @ts-expect-error
63-
t.true(calculator.add(1, 1)[SubstituteNode.instance] instanceof SubstituteNode)
6428
})

src/SubstituteNode.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ClearType as ClearTypeMap, PropertyType as PropertyTypeMap, isAssertion
66
import { SubstituteException } from './SubstituteException'
77
import type { FilterFunction, SubstituteContext, SubstitutionMethod, ClearType, PropertyType } from './Types'
88
import type { ObjectSubstitute } from './Transformations'
9+
import { didNotReceive, mimick, mimicks, received, rejects, resolves, returns, throws } from './Symbols'
910

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

117-
public received(amount?: number): SubstituteNode {
118+
public [received](amount?: number): SubstituteNode {
118119
this.handleMethod([amount])
119120
return this.proxy
120121
}
121122

122-
public didNotReceive(): SubstituteNode {
123+
public [didNotReceive](): SubstituteNode {
123124
this.handleMethod([0])
124125
return this.proxy
125126
}
126127

127-
public mimick() {
128+
public [mimick]() {
128129
throw new Error('Mimick is not implemented yet')
129130
}
130131

@@ -165,17 +166,17 @@ export class SubstituteNode extends SubstituteNodeBase implements ObjectSubstitu
165166
? this.child.recordedArguments.value?.shift()
166167
: this.child.recordedArguments.value[0]
167168
switch (substitutionMethod) {
168-
case 'throws':
169+
case throws:
169170
throw substitutionValue
170-
case 'mimicks':
171+
case mimicks:
171172
if (this.propertyType === PropertyTypeMap.Property) return substitutionValue()
172173
if (!contextArguments.hasArguments()) throw new TypeError('Context arguments cannot be undefined')
173174
return substitutionValue(...contextArguments.value)
174-
case 'resolves':
175+
case resolves:
175176
return Promise.resolve(substitutionValue)
176-
case 'rejects':
177+
case rejects:
177178
return Promise.reject(substitutionValue)
178-
case 'returns':
179+
case returns:
179180
return substitutionValue
180181
default:
181182
throw SubstituteException.generic(`Substitution method '${substitutionMethod}' not implemented`)

src/Symbols.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
export const received = Symbol('received');
33
export const didNotReceive = Symbol('didNotReceive');
44
export const mimick = Symbol('mimick');
5-
export const clearSubstitute = Symbol('clearSubstitute');
5+
export const clearReceivedCalls = Symbol('clearReceivedCalls');
66

77
export const mimicks = Symbol('mimicks');
88
export const throws = Symbol('throws');

src/Transformations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { AllArguments } from './Arguments';
2-
import { clearSubstitute, didNotReceive, mimick, received } from './Symbols';
2+
import { clearReceivedCalls, didNotReceive, mimick, received } from './Symbols';
33
import type { ClearType, FirstLevelMethod } from './Types';
44

55
type FunctionSubstituteWithOverloads<TFunc, Terminating = false> =
@@ -96,6 +96,6 @@ export type ObjectSubstitute<T> = ObjectSubstituteTransformation<T> & {
9696
[received](amount?: number): TerminatingObject<T>;
9797
[didNotReceive](): TerminatingObject<T>;
9898
[mimick](instance: OmitProxyMethods<T>): void;
99-
[clearSubstitute](clearType?: ClearType): void;
99+
[clearReceivedCalls](clearType?: ClearType): void;
100100
}
101101
export type DisabledSubstituteObject<T> = T extends ObjectSubstitute<infer K> ? K : never;

src/Types.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import type { clearSubstitute, didNotReceive, mimick, mimicks, received, rejects, resolves, returns, throws } from "./Symbols"
1+
import type { clearReceivedCalls, didNotReceive, mimick, mimicks, received, rejects, resolves, returns, throws } from "./Symbols"
22

33
export type PropertyType = 'method' | 'property'
44
export type AssertionMethod = typeof received | typeof didNotReceive
5-
export type ConfigurationMethod = typeof clearSubstitute | typeof mimick
5+
export type ConfigurationMethod = typeof clearReceivedCalls | typeof mimick
66
export type SubstitutionMethod = typeof mimicks | typeof throws | typeof returns | typeof resolves | typeof rejects
77

88
export type FirstLevelMethod = AssertionMethod | ConfigurationMethod
99
export type SubstituteMethod = FirstLevelMethod | SubstitutionMethod
1010
export type SubstituteContext = SubstituteMethod | 'none'
1111

12-
export type ClearType = 'all' | 'receivedCalls' | 'substituteValues'
13-
1412
export type FilterFunction<T> = (item: T) => boolean

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { Substitute, SubstituteOf } from './Substitute'
33
export { Arg } from './Arguments'
44
export { Substitute, SubstituteOf }
55
export { ClearType } from './Utilities'
6-
export { clearSubstitute, didNotReceive, mimick, received } from './Symbols'
6+
export { clearReceivedCalls, didNotReceive, mimick, received } from './Symbols'
77

88
export default Substitute

0 commit comments

Comments
 (0)