diff --git a/blueprints/controller-test/index.js b/blueprints/controller-test/index.js index edd7db3f429..bd0abc6be45 100644 --- a/blueprints/controller-test/index.js +++ b/blueprints/controller-test/index.js @@ -3,15 +3,54 @@ const stringUtil = require('ember-cli-string-utils'); const useTestFrameworkDetector = require('../test-framework-detector'); +const isModuleUnificationProject = require('../module-unification').isModuleUnificationProject; +const path = require('path'); module.exports = useTestFrameworkDetector({ description: 'Generates a controller unit test.', locals: function(options) { let dasherizedModuleName = stringUtil.dasherize(options.entity.name); let controllerPathName = dasherizedModuleName; + return { controllerPathName: controllerPathName, friendlyTestDescription: ['Unit', 'Controller', dasherizedModuleName].join(' | '), }; }, + fileMapTokens: function() { + if (isModuleUnificationProject(this.project)) { + return { + __test__() { + return 'controller-test'; + }, + __testType__() { + return ''; + }, + __root__(options) { + if (options.pod) { + throw "Pods aren't supported within a module unification app"; + } + return 'src'; + }, + __path__(options) { + return path.join('ui', 'routes', options.dasherizedModuleName); + }, + }; + } else { + return { + __root__() { + return 'tests'; + }, + __testType__() { + return 'unit'; + }, + __path__(options) { + if (options.pod) { + return path.join(options.podPath, options.dasherizedModuleName); + } + return 'controllers'; + }, + }; + } + }, }); diff --git a/blueprints/controller-test/mocha-0.12-files/tests/unit/__path__/__test__.js b/blueprints/controller-test/mocha-0.12-files/__root__/__testType__/__path__/__test__.js similarity index 100% rename from blueprints/controller-test/mocha-0.12-files/tests/unit/__path__/__test__.js rename to blueprints/controller-test/mocha-0.12-files/__root__/__testType__/__path__/__test__.js diff --git a/blueprints/controller-test/mocha-files/tests/unit/__path__/__test__.js b/blueprints/controller-test/mocha-files/__root__/__testType__/__path__/__test__.js similarity index 100% rename from blueprints/controller-test/mocha-files/tests/unit/__path__/__test__.js rename to blueprints/controller-test/mocha-files/__root__/__testType__/__path__/__test__.js diff --git a/blueprints/controller-test/qunit-files/tests/unit/__path__/__test__.js b/blueprints/controller-test/qunit-files/__root__/__testType__/__path__/__test__.js similarity index 100% rename from blueprints/controller-test/qunit-files/tests/unit/__path__/__test__.js rename to blueprints/controller-test/qunit-files/__root__/__testType__/__path__/__test__.js diff --git a/blueprints/controller-test/qunit-rfc-232-files/tests/unit/__path__/__test__.js b/blueprints/controller-test/qunit-rfc-232-files/__root__/__testType__/__path__/__test__.js similarity index 100% rename from blueprints/controller-test/qunit-rfc-232-files/tests/unit/__path__/__test__.js rename to blueprints/controller-test/qunit-rfc-232-files/__root__/__testType__/__path__/__test__.js diff --git a/blueprints/controller/index.js b/blueprints/controller/index.js index 68960e4b276..ddb1b7b1a43 100644 --- a/blueprints/controller/index.js +++ b/blueprints/controller/index.js @@ -1,5 +1,29 @@ 'use strict'; +const isModuleUnificationProject = require('../module-unification').isModuleUnificationProject; +const path = require('path'); + module.exports = { description: 'Generates a controller.', + fileMapTokens() { + if (isModuleUnificationProject(this.project)) { + return { + __root__(options) { + if (options.pod) { + throw "Pods aren't supported within a module unification app"; + } + if (options.inDummy) { + return path.join('tests', 'dummy', 'src'); + } + return 'src'; + }, + __path__(options) { + return path.join('ui', 'routes', options.dasherizedModuleName); + }, + __name__() { + return 'controller'; + }, + }; + } + }, }; diff --git a/blueprints/module-unification.js b/blueprints/module-unification.js index 90897cdc51b..aa7f4683ae7 100644 --- a/blueprints/module-unification.js +++ b/blueprints/module-unification.js @@ -2,6 +2,6 @@ module.exports = { isModuleUnificationProject(project) { - return project.isModuleUnification && project.isModuleUnification(); + return project && project.isModuleUnification && project.isModuleUnification(); }, }; diff --git a/node-tests/blueprints/controller-test-test.js b/node-tests/blueprints/controller-test-test.js index ad4f4c73f6b..b8adc2d2145 100644 --- a/node-tests/blueprints/controller-test-test.js +++ b/node-tests/blueprints/controller-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: controller-test', function() { setupTestHooks(this); @@ -28,6 +29,14 @@ describe('Blueprint: controller-test', function() { }); }); + it('controller-test foo/bar', function() { + return emberGenerateDestroy(['controller-test', 'foo/bar'], _file => { + expect(_file('tests/unit/controllers/foo/bar-test.js')).to.equal( + fixture('controller-test/default-nested.js') + ); + }); + }); + describe('with ember-cli-qunit@4.2.0', function() { beforeEach(function() { generateFakePackageManifest('ember-cli-qunit', '4.2.0'); @@ -40,6 +49,14 @@ describe('Blueprint: controller-test', function() { ); }); }); + + it('controller-test foo/bar', function() { + return emberGenerateDestroy(['controller-test', 'foo/bar'], _file => { + expect(_file('tests/unit/controllers/foo/bar-test.js')).to.equal( + fixture('controller-test/rfc232-nested.js') + ); + }); + }); }); describe('with ember-cli-mocha@0.11.0', function() { @@ -58,6 +75,14 @@ describe('Blueprint: controller-test', function() { ); }); }); + + it('controller-test foo/bar for mocha', function() { + return emberGenerateDestroy(['controller-test', 'foo/bar'], _file => { + expect(_file('tests/unit/controllers/foo/bar-test.js')).to.equal( + fixture('controller-test/mocha-nested.js') + ); + }); + }); }); describe('with ember-cli-mocha@0.12.0', function() { @@ -76,6 +101,110 @@ describe('Blueprint: controller-test', function() { ); }); }); + + it('controller-test foo/bar', function() { + return emberGenerateDestroy(['controller-test', 'foo/bar'], _file => { + expect(_file('tests/unit/controllers/foo/bar-test.js')).to.equal( + fixture('controller-test/mocha-0.12-nested.js') + ); + }); + }); + }); + }); + + describe('in app - module unification', function() { + beforeEach(function() { + return emberNew().then(() => fs.ensureDirSync('src')); + }); + + it('controller-test foo', function() { + return emberGenerateDestroy(['controller-test', 'foo'], _file => { + expect(_file('src/ui/routes/foo/controller-test.js')).to.equal( + fixture('controller-test/default.js') + ); + }); + }); + + it('controller-test foo/bar', function() { + return emberGenerateDestroy(['controller-test', 'foo/bar'], _file => { + expect(_file('src/ui/routes/foo/bar/controller-test.js')).to.equal( + fixture('controller-test/default-nested.js') + ); + }); + }); + + describe('with ember-cli-qunit@4.2.0', function() { + beforeEach(function() { + generateFakePackageManifest('ember-cli-qunit', '4.2.0'); + }); + + it('controller-test foo', function() { + return emberGenerateDestroy(['controller-test', 'foo'], _file => { + expect(_file('src/ui/routes/foo/controller-test.js')).to.equal( + fixture('controller-test/rfc232.js') + ); + }); + }); + + it('controller-test foo/bar', function() { + return emberGenerateDestroy(['controller-test', 'foo/bar'], _file => { + expect(_file('src/ui/routes/foo/bar/controller-test.js')).to.equal( + fixture('controller-test/rfc232-nested.js') + ); + }); + }); + }); + + describe('with ember-cli-mocha@0.11.0', function() { + beforeEach(function() { + modifyPackages([ + { name: 'ember-cli-qunit', delete: true }, + { name: 'ember-cli-mocha', dev: true }, + ]); + generateFakePackageManifest('ember-cli-mocha', '0.11.0'); + }); + + it('controller-test foo for mocha', function() { + return emberGenerateDestroy(['controller-test', 'foo'], _file => { + expect(_file('src/ui/routes/foo/controller-test.js')).to.equal( + fixture('controller-test/mocha.js') + ); + }); + }); + + it('controller-test foo/bar for mocha', function() { + return emberGenerateDestroy(['controller-test', 'foo/bar'], _file => { + expect(_file('src/ui/routes/foo/bar/controller-test.js')).to.equal( + fixture('controller-test/mocha-nested.js') + ); + }); + }); + }); + + describe('with ember-cli-mocha@0.12.0', function() { + beforeEach(function() { + modifyPackages([ + { name: 'ember-cli-qunit', delete: true }, + { name: 'ember-cli-mocha', dev: true }, + ]); + generateFakePackageManifest('ember-cli-mocha', '0.12.0'); + }); + + it('controller-test foo', function() { + return emberGenerateDestroy(['controller-test', 'foo'], _file => { + expect(_file('src/ui/routes/foo/controller-test.js')).to.equal( + fixture('controller-test/mocha-0.12.js') + ); + }); + }); + + it('controller-test foo/bar', function() { + return emberGenerateDestroy(['controller-test', 'foo/bar'], _file => { + expect(_file('src/ui/routes/foo/bar/controller-test.js')).to.equal( + fixture('controller-test/mocha-0.12-nested.js') + ); + }); + }); }); }); @@ -91,5 +220,35 @@ describe('Blueprint: controller-test', function() { ); }); }); + + it('controller-test foo/bar', function() { + return emberGenerateDestroy(['controller-test', 'foo/bar'], _file => { + expect(_file('tests/unit/controllers/foo/bar-test.js')).to.equal( + fixture('controller-test/default-nested.js') + ); + }); + }); + }); + + describe('in addon - module unification', function() { + beforeEach(function() { + return emberNew().then(() => fs.ensureDirSync('src')); + }); + + it('controller-test foo', function() { + return emberGenerateDestroy(['controller-test', 'foo'], _file => { + expect(_file('src/ui/routes/foo/controller-test.js')).to.equal( + fixture('controller-test/default.js') + ); + }); + }); + + it('controller-test foo/bar', function() { + return emberGenerateDestroy(['controller-test', 'foo/bar'], _file => { + expect(_file('src/ui/routes/foo/bar/controller-test.js')).to.equal( + fixture('controller-test/default-nested.js') + ); + }); + }); }); }); diff --git a/node-tests/blueprints/controller-test.js b/node-tests/blueprints/controller-test.js index cff787cabbc..a1613830f4d 100644 --- a/node-tests/blueprints/controller-test.js +++ b/node-tests/blueprints/controller-test.js @@ -6,7 +6,9 @@ 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 fs = require('fs-extra'); const expect = chai.expect; describe('Blueprint: controller', function() { @@ -158,6 +160,120 @@ describe('Blueprint: controller', function() { }); }); + describe('in app - module unification', function() { + beforeEach(function() { + return emberNew().then(() => fs.ensureDirSync('src')); + }); + + it('controller foo', function() { + return emberGenerateDestroy(['controller', 'foo'], _file => { + expect(_file('src/ui/routes/foo/controller.js')) + .to.contain("import Controller from '@ember/controller';") + .to.contain('export default Controller.extend({\n});'); + + expect(_file('src/ui/routes/foo/controller-test.js')) + .to.contain("import { moduleFor, test } from 'ember-qunit';") + .to.contain("moduleFor('controller:foo'"); + }); + }); + + it('controller foo/bar', function() { + return emberGenerateDestroy(['controller', 'foo/bar'], _file => { + expect(_file('src/ui/routes/foo/bar/controller.js')) + .to.contain("import Controller from '@ember/controller';") + .to.contain('export default Controller.extend({\n});'); + + expect(_file('src/ui/routes/foo/bar/controller-test.js')) + .to.contain("import { moduleFor, test } from 'ember-qunit';") + .to.contain("moduleFor('controller:foo/bar'"); + }); + }); + + describe('with podModulePrefix', function() { + beforeEach(function() { + setupPodConfig({ podModulePrefix: true }); + }); + + it('controller foo --pod podModulePrefix', function() { + return expectError( + emberGenerateDestroy(['controller', 'foo', '--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('controller foo', function() { + return emberGenerateDestroy(['controller', 'foo'], _file => { + expect(_file('src/ui/routes/foo/controller.js')) + .to.contain("import Controller from '@ember/controller';") + .to.contain('export default Controller.extend({\n});'); + + expect(_file('src/ui/routes/foo/controller-test.js')) + .to.contain("import { moduleFor, test } from 'ember-qunit';") + .to.contain("moduleFor('controller:foo'"); + + expect(_file('app/controllers/foo.js')).to.not.exist; + }); + }); + + it('controller foo/bar', function() { + return emberGenerateDestroy(['controller', 'foo/bar'], _file => { + expect(_file('src/ui/routes/foo/bar/controller.js')) + .to.contain("import Controller from '@ember/controller';") + .to.contain('export default Controller.extend({\n});'); + + expect(_file('src/ui/routes/foo/bar/controller-test.js')) + .to.contain("import { moduleFor, test } from 'ember-qunit';") + .to.contain("moduleFor('controller:foo/bar'"); + + expect(_file('app/controllers/foo/bar.js')).to.not.exist; + }); + }); + + it('controller foo --dummy', function() { + return emberGenerateDestroy(['controller', 'foo', '--dummy'], _file => { + expect(_file('tests/dummy/src/ui/routes/foo/controller.js')) + .to.contain("import Controller from '@ember/controller';") + .to.contain('export default Controller.extend({\n});'); + + expect(_file('src/ui/routes/foo/controller.js')).to.not.exist; + + expect(_file('src/ui/routes/foo/controller-test.js')).to.not.exist; + }); + }); + + it('controller foo/bar --dummy', function() { + return emberGenerateDestroy(['controller', 'foo/bar', '--dummy'], _file => { + expect(_file('tests/dummy/src/ui/routes/foo/bar/controller.js')) + .to.contain("import Controller from '@ember/controller';") + .to.contain('export default Controller.extend({\n});'); + + expect(_file('src/ui/routes/foo/bar/controller.js')).to.not.exist; + + expect(_file('src/ui/routes/foo/bar/controller-test.js')).to.not.exist; + }); + }); + + describe('with podModulePrefix', function() { + beforeEach(function() { + setupPodConfig({ podModulePrefix: true }); + }); + + it('controller foo --pod podModulePrefix', function() { + return expectError( + emberGenerateDestroy(['controller', 'foo', '--pod']), + "Pods aren't supported within a module unification app" + ); + }); + }); + }); + describe('in in-repo-addon', function() { beforeEach(function() { return emberNew({ target: 'in-repo-addon' }); diff --git a/node-tests/fixtures/controller-test/default-nested.js b/node-tests/fixtures/controller-test/default-nested.js new file mode 100644 index 00000000000..570f701ed50 --- /dev/null +++ b/node-tests/fixtures/controller-test/default-nested.js @@ -0,0 +1,12 @@ +import { moduleFor, test } from 'ember-qunit'; + +moduleFor('controller:foo/bar', 'Unit | Controller | foo/bar', { + // Specify the other units that are required for this test. + // needs: ['controller:foo'] +}); + +// Replace this with your real tests. +test('it exists', function(assert) { + let controller = this.subject(); + assert.ok(controller); +}); diff --git a/node-tests/fixtures/controller-test/mocha-0.12-nested.js b/node-tests/fixtures/controller-test/mocha-0.12-nested.js new file mode 100644 index 00000000000..fbd82bbdbc4 --- /dev/null +++ b/node-tests/fixtures/controller-test/mocha-0.12-nested.js @@ -0,0 +1,16 @@ +import { expect } from 'chai'; +import { describe, it } from 'mocha'; +import { setupTest } from 'ember-mocha'; + +describe('Unit | Controller | foo/bar', function() { + setupTest('controller:foo/bar', { + // Specify the other units that are required for this test. + // needs: ['controller:foo'] + }); + + // Replace this with your real tests. + it('exists', function() { + let controller = this.subject(); + expect(controller).to.be.ok; + }); +}); diff --git a/node-tests/fixtures/controller-test/mocha-nested.js b/node-tests/fixtures/controller-test/mocha-nested.js new file mode 100644 index 00000000000..9f96c6391bd --- /dev/null +++ b/node-tests/fixtures/controller-test/mocha-nested.js @@ -0,0 +1,16 @@ +import { expect } from 'chai'; +import { describeModule, it } from 'ember-mocha'; + +describeModule('controller:foo/bar', 'Unit | Controller | foo/bar', + { + // Specify the other units that are required for this test. + // needs: ['controller:foo'] + }, + function() { + // Replace this with your real tests. + it('exists', function() { + let controller = this.subject(); + expect(controller).to.be.ok; + }); + } +); diff --git a/node-tests/fixtures/controller-test/rfc232-nested.js b/node-tests/fixtures/controller-test/rfc232-nested.js new file mode 100644 index 00000000000..0efcb96232b --- /dev/null +++ b/node-tests/fixtures/controller-test/rfc232-nested.js @@ -0,0 +1,12 @@ +import { module, test } from 'qunit'; +import { setupTest } from 'ember-qunit'; + +module('Unit | Controller | foo/bar', function(hooks) { + setupTest(hooks); + + // Replace this with your real tests. + test('it exists', function(assert) { + let controller = this.owner.lookup('controller:foo/bar'); + assert.ok(controller); + }); +});