Skip to content

Commit b95946a

Browse files
committed
Add instance-initializer, instance-initializer-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.
1 parent d488153 commit b95946a

File tree

12 files changed

+561
-0
lines changed

12 files changed

+561
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const stringUtils = require('ember-cli-string-utils');
4+
5+
const useTestFrameworkDetector = require('../test-framework-detector');
6+
7+
module.exports = useTestFrameworkDetector({
8+
description: 'Generates an instance initializer unit test.',
9+
locals: function(options) {
10+
return {
11+
friendlyTestName: ['Unit', 'Instance Initializer', options.entity.name].join(' | '),
12+
dasherizedModulePrefix: stringUtils.dasherize(options.project.config().modulePrefix)
13+
};
14+
}
15+
});
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { expect } from 'chai';
2+
import { describe, it, beforeEach } from 'mocha';
3+
import Application from '@ember/application';
4+
import { run } from '@ember/runloop';
5+
import { initialize } from '<%= dasherizedModulePrefix %>/instance-initializers/<%= dasherizedModuleName %>';
6+
import destroyApp from '../../helpers/destroy-app';
7+
8+
describe('<%= friendlyTestName %>', function() {
9+
let application, appInstance;
10+
11+
beforeEach(function() {
12+
run(function() {
13+
application = Application.create();
14+
appInstance = application.buildInstance();
15+
});
16+
});
17+
18+
afterEach(function() {
19+
run(appInstance, 'destroy');
20+
destroyApp(application);
21+
});
22+
23+
// Replace this with your real tests.
24+
it('works', function() {
25+
initialize(appInstance);
26+
27+
// you would normally confirm the results of the initializer here
28+
expect(true).to.be.ok;
29+
});
30+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Application from '@ember/application';
2+
import { run } from '@ember/runloop';
3+
import { initialize } from '<%= dasherizedModulePrefix %>/instance-initializers/<%= dasherizedModuleName %>';
4+
import { module, test } from 'qunit';
5+
import destroyApp from '../../helpers/destroy-app';
6+
7+
module('<%= friendlyTestName %>', {
8+
beforeEach() {
9+
run(() => {
10+
this.application = Application.create();
11+
this.appInstance = this.application.buildInstance();
12+
});
13+
},
14+
afterEach() {
15+
run(this.appInstance, 'destroy');
16+
destroyApp(this.application);
17+
}
18+
});
19+
20+
// Replace this with your real tests.
21+
test('it works', function(assert) {
22+
initialize(this.appInstance);
23+
24+
// you would normally confirm the results of the initializer here
25+
assert.ok(true);
26+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Application from '@ember/application';
2+
3+
import { initialize } from '<%= dasherizedModulePrefix %>/instance-initializers/<%= dasherizedModuleName %>';
4+
import { module, test } from 'qunit';
5+
import { setupTest } from 'ember-qunit';
6+
import destroyApp from '../../helpers/destroy-app';
7+
8+
module('<%= friendlyTestName %>', function(hooks) {
9+
setupTest(hooks);
10+
11+
hooks.beforeEach(function() {
12+
this.TestApplication = Application.extend();
13+
this.TestApplication.instanceInitializer({
14+
name: 'initializer under test',
15+
initialize
16+
});
17+
this.application = this.TestApplication.create({ autoboot: false });
18+
this.instance = this.application.buildInstance();
19+
});
20+
hooks.afterEach(function() {
21+
destroyApp(this.application);
22+
destroyApp(this.instance);
23+
});
24+
25+
// Replace this with your real tests.
26+
test('it works', async function(assert) {
27+
await this.instance.boot();
28+
29+
assert.ok(true);
30+
});
31+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function initialize(/* appInstance */) {
2+
// appInstance.inject('route', 'foo', 'service:foo');
3+
}
4+
5+
export default {
6+
initialize
7+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = {
4+
description: 'Generates an instance initializer.'
5+
};
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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: instance-initializer-test', function() {
16+
setupTestHooks(this);
17+
18+
describe('in app', function() {
19+
beforeEach(function() {
20+
return emberNew();
21+
});
22+
23+
it('instance-initializer-test foo', function() {
24+
return emberGenerateDestroy(['instance-initializer-test', 'foo'], _file => {
25+
expect(_file('tests/unit/instance-initializers/foo-test.ts')).to.equal(
26+
fixture('instance-initializer-test/default.ts')
27+
);
28+
});
29+
});
30+
31+
describe('with ember-cli-qunit@4.2.0', function() {
32+
beforeEach(function() {
33+
generateFakePackageManifest('ember-cli-qunit', '4.2.0');
34+
});
35+
36+
it('instance-initializer-test foo', function() {
37+
return emberGenerateDestroy(['instance-initializer-test', 'foo'], _file => {
38+
expect(_file('tests/unit/instance-initializers/foo-test.ts')).to.equal(
39+
fixture('instance-initializer-test/rfc232.ts')
40+
);
41+
});
42+
});
43+
});
44+
45+
describe('with ember-cli-mocha', function() {
46+
beforeEach(function() {
47+
modifyPackages([
48+
{ name: 'ember-cli-qunit', delete: true },
49+
{ name: 'ember-cli-mocha', dev: true },
50+
]);
51+
});
52+
53+
it('instance-initializer-test foo for mocha', function() {
54+
return emberGenerateDestroy(['instance-initializer-test', 'foo'], _file => {
55+
expect(_file('tests/unit/instance-initializers/foo-test.ts')).to.equal(
56+
fixture('instance-initializer-test/mocha.ts')
57+
);
58+
});
59+
});
60+
});
61+
});
62+
63+
describe('in addon', function() {
64+
beforeEach(function() {
65+
return emberNew({ target: 'addon' });
66+
});
67+
68+
it('instance-initializer-test foo', function() {
69+
return emberGenerateDestroy(['instance-initializer-test', 'foo'], _file => {
70+
expect(_file('tests/unit/instance-initializers/foo-test.ts')).to.equal(
71+
fixture('instance-initializer-test/dummy.ts')
72+
);
73+
});
74+
});
75+
});
76+
});

0 commit comments

Comments
 (0)