-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
340 lines (286 loc) · 9.45 KB
/
test.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
'use strict';
process.env.NODE_ENV = 'test';
require('mocha');
var assert = require('assert');
var App = require('base');
var store = require('base-store');
var option = require('base-option');
var config = require('base-config-process');
var intercept = require('intercept-stdout');
var bddStdin = require('bdd-stdin');
var data = require('base-data');
var questions = require('./');
var app, base, site;
function interception(re) {
return intercept(function(str) {
if (re.test(str)) {
return '';
} else {
return str.trim();
}
});
}
describe('base-questions', function() {
describe('plugin', function() {
beforeEach(function() {
base = new App({isApp: true});
base.use(store('base-questions-tests/base'));
app = new App({isApp: true});
app.use(store('base-questions-tests/app'));
app.use(questions());
});
it('should export a function', function() {
assert.equal(typeof questions, 'function');
});
it('should expose a `questions` object on "app"', function() {
assert.equal(typeof app.questions, 'object');
});
it('should expose a `set` method on "app.questions"', function() {
assert.equal(typeof app.questions.set, 'function');
});
it('should expose a `get` method on "app.questions"', function() {
assert.equal(typeof app.questions.get, 'function');
});
it('should expose an `ask` method on "app.questions"', function() {
assert.equal(typeof app.questions.ask, 'function');
});
it('should expose an `ask` method on "app"', function() {
assert.equal(typeof app.ask, 'function');
});
it('should expose a `question` method on "app"', function() {
assert.equal(typeof app.question, 'function');
});
});
describe('app.ask', function() {
beforeEach(function() {
app = new App({isApp: true});
app.use(data());
app.use(option());
app.use(config());
app.use(questions());
app.use(store('base-questions-tests/app.ask'));
});
afterEach(function() {
app.store.del({force: true});
app.questions.clear();
app.cache.data = {};
});
it('should force all questions to be asked', function(cb) {
var unhook = interception(/Name|Description/);
var answers = {
name: 'Brian Woodward',
desc: 'Foo'
};
app.on('ask', function(val, key) {
bddStdin(answers[key], '\n');
});
app.question('name', {message: 'Name?'});
app.question('desc', {message: 'Description?'});
app.ask({force: true}, function(err, answers) {
if (err) {
cb(err);
return;
}
assert.deepEqual(answers, {name: 'Brian Woodward', desc: 'Foo'});
unhook();
cb();
});
});
it('should store a question:', function() {
app.question('a', 'b');
assert(app.questions);
assert(app.questions.cache);
assert(app.questions.cache.a);
assert.equal(app.questions.cache.a.name, 'a');
assert.equal(app.questions.cache.a.message, 'b');
});
it('should force a specific question:', function(cb) {
var unhook = interception(/foo/);
app.on('ask', function() {
bddStdin('foo', '\n');
});
app.disable('force');
app.question('a', 'b');
app.question('c', 'd');
app.question('e', 'f');
app.data({a: 'b', c: 'd', e: 'f'});
var question = app.questions.get('e');
question.options.force = true;
app.ask(function(err, answers) {
if (err) return cb(err);
assert.deepEqual(answers, {a: 'b', c: 'd', e: 'foo'});
unhook();
cb();
});
});
it('should ask a question defined on `ask`', function(cb) {
app.data('name', 'Brian Woodward');
app.ask('name', function(err, answers) {
if (err) return cb(err);
assert.equal(answers.name, 'Brian Woodward');
cb();
});
});
it('should ask a question and use a `cache.data` value to answer:', function(cb) {
app.question('a', 'this is a question');
app.data('a', 'b');
app.ask('a', function(err, answers) {
if (err) return cb(err);
assert.equal(answers.a, 'b');
app.data('a', 'zzz');
app.ask('a', function(err, answers) {
if (err) return cb(err);
assert.equal(answers.a, 'zzz');
cb();
});
});
});
it('should ask a question and use a `store.data` value to answer:', function(cb) {
app.question('a', 'this is another question');
app.store.set('a', 'c');
app.ask('a', function(err, answers) {
if (err) return cb(err);
assert(answers);
assert.equal(answers.a, 'c');
cb();
});
});
it('should ask a question and use a config value to answer:', function(cb) {
app.question('a', 'b');
app.config.process({data: {a: 'foo'}}, function(err) {
if (err) return cb(err);
app.store.set('a', 'c');
app.ask('a', function(err, answer) {
if (err) return cb(err);
assert(answer);
assert.equal(answer.a, 'foo');
cb();
});
});
});
it('should prefer `cache.data` to `store.data`', function(cb) {
app.question('a', 'b');
app.data('a', 'b');
app.store.set('a', 'c');
app.ask('a', function(err, answer) {
assert(!err);
assert(answer);
assert.equal(answer.a, 'b');
cb();
});
});
it('should update data with data loaded by config', function(cb) {
app.question('a', 'this is a question');
app.data('a', 'b');
app.config.process({data: {a: 'foo'}}, function(err) {
if (err) return cb(err);
app.ask('a', function(err, answer) {
if (err) return cb(err);
assert(answer);
assert.equal(answer.a, 'foo');
cb();
});
});
});
});
describe('session data', function() {
before(function() {
site = new App();
site.isApp = true;
site.use(store('base-questions-tests/site'));
site.use(data());
site.use(config());
site.use(option());
site.use(questions());
app = new App();
app.isApp = true;
app.use(store('base-questions-tests/app'));
app.use(data());
app.use(config());
app.use(option());
app.use(questions());
});
after(function() {
site.store.del({force: true});
site.questions.clear();
app.cache.data = {};
app.store.del({force: true});
app.questions.clear();
app.cache.data = {};
});
it('[app] should ask a question and use a `cache.data` value to answer:', function(cb) {
app.question('package.name', 'this is a question');
app.data('package.name', 'base-questions');
app.ask('package.name', function(err, answers) {
if (err) return cb(err);
assert.equal(answers.package.name, 'base-questions');
app.data('package.name', 'question-store');
app.ask('package.name', function(err, answers) {
if (err) return cb(err);
assert.equal(answers.package.name, 'question-store');
cb();
});
});
});
it('[site] should ask a question and use a `cache.data` value to answer:', function(cb) {
site.question('package.name', 'this is a question');
site.data('package.name', 'base-questions');
site.ask('package.name', function(err, answers) {
if (err) return cb(err);
assert.equal(answers.package.name, 'base-questions');
site.data('package.name', 'question-store');
site.ask('package.name', function(err, answers) {
if (err) return cb(err);
assert.equal(answers.package.name, 'question-store');
cb();
});
});
});
it('[app] should ask a question and use a `store.data` value to answer:', function(cb) {
app.question('author.name', 'author name?');
app.store.set('author.name', 'Brian Woodward');
app.disable('common-config');
app.ask('author.name', function(err, answers) {
if (err) return cb(err);
assert(answers);
assert.equal(answers.author.name, 'Brian Woodward');
cb();
});
});
it('[site] should ask a question and use a `store.data` value to answer:', function(cb) {
site.question('author.name', 'author name?');
site.store.set('author.name', 'Jon Schlinkert');
site.ask('author.name', function(err, answers) {
if (err) return cb(err);
assert(answers);
assert.equal(answers.author.name, 'Jon Schlinkert');
cb();
});
});
it('[app] should ask a question and use a config value to answer:', function(cb) {
app.question('foo', 'Username?');
app.config.process({data: {foo: 'jonschlinkert'}}, function(err) {
if (err) return cb(err);
app.store.set('foo', 'doowb');
app.ask('foo', function(err, answer) {
assert(!err);
assert(answer);
assert.equal(answer.foo, 'jonschlinkert');
cb();
});
});
});
it('[site] should ask a question and use a config value to answer:', function(cb) {
site.question('foo', 'Username?');
site.config.process({data: {foo: 'doowb'}}, function(err) {
if (err) return cb(err);
site.ask('foo', function(err, answer) {
if (err) return cb(err);
assert(answer);
assert.equal(answer.foo, 'doowb');
cb();
});
});
});
});
});