Skip to content

Commit 45ed951

Browse files
committed
implement stubbing-finder
1 parent 66fb47c commit 45ed951

File tree

4 files changed

+115
-10
lines changed

4 files changed

+115
-10
lines changed
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
1-
export default function findLastStubbingMatch () {
1+
import _ from '../wrap/lodash'
2+
3+
import StubbingRegister from '../value/stubbing-register'
4+
import argsMatch from '../args-match'
5+
6+
export default function findLastStubbingMatch (double, call) {
7+
return _.findLast(StubbingRegister.instance.get(double), (stubbing) =>
8+
argsMatch(stubbing.args, call.args, stubbing.config) && stubbing.hasTimesRemaining
9+
)
210
}

src/value/stubbing.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ export default class Stubbing {
77
this.satisfactionCount = 0
88
}
99

10-
get timesSatisfied () {
11-
return this.satisfactionCount
10+
get hasTimesRemaining () {
11+
if (this.options.times == null) return true
12+
return this.satisfactionCount < this.options.times
1213
}
1314

1415
incrementSatisfactions () {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import Call from '../../../src/value/call'
2+
import Double from '../../../src/value/double'
3+
import Stubbing from '../../../src/value/stubbing'
4+
import StubbingRegister from '../../../src/value/stubbing-register'
5+
6+
import subject from '../../../src/satisfy/find-last-stubbing-match'
7+
8+
let stubbingRegister
9+
module.exports = {
10+
beforeEach: () => {
11+
stubbingRegister = StubbingRegister.instance
12+
},
13+
'no stubbings': () => {
14+
const double = Double.create()
15+
const call = new Call(this, [])
16+
17+
const result = subject(double, call)
18+
19+
assert.equal(result, null)
20+
},
21+
'2 stub 1 match': () => {
22+
const double = Double.create()
23+
const call = new Call(this, [42])
24+
const stubbing1 = new Stubbing('thenBlah', [42], ['blah'])
25+
const stubbing2 = new Stubbing('thenBlah', [43], ['blah'])
26+
stubbingRegister.add(double, stubbing1)
27+
stubbingRegister.add(double, stubbing2)
28+
29+
const result = subject(double, call)
30+
31+
assert.equal(result, stubbing1)
32+
},
33+
'3 stub 2 matches': () => {
34+
const double = Double.create()
35+
const call = new Call(this, [42])
36+
const stubbing1 = new Stubbing('thenBlah', [42], ['blah'])
37+
const stubbing2 = new Stubbing('thenBlah', ['pants'], ['blah'])
38+
const stubbing3 = new Stubbing('thenBlah', [42], ['blah'])
39+
stubbingRegister.add(double, stubbing1)
40+
stubbingRegister.add(double, stubbing2)
41+
stubbingRegister.add(double, stubbing3)
42+
43+
const result = subject(double, call)
44+
45+
assert.equal(result, stubbing3)
46+
},
47+
'stubbing has limited satisfactions': () => {
48+
const double = Double.create()
49+
const call = new Call(this, [42])
50+
const stubbing1 = new Stubbing('thenBlah', [42], ['blah'])
51+
const stubbing2 = new Stubbing('thenBlah', ['pants'], ['blah'])
52+
const stubbing3 = new Stubbing('thenBlah', [42], ['blah'], {times: 2})
53+
stubbingRegister.add(double, stubbing1)
54+
stubbingRegister.add(double, stubbing2)
55+
stubbingRegister.add(double, stubbing3)
56+
stubbing3.incrementSatisfactions()
57+
stubbing3.incrementSatisfactions()
58+
59+
const result = subject(double, call)
60+
61+
assert.equal(result, stubbing1)
62+
}
63+
}
64+

test/unit/value/stubbing.test.js

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,47 @@
11
import Stubbing from '../../../src/value/stubbing'
22

33
module.exports = {
4-
'increments timesSatisfied (used for satisfaction-limits)': () => {
5-
const subject = new Stubbing()
4+
'.hasTimesRemaining': {
5+
'no option set': () => {
6+
const subject = new Stubbing()
67

7-
assert.equal(subject.timesSatisfied, 0)
8+
assert.equal(subject.hasTimesRemaining, true)
89

9-
subject.incrementSatisfactions()
10-
assert.equal(subject.timesSatisfied, 1)
10+
subject.incrementSatisfactions()
11+
12+
assert.equal(subject.hasTimesRemaining, true)
13+
},
14+
'times set to 0': () => {
15+
const subject = new Stubbing(null, null, null, {times: 0})
16+
17+
assert.equal(subject.hasTimesRemaining, false)
18+
19+
subject.incrementSatisfactions()
20+
21+
assert.equal(subject.hasTimesRemaining, false)
22+
},
23+
'times set to 1': () => {
24+
const subject = new Stubbing(null, null, null, {times: 1})
25+
26+
assert.equal(subject.hasTimesRemaining, true)
27+
28+
subject.incrementSatisfactions()
29+
30+
assert.equal(subject.hasTimesRemaining, false)
31+
},
32+
'times set to 2': () => {
33+
const subject = new Stubbing(null, null, null, {times: 2})
34+
35+
assert.equal(subject.hasTimesRemaining, true)
36+
37+
subject.incrementSatisfactions()
38+
39+
assert.equal(subject.hasTimesRemaining, true)
40+
41+
subject.incrementSatisfactions()
42+
43+
assert.equal(subject.hasTimesRemaining, false)
44+
}
1145

12-
subject.incrementSatisfactions()
13-
assert.equal(subject.timesSatisfied, 2)
1446
}
1547
}

0 commit comments

Comments
 (0)