This repository was archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathx-ray-specs.js
51 lines (43 loc) · 1.49 KB
/
x-ray-specs.js
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
// Subject under test
var _ = require('lodash')
function SeatMap (ticket, originalSeat) {
this.fareClass = ticket.fareClass
this.__currentSeat = originalSeat
this.__approvals = {}
}
SeatMap.prototype.moveTo = function (newSeat) {
if (!_.has(this.__approvals, this.fareClass + '.' + newSeat)) {
this.__qualifyFareClassForSeat(newSeat)
}
if (this.__approvals[this.fareClass][newSeat]) {
this.__currentSeat = newSeat
}
}
// Test
module.exports = {
beforeEach: function () {
this.ticket = {fareClass: 'M'}
this.subject = new SeatMap(this.ticket, '18D')
},
approveIfBehindRowTen: function () {
this.subject.moveTo('11B')
assert.equal(this.subject.__approvals['M']['11B'], true)
assert.equal(this.subject.__currentSeat, '11B')
},
denyIfAheadOfRowTen: function () {
this.subject.moveTo('9J')
assert.equal(this.subject.__approvals['M']['9J'], false)
assert.equal(this.subject.__currentSeat, '18D')
},
willShortCircuitApprovalProcessWhenMemoized: function () {
this.subject.__approvals['M'] = {'Havanna': 'Sure, why not'}
this.subject.moveTo('Havanna')
assert.equal(this.subject.__approvals['M']['Havanna'], 'Sure, why not')
assert.equal(this.subject.__currentSeat, 'Havanna')
}
}
// Fake production implementations to simplify example test of subject
SeatMap.prototype.__qualifyFareClassForSeat = function (seat) {
var allowed = parseInt(seat.match(/^(\d+)/)[0], 10) > 10
_.set(this.__approvals, this.fareClass + '.' + seat, allowed)
}