/
exercise-two.spec.js
141 lines (120 loc) · 3.93 KB
/
exercise-two.spec.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
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
'use strict';
var path = require('path');
var chai = require('chai');
var expect = chai.expect;
chai.use(require('chai-spies'));
var utils = require('./utils');
var blue = chai.spy.on(utils, 'blue');
var magenta = chai.spy.on(utils, 'magenta');
var fs = require('fs');
var exercise = require('./exercise-two');
var dirpath = path.join(__dirname, 'poem-two');
var stanzas = fs.readdirSync(dirpath)
.filter(function (filename) {
return filename[0] !== '.'
})
.map(function (filename) {
return fs.readFileSync(path.join(dirpath, filename)).toString();
});
describe('exercise two (involving poem two)', function () {
beforeEach(function () {
blue.reset();
magenta.reset();
});
var blueCalls, redCalls;
beforeEach(function () {
blueCalls = blue.__spy.calls;
redCalls = magenta.__spy.calls;
});
var originalLog = console.log;
beforeEach(function () {
console.log = function () {
var args = [].slice.call(arguments);
console.log.calls.push({
args: args,
priorNumBlueCalls: blue.__spy.calls.length,
priorNumRedCalls: magenta.__spy.calls.length
});
return originalLog.apply(console, arguments);
}
console.log.calls = [];
});
function getLoggedDoneCalls () {
return console.log.calls.filter(function (call) {
return call.args.some(function (arg) {
return /done/.test(arg);
});
});
}
describe('problemA', function () {
it('ignoring errors, logs the first and second stanza in any order, and a done message when both are complete', function (done) {
exercise.problemA();
setTimeout(function () {
expect(blue).to.have.been.called.with(stanzas[0]);
expect(blue).to.have.been.called.with(stanzas[1]);
var loggedDoneCalls = getLoggedDoneCalls();
expect(loggedDoneCalls).to.have.length(1);
var loggedDoneCall = loggedDoneCalls[0];
expect(loggedDoneCall.priorNumBlueCalls).to.equal(2);
done();
}, 500);
});
});
describe('problemB', function () {
it('ignoring errors, logs all stanzas in any order, and a done message when all are complete', function (done) {
this.timeout(3000);
exercise.problemB();
setTimeout(function () {
stanzas.forEach(function (stanza) {
expect(blue).to.have.been.called.with(stanza);
});
var loggedDoneCalls = getLoggedDoneCalls();
expect(loggedDoneCalls).to.have.length(1);
var loggedDoneCall = loggedDoneCalls[0];
expect(loggedDoneCall.priorNumBlueCalls).to.equal(stanzas.length);
done();
}, 2000);
});
});
describe('problemC', function () {
it('ignoring errors, logs all stanzas in the correct order, and a done message when all are complete', function (done) {
this.timeout(3000);
exercise.problemC();
setTimeout(function () {
stanzas.forEach(function (stanza, index) {
var callArgs = blueCalls[index];
expect(callArgs[0]).to.equal(stanza);
});
var loggedDoneCalls = getLoggedDoneCalls();
expect(loggedDoneCalls).to.have.length(1);
var loggedDoneCall = loggedDoneCalls[0];
expect(loggedDoneCall.priorNumBlueCalls).to.equal(8);
done();
}, 2000);
});
});
describe('problemD', function () {
it('logs all stanzas in the correct order; if an error occurs does not read the next file and instead logs the error; always logs done at the end', function (done) {
this.timeout(3000);
exercise.problemD();
setTimeout(function () {
blueCalls.forEach(function (callArgs, index) {
expect(callArgs[0]).to.equal(stanzas[index]);
});
if (redCalls.length) {
expect(redCalls.length).to.equal(1);
expect(redCalls[0][0]).to.be.instanceof(Error);
expect(blueCalls.length).to.be.below(8);
}
var loggedDoneCalls = getLoggedDoneCalls();
expect(loggedDoneCalls).to.have.length(1);
var loggedDoneCall = loggedDoneCalls[0];
expect(loggedDoneCall.priorNumBlueCalls).to.equal(blueCalls.length);
if (blueCalls.length !== stanzas.length) {
expect(loggedDoneCall.priorNumRedCalls).to.equal(1);
}
done();
}, 2000);
});
});
});