From 36610075ad612784f6892441cc4fc1db1104002a Mon Sep 17 00:00:00 2001 From: Sebastian Duque Date: Fri, 8 Jun 2018 13:21:43 +0100 Subject: [PATCH] [Feature] Update initializer blueprint for module unification --- blueprints/initializer-test/index.js | 35 +++- .../__testType__/__path__}/__name__-test.js | 2 +- .../__testType__/__path__}/__name__-test.js | 2 +- .../__testType__/__path__}/__name__-test.js | 2 +- blueprints/initializer/index.js | 18 ++ .../blueprints/initializer-test-test.js | 68 ++++++++ node-tests/blueprints/initializer-test.js | 156 +++++++++++++++++- .../module-unification/default.js | 26 +++ .../module-unification/dummy.js | 26 +++ .../module-unification/mocha.js | 29 ++++ .../module-unification/rfc232.js | 31 ++++ 11 files changed, 390 insertions(+), 5 deletions(-) rename blueprints/initializer-test/mocha-files/{tests/unit/initializers => __root__/__testType__/__path__}/__name__-test.js (88%) rename blueprints/initializer-test/qunit-files/{tests/unit/initializers => __root__/__testType__/__path__}/__name__-test.js (87%) rename blueprints/initializer-test/qunit-rfc-232-files/{tests/unit/initializers => __root__/__testType__/__path__}/__name__-test.js (90%) create mode 100644 node-tests/fixtures/initializer-test/module-unification/default.js create mode 100644 node-tests/fixtures/initializer-test/module-unification/dummy.js create mode 100644 node-tests/fixtures/initializer-test/module-unification/mocha.js create mode 100644 node-tests/fixtures/initializer-test/module-unification/rfc232.js diff --git a/blueprints/initializer-test/index.js b/blueprints/initializer-test/index.js index d12730ad441..7fc95b6611b 100644 --- a/blueprints/initializer-test/index.js +++ b/blueprints/initializer-test/index.js @@ -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') ), diff --git a/blueprints/initializer-test/mocha-files/tests/unit/initializers/__name__-test.js b/blueprints/initializer-test/mocha-files/__root__/__testType__/__path__/__name__-test.js similarity index 88% rename from blueprints/initializer-test/mocha-files/tests/unit/initializers/__name__-test.js rename to blueprints/initializer-test/mocha-files/__root__/__testType__/__path__/__name__-test.js index 0f233516706..e6538b63373 100644 --- a/blueprints/initializer-test/mocha-files/tests/unit/initializers/__name__-test.js +++ b/blueprints/initializer-test/mocha-files/__root__/__testType__/__path__/__name__-test.js @@ -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() { diff --git a/blueprints/initializer-test/qunit-files/tests/unit/initializers/__name__-test.js b/blueprints/initializer-test/qunit-files/__root__/__testType__/__path__/__name__-test.js similarity index 87% rename from blueprints/initializer-test/qunit-files/tests/unit/initializers/__name__-test.js rename to blueprints/initializer-test/qunit-files/__root__/__testType__/__path__/__name__-test.js index bd7fab05b45..f427bcb1f25 100644 --- a/blueprints/initializer-test/qunit-files/tests/unit/initializers/__name__-test.js +++ b/blueprints/initializer-test/qunit-files/__root__/__testType__/__path__/__name__-test.js @@ -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';<% } %> diff --git a/blueprints/initializer-test/qunit-rfc-232-files/tests/unit/initializers/__name__-test.js b/blueprints/initializer-test/qunit-rfc-232-files/__root__/__testType__/__path__/__name__-test.js similarity index 90% rename from blueprints/initializer-test/qunit-rfc-232-files/tests/unit/initializers/__name__-test.js rename to blueprints/initializer-test/qunit-rfc-232-files/__root__/__testType__/__path__/__name__-test.js index 40609d2c3cd..8fae99adfc9 100644 --- a/blueprints/initializer-test/qunit-rfc-232-files/tests/unit/initializers/__name__-test.js +++ b/blueprints/initializer-test/qunit-rfc-232-files/__root__/__testType__/__path__/__name__-test.js @@ -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';<% } %> diff --git a/blueprints/initializer/index.js b/blueprints/initializer/index.js index 201732241cd..d2c81c49169 100644 --- a/blueprints/initializer/index.js +++ b/blueprints/initializer/index.js @@ -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'; + }, + }; + } + }, }; diff --git a/node-tests/blueprints/initializer-test-test.js b/node-tests/blueprints/initializer-test-test.js index e718fe2a267..d472fab8ffb 100644 --- a/node-tests/blueprints/initializer-test-test.js +++ b/node-tests/blueprints/initializer-test-test.js @@ -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); @@ -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') + ); + }); + }); + }); }); diff --git a/node-tests/blueprints/initializer-test.js b/node-tests/blueprints/initializer-test.js index 70c0170c66b..faa73d345b0 100644 --- a/node-tests/blueprints/initializer-test.js +++ b/node-tests/blueprints/initializer-test.js @@ -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); @@ -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' + @@ -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; + }); + }); + }); }); diff --git a/node-tests/fixtures/initializer-test/module-unification/default.js b/node-tests/fixtures/initializer-test/module-unification/default.js new file mode 100644 index 00000000000..e07637d084d --- /dev/null +++ b/node-tests/fixtures/initializer-test/module-unification/default.js @@ -0,0 +1,26 @@ +import Application from '@ember/application'; +import { run } from '@ember/runloop'; + +import { initialize } from 'my-app/init/initializers/foo'; +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); +}); diff --git a/node-tests/fixtures/initializer-test/module-unification/dummy.js b/node-tests/fixtures/initializer-test/module-unification/dummy.js new file mode 100644 index 00000000000..cb77e3343f8 --- /dev/null +++ b/node-tests/fixtures/initializer-test/module-unification/dummy.js @@ -0,0 +1,26 @@ +import Application from '@ember/application'; +import { run } from '@ember/runloop'; + +import { initialize } from 'dummy/init/initializers/foo'; +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); +}); diff --git a/node-tests/fixtures/initializer-test/module-unification/mocha.js b/node-tests/fixtures/initializer-test/module-unification/mocha.js new file mode 100644 index 00000000000..710cf179b2e --- /dev/null +++ b/node-tests/fixtures/initializer-test/module-unification/mocha.js @@ -0,0 +1,29 @@ +import { expect } from 'chai'; +import { describe, it, beforeEach, afterEach } from 'mocha'; +import { run } from '@ember/runloop'; +import Application from '@ember/application'; +import { initialize } from 'my-app/init/initializers/foo'; + + +describe('Unit | Initializer | foo', function() { + let application; + + beforeEach(function() { + run(function() { + application = Application.create(); + application.deferReadiness(); + }); + }); + + afterEach(function() { + run(application, 'destroy'); + }); + + // Replace this with your real tests. + it('works', function() { + initialize(application); + + // you would normally confirm the results of the initializer here + expect(true).to.be.ok; + }); +}); diff --git a/node-tests/fixtures/initializer-test/module-unification/rfc232.js b/node-tests/fixtures/initializer-test/module-unification/rfc232.js new file mode 100644 index 00000000000..53bb30f1a81 --- /dev/null +++ b/node-tests/fixtures/initializer-test/module-unification/rfc232.js @@ -0,0 +1,31 @@ +import Application from '@ember/application'; + +import { initialize } from 'my-app/init/initializers/foo'; +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; +import { run } from '@ember/runloop'; + +module('Unit | Initializer | foo', function(hooks) { + setupTest(hooks); + + hooks.beforeEach(function() { + this.TestApplication = Application.extend(); + this.TestApplication.initializer({ + name: 'initializer under test', + initialize + }); + + this.application = this.TestApplication.create({ autoboot: false }); + }); + + hooks.afterEach(function() { + run(this.application, 'destroy'); + }); + + // Replace this with your real tests. + test('it works', async function(assert) { + await this.application.boot(); + + assert.ok(true); + }); +});