Skip to content

Commit e220caa

Browse files
committed
rework test to use TestAdapter
1 parent ee33715 commit e220caa

File tree

7 files changed

+93
-182
lines changed

7 files changed

+93
-182
lines changed

package-lock.json

Lines changed: 14 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@
5959
"natives": "1.1.3"
6060
},
6161
"dependencies": {
62-
"@yeoman/adapter": "^2.0.0",
63-
"@yeoman/types": "^1.5.0",
62+
"@yeoman/adapter": "^2.1.1",
63+
"@yeoman/types": "^1.6.0",
6464
"chalk": "^5.4.1",
6565
"cli-list": "^1.0.0",
6666
"configstore": "^7.0.0",

test/route-clear-config.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,11 @@ describe('clear config route', () => {
8888
});
8989

9090
it('shows generator with global config entry', async function () {
91-
let choices = [];
92-
93-
this.sandbox.stub(adapter, 'prompt').callsFake(argument => {
94-
({choices} = argument[0]);
95-
return Promise.resolve({whatNext: 'foo'});
96-
});
91+
adapter.addAnswers({whatNext: 'foo'});
9792

9893
await this.router.navigate('clearConfig');
9994

95+
const {choices} = adapter.calls[0].question;
10096
// Clear all generators entry is present
10197
assert.ok(_.find(choices, {value: '*'}));
10298

test/route-help.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ describe('help route', () => {
1212
let adapter;
1313

1414
beforeEach(async function () {
15-
this.sandbox = sinon.createSandbox();
1615
this.homeRoute = sinon.stub().returns(Promise.resolve());
1716
adapter = new TestAdapter();
1817
this.router = new Router({adapter, env: sinon.stub()});
@@ -21,24 +20,25 @@ describe('help route', () => {
2120
this.router.registerRoute('help', helpRoute);
2221
});
2322

24-
afterEach(function () {
25-
this.sandbox.restore();
23+
afterEach(() => {
2624
esmocha.clearAllMocks();
2725
});
2826

2927
it('allow returning home', async function () {
30-
this.sandbox.stub(adapter, 'prompt').returns(Promise.resolve({whereTo: 'home'}));
31-
await this.router.navigate('help').then(() => {
32-
sinon.assert.calledOnce(this.homeRoute);
33-
});
28+
adapter.addAnswers({whereTo: 'home'});
29+
30+
await this.router.navigate('help');
31+
32+
sinon.assert.calledOnce(this.homeRoute);
3433
});
3534

3635
it('open urls in browsers', async function () {
3736
const url = 'http://yeoman.io';
38-
this.sandbox.stub(adapter, 'prompt').returns(Promise.resolve({whereTo: url}));
39-
await this.router.navigate('help').then(() => {
40-
expect(open).toHaveBeenCalledTimes(1);
41-
expect(open).toHaveBeenCalledWith(url);
42-
});
37+
adapter.addAnswers({whereTo: url});
38+
39+
await this.router.navigate('help');
40+
41+
expect(open).toHaveBeenCalledTimes(1);
42+
expect(open).toHaveBeenCalledWith(url);
4343
});
4444
});

test/route-home.js

Lines changed: 30 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ describe('home route', () => {
1111
let adapter;
1212

1313
beforeEach(async function () {
14-
this.sandbox = sinon.createSandbox();
1514
this.env = await fakeEnv();
1615
adapter = new TestAdapter();
1716
this.router = new Router({adapter, env: this.env});
@@ -26,32 +25,29 @@ describe('home route', () => {
2625
this.router.registerRoute('update', this.updateRoute);
2726
});
2827

29-
afterEach(function () {
30-
this.sandbox.restore();
31-
});
32-
3328
it('allow going to help', async function () {
34-
this.sandbox.stub(adapter, 'prompt').returns(Promise.resolve({whatNext: 'help'}));
35-
await this.router.navigate('home').then(() => {
36-
sinon.assert.calledOnce(this.helpRoute);
37-
});
29+
adapter.addAnswers({whatNext: 'help'});
30+
31+
await this.router.navigate('home');
32+
33+
sinon.assert.calledOnce(this.helpRoute);
3834
});
3935

4036
it('allow going to install', async function () {
41-
this.sandbox.stub(adapter, 'prompt').returns(Promise.resolve({whatNext: 'install'}));
42-
await this.router.navigate('home').then(() => {
43-
sinon.assert.calledOnce(this.installRoute);
44-
});
37+
adapter.addAnswers({whatNext: 'install'});
38+
39+
await this.router.navigate('home');
40+
41+
sinon.assert.calledOnce(this.installRoute);
4542
});
4643

4744
it('does not display update options if no generators is installed', async function () {
4845
this.router.generator = [];
49-
this.sandbox.stub(adapter, 'prompt').callsFake(prompts => {
50-
assert.strictEqual(_.map(prompts[0].choices, 'value').includes('update'), false);
51-
return Promise.resolve({whatNext: 'exit'});
52-
});
46+
adapter.addAnswers({whatNext: 'exit'});
5347

5448
await this.router.navigate('home');
49+
50+
assert.strictEqual(_.map(adapter.calls[0].question.choices, 'value').includes('update'), false);
5551
});
5652

5753
it('show update menu option if there is installed generators', async function () {
@@ -62,14 +58,12 @@ describe('home route', () => {
6258
updateAvailable: false,
6359
}];
6460

65-
this.sandbox.stub(adapter, 'prompt').callsFake(prompts => {
66-
assert(_.map(prompts[0].choices, 'value').includes('update'));
67-
return Promise.resolve({whatNext: 'update'});
68-
});
61+
adapter.addAnswers({whatNext: 'update'});
6962

70-
await this.router.navigate('home').then(() => {
71-
sinon.assert.calledOnce(this.updateRoute);
72-
});
63+
await this.router.navigate('home');
64+
65+
assert(_.map(adapter.calls[0].question.choices, 'value').includes('update'));
66+
sinon.assert.calledOnce(this.updateRoute);
7367
});
7468

7569
it('list runnable generators', async function () {
@@ -80,19 +74,17 @@ describe('home route', () => {
8074
updateAvailable: false,
8175
}];
8276

83-
this.sandbox.stub(adapter, 'prompt').callsFake(prompts => {
84-
assert.strictEqual(prompts[0].choices[1].value.generator, 'unicorn:app');
85-
return Promise.resolve({
86-
whatNext: {
87-
method: 'run',
88-
generator: 'unicorn:app',
89-
},
90-
});
77+
adapter.addAnswers({
78+
whatNext: {
79+
method: 'run',
80+
generator: 'unicorn:app',
81+
},
9182
});
9283

93-
await this.router.navigate('home').then(() => {
94-
sinon.assert.calledWith(this.runRoute, this.router, 'unicorn:app');
95-
});
84+
await this.router.navigate('home');
85+
86+
assert.strictEqual(adapter.calls[0].question.choices[1].value.generator, 'unicorn:app');
87+
sinon.assert.calledWith(this.runRoute, this.router, 'unicorn:app');
9688
});
9789

9890
it('show update available message behind generator name', async function () {
@@ -103,11 +95,10 @@ describe('home route', () => {
10395
updateAvailable: true,
10496
}];
10597

106-
this.sandbox.stub(adapter, 'prompt').callsFake(prompts => {
107-
assert(prompts[0].choices[1].name.includes('♥ Update Available!'));
108-
return Promise.resolve({whatNext: 'exit'});
109-
});
98+
adapter.addAnswers({whatNext: 'exit'});
11099

111100
await this.router.navigate('home');
101+
102+
assert(adapter.calls[0].question.choices[1].name.includes('♥ Update Available!'));
112103
});
113104
});

0 commit comments

Comments
 (0)