diff --git a/blueprints/model-test/index.js b/blueprints/model-test/index.js index d8e0f7f1092..2956ab7bb19 100644 --- a/blueprints/model-test/index.js +++ b/blueprints/model-test/index.js @@ -1,12 +1,40 @@ -var ModelBlueprint = require('../model'); -var testInfo = require('ember-cli-test-info'); -var useTestFrameworkDetector = require('../test-framework-detector'); +const ModelBlueprint = require('../model'); +const testInfo = require('ember-cli-test-info'); +const useTestFrameworkDetector = require('../test-framework-detector'); +const isModuleUnificationProject = require('../../lib/utilities/module-unification') + .isModuleUnificationProject; +const path = require('path'); module.exports = useTestFrameworkDetector({ description: 'Generates a model unit test.', - locals: function(options) { - var result = ModelBlueprint.locals.apply(this, arguments); + fileMapTokens(options) { + if (isModuleUnificationProject(this.project)) { + return { + __root__() { + return 'src'; + }, + __path__(options) { + return path.join('data', 'models', options.dasherizedModuleName); + }, + __test__() { + return 'model-test'; + }, + }; + } else { + return { + __root__() { + return 'tests'; + }, + __path__() { + return path.join('unit', 'models'); + }, + }; + } + }, + + locals(options) { + const result = ModelBlueprint.locals.apply(this, arguments); result.friendlyTestDescription = testInfo.description(options.entity.name, 'Unit', 'Model'); diff --git a/blueprints/model-test/mocha-files/tests/unit/__path__/__test__.js b/blueprints/model-test/mocha-files/__root__/__path__/__test__.js similarity index 100% rename from blueprints/model-test/mocha-files/tests/unit/__path__/__test__.js rename to blueprints/model-test/mocha-files/__root__/__path__/__test__.js diff --git a/blueprints/model-test/qunit-files/tests/unit/__path__/__test__.js b/blueprints/model-test/qunit-files/__root__/__path__/__test__.js similarity index 100% rename from blueprints/model-test/qunit-files/tests/unit/__path__/__test__.js rename to blueprints/model-test/qunit-files/__root__/__path__/__test__.js diff --git a/blueprints/model/index.js b/blueprints/model/index.js index 9ea731000dd..102322acdec 100644 --- a/blueprints/model/index.js +++ b/blueprints/model/index.js @@ -1,33 +1,52 @@ -var inflection = require('inflection'); -var stringUtils = require('ember-cli-string-utils'); -var EOL = require('os').EOL; +const inflection = require('inflection'); +const stringUtils = require('ember-cli-string-utils'); +const EOL = require('os').EOL; +const isModuleUnificationProject = require('../../lib/utilities/module-unification') + .isModuleUnificationProject; +const path = require('path'); module.exports = { description: 'Generates an ember-data model.', anonymousOptions: ['name', 'attr:type'], - locals: function(options) { - var attrs = []; - var needs = []; - var entityOptions = options.entity.options; + fileMapTokens(options) { + if (isModuleUnificationProject(this.project)) { + return { + __root__() { + return 'src'; + }, + __path__(options) { + return path.join('data', 'models', options.dasherizedModuleName); + }, + __name__() { + return 'model'; + }, + }; + } + }, + + locals(options) { + let attrs = []; + let needs = []; + let entityOptions = options.entity.options; - for (var name in entityOptions) { - var type = entityOptions[name] || ''; - var foreignModel = name; + for (let name in entityOptions) { + let type = entityOptions[name] || ''; + let foreignModel = name; if (type.indexOf(':') > -1) { foreignModel = type.split(':')[1]; type = type.split(':')[0]; } - var dasherizedName = stringUtils.dasherize(name); - var camelizedName = stringUtils.camelize(name); - var dasherizedType = stringUtils.dasherize(type); - var dasherizedForeignModel = stringUtils.dasherize(foreignModel); - var dasherizedForeignModelSingular = inflection.singularize(dasherizedForeignModel); + let dasherizedName = stringUtils.dasherize(name); + let camelizedName = stringUtils.camelize(name); + let dasherizedType = stringUtils.dasherize(type); + let dasherizedForeignModel = stringUtils.dasherize(foreignModel); + let dasherizedForeignModelSingular = inflection.singularize(dasherizedForeignModel); - var attr; + let attr; if (/has-many/.test(dasherizedType)) { - var camelizedNamePlural = inflection.pluralize(camelizedName); + let camelizedNamePlural = inflection.pluralize(camelizedName); attr = dsAttr(dasherizedForeignModelSingular, dasherizedType); attrs.push(camelizedNamePlural + ': ' + attr); } else if (/belongs-to/.test(dasherizedType)) { @@ -43,7 +62,7 @@ module.exports = { } } - var needsDeduplicated = needs.filter(function(need, i) { + let needsDeduplicated = needs.filter(function(need, i) { return needs.indexOf(need) === i; }); diff --git a/node-tests/blueprints/model-test.js b/node-tests/blueprints/model-test.js index 820411b1d27..cc480a79e78 100644 --- a/node-tests/blueprints/model-test.js +++ b/node-tests/blueprints/model-test.js @@ -15,120 +15,285 @@ const fixture = require('../helpers/fixture'); describe('Acceptance: generate and destroy model blueprints', function() { setupTestHooks(this); - beforeEach(function() { - return emberNew(); - }); + describe('classic', function() { + beforeEach(function() { + return emberNew(); + }); - it('model', function() { - let args = ['model', 'foo']; + it('model', function() { + let args = ['model', 'foo']; - return emberGenerateDestroy(args, _file => { - expect(_file('app/models/foo.js')) - .to.contain("import DS from 'ember-data';") - .to.contain('export default DS.Model.extend('); + return emberGenerateDestroy(args, _file => { + expect(_file('app/models/foo.js')) + .to.contain("import DS from 'ember-data';") + .to.contain('export default DS.Model.extend('); - expect(_file('tests/unit/models/foo-test.js')).to.equal(fixture('model-test/foo-default.js')); + expect(_file('tests/unit/models/foo-test.js')).to.equal( + fixture('model-test/foo-default.js') + ); + }); }); - }); - it('model with attrs', function() { - let args = [ - 'model', - 'foo', - 'misc', - 'skills:array', - 'isActive:boolean', - 'birthday:date', - 'someObject:object', - 'age:number', - 'name:string', - 'customAttr:custom-transform', - ]; - - return emberGenerateDestroy(args, _file => { - expect(_file('app/models/foo.js')) - .to.contain("import DS from 'ember-data';") - .to.contain('export default DS.Model.extend(') - .to.contain('misc: DS.attr()') - .to.contain("skills: DS.attr('array')") - .to.contain("isActive: DS.attr('boolean')") - .to.contain("birthday: DS.attr('date')") - .to.contain("someObject: DS.attr('object')") - .to.contain("age: DS.attr('number')") - .to.contain("name: DS.attr('string')") - .to.contain("customAttr: DS.attr('custom-transform')"); - - expect(_file('tests/unit/models/foo-test.js')).to.equal(fixture('model-test/foo-default.js')); + it('model with attrs', function() { + let args = [ + 'model', + 'foo', + 'misc', + 'skills:array', + 'isActive:boolean', + 'birthday:date', + 'someObject:object', + 'age:number', + 'name:string', + 'customAttr:custom-transform', + ]; + + return emberGenerateDestroy(args, _file => { + expect(_file('app/models/foo.js')) + .to.contain("import DS from 'ember-data';") + .to.contain('export default DS.Model.extend(') + .to.contain('misc: DS.attr()') + .to.contain("skills: DS.attr('array')") + .to.contain("isActive: DS.attr('boolean')") + .to.contain("birthday: DS.attr('date')") + .to.contain("someObject: DS.attr('object')") + .to.contain("age: DS.attr('number')") + .to.contain("name: DS.attr('string')") + .to.contain("customAttr: DS.attr('custom-transform')"); + + expect(_file('tests/unit/models/foo-test.js')).to.equal( + fixture('model-test/foo-default.js') + ); + }); }); - }); - it('model with belongsTo', function() { - let args = ['model', 'comment', 'post:belongs-to', 'author:belongs-to:user']; + it('model with belongsTo', function() { + let args = ['model', 'comment', 'post:belongs-to', 'author:belongs-to:user']; - return emberGenerateDestroy(args, _file => { - expect(_file('app/models/comment.js')) - .to.contain("import DS from 'ember-data';") - .to.contain('export default DS.Model.extend(') - .to.contain("post: DS.belongsTo('post')") - .to.contain("author: DS.belongsTo('user')"); + return emberGenerateDestroy(args, _file => { + expect(_file('app/models/comment.js')) + .to.contain("import DS from 'ember-data';") + .to.contain('export default DS.Model.extend(') + .to.contain("post: DS.belongsTo('post')") + .to.contain("author: DS.belongsTo('user')"); - expect(_file('tests/unit/models/comment-test.js')).to.equal( - fixture('model-test/comment-default.js') - ); + expect(_file('tests/unit/models/comment-test.js')).to.equal( + fixture('model-test/comment-default.js') + ); + }); }); - }); - it('model with hasMany', function() { - let args = ['model', 'post', 'comments:has-many', 'otherComments:has-many:comment']; + it('model with hasMany', function() { + let args = ['model', 'post', 'comments:has-many', 'otherComments:has-many:comment']; - return emberGenerateDestroy(args, _file => { - expect(_file('app/models/post.js')) - .to.contain("import DS from 'ember-data';") - .to.contain('export default DS.Model.extend(') - .to.contain("comments: DS.hasMany('comment')") - .to.contain("otherComments: DS.hasMany('comment')"); + return emberGenerateDestroy(args, _file => { + expect(_file('app/models/post.js')) + .to.contain("import DS from 'ember-data';") + .to.contain('export default DS.Model.extend(') + .to.contain("comments: DS.hasMany('comment')") + .to.contain("otherComments: DS.hasMany('comment')"); - expect(_file('tests/unit/models/post-test.js')).to.equal( - fixture('model-test/post-default.js') - ); + expect(_file('tests/unit/models/post-test.js')).to.equal( + fixture('model-test/post-default.js') + ); + }); }); - }); - it('model-test', function() { - let args = ['model-test', 'foo']; + it('model-test', function() { + let args = ['model-test', 'foo']; - return emberGenerateDestroy(args, _file => { - expect(_file('tests/unit/models/foo-test.js')).to.equal(fixture('model-test/foo-default.js')); + return emberGenerateDestroy(args, _file => { + expect(_file('tests/unit/models/foo-test.js')).to.equal( + fixture('model-test/foo-default.js') + ); + }); }); - }); - describe('model-test with ember-cli-qunit@4.2.0', function() { - beforeEach(function() { - generateFakePackageManifest('ember-cli-qunit', '4.2.0'); + describe('model-test with ember-cli-qunit@4.2.0', function() { + beforeEach(function() { + generateFakePackageManifest('ember-cli-qunit', '4.2.0'); + }); + + it('model-test-test foo', function() { + return emberGenerateDestroy(['model-test', 'foo'], _file => { + expect(_file('tests/unit/models/foo-test.js')).to.equal(fixture('model-test/rfc232.js')); + }); + }); }); - it('model-test-test foo', function() { - return emberGenerateDestroy(['model-test', 'foo'], _file => { - expect(_file('tests/unit/models/foo-test.js')).to.equal(fixture('model-test/rfc232.js')); + describe('with ember-cli-mocha v0.12+', function() { + beforeEach(function() { + modifyPackages([ + { name: 'ember-cli-qunit', delete: true }, + { name: 'ember-cli-mocha', dev: true }, + ]); + generateFakePackageManifest('ember-cli-mocha', '0.12.0'); + }); + + it('model-test for mocha v0.12+', function() { + let args = ['model-test', 'foo']; + + return emberGenerateDestroy(args, _file => { + expect(_file('tests/unit/models/foo-test.js')).to.equal( + fixture('model-test/foo-mocha-0.12.js') + ); + }); }); }); }); - describe('with ember-cli-mocha v0.12+', function() { + describe('module unification', function() { beforeEach(function() { - modifyPackages([ - { name: 'ember-cli-qunit', delete: true }, - { name: 'ember-cli-mocha', dev: true }, - ]); - generateFakePackageManifest('ember-cli-mocha', '0.12.0'); + return emberNew({ isModuleUnification: true }); + }); + + it('model', function() { + let args = ['model', 'foo']; + + return emberGenerateDestroy( + args, + _file => { + expect(_file('src/data/models/foo/model.js')) + .to.contain("import DS from 'ember-data';") + .to.contain('export default DS.Model.extend('); + + expect(_file('src/data/models/foo/model-test.js')).to.equal( + fixture('model-test/foo-default.js') + ); + }, + { isModuleUnification: true } + ); + }); + + it('model with attrs', function() { + let args = [ + 'model', + 'foo', + 'misc', + 'skills:array', + 'isActive:boolean', + 'birthday:date', + 'someObject:object', + 'age:number', + 'name:string', + 'customAttr:custom-transform', + ]; + + return emberGenerateDestroy( + args, + _file => { + expect(_file('src/data/models/foo/model.js')) + .to.contain("import DS from 'ember-data';") + .to.contain('export default DS.Model.extend(') + .to.contain('misc: DS.attr()') + .to.contain("skills: DS.attr('array')") + .to.contain("isActive: DS.attr('boolean')") + .to.contain("birthday: DS.attr('date')") + .to.contain("someObject: DS.attr('object')") + .to.contain("age: DS.attr('number')") + .to.contain("name: DS.attr('string')") + .to.contain("customAttr: DS.attr('custom-transform')"); + + expect(_file('src/data/models/foo/model-test.js')).to.equal( + fixture('model-test/foo-default.js') + ); + }, + { isModuleUnification: true } + ); + }); + + it('model with belongsTo', function() { + let args = ['model', 'comment', 'post:belongs-to', 'author:belongs-to:user']; + + return emberGenerateDestroy( + args, + _file => { + expect(_file('src/data/models/comment/model.js')) + .to.contain("import DS from 'ember-data';") + .to.contain('export default DS.Model.extend(') + .to.contain("post: DS.belongsTo('post')") + .to.contain("author: DS.belongsTo('user')"); + + expect(_file('src/data/models/comment/model-test.js')).to.equal( + fixture('model-test/comment-default.js') + ); + }, + { isModuleUnification: true } + ); + }); + + it('model with hasMany', function() { + let args = ['model', 'post', 'comments:has-many', 'otherComments:has-many:comment']; + + return emberGenerateDestroy( + args, + _file => { + expect(_file('src/data/models/post/model.js')) + .to.contain("import DS from 'ember-data';") + .to.contain('export default DS.Model.extend(') + .to.contain("comments: DS.hasMany('comment')") + .to.contain("otherComments: DS.hasMany('comment')"); + + expect(_file('src/data/models/post/model-test.js')).to.equal( + fixture('model-test/post-default.js') + ); + }, + { isModuleUnification: true } + ); }); - it('model-test for mocha v0.12+', function() { + it('model-test', function() { let args = ['model-test', 'foo']; - return emberGenerateDestroy(args, _file => { - expect(_file('tests/unit/models/foo-test.js')).to.equal( - fixture('model-test/foo-mocha-0.12.js') + return emberGenerateDestroy( + args, + _file => { + expect(_file('src/data/models/foo/model-test.js')).to.equal( + fixture('model-test/foo-default.js') + ); + }, + { isModuleUnification: true } + ); + }); + + describe('model-test with ember-cli-qunit@4.2.0', function() { + beforeEach(function() { + generateFakePackageManifest('ember-cli-qunit', '4.2.0'); + }); + + it('model-test-test foo', function() { + return emberGenerateDestroy( + ['model-test', 'foo'], + _file => { + expect(_file('src/data/models/foo/model-test.js')).to.equal( + fixture('model-test/rfc232.js') + ); + }, + { isModuleUnification: true } + ); + }); + }); + + describe('with ember-cli-mocha v0.12+', function() { + beforeEach(function() { + modifyPackages([ + { name: 'ember-cli-qunit', delete: true }, + { name: 'ember-cli-mocha', dev: true }, + ]); + generateFakePackageManifest('ember-cli-mocha', '0.12.0'); + }); + + it('model-test for mocha v0.12+', function() { + let args = ['model-test', 'foo']; + + return emberGenerateDestroy( + args, + _file => { + expect(_file('src/data/models/foo/model-test.js')).to.equal( + fixture('model-test/foo-mocha-0.12.js') + ); + }, + { isModuleUnification: true } ); }); }); diff --git a/package.json b/package.json index 78f065e97bb..ea2938b6c52 100644 --- a/package.json +++ b/package.json @@ -74,7 +74,7 @@ "common-tags": "^1.8.0", "ember-cli": "^3.3.0", "ember-cli-app-version": "^3.2.0", - "ember-cli-blueprint-test-helpers": "^0.18.3", + "ember-cli-blueprint-test-helpers": "^0.19.1", "ember-cli-dependency-checker": "^3.0.0", "ember-cli-htmlbars": "^3.0.0", "ember-cli-htmlbars-inline-precompile": "^1.0.3", diff --git a/yarn.lock b/yarn.lock index 5726264fa7a..a8dbaa1cc40 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2372,16 +2372,16 @@ ember-cli-babel@^6.0.0, ember-cli-babel@^6.0.0-beta.4, ember-cli-babel@^6.0.0-be ember-cli-version-checker "^2.1.2" semver "^5.5.0" -ember-cli-blueprint-test-helpers@^0.18.3: - version "0.18.3" - resolved "https://registry.yarnpkg.com/ember-cli-blueprint-test-helpers/-/ember-cli-blueprint-test-helpers-0.18.3.tgz#945c606d855f0263f5e8c03522e4040a74f259cc" +ember-cli-blueprint-test-helpers@^0.19.1: + version "0.19.1" + resolved "https://registry.yarnpkg.com/ember-cli-blueprint-test-helpers/-/ember-cli-blueprint-test-helpers-0.19.1.tgz#26df9724a6cb48f7acfa391dec7ec772888b0929" dependencies: chai "^4.1.0" chai-as-promised "^7.0.0" chai-files "^1.0.0" debug "^3.0.0" ember-cli-internal-test-helpers "^0.9.1" - fs-extra "^4.0.0" + fs-extra "^5.0.0" testdouble "^3.2.6" tmp-sync "^1.0.0" @@ -3540,7 +3540,7 @@ fs-extra@^0.30.0: path-is-absolute "^1.0.0" rimraf "^2.2.8" -fs-extra@^4.0.0, fs-extra@^4.0.2, fs-extra@^4.0.3: +fs-extra@^4.0.2, fs-extra@^4.0.3: version "4.0.3" resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-4.0.3.tgz#0d852122e5bc5beb453fb028e9c0c9bf36340c94" dependencies: