-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathindex.test.ts
More file actions
158 lines (117 loc) · 4.72 KB
/
index.test.ts
File metadata and controls
158 lines (117 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import test from 'ava'
import { Substitute, Arg, SubstituteOf, received } from '../../src'
class Dummy {
}
export class Example {
a = '1337';
c(arg1: string, arg2: string) {
return 'hello ' + arg1 + ' world (' + arg2 + ')'
}
get d() {
return 1337
}
set v(x: string | null | undefined) {
}
received(_stuff: string) {
}
returnPromise() {
return Promise.resolve(new Dummy())
}
foo(_arg?: string): string | undefined | null {
return 'stuff'
}
bar(a: number, b?: number): number {
return a + (b ?? 0)
}
}
let instance: Example
let substitute: SubstituteOf<Example>
function initialize() {
instance = new Example()
substitute = Substitute.for<Example>()
};
const textModifierRegex = /\x1b\[\d+m/g
test('class with method called \'received\' can be used for call count verification when using symbols', t => {
const substitute = Substitute.for<Example>()
substitute.received("foo")
t.notThrows(() => substitute[received](1).received("foo"))
t.throws(() => substitute[received](2).received("foo"))
})
test('class string field set received', t => {
initialize()
const runLogic = (example: Example) => {
example.v = undefined
example.v = null
example.v = 'hello'
example.v = 'hello'
example.v = 'world'
}
runLogic(substitute)
t.notThrows(() => substitute[received]().v = 'hello')
t.notThrows(() => substitute[received](5).v = Arg.any())
t.notThrows(() => substitute[received]().v = Arg.any())
t.notThrows(() => substitute[received](2).v = 'hello')
t.notThrows(() => substitute[received](2).v = Arg.is(x => typeof x === 'string' && x.indexOf('ll') > -1))
t.throws(() => substitute[received](2).v = Arg.any())
t.throws(() => substitute[received](1).v = Arg.any())
t.throws(() => substitute[received](1).v = Arg.is(x => typeof x === 'string' && x.indexOf('ll') > -1))
t.throws(() => substitute[received](3).v = 'hello')
})
test('resolving promises works', async t => {
initialize()
substitute.returnPromise().resolves(1338)
t.is(1338, await substitute.returnPromise() as number)
})
test('class void returns', t => {
initialize()
substitute.foo().returns(void 0, null)
t.is(substitute.foo(), void 0)
t.is(substitute.foo(), null)
})
test('class method received', t => {
initialize()
void substitute.c('hi', 'there')
void substitute.c('hi', 'the1re')
void substitute.c('hi', 'there')
void substitute.c('hi', 'there')
void substitute.c('hi', 'there')
t.notThrows(() => substitute[received](4).c('hi', 'there'))
t.notThrows(() => substitute[received](1).c('hi', 'the1re'))
t.notThrows(() => substitute[received]().c('hi', 'there'))
const expectedMessage = 'Call count mismatch in @Substitute.c:\n' +
`Expected to receive 7 method calls matching c('hi', 'there'), but received 4.\n` +
'All property or method calls to @Substitute.c received so far:\n' +
`› ✔ @Substitute.c('hi', 'there')\n` +
` called at <anonymous> (${process.cwd()}/spec/regression/index.test.ts:105:18)\n` +
`› ✘ @Substitute.c('hi', 'the1re')\n` +
` called at <anonymous> (${process.cwd()}/spec/regression/index.test.ts:106:18)\n` +
`› ✔ @Substitute.c('hi', 'there')\n` +
` called at <anonymous> (${process.cwd()}/spec/regression/index.test.ts:107:18)\n` +
`› ✔ @Substitute.c('hi', 'there')\n` +
` called at <anonymous> (${process.cwd()}/spec/regression/index.test.ts:108:18)\n` +
`› ✔ @Substitute.c('hi', 'there')\n` +
` called at <anonymous> (${process.cwd()}/spec/regression/index.test.ts:109:18)\n`
const { message } = t.throws(() => { substitute[received](7).c('hi', 'there') })
t.is(message.replace(textModifierRegex, ''), expectedMessage)
})
test('received call matches after partial mocks using property instance mimicks', t => {
initialize()
substitute.d.mimicks(() => instance.d)
substitute.c('lala', 'bar')
substitute[received](1).c('lala', 'bar')
substitute[received](1).c('lala', 'bar')
t.notThrows(() => substitute[received](1).c('lala', 'bar'))
const expectedMessage = 'Call count mismatch in @Substitute.c:\n' +
`Expected to receive 2 method calls matching c('lala', 'bar'), but received 1.\n` +
'All property or method calls to @Substitute.c received so far:\n' +
`› ✔ @Substitute.c('lala', 'bar')\n` +
` called at <anonymous> (${process.cwd()}/spec/regression/index.test.ts:136:13)\n`
const { message } = t.throws(() => substitute[received](2).c('lala', 'bar'))
t.is(message.replace(textModifierRegex, ''), expectedMessage)
t.deepEqual(substitute.d, 1337)
})
test('partial mocks using property instance mimicks', t => {
initialize()
substitute.d.mimicks(() => instance.d)
t.deepEqual(substitute.d, 1337)
})