Skip to content

Commit

Permalink
[Feature] utils blueprint for module unification
Browse files Browse the repository at this point in the history
  • Loading branch information
David authored and mixonic committed Apr 27, 2018
1 parent 24b56fa commit 979c4f5
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 0 deletions.
29 changes: 29 additions & 0 deletions blueprints/util-test/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
'use strict';

const stringUtils = require('ember-cli-string-utils');
const path = require('path');

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

module.exports = useTestFrameworkDetector({
description: 'Generates a util unit test.',

fileMapTokens() {
if (isModuleUnificationProject(this.project)) {
return {
__root__(options) {
if (options.pod) {
throw "Pods aren't supported within a module unification app";
}

return 'src';
},
__testType__() {
return 'utils';
},
};
} else {
return {
__root__() {
return 'tests';
},
__testType__() {
return path.join('unit', 'utils');
},
};
}
},

locals: function(options) {
return {
friendlyTestName: ['Unit', 'Utility', options.entity.name].join(' | '),
Expand Down
19 changes: 19 additions & 0 deletions blueprints/util/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
'use strict';

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

module.exports = {
description: 'Generates a simple utility module/function.',

fileMapTokens() {
if (isModuleUnificationProject(this.project)) {
return {
__root__(options) {
if (options.pod) {
throw "Pods aren't supported within a module unification app";
}

return 'src';
},
__testType__() {
return '';
},
};
}
},
};
52 changes: 52 additions & 0 deletions node-tests/blueprints/util-test-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const modifyPackages = blueprintHelpers.modifyPackages;

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

const generateFakePackageManifest = require('../helpers/generate-fake-package-manifest');
const fixture = require('../helpers/fixture');
Expand Down Expand Up @@ -56,6 +57,45 @@ describe('Blueprint: util-test', function() {
});
});

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

it('util-test foo-bar', function() {
return emberGenerateDestroy(['util-test', 'foo-bar'], _file => {
expect(_file('src/utils/foo-bar-test.js')).to.equal(fixture('util-test/default.js'));
});
});

describe('with [email protected]', function() {
beforeEach(function() {
generateFakePackageManifest('ember-cli-qunit', '4.2.0');
});

it('util-test foo-bar', function() {
return emberGenerateDestroy(['util-test', 'foo-bar'], _file => {
expect(_file('src/utils/foo-bar-test.js')).to.equal(fixture('util-test/rfc232.js'));
});
});
});

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

it('util-test foo-bar', function() {
return emberGenerateDestroy(['util-test', 'foo-bar'], _file => {
expect(_file('src/utils/foo-bar-test.js')).to.equal(fixture('util-test/mocha.js'));
});
});
});
});

describe('in addon', function() {
beforeEach(function() {
return emberNew({ target: 'addon' });
Expand All @@ -67,4 +107,16 @@ describe('Blueprint: util-test', function() {
});
});
});

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

it('util-test foo-bar', function() {
return emberGenerateDestroy(['util-test', 'foo-bar'], _file => {
expect(_file('src/utils/foo-bar-test.js')).to.equal(fixture('util-test/dummy.js'));
});
});
});
});
113 changes: 113 additions & 0 deletions node-tests/blueprints/util-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: util', function() {
setupTestHooks(this);
Expand Down Expand Up @@ -84,6 +86,43 @@ describe('Blueprint: util', function() {
});
});

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

it('util foo-bar', function() {
return emberGenerateDestroy(['util', 'foo-bar'], _file => {
expect(_file('src/utils/foo-bar.js')).to.contain(
'export default function fooBar() {\n' + ' return true;\n' + '}'
);

expect(_file('src/utils/foo-bar-test.js')).to.contain(
"import fooBar from 'my-app/utils/foo-bar';"
);
});
});

it('util foo-bar/baz', function() {
return emberGenerateDestroy(['util', 'foo/bar-baz'], _file => {
expect(_file('src/utils/foo/bar-baz.js')).to.contain(
'export default function fooBarBaz() {\n' + ' return true;\n' + '}'
);

expect(_file('src/utils/foo/bar-baz-test.js')).to.contain(
"import fooBarBaz from 'my-app/utils/foo/bar-baz';"
);
});
});

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

describe('in addon', function() {
beforeEach(function() {
return emberNew({ target: 'addon' });
Expand Down Expand Up @@ -121,4 +160,78 @@ describe('Blueprint: util', function() {
});
});
});

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

it('util foo-bar', function() {
return emberGenerateDestroy(['util', 'foo-bar'], _file => {
expect(_file('src/utils/foo-bar.js')).to.contain(
'export default function fooBar() {\n' + ' return true;\n' + '}'
);

expect(_file('src/utils/foo-bar-test.js')).to.contain(
"import fooBar from 'dummy/utils/foo-bar';"
);

expect(_file('app/utils/foo-bar.js')).to.not.exist;
});
});

it('util foo-bar/baz', function() {
return emberGenerateDestroy(['util', 'foo/bar-baz'], _file => {
expect(_file('src/utils/foo/bar-baz.js')).to.contain(
'export default function fooBarBaz() {\n' + ' return true;\n' + '}'
);

expect(_file('src/utils/foo/bar-baz-test.js')).to.contain(
"import fooBarBaz from 'dummy/utils/foo/bar-baz';"
);

expect(_file('app/utils/foo/bar-baz.js')).to.not.exist;
});
});
});

describe('in in-repo-addon', function() {
beforeEach(function() {
return emberNew({ target: 'in-repo-addon' });
});

it('util foo --in-repo-addon=my-addon', function() {
return emberGenerateDestroy(['util', 'foo', '--in-repo-addon=my-addon'], _file => {
expect(_file('lib/my-addon/addon/utils/foo.js')).to.contain(
'export default function foo() {\n' + ' return true;\n' + '}'
);

expect(_file('lib/my-addon/app/utils/foo.js')).to.contain(
"export { default } from 'my-addon/utils/foo';"
);

expect(_file('tests/unit/utils/foo-test.js'))
.to.contain("import foo from 'my-app/utils/foo';")
.to.contain("import { module, test } from 'qunit';")
.to.contain("module('Unit | Utility | foo');");
});
});

it('util foo/bar --in-repo-addon=my-addon', function() {
return emberGenerateDestroy(['util', 'foo/bar', '--in-repo-addon=my-addon'], _file => {
expect(_file('lib/my-addon/addon/utils/foo/bar.js')).to.contain(
'export default function fooBar() {\n' + ' return true;\n' + '}'
);

expect(_file('lib/my-addon/app/utils/foo/bar.js')).to.contain(
"export { default } from 'my-addon/utils/foo/bar';"
);

expect(_file('tests/unit/utils/foo/bar-test.js'))
.to.contain("import fooBar from 'my-app/utils/foo/bar';")
.to.contain("import { module, test } from 'qunit';")
.to.contain("module('Unit | Utility | foo/bar');");
});
});
});
});

0 comments on commit 979c4f5

Please sign in to comment.