1+ import test from 'ava'
2+
3+ import { Substitute , SubstituteOf } from '../src'
4+ import { SubstituteBase } from '../src/SubstituteBase'
5+ import { SubstituteNode } from '../src/SubstituteNode'
6+
7+ interface Calculator {
8+ add ( a : number , b : number ) : number
9+ subtract ( a : number , b : number ) : number
10+ divide ( a : number , b : number ) : number
11+ isEnabled : boolean
12+ }
13+
14+ type InstanceReturningSubstitute < T > = SubstituteOf < T > & {
15+ [ SubstituteBase . instance ] : Substitute
16+ }
17+
18+ test ( 'clears everything on a substitute' , t => {
19+ const calculator = Substitute . for < Calculator > ( ) as InstanceReturningSubstitute < Calculator >
20+ calculator . add ( 1 , 1 )
21+ calculator . received ( ) . add ( 1 , 1 )
22+ calculator . clearSubstitute ( )
23+
24+ t . is ( calculator [ Substitute . instance ] . recorder . records . size , 0 )
25+ t . is ( calculator [ Substitute . instance ] . recorder . indexedRecords . size , 0 )
26+
27+ t . throws ( ( ) => calculator . received ( ) . add ( 1 , 1 ) )
28+
29+ // explicitly using 'all'
30+ calculator . add ( 1 , 1 )
31+ calculator . received ( ) . add ( 1 , 1 )
32+ calculator . clearSubstitute ( 'all' )
33+
34+ t . is ( calculator [ Substitute . instance ] . recorder . records . size , 0 )
35+ t . is ( calculator [ Substitute . instance ] . recorder . indexedRecords . size , 0 )
36+
37+ t . throws ( ( ) => calculator . received ( ) . add ( 1 , 1 ) )
38+ } )
39+
40+ test ( 'clears received calls on a substitute' , t => {
41+ const calculator = Substitute . for < Calculator > ( ) as InstanceReturningSubstitute < Calculator >
42+ calculator . add ( 1 , 1 )
43+ calculator . add ( 1 , 1 ) . returns ( 2 )
44+ calculator . clearSubstitute ( 'receivedCalls' )
45+
46+ t . is ( calculator [ Substitute . instance ] . recorder . records . size , 2 )
47+ t . is ( calculator [ Substitute . instance ] . recorder . indexedRecords . size , 2 )
48+
49+ t . throws ( ( ) => calculator . received ( ) . add ( 1 , 1 ) )
50+ t . is ( calculator . add ( 1 , 1 ) , 2 )
51+ } )
52+
53+ test ( 'clears return values on a substitute' , t => {
54+ const calculator = Substitute . for < Calculator > ( ) as InstanceReturningSubstitute < Calculator >
55+ calculator . add ( 1 , 1 )
56+ calculator . add ( 1 , 1 ) . returns ( 2 )
57+ calculator . clearSubstitute ( 'substituteValues' )
58+
59+ t . is ( calculator [ Substitute . instance ] . recorder . records . size , 2 )
60+ t . is ( calculator [ Substitute . instance ] . recorder . indexedRecords . size , 2 )
61+
62+ t . notThrows ( ( ) => calculator . received ( ) . add ( 1 , 1 ) )
63+ // @ts -expect-error
64+ t . true ( calculator . add ( 1 , 1 ) [ SubstituteBase . instance ] instanceof SubstituteNode )
65+ } )
0 commit comments