Skip to content

Commit e437db9

Browse files
committed
Add route, route-test generator.
Note that we're intentionally skipping a couple edge-case-ish scenarios here, because they're sufficiently rare, and this resolution issue seems to be kind of buggy on the Ember CLI side, that it's not worth pursuing further at present. Also, we do not (and cannot) generate or update the `router.ts` file at present; we leave it as JavaScript. We'll need upstream changes to ember-cli to support that.
1 parent b95946a commit e437db9

File tree

14 files changed

+723
-709
lines changed

14 files changed

+723
-709
lines changed

blueprints/route-test/index.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const stringUtil = require('ember-cli-string-utils');
5+
const useTestFrameworkDetector = require('../test-framework-detector');
6+
7+
module.exports = useTestFrameworkDetector({
8+
description: 'Generates a route unit test.',
9+
10+
availableOptions: [
11+
{
12+
name: 'reset-namespace',
13+
type: Boolean
14+
}
15+
],
16+
17+
fileMapTokens: function() {
18+
return {
19+
__test__: function (options) {
20+
let moduleName = options.locals.moduleName;
21+
22+
if (options.pod) {
23+
moduleName = 'route';
24+
}
25+
26+
return `${moduleName}-test`;
27+
},
28+
__path__: function(options) {
29+
if (options.pod) {
30+
return path.join(options.podPath, options.locals.moduleName);
31+
}
32+
return 'routes';
33+
}
34+
};
35+
},
36+
37+
locals: function(options) {
38+
let moduleName = options.entity.name;
39+
40+
if (options.resetNamespace) {
41+
moduleName = moduleName.split('/').pop();
42+
}
43+
44+
return {
45+
friendlyTestDescription: ['Unit', 'Route', options.entity.name].join(' | '),
46+
moduleName: stringUtil.dasherize(moduleName)
47+
};
48+
},
49+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect } from 'chai';
2+
import { describe, it } from 'mocha';
3+
import { setupTest } from 'ember-mocha';
4+
5+
describe('<%= friendlyTestDescription %>', function() {
6+
setupTest('route:<%= moduleName %>', {
7+
// Specify the other units that are required for this test.
8+
// needs: ['controller:foo']
9+
});
10+
11+
it('exists', function() {
12+
let route = this.subject();
13+
expect(route).to.be.ok;
14+
});
15+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { expect } from 'chai';
2+
import { describeModule, it } from 'ember-mocha';
3+
4+
describeModule('route:<%= moduleName %>', '<%= friendlyTestDescription %>',
5+
{
6+
// Specify the other units that are required for this test.
7+
// needs: ['controller:foo']
8+
},
9+
function() {
10+
it('exists', function() {
11+
let route = this.subject();
12+
expect(route).to.be.ok;
13+
});
14+
}
15+
);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { moduleFor, test } from 'ember-qunit';
2+
3+
moduleFor('route:<%= moduleName %>', '<%= friendlyTestDescription %>', {
4+
// Specify the other units that are required for this test.
5+
// needs: ['controller:foo']
6+
});
7+
8+
test('it exists', function(assert) {
9+
let route = this.subject();
10+
assert.ok(route);
11+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { module, test } from 'qunit';
2+
import { setupTest } from 'ember-qunit';
3+
4+
module('<%= friendlyTestDescription %>', function(hooks) {
5+
setupTest(hooks);
6+
7+
test('it exists', function(assert) {
8+
let route = this.owner.lookup('route:<%= moduleName %>');
9+
assert.ok(route);
10+
});
11+
});

blueprints/route/index.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ module.exports = {
1313
{
1414
name: 'path',
1515
type: String,
16-
default: ''
16+
default: '',
1717
},
1818
{
1919
name: 'skip-router',
2020
type: Boolean,
21-
default: false
21+
default: false,
2222
},
2323
{
2424
name: 'reset-namespace',
25-
type: Boolean
26-
}
25+
type: Boolean,
26+
},
2727
],
2828

2929
fileMapTokens: function() {
3030
return {
31-
__name__: function (options) {
31+
__name__: function(options) {
3232
if (options.pod) {
3333
return 'route';
3434
}
@@ -66,7 +66,7 @@ module.exports = {
6666
}
6767

6868
return 'app';
69-
}
69+
},
7070
};
7171
},
7272

@@ -78,7 +78,7 @@ module.exports = {
7878
}
7979

8080
return {
81-
moduleName: stringUtil.dasherize(moduleName)
81+
moduleName: stringUtil.dasherize(moduleName),
8282
};
8383
},
8484

@@ -94,9 +94,15 @@ module.exports = {
9494
let entityTouchesRouter = this.shouldEntityTouchRouter(name);
9595
let isDummy = !!options.dummy;
9696
let isAddon = !!options.project.isEmberCLIAddon();
97-
let isAddonDummyOrApp = (isDummy === isAddon);
98-
99-
return (entityTouchesRouter && isAddonDummyOrApp && !options.dryRun && !options.inRepoAddon && !options.skipRouter);
97+
let isAddonDummyOrApp = isDummy === isAddon;
98+
99+
return (
100+
entityTouchesRouter &&
101+
isAddonDummyOrApp &&
102+
!options.dryRun &&
103+
!options.inRepoAddon &&
104+
!options.skipRouter
105+
);
100106
},
101107

102108
afterInstall: function(options) {
@@ -105,14 +111,14 @@ module.exports = {
105111

106112
afterUninstall: function(options) {
107113
updateRouter.call(this, 'remove', options);
108-
}
114+
},
109115
};
110116

111117
function updateRouter(action, options) {
112118
let entity = options.entity;
113119
let actionColorMap = {
114120
add: 'green',
115-
remove: 'red'
121+
remove: 'red',
116122
};
117123
let color = actionColorMap[action] || 'gray';
118124

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
'use strict';
2+
3+
const blueprintHelpers = require('ember-cli-blueprint-test-helpers/helpers');
4+
const setupTestHooks = blueprintHelpers.setupTestHooks;
5+
const emberNew = blueprintHelpers.emberNew;
6+
const emberGenerateDestroy = blueprintHelpers.emberGenerateDestroy;
7+
const modifyPackages = blueprintHelpers.modifyPackages;
8+
9+
const chai = require('ember-cli-blueprint-test-helpers/chai');
10+
const expect = chai.expect;
11+
12+
const generateFakePackageManifest = require('../helpers/generate-fake-package-manifest');
13+
const fixture = require('../helpers/fixture');
14+
15+
describe('Blueprint: route-test', function() {
16+
setupTestHooks(this);
17+
18+
describe('in app', function() {
19+
beforeEach(function() {
20+
return emberNew();
21+
});
22+
23+
it('route-test foo', function() {
24+
return emberGenerateDestroy(['route-test', 'foo'], _file => {
25+
expect(_file('tests/unit/routes/foo-test.ts')).to.equal(fixture('route-test/default.ts'));
26+
});
27+
});
28+
29+
describe('with ember-cli-qunit@4.2.0', function() {
30+
beforeEach(function() {
31+
generateFakePackageManifest('ember-cli-qunit', '4.2.0');
32+
});
33+
34+
it('route-test foo', function() {
35+
return emberGenerateDestroy(['route-test', 'foo'], _file => {
36+
expect(_file('tests/unit/routes/foo-test.ts')).to.equal(fixture('route-test/rfc232.ts'));
37+
});
38+
});
39+
});
40+
41+
describe('with ember-cli-mocha@0.11.0', function() {
42+
beforeEach(function() {
43+
modifyPackages([
44+
{ name: 'ember-cli-qunit', delete: true },
45+
{ name: 'ember-cli-mocha', dev: true },
46+
]);
47+
generateFakePackageManifest('ember-cli-mocha', '0.11.0');
48+
});
49+
50+
it('route-test foo', function() {
51+
return emberGenerateDestroy(['route-test', 'foo'], _file => {
52+
expect(_file('tests/unit/routes/foo-test.ts')).to.equal(fixture('route-test/mocha.ts'));
53+
});
54+
});
55+
});
56+
57+
describe('with ember-cli-mocha@0.12.0', function() {
58+
beforeEach(function() {
59+
modifyPackages([
60+
{ name: 'ember-cli-qunit', delete: true },
61+
{ name: 'ember-cli-mocha', dev: true },
62+
]);
63+
generateFakePackageManifest('ember-cli-mocha', '0.12.0');
64+
});
65+
66+
it('route-test foo', function() {
67+
return emberGenerateDestroy(['route-test', 'foo'], _file => {
68+
expect(_file('tests/unit/routes/foo-test.ts')).to.equal(
69+
fixture('route-test/mocha-0.12.ts')
70+
);
71+
});
72+
});
73+
});
74+
});
75+
76+
describe('in addon', function() {
77+
beforeEach(function() {
78+
return emberNew({ target: 'addon' });
79+
});
80+
81+
it('route-test foo', function() {
82+
return emberGenerateDestroy(['route-test', 'foo'], _file => {
83+
expect(_file('tests/unit/routes/foo-test.ts')).to.equal(fixture('route-test/default.ts'));
84+
});
85+
});
86+
});
87+
});

0 commit comments

Comments
 (0)