Skip to content

Commit 531a26a

Browse files
fix: use symbol representation on clear method
1 parent 3a166ea commit 531a26a

File tree

5 files changed

+15
-14
lines changed

5 files changed

+15
-14
lines changed

ava.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
module.exports = {
3-
files: ['spec/**/*.ts'],
3+
files: ['spec/**/*.spec.ts'],
44
typescript: {
55
compile: false,
66
rewritePaths: {
@@ -10,4 +10,4 @@ module.exports = {
1010
cache: false,
1111
failFast: true,
1212
failWithoutAssertions: true
13-
}
13+
}

spec/ClearSubstitute.spec.ts renamed to spec/regression/clearReceivedCalls.spec.ts

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

3-
import { Substitute, SubstituteOf } from '../src'
4-
import { SubstituteNode, instance } from '../src/internals/SubstituteNode'
3+
import { Substitute, SubstituteOf, clearReceivedCalls } from '../../src'
4+
import { SubstituteNode, instance } from '../../src/internals/SubstituteNode'
55

66
interface Calculator {
77
add(a: number, b: number): number
@@ -14,11 +14,11 @@ type InstanceReturningSubstitute<T> = SubstituteOf<T> & {
1414
[instance]: SubstituteNode
1515
}
1616

17-
test.skip('clears received calls on a substitute', t => {
17+
test('clears received calls on a substitute', t => {
1818
const calculator = Substitute.for<Calculator>() as InstanceReturningSubstitute<Calculator>
1919
calculator.add(1, 1)
2020
calculator.add(1, 1).returns(2)
21-
calculator.clearReceivedCalls();
21+
calculator[clearReceivedCalls]();
2222

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

src/api/types/SubstituteMethods.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,4 @@ export type ObjectSubstituteMethods<T> =
2626
(T extends { mimick: any } ?
2727
{ [mimick](instance: T): void } :
2828
{ mimick(instance: T): void }) &
29-
(T extends { clearReceivedCalls: any } ?
30-
{ [clearReceivedCalls](): void } :
31-
{ clearReceivedCalls(): void })
29+
{ [clearReceivedCalls](): void }

src/api/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export type ObjectSubstitute<T> =
55
ObjectSubstituteMethods<T> &
66
ObjectSubstituteTransformation<T>
77

8-
export { received } from './SubstituteMethods'
8+
export { received, didNotReceive, clearReceivedCalls, mimick } from './SubstituteMethods'
99
export { returns, throws, resolves, rejects, mimicks } from './SubstitutionLevel'

src/internals/SubstituteNode.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { SubstituteException } from './SubstituteException'
66
import { is, stringify, transform } from './utilities'
77

88
import { constants } from './Constants'
9-
import type { SubstituteContext, PropertyType, SubstituteNodeModel, AccessorType, SubstituteMethod, SubstitutionMethod } from './Types'
9+
import type { SubstituteContext, PropertyType, SubstituteNodeModel, AccessorType, SubstituteMethod, SubstitutionMethod, AssertionMethod } from './Types'
1010
import { constants as sharedConstants } from '../shared/Constants'
1111

1212

@@ -48,10 +48,12 @@ export class SubstituteNode extends SubstituteNodeBase implements ObjectSubstitu
4848

4949
const newNode = SubstituteNode.createChild(property, target)
5050
if (target.isAssertion) newNode.executeAssertion()
51-
if (target.hasDepthOfAtLeast(1) && !target.hasContext && is.method.assertion(target.property)) {
51+
const unresolvedAssertionFollowedBySubstitution = !target.hasContext && is.method.assertion(target.property) && !is.method.substitution(newNode.property)
52+
if (target.hasDepthOfAtLeast(1) && unresolvedAssertionFollowedBySubstitution) {
5253
target.assignContext(target.property)
53-
target[target.property](...Array.isArray(target.recordedArguments.value) ? target.recordedArguments.value : [undefined])
54+
target[target.context as AssertionMethod](...Array.isArray(target.recordedArguments.value) ? target.recordedArguments.value : [undefined])
5455
if (target.isAssertion) newNode.executeAssertion()
56+
// if (target.isConfiguration) newNode.executeConfiguration()
5557
}
5658
if (target.isRoot() && is.method.contextValue(property) && (is.method.assertion(property) || is.method.configuration(property))) {
5759
newNode.assignContext(property)
@@ -238,7 +240,8 @@ export class SubstituteNode extends SubstituteNodeBase implements ObjectSubstitu
238240
return
239241
}
240242

241-
const withContext = this.parent.property === sharedConstants.CONTEXT.received.symbol
243+
// const withContext = this.parent.property === sharedConstants.CONTEXT.received.symbol
244+
const withContext = false
242245
const siblings = [...this.getAllSiblings().filter(n => (withContext || !n.hasContext) && n.accessorType === this.accessorType)]
243246
const hasBeenCalled = siblings.length > 0
244247
const hasSiblingOfSamePropertyType = siblings.some(sibling => sibling.propertyType === this.propertyType)

0 commit comments

Comments
 (0)