Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion blueprints/initializer-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,46 @@ const path = require('path');
const stringUtils = require('ember-cli-string-utils');

const useTestFrameworkDetector = require('../test-framework-detector');
const isModuleUnificationProject = require('../module-unification').isModuleUnificationProject;

module.exports = useTestFrameworkDetector({
description: 'Generates an initializer unit test.',

fileMapTokens: function() {
if (isModuleUnificationProject(this.project)) {
return {
__root__(options) {
if (options.pod) {
throw 'Pods arenʼt supported within a module unification app';
} else if (options.inDummy) {
return path.join('tests', 'dummy', 'src', 'init');
}
return path.join('src', 'init');
},
__testType__() {
return '';
},
};
} else {
return {
__root__() {
return 'tests';
},
__testType__() {
return 'unit';
},
};
}
},

locals: function(options) {
let modulePrefix = stringUtils.dasherize(options.project.config().modulePrefix);
if (isModuleUnificationProject(this.project)) {
modulePrefix += '/init';
}
return {
friendlyTestName: ['Unit', 'Initializer', options.entity.name].join(' | '),
dasherizedModulePrefix: stringUtils.dasherize(options.project.config().modulePrefix),
modulePrefix,
destroyAppExists: fs.existsSync(
path.join(this.project.root, '/tests/helpers/destroy-app.js')
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { expect } from 'chai';
import { describe, it, beforeEach, afterEach } from 'mocha';
import { run } from '@ember/runloop';
import Application from '@ember/application';
import { initialize } from '<%= dasherizedModulePrefix %>/initializers/<%= dasherizedModuleName %>';
import { initialize } from '<%= modulePrefix %>/initializers/<%= dasherizedModuleName %>';
<% if (destroyAppExists) { %>import destroyApp from '../../helpers/destroy-app';<% } %>

describe('<%= friendlyTestName %>', function() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Application from '@ember/application';
import { run } from '@ember/runloop';

import { initialize } from '<%= dasherizedModulePrefix %>/initializers/<%= dasherizedModuleName %>';
import { initialize } from '<%= modulePrefix %>/initializers/<%= dasherizedModuleName %>';
import { module, test } from 'qunit';
<% if (destroyAppExists) { %>import destroyApp from '../../helpers/destroy-app';<% } %>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Application from '@ember/application';

import { initialize } from '<%= dasherizedModulePrefix %>/initializers/<%= dasherizedModuleName %>';
import { initialize } from '<%= modulePrefix %>/initializers/<%= dasherizedModuleName %>';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
<% if (destroyAppExists) { %>import destroyApp from '../../helpers/destroy-app';<% } else { %>import { run } from '@ember/runloop';<% } %>
Expand Down
18 changes: 18 additions & 0 deletions blueprints/initializer/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
'use strict';

const path = require('path');
const isModuleUnificationProject = require('../module-unification').isModuleUnificationProject;

module.exports = {
description: 'Generates an initializer.',

fileMapTokens() {
if (isModuleUnificationProject(this.project)) {
return {
__root__(options) {
if (options.pod) {
throw 'Pods arenʼt supported within a module unification app';
} else if (options.inDummy) {
return path.join('tests', 'dummy', 'src/init');
}
return 'src/init';
},
};
}
},
};
68 changes: 68 additions & 0 deletions node-tests/blueprints/initializer-test-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const expect = chai.expect;

const generateFakePackageManifest = require('../helpers/generate-fake-package-manifest');
const fixture = require('../helpers/fixture');
const fs = require('fs-extra');

describe('Blueprint: initializer-test', function() {
setupTestHooks(this);
Expand Down Expand Up @@ -73,4 +74,71 @@ describe('Blueprint: initializer-test', function() {
});
});
});

describe('in app – module unification', function() {
beforeEach(function() {
return emberNew().then(() => fs.ensureDirSync('src'));
});

it('initializer-test foo', function() {
return emberGenerateDestroy(['initializer-test', 'foo'], _file => {
expect(_file('src/init/initializers/foo-test.js')).to.equal(
fixture('initializer-test/module-unification/default.js')
);
});
});

describe('with ember-cli-qunit@4.2.0', function() {
beforeEach(function() {
generateFakePackageManifest('ember-cli-qunit', '4.2.0');
});

it('initializer-test foo', function() {
return emberGenerateDestroy(['initializer-test', 'foo'], _file => {
expect(_file('src/init/initializers/foo-test.js')).to.equal(
fixture('initializer-test/module-unification/rfc232.js')
);
});
});
});

describe('with ember-cli-mocha', function() {
beforeEach(function() {
modifyPackages([
{ name: 'ember-cli-qunit', delete: true },
{ name: 'ember-cli-mocha', dev: true },
]);
});

it('initializer-test foo', function() {
return emberGenerateDestroy(['initializer-test', 'foo'], _file => {
expect(_file('src/init/initializers/foo-test.js')).to.equal(
fixture('initializer-test/module-unification/mocha.js')
);
});
});
});
});

describe('in addon - module unification', function() {
beforeEach(function() {
return emberNew({ target: 'addon' }).then(() => fs.ensureDirSync('src'));
});

it('initializer-test foo', function() {
return emberGenerateDestroy(['initializer-test', 'foo'], _file => {
expect(_file('src/init/initializers/foo-test.js')).to.equal(
fixture('initializer-test/module-unification/dummy.js')
);
});
});

it('initializer-test foo --dummy', function() {
return emberGenerateDestroy(['initializer-test', 'foo', '--dummy'], _file => {
expect(_file('tests/dummy/src/init/initializers/foo-test.js')).to.equal(
fixture('initializer-test/module-unification/dummy.js')
);
});
});
});
});
156 changes: 155 additions & 1 deletion node-tests/blueprints/initializer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ const setupTestHooks = blueprintHelpers.setupTestHooks;
const emberNew = blueprintHelpers.emberNew;
const emberGenerateDestroy = blueprintHelpers.emberGenerateDestroy;
const setupPodConfig = blueprintHelpers.setupPodConfig;
const expectError = require('../helpers/expect-error');

const chai = require('ember-cli-blueprint-test-helpers/chai');
const expect = chai.expect;
const fs = require('fs-extra');

describe('Blueprint: initializer', function() {
setupTestHooks(this);
Expand Down Expand Up @@ -161,7 +163,7 @@ describe('Blueprint: initializer', function() {
});
});

it('initializer foo --dumy', function() {
it('initializer foo --dummy', function() {
return emberGenerateDestroy(['initializer', 'foo', '--dummy'], _file => {
expect(_file('tests/dummy/app/initializers/foo.js')).to.contain(
'export function initialize(/* application */) {\n' +
Expand Down Expand Up @@ -253,4 +255,156 @@ describe('Blueprint: initializer', function() {
});
});
});

describe('in app – module unification', function() {
beforeEach(function() {
return emberNew().then(() => fs.ensureDirSync('src'));
});

it('initializer foo', function() {
return emberGenerateDestroy(['initializer', 'foo'], _file => {
expect(_file('src/init/initializers/foo.js')).to.contain(
'export function initialize(/* application */) {\n' +
" // application.inject('route', 'foo', 'service:foo');\n" +
'}\n' +
'\n' +
'export default {\n' +
' initialize\n' +
'};'
);

expect(_file('src/init/initializers/foo-test.js')).to.contain(
"import { initialize } from 'my-app/init/initializers/foo';"
);
});
});

it('initializer foo/bar', function() {
return emberGenerateDestroy(['initializer', 'foo/bar'], _file => {
expect(_file('src/init/initializers/foo/bar.js')).to.contain(
'export function initialize(/* application */) {\n' +
" // application.inject('route', 'foo', 'service:foo');\n" +
'}\n' +
'\n' +
'export default {\n' +
' initialize\n' +
'};'
);

expect(_file('src/init/initializers/foo/bar-test.js')).to.contain(
"import { initialize } from 'my-app/init/initializers/foo/bar';"
);
});
});

it('initializer foo --pod', function() {
return expectError(
emberGenerateDestroy(['initializer', 'foo', '--pod']),
'Pods arenʼt supported within a module unification app'
);
});

it('initializer foo/bar --pod', function() {
return expectError(
emberGenerateDestroy(['initializer', 'foo/bar', '--pod']),
'Pods arenʼt supported within a module unification app'
);
});

describe('with podModulePrefix', function() {
beforeEach(function() {
setupPodConfig({ podModulePrefix: true });
});

it('initializer foo --pod', function() {
return expectError(
emberGenerateDestroy(['initializer', 'foo', '--pod']),
'Pods arenʼt supported within a module unification app'
);
});

it('initializer foo/bar --pod', function() {
return expectError(
emberGenerateDestroy(['initializer', 'foo/bar', '--pod']),
'Pods arenʼt supported within a module unification app'
);
});
});
});

describe('in addon – module unification', function() {
beforeEach(function() {
return emberNew({ target: 'addon' }).then(() => fs.ensureDirSync('src'));
});

it('initializer foo', function() {
return emberGenerateDestroy(['initializer', 'foo'], _file => {
expect(_file('src/init/initializers/foo.js')).to.contain(
'export function initialize(/* application */) {\n' +
" // application.inject('route', 'foo', 'service:foo');\n" +
'}\n' +
'\n' +
'export default {\n' +
' initialize\n' +
'};'
);

expect(_file('src/init/initializers/foo-test.js')).to.contain(
"import { initialize } from 'dummy/init/initializers/foo';"
);
});
});

it('initializer foo/bar', function() {
return emberGenerateDestroy(['initializer', 'foo/bar'], _file => {
expect(_file('src/init/initializers/foo/bar.js')).to.contain(
'export function initialize(/* application */) {\n' +
" // application.inject('route', 'foo', 'service:foo');\n" +
'}\n' +
'\n' +
'export default {\n' +
' initialize\n' +
'};'
);

expect(_file('src/init/initializers/foo/bar-test.js')).to.contain(
"import { initialize } from 'dummy/init/initializers/foo/bar';"
);
});
});

it('initializer foo --dummy', function() {
return emberGenerateDestroy(['initializer', 'foo', '--dummy'], _file => {
expect(_file('tests/dummy/src/init/initializers/foo.js')).to.contain(
'export function initialize(/* application */) {\n' +
" // application.inject('route', 'foo', 'service:foo');\n" +
'}\n' +
'\n' +
'export default {\n' +
' initialize\n' +
'};'
);

expect(_file('src/init/initializers/foo.js')).to.not.exist;
expect(_file('src/init/initializers/foo-test.js')).to.not.exist;
});
});

it('initializer foo/bar --dummy', function() {
return emberGenerateDestroy(['initializer', 'foo/bar', '--dummy'], _file => {
expect(_file('tests/dummy/src/init/initializers/foo/bar.js')).to.contain(
'export function initialize(/* application */) {\n' +
" // application.inject('route', 'foo', 'service:foo');\n" +
'}\n' +
'\n' +
'export default {\n' +
' initialize\n' +
'};'
);

expect(_file('src/init/initializers/foo/bar.js')).to.not.exist;
expect(_file('src/init/initializers/foo/bar-test.js')).to.not.exist;
});
});
});
});
26 changes: 26 additions & 0 deletions node-tests/fixtures/initializer-test/module-unification/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Application from '@ember/application';
import { run } from '@ember/runloop';

import { initialize } from 'my-app/init/initializers/foo';
Copy link
Contributor Author

@sduquej sduquej Jun 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the addition of init/ is the only new thing in these fixture files.

There's a possibility that they can get out of date if changes to the classic fixtures happened. While not ideal the risk seems minimum given how infrequent these type of changes are and duplicating these has the upside of easier clean-up and similarity to what was done in ember-cli

import { module, test } from 'qunit';


module('Unit | Initializer | foo', {
beforeEach() {
run(() => {
this.application = Application.create();
this.application.deferReadiness();
});
},
afterEach() {
run(this.application, 'destroy');
}
});

// Replace this with your real tests.
test('it works', function(assert) {
initialize(this.application);

// you would normally confirm the results of the initializer here
assert.ok(true);
});
Loading