Skip to content

Commit 416c1fc

Browse files
committed
Add component-test generator.
1 parent ea24362 commit 416c1fc

File tree

15 files changed

+575
-0
lines changed

15 files changed

+575
-0
lines changed

.vscode/settings.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"files.exclude": {
3+
"**/.git": true,
4+
"**/.svn": true,
5+
"**/.hg": true,
6+
"**/CVS": true,
7+
"**/.DS_Store": true,
8+
"node_modules": true,
9+
"dist": true,
10+
"tmp": true,
11+
"integrated-node-tests": true
12+
}
13+
}

blueprints/component-test/index.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const stringUtil = require('ember-cli-string-utils');
5+
const isPackageMissing = require('ember-cli-is-package-missing');
6+
const getPathOption = require('ember-cli-get-component-path-option');
7+
8+
const useTestFrameworkDetector = require('../test-framework-detector');
9+
10+
module.exports = useTestFrameworkDetector({
11+
description: 'Generates a component integration or unit test.',
12+
13+
availableOptions: [
14+
{
15+
name: 'test-type',
16+
type: ['integration', 'unit'],
17+
default: 'integration',
18+
aliases: [
19+
{ 'i': 'integration' },
20+
{ 'u': 'unit' },
21+
{ 'integration': 'integration' },
22+
{ 'unit': 'unit' }
23+
]
24+
}
25+
],
26+
27+
fileMapTokens: function() {
28+
return {
29+
__testType__: function(options) {
30+
return options.locals.testType || 'integration';
31+
},
32+
__path__: function(options) {
33+
if (options.pod) {
34+
return path.join(options.podPath, options.locals.path, options.dasherizedModuleName);
35+
}
36+
return 'components';
37+
}
38+
};
39+
},
40+
locals: function(options) {
41+
let dasherizedModuleName = stringUtil.dasherize(options.entity.name);
42+
let componentPathName = dasherizedModuleName;
43+
let testType = options.testType || 'integration';
44+
45+
let friendlyTestDescription = [
46+
testType === 'unit' ? 'Unit' : 'Integration',
47+
'Component',
48+
dasherizedModuleName,
49+
].join(' | ');
50+
51+
if (options.pod && options.path !== 'components' && options.path !== '') {
52+
componentPathName = [options.path, dasherizedModuleName].filter(Boolean).join('/');
53+
}
54+
55+
return {
56+
path: getPathOption(options),
57+
testType: testType,
58+
componentPathName: componentPathName,
59+
friendlyTestDescription: friendlyTestDescription
60+
};
61+
},
62+
afterInstall: function(options) {
63+
if (!options.dryRun && options.testType === 'integration' && isPackageMissing(this, 'ember-cli-htmlbars-inline-precompile')) {
64+
return this.addPackagesToProject([
65+
{ name: 'ember-cli-htmlbars-inline-precompile', target: '^0.3.1' }
66+
]);
67+
}
68+
}
69+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { expect } from 'chai';
2+
import { describe, it } from 'mocha';
3+
import { setupComponentTest } from 'ember-mocha';<% if (testType === 'integration') { %>
4+
import hbs from 'htmlbars-inline-precompile';<% } %>
5+
6+
describe('<%= friendlyTestDescription %>', function() {
7+
setupComponentTest('<%= componentPathName %>', {
8+
<% if (testType === 'integration' ) { %>integration: true<% } else if(testType === 'unit') { %>// Specify the other units that are required for this test
9+
// needs: ['component:foo', 'helper:bar'],
10+
unit: true<% } %>
11+
});
12+
13+
it('renders', function() {
14+
<% if (testType === 'integration' ) { %>// Set any properties with this.set('myProperty', 'value');
15+
// Handle any actions with this.on('myAction', function(val) { ... });
16+
// Template block usage:
17+
// this.render(hbs`
18+
// {{#<%= dasherizedModuleName %>}}
19+
// template content
20+
// {{/<%= dasherizedModuleName %>}}
21+
// `);
22+
23+
this.render(hbs`{{<%= dasherizedModuleName %>}}`);
24+
expect(this.$()).to.have.length(1);<% } else if(testType === 'unit') { %>// creates the component instance
25+
let component = this.subject();
26+
// renders the component on the page
27+
this.render();
28+
expect(component).to.be.ok;
29+
expect(this.$()).to.have.length(1);<% } %>
30+
});
31+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { expect } from 'chai';
2+
import { describeComponent, it } from 'ember-mocha';<% if (testType === 'integration') { %>
3+
import hbs from 'htmlbars-inline-precompile';<% } %>
4+
5+
describeComponent('<%= componentPathName %>', '<%= friendlyTestDescription %>',
6+
{
7+
<% if (testType === 'integration' ) { %>integration: true<% } else if(testType === 'unit') { %>// Specify the other units that are required for this test
8+
// needs: ['component:foo', 'helper:bar'],
9+
unit: true<% } %>
10+
},
11+
function() {
12+
it('renders', function() {
13+
<% if (testType === 'integration' ) { %>// Set any properties with this.set('myProperty', 'value');
14+
// Handle any actions with this.on('myAction', function(val) { ... });
15+
// Template block usage:
16+
// this.render(hbs`
17+
// {{#<%= dasherizedModuleName %>}}
18+
// template content
19+
// {{/<%= dasherizedModuleName %>}}
20+
// `);
21+
22+
this.render(hbs`{{<%= dasherizedModuleName %>}}`);
23+
expect(this.$()).to.have.length(1);<% } else if(testType === 'unit') { %>// creates the component instance
24+
let component = this.subject();
25+
// renders the component on the page
26+
this.render();
27+
expect(component).to.be.ok;
28+
expect(this.$()).to.have.length(1);<% } %>
29+
});
30+
}
31+
);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { moduleForComponent, test } from 'ember-qunit';<% if (testType === 'integration') { %>
2+
import hbs from 'htmlbars-inline-precompile';<% } %>
3+
4+
moduleForComponent('<%= componentPathName %>', '<%= friendlyTestDescription %>', {
5+
<% if (testType === 'integration' ) { %>integration: true<% } else if(testType === 'unit') { %>// Specify the other units that are required for this test
6+
// needs: ['component:foo', 'helper:bar'],
7+
unit: true<% } %>
8+
});
9+
10+
test('it renders', function(assert) {
11+
<% if (testType === 'integration' ) { %>// Set any properties with this.set('myProperty', 'value');
12+
// Handle any actions with this.on('myAction', function(val) { ... });
13+
14+
this.render(hbs`{{<%= componentPathName %>}}`);
15+
16+
assert.equal(this.$().text().trim(), '');
17+
18+
// Template block usage:
19+
this.render(hbs`
20+
{{#<%= componentPathName %>}}
21+
template block text
22+
{{/<%= componentPathName %>}}
23+
`);
24+
25+
assert.equal(this.$().text().trim(), 'template block text');<% } else if(testType === 'unit') { %>
26+
// Creates the component instance
27+
/*let component =*/ this.subject();
28+
// Renders the component to the page
29+
this.render();
30+
assert.equal(this.$().text().trim(), '');<% } %>
31+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<% if (testType === 'integration') { %>import { module, test } from 'qunit';
2+
import { setupRenderingTest } from 'ember-qunit';
3+
import { render } from '@ember/test-helpers';
4+
import hbs from 'htmlbars-inline-precompile';
5+
6+
module('<%= friendlyTestDescription %>', function(hooks) {
7+
setupRenderingTest(hooks);
8+
9+
test('it renders', async function(assert) {
10+
// Set any properties with this.set('myProperty', 'value');
11+
// Handle any actions with this.set('myAction', function(val) { ... });
12+
13+
await render(hbs`{{<%= componentPathName %>}}`);
14+
15+
assert.equal(this.element.textContent.trim(), '');
16+
17+
// Template block usage:
18+
await render(hbs`
19+
{{#<%= componentPathName %>}}
20+
template block text
21+
{{/<%= componentPathName %>}}
22+
`);
23+
24+
assert.equal(this.element.textContent.trim(), 'template block text');
25+
});
26+
});<% } else if (testType === 'unit') { %>import { module, test } from 'qunit';
27+
import { setupTest } from 'ember-qunit';
28+
29+
module('<%= friendlyTestDescription %>', function(hooks) {
30+
setupTest(hooks);
31+
32+
test('it exists', function(assert) {
33+
let component = this.owner.factoryFor('component:<%= componentPathName %>').create();
34+
assert.ok(component);
35+
});
36+
});<% } %>

0 commit comments

Comments
 (0)