Skip to content

Commit ce431db

Browse files
authored
promisify route tests (yeoman#889)
1 parent 3f46882 commit ce431db

File tree

6 files changed

+88
-81
lines changed

6 files changed

+88
-81
lines changed

test/route-clear-config.js

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -56,48 +56,52 @@ describe('clear config route', () => {
5656
esmocha.clearAllMocks();
5757
});
5858

59-
it('allow returning home', function () {
59+
it('allow returning home', async function () {
6060
this.sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({whatNext: 'home'}));
61-
return this.router.navigate('clearConfig').then(() => {
62-
sinon.assert.calledOnce(this.homeRoute);
63-
});
61+
62+
await this.router.navigate('clearConfig');
63+
64+
sinon.assert.calledOnce(this.homeRoute);
6465
});
6566

66-
it('allows clearing a generator and return user to home screen', function () {
67+
it('allows clearing a generator and return user to home screen', async function () {
6768
this.sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({whatNext: 'foo'}));
68-
return this.router.navigate('clearConfig').then(() => {
69-
sinon.assert.calledOnce(this.globalConfig.remove);
70-
sinon.assert.calledWith(this.globalConfig.remove, 'foo');
71-
sinon.assert.calledOnce(this.homeRoute);
72-
});
69+
70+
await this.router.navigate('clearConfig');
71+
72+
sinon.assert.calledOnce(this.globalConfig.remove);
73+
sinon.assert.calledWith(this.globalConfig.remove, 'foo');
74+
sinon.assert.calledOnce(this.homeRoute);
7375
});
7476

75-
it('allows clearing all generators and return user to home screen', function () {
77+
it('allows clearing all generators and return user to home screen', async function () {
7678
this.sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({whatNext: '*'}));
77-
return this.router.navigate('clearConfig').then(() => {
78-
sinon.assert.calledOnce(this.globalConfig.removeAll);
79-
sinon.assert.calledOnce(this.homeRoute);
80-
});
79+
80+
await this.router.navigate('clearConfig');
81+
82+
sinon.assert.calledOnce(this.globalConfig.removeAll);
83+
sinon.assert.calledOnce(this.homeRoute);
8184
});
8285

83-
it('shows generator with global config entry', function () {
86+
it('shows generator with global config entry', async function () {
8487
let choices = [];
8588

8689
this.sandbox.stub(inquirer, 'prompt').callsFake(argument => {
8790
({choices} = argument[0]);
8891
return Promise.resolve({whatNext: 'foo'});
8992
});
90-
return this.router.navigate('clearConfig').then(() => {
91-
// Clear all generators entry is present
92-
assert.ok(_.find(choices, {value: '*'}));
93-
94-
assert.ok(_.find(choices, {value: 'generator-unicorn'}));
95-
assert.ok(_.find(choices, {value: 'generator-phoenix'}));
96-
assert.ok(_.find(choices, {name: 'Unicorn'}));
97-
assert.ok(
98-
_.find(choices, {name: 'phoenix\u001B[31m (not installed anymore)\u001B[39m'})
99-
|| _.find(choices, {name: 'phoenix (not installed anymore)'}),
100-
);
101-
});
93+
94+
await this.router.navigate('clearConfig');
95+
96+
// Clear all generators entry is present
97+
assert.ok(_.find(choices, {value: '*'}));
98+
99+
assert.ok(_.find(choices, {value: 'generator-unicorn'}));
100+
assert.ok(_.find(choices, {value: 'generator-phoenix'}));
101+
assert.ok(_.find(choices, {name: 'Unicorn'}));
102+
assert.ok(
103+
_.find(choices, {name: 'phoenix\u001B[31m (not installed anymore)\u001B[39m'})
104+
|| _.find(choices, {name: 'phoenix (not installed anymore)'}),
105+
);
102106
});
103107
});

test/route-help.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ describe('help route', () => {
2222
esmocha.clearAllMocks();
2323
});
2424

25-
it('allow returning home', function () {
25+
it('allow returning home', async function () {
2626
this.sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({whereTo: 'home'}));
27-
return this.router.navigate('help').then(() => {
27+
await this.router.navigate('help').then(() => {
2828
sinon.assert.calledOnce(this.homeRoute);
2929
});
3030
});
3131

32-
it('open urls in browsers', function () {
32+
it('open urls in browsers', async function () {
3333
const url = 'http://yeoman.io';
3434
this.sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({whereTo: url}));
35-
return this.router.navigate('help').then(() => {
35+
await this.router.navigate('help').then(() => {
3636
expect(open).toHaveBeenCalledTimes(1);
3737
expect(open).toHaveBeenCalledWith(url);
3838
});

test/route-home.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,31 +26,31 @@ describe('home route', () => {
2626
this.sandbox.restore();
2727
});
2828

29-
it('allow going to help', function () {
29+
it('allow going to help', async function () {
3030
this.sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({whatNext: 'help'}));
31-
return this.router.navigate('home').then(() => {
31+
await this.router.navigate('home').then(() => {
3232
sinon.assert.calledOnce(this.helpRoute);
3333
});
3434
});
3535

36-
it('allow going to install', function () {
36+
it('allow going to install', async function () {
3737
this.sandbox.stub(inquirer, 'prompt').returns(Promise.resolve({whatNext: 'install'}));
38-
return this.router.navigate('home').then(() => {
38+
await this.router.navigate('home').then(() => {
3939
sinon.assert.calledOnce(this.installRoute);
4040
});
4141
});
4242

43-
it('does not display update options if no generators is installed', function () {
43+
it('does not display update options if no generators is installed', async function () {
4444
this.router.generator = [];
4545
this.sandbox.stub(inquirer, 'prompt').callsFake(prompts => {
4646
assert.strictEqual(_.map(prompts[0].choices, 'value').includes('update'), false);
4747
return Promise.resolve({whatNext: 'exit'});
4848
});
4949

50-
return this.router.navigate('home');
50+
await this.router.navigate('home');
5151
});
5252

53-
it('show update menu option if there is installed generators', function () {
53+
it('show update menu option if there is installed generators', async function () {
5454
this.router.generators = [{
5555
namespace: 'unicorn:app',
5656
appGenerator: true,
@@ -63,12 +63,12 @@ describe('home route', () => {
6363
return Promise.resolve({whatNext: 'update'});
6464
});
6565

66-
return this.router.navigate('home').then(() => {
66+
await this.router.navigate('home').then(() => {
6767
sinon.assert.calledOnce(this.updateRoute);
6868
});
6969
});
7070

71-
it('list runnable generators', function () {
71+
it('list runnable generators', async function () {
7272
this.router.generators = [{
7373
namespace: 'unicorn:app',
7474
appGenerator: true,
@@ -86,12 +86,12 @@ describe('home route', () => {
8686
});
8787
});
8888

89-
return this.router.navigate('home').then(() => {
89+
await this.router.navigate('home').then(() => {
9090
sinon.assert.calledWith(this.runRoute, this.router, 'unicorn:app');
9191
});
9292
});
9393

94-
it('show update available message behind generator name', function () {
94+
it('show update available message behind generator name', async function () {
9595
this.router.generators = [{
9696
namespace: 'unicorn:app',
9797
appGenerator: true,
@@ -104,6 +104,6 @@ describe('home route', () => {
104104
return Promise.resolve({whatNext: 'exit'});
105105
});
106106

107-
return this.router.navigate('home');
107+
await this.router.navigate('home');
108108
});
109109
});

test/route-install.js

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -104,57 +104,59 @@ describe('install route', () => {
104104
.reply(200, this.pkgData);
105105
});
106106

107-
it('filters already installed generators and match search term', function (done) {
107+
it('filters already installed generators and match search term', async function () {
108108
if (process.platform === 'darwin') {
109109
this.skip();
110110
}
111111

112112
let call = 0;
113+
let choices;
113114
inquirer.prompt.mockImplementation(argument => {
114115
call++;
115116
if (call === 1) {
116117
return Promise.resolve({searchTerm: 'unicorn'});
117118
}
118119

119120
if (call === 2) {
120-
const {choices} = argument[0];
121-
assert.strictEqual(_.filter(choices, {value: 'generator-foo'}).length, 1);
122-
assert.strictEqual(_.filter(choices, {value: 'generator-unicorn-1'}).length, 1);
123-
assert.strictEqual(_.filter(choices, {value: 'generator-unicorn'}).length, 0);
124-
assert.strictEqual(_.filter(choices, {value: 'generator-unrelated'}).length, 0);
125-
done();
121+
choices = argument[0].choices;
126122
}
127123

128124
return Promise.resolve({toInstall: 'home'});
129125
});
130126

131-
this.router.navigate('install');
127+
await this.router.navigate('install');
128+
129+
assert.strictEqual(_.filter(choices, {value: 'generator-foo'}).length, 1);
130+
assert.strictEqual(_.filter(choices, {value: 'generator-unicorn-1'}).length, 1);
131+
assert.strictEqual(_.filter(choices, {value: 'generator-unicorn'}).length, 0);
132+
assert.strictEqual(_.filter(choices, {value: 'generator-unrelated'}).length, 0);
132133
});
133134

134-
it('filters blacklisted generators and match search term', function (done) {
135+
it('filters blacklisted generators and match search term', async function () {
135136
if (process.platform === 'darwin') {
136137
this.skip();
137138
}
138139

139140
let call = 0;
141+
let choices;
140142
inquirer.prompt.mockImplementation(argument => {
141143
call++;
142144
if (call === 1) {
143145
return Promise.resolve({searchTerm: 'blacklist'});
144146
}
145147

146148
if (call === 2) {
147-
const {choices} = argument[0];
148-
assert.strictEqual(_.filter(choices, {value: 'generator-blacklist-1'}).length, 0);
149-
assert.strictEqual(_.filter(choices, {value: 'generator-blacklist-2'}).length, 0);
150-
assert.strictEqual(_.filter(choices, {value: 'generator-blacklist-3'}).length, 1);
151-
done();
149+
choices = argument[0].choices;
152150
}
153151

154152
return Promise.resolve({toInstall: 'home'});
155153
});
156154

157-
this.router.navigate('install');
155+
await this.router.navigate('install');
156+
157+
assert.strictEqual(_.filter(choices, {value: 'generator-blacklist-1'}).length, 0);
158+
assert.strictEqual(_.filter(choices, {value: 'generator-blacklist-2'}).length, 0);
159+
assert.strictEqual(_.filter(choices, {value: 'generator-blacklist-3'}).length, 1);
158160
});
159161

160162
it('allow redo the search', async function () {
@@ -180,7 +182,7 @@ describe('install route', () => {
180182
await this.router.navigate('install');
181183
});
182184

183-
it('allow going back home', function () {
185+
it('allow going back home', async function () {
184186
let call = 0;
185187
inquirer.prompt.mockImplementation(() => {
186188
call++;
@@ -191,12 +193,12 @@ describe('install route', () => {
191193
return Promise.resolve({toInstall: 'home'});
192194
});
193195

194-
return this.router.navigate('install').then(() => {
195-
expect(this.homeRoute).toHaveBeenCalledTimes(1);
196-
});
196+
await this.router.navigate('install');
197+
198+
expect(this.homeRoute).toHaveBeenCalledTimes(1);
197199
});
198200

199-
it('install a generator', function () {
201+
it('install a generator', async function () {
200202
let call = 0;
201203
inquirer.prompt.mockImplementation(() => {
202204
call++;
@@ -211,11 +213,11 @@ describe('install route', () => {
211213
return Promise.resolve({toInstall: 'home'});
212214
});
213215

214-
return this.router.navigate('install').then(() => {
215-
expect(spawn.default).toHaveBeenCalledTimes(1);
216-
expect(spawn.default).toHaveBeenCalledWith('npm', ['install', '--global', 'generator-unicorn'], {stdio: 'inherit'});
217-
expect(this.homeRoute).toHaveBeenCalledTimes(1);
218-
});
216+
await this.router.navigate('install');
217+
218+
expect(spawn.default).toHaveBeenCalledTimes(1);
219+
expect(spawn.default).toHaveBeenCalledWith('npm', ['install', '--global', 'generator-unicorn'], {stdio: 'inherit'});
220+
expect(this.homeRoute).toHaveBeenCalledTimes(1);
219221
});
220222
});
221223

test/route-update.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ describe('update route', () => {
2929
this.sandbox.restore();
3030
});
3131

32-
it('allows updating generators and return user to home screen', function () {
32+
it('allows updating generators and return user to home screen', async function () {
3333
const generators = ['generator-cat', 'generator-unicorn'];
3434
this.sandbox.stub(inquirer, 'prompt').returns(
3535
Promise.resolve({generators}),
3636
);
37-
return this.router.navigate('update').then(() => {
38-
sinon.assert.calledWith(
39-
this.crossSpawn,
40-
'npm',
41-
['install', '--global', ...generators],
42-
);
43-
sinon.assert.calledOnce(this.homeRoute);
44-
sinon.assert.calledOnce(this.env.lookup);
45-
});
37+
38+
await this.router.navigate('update');
39+
40+
sinon.assert.calledWith(
41+
this.crossSpawn,
42+
'npm',
43+
['install', '--global', ...generators],
44+
);
45+
sinon.assert.calledOnce(this.homeRoute);
46+
sinon.assert.calledOnce(this.env.lookup);
4647
});
4748
});

test/router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ describe('Router', () => {
4949
sinon.assert.calledOnce(this.route);
5050
});
5151

52-
it('call a route passing arguments', function () {
53-
this.router.navigate('foo', 'dummy');
52+
it('call a route passing arguments', async function () {
53+
await this.router.navigate('foo', 'dummy');
5454
sinon.assert.calledWith(this.route, this.router, 'dummy');
5555
sinon.assert.calledOnce(this.route);
5656
});

0 commit comments

Comments
 (0)