Skip to content

Commit 5c57a58

Browse files
committed
improve component blueprint for octane
1 parent 7f46c5c commit 5c57a58

File tree

11 files changed

+141
-73
lines changed

11 files changed

+141
-73
lines changed

blueprints/component-test/index.js

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33
const path = require('path');
44
const stringUtil = require('ember-cli-string-utils');
5+
const isPackageMissing = require('ember-cli-is-package-missing');
56
const getPathOption = require('ember-cli-get-component-path-option');
7+
const semver = require('semver');
68

79
const useTestFrameworkDetector = require('../test-framework-detector');
810

11+
function invocationFor(options) {
12+
let parts = options.entity.name.split('/');
13+
return parts.map((p) => stringUtil.classify(p)).join('::');
14+
}
15+
916
module.exports = useTestFrameworkDetector({
1017
description: 'Generates a component integration or unit test.',
1118

@@ -23,7 +30,7 @@ module.exports = useTestFrameworkDetector({
2330
},
2431
],
2532

26-
fileMapTokens: function() {
33+
fileMapTokens: function () {
2734
return {
2835
__root__() {
2936
return 'tests';
@@ -40,7 +47,7 @@ module.exports = useTestFrameworkDetector({
4047
};
4148
},
4249

43-
locals: function(options) {
50+
locals: function (options) {
4451
let dasherizedModuleName = stringUtil.dasherize(options.entity.name);
4552
let componentPathName = dasherizedModuleName;
4653
let testType = options.testType || 'integration';
@@ -55,11 +62,50 @@ module.exports = useTestFrameworkDetector({
5562
componentPathName = [options.path, dasherizedModuleName].filter(Boolean).join('/');
5663
}
5764

65+
let hbsImportStatement = this._useNamedHbsImport()
66+
? "import { hbs } from 'ember-cli-htmlbars';"
67+
: "import hbs from 'htmlbars-inline-precompile';";
68+
69+
let templateInvocation = invocationFor(options);
70+
let componentName = templateInvocation;
71+
let openComponent = (descriptor) => `<${descriptor}>`;
72+
let closeComponent = (descriptor) => `</${descriptor}>`;
73+
let selfCloseComponent = (descriptor) => `<${descriptor} />`;
74+
5875
return {
5976
path: getPathOption(options),
6077
testType: testType,
61-
componentPathName: componentPathName,
62-
friendlyTestDescription: friendlyTestDescription,
78+
componentName,
79+
componentPathName,
80+
templateInvocation,
81+
openComponent,
82+
closeComponent,
83+
selfCloseComponent,
84+
friendlyTestDescription,
85+
hbsImportStatement,
6386
};
64-
}
65-
});
87+
},
88+
89+
_useNamedHbsImport() {
90+
let htmlbarsAddon = this.project.addons.find((a) => a.name === 'ember-cli-htmlbars');
91+
92+
if (htmlbarsAddon && semver.gte(htmlbarsAddon.pkg.version, '4.0.0-alpha.1')) {
93+
return true;
94+
}
95+
96+
return false;
97+
},
98+
99+
afterInstall: function (options) {
100+
if (
101+
!options.dryRun &&
102+
options.testType === 'integration' &&
103+
!this._useNamedHbsImport() &&
104+
isPackageMissing(this, 'ember-cli-htmlbars-inline-precompile')
105+
) {
106+
return this.addPackagesToProject([
107+
{ name: 'ember-cli-htmlbars-inline-precompile', target: '^0.3.1' },
108+
]);
109+
}
110+
},
111+
});

blueprints/component-test/mocha-0.12-files/__root__/__testType__/__path__/__test__.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,22 @@ describe('<%= friendlyTestDescription %>', function() {
1313
it('renders', function() {
1414
<% if (testType === 'integration' ) { %>// Set any properties with this.set('myProperty', 'value');
1515
// Handle any actions with this.on('myAction', function(val) { ... });
16+
17+
this.render(hbs`<%= selfCloseComponent(componentName) %>`);
18+
expect(this.$()).to.have.length(1);
19+
1620
// Template block usage:
17-
// this.render(hbs`
18-
// {{#<%= dasherizedModuleName %>}}
19-
// template content
20-
// {{/<%= dasherizedModuleName %>}}
21-
// `);
21+
this.render(hbs`
22+
<%= openComponent(componentName) %>
23+
template block text
24+
<%= closeComponent(componentName) %>
25+
`);
2226

23-
this.render(hbs`{{<%= dasherizedModuleName %>}}`);
24-
expect(this.$()).to.have.length(1);<% } else if(testType === 'unit') { %>// creates the component instance
27+
expect(this.$().text().trim()).to.equal('template block text');<% } else if(testType === 'unit') { %>// creates the component instance
2528
let component = this.subject();
2629
// renders the component on the page
2730
this.render();
2831
expect(component).to.be.ok;
2932
expect(this.$()).to.have.length(1);<% } %>
3033
});
31-
});
34+
});

blueprints/component-test/mocha-files/__root__/__testType__/__path__/__test__.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@ describeComponent('<%= componentPathName %>', '<%= friendlyTestDescription %>',
1212
it('renders', function() {
1313
<% if (testType === 'integration' ) { %>// Set any properties with this.set('myProperty', 'value');
1414
// Handle any actions with this.on('myAction', function(val) { ... });
15+
16+
this.render(hbs`<%= selfCloseComponent(componentName) %>`);
17+
expect(this.$()).to.have.length(1);
18+
1519
// Template block usage:
16-
// this.render(hbs`
17-
// {{#<%= dasherizedModuleName %>}}
18-
// template content
19-
// {{/<%= dasherizedModuleName %>}}
20-
// `);
20+
this.render(hbs`
21+
<%= openComponent(componentName) %>
22+
template block text
23+
<%= closeComponent(componentName) %>
24+
`);
2125

22-
this.render(hbs`{{<%= dasherizedModuleName %>}}`);
23-
expect(this.$()).to.have.length(1);<% } else if(testType === 'unit') { %>// creates the component instance
26+
expect(this.$().text().trim()).to.equal('template block text');<% } else if(testType === 'unit') { %>// creates the component instance
2427
let component = this.subject();
2528
// renders the component on the page
2629
this.render();
2730
expect(component).to.be.ok;
2831
expect(this.$()).to.have.length(1);<% } %>
2932
});
3033
}
31-
);
34+
);

blueprints/component-test/mocha-rfc-232-files/__root__/__testType__/__path__/__test__.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { describe, it } from 'mocha';
33
import { setupRenderingTest } from 'ember-mocha';
44
import { render } from '@ember/test-helpers';
5-
import hbs from 'htmlbars-inline-precompile';
5+
<%= hbsImportStatement %>
66

77
describe('<%= friendlyTestDescription %>', function() {
88
setupRenderingTest();
@@ -11,15 +11,15 @@ describe('<%= friendlyTestDescription %>', function() {
1111
// Set any properties with this.set('myProperty', 'value');
1212
// Handle any actions with this.set('myAction', function(val) { ... });
1313

14-
await render(hbs`{{<%= componentPathName %>}}`);
14+
await render(hbs`<%= selfCloseComponent(componentName) %>`);
1515

1616
expect(this.element.textContent.trim()).to.equal('');
1717

1818
// Template block usage:
1919
await render(hbs`
20-
{{#<%= componentPathName %>}}
20+
<%= openComponent(componentName) %>
2121
template block text
22-
{{/<%= componentPathName %>}}
22+
<%= closeComponent(componentName) %>
2323
`);
2424

2525
expect(this.element.textContent.trim()).to.equal('template block text');
@@ -35,4 +35,4 @@ describe('<%= friendlyTestDescription %>', function() {
3535
let component = this.owner.factoryFor('component:<%= componentPathName %>').create();
3636
expect(component).to.be.ok;
3737
});
38-
});<% } %>
38+
});<% } %>
Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,37 @@
11
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-
});
2+
import hbs from 'htmlbars-inline-precompile';<% } %>
3+
import { TestContext } from 'ember-test-helpers';
4+
5+
type Context = TestContext & {
6+
<% if (testType === 'integration') { %>element: HTMLElement<% } %>
7+
// add your test properties here
8+
}
9+
10+
moduleForComponent('<%= componentPathName %>', '<%= friendlyTestDescription %>', {
11+
<% if (testType === 'integration' ) { %>integration: true<% } else if(testType === 'unit') { %>// Specify the other units that are required for this test
12+
// needs: ['component:foo', 'helper:bar'],
13+
unit: true<% } %>
14+
});
15+
16+
test('it renders', function(this: Context, assert: Assert) {
17+
<% if (testType === 'integration' ) { %>// Set any properties with this.set('myProperty', 'value');
18+
// Handle any actions with this.on('myAction', function(val) { ... });
19+
20+
this.render(hbs`<%= selfCloseComponent(componentName) %>`);
21+
22+
assert.equal(this.$().text().trim(), '');
23+
24+
// Template block usage:
25+
this.render(hbs`
26+
<%= openComponent(componentName) %>
27+
template block text
28+
<%= closeComponent(componentName) %>
29+
`);
30+
31+
assert.equal(this.$().text().trim(), 'template block text');<% } else if(testType === 'unit') { %>
32+
// Creates the component instance
33+
/*let component =*/ this.subject();
34+
// Renders the component to the page
35+
this.render();
36+
assert.equal(this.$().text().trim(), '');<% } %>
37+
});
Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
<% if (testType === 'integration') { %>import { module, test } from 'qunit';
22
import { setupRenderingTest } from 'ember-qunit';
3+
import { TestContext } from 'ember-test-helpers';
34
import { render } from '@ember/test-helpers';
4-
import hbs from 'htmlbars-inline-precompile';
5+
<%= hbsImportStatement %>
6+
7+
type Context = TestContext & {
8+
<% if (testType === 'integration') { %>element: HTMLElement<% } %>
9+
// add your test properties here
10+
}
511

612
module('<%= friendlyTestDescription %>', function(hooks) {
713
setupRenderingTest(hooks);
814

9-
test('it renders', async function(assert) {
15+
test('it renders', async function(this: Context, assert: Assert) {
1016
// Set any properties with this.set('myProperty', 'value');
1117
// Handle any actions with this.set('myAction', function(val) { ... });
1218

13-
await render(hbs`{{<%= componentPathName %>}}`);
19+
await render(hbs`<%= selfCloseComponent(componentName) %>`);
1420

1521
assert.equal(this.element.textContent.trim(), '');
1622

1723
// Template block usage:
1824
await render(hbs`
19-
{{#<%= componentPathName %>}}
25+
<%= openComponent(componentName) %>
2026
template block text
21-
{{/<%= componentPathName %>}}
27+
<%= closeComponent(componentName) %>
2228
`);
2329

2430
assert.equal(this.element.textContent.trim(), 'template block text');
2531
});
2632
});<% } else if (testType === 'unit') { %>import { module, test } from 'qunit';
2733
import { setupTest } from 'ember-qunit';
34+
import { TestContext } from 'ember-test-helpers';
35+
36+
type Context = TestContext & {
37+
// add your test properties here
38+
}
2839

2940
module('<%= friendlyTestDescription %>', function(hooks) {
3041
setupTest(hooks);
3142

32-
test('it exists', function(assert) {
43+
test('it exists', function(this: Context, assert: Assert) {
3344
let component = this.owner.factoryFor('component:<%= componentPathName %>').create();
3445
assert.ok(component);
3546
});
36-
});<% } %>
47+
});<% } %>
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import Component from '@ember/component';
22
<%= importTemplate %>
3-
export default class <%= classifiedModuleName %> extends Component.extend({
4-
// anything which *must* be merged to prototype here
5-
}) {<%= contents %>
6-
// normal class body definition here
3+
export default class <%= classifiedModuleName %> extends Component {
4+
// class body definition here
75
};

blueprints/component/glimmer-files/__root__/__path__/__name__.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ import Component from '@glimmer/component';
22

33
interface <%= classifiedModuleName %>Args {}
44

5-
export default class <%= classifiedModuleName %> extends Component<<%= classifiedModuleName %>Args> {}
5+
export default class <%= classifiedModuleName %> extends Component<<%= classifiedModuleName %>Args> {
6+
// class body definition here
7+
};
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import Component from '@ember/component';
22

3-
export default class <%= classifiedModuleName %> extends Component.extend({
4-
// anything which *must* be merged to prototype here
5-
}) {
6-
// normal class body definition here
3+
export default class <%= classifiedModuleName %> extends Component {
4+
// class body definition here
75
};

0 commit comments

Comments
 (0)