Skip to content

Commit

Permalink
Merge pull request #18395 from emberjs/component-tests
Browse files Browse the repository at this point in the history
[BUGFIX] Use `<Nested::Invocation>` in component tests blueprint
  • Loading branch information
rwjblue authored Sep 17, 2019
2 parents a385e62 + 2dd2fea commit e1871c8
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 407 deletions.
94 changes: 22 additions & 72 deletions blueprints/component-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ const isPackageMissing = require('ember-cli-is-package-missing');
const getPathOption = require('ember-cli-get-component-path-option');

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

function needsCurlyBracketInvocation(options) {
let path = options.path || '';
let fullPaths = [...path.split('/'), ...options.entity.name.split('/')];
let ignoreCommonPrefix = ['', 'components'].includes(fullPaths[0]);
if (ignoreCommonPrefix) fullPaths.shift();
return fullPaths.length > 1;
function invocationFor(options) {
let parts = options.entity.name.split('/');
return parts.map(p => stringUtil.classify(p)).join('::');
}

module.exports = useTestFrameworkDetector({
Expand All @@ -34,56 +30,26 @@ module.exports = useTestFrameworkDetector({
],

fileMapTokens: function() {
if (isModuleUnificationProject(this.project)) {
return {
__test__() {
return 'component-test';
},
__root__(options) {
if (options.inRepoAddon) {
return path.join('packages', options.inRepoAddon, 'src');
}
return 'src';
},
__testType__(options) {
if (options.locals.testType === 'unit') {
throw new Error("The --unit flag isn't supported within a module unification app");
}

return '';
},
__path__(options) {
if (options.pod) {
throw new Error("Pods aren't supported within a module unification app");
}
return path.join('ui', 'components', options.dasherizedModuleName);
},
};
} else {
return {
__root__() {
return 'tests';
},
__testType__(options) {
return options.locals.testType || 'integration';
},
__path__(options) {
if (options.pod) {
return path.join(options.podPath, options.locals.path, options.dasherizedModuleName);
}
return 'components';
},
};
}
return {
__root__() {
return 'tests';
},
__testType__(options) {
return options.locals.testType || 'integration';
},
__path__(options) {
if (options.pod) {
return path.join(options.podPath, options.locals.path, options.dasherizedModuleName);
}
return 'components';
},
};
},

locals: function(options) {
let dasherizedModuleName = stringUtil.dasherize(options.entity.name);
let componentPathName = dasherizedModuleName;
let classifiedModuleName = stringUtil.classify(options.entity.name);
let templateInvocation = classifiedModuleName;
let testType = options.testType || 'integration';
let componentName, openComponent, closeComponent, selfCloseComponent;

let friendlyTestDescription = [
testType === 'unit' ? 'Unit' : 'Integration',
Expand All @@ -93,29 +59,13 @@ module.exports = useTestFrameworkDetector({

if (options.pod && options.path !== 'components' && options.path !== '') {
componentPathName = [options.path, dasherizedModuleName].filter(Boolean).join('/');
} else if (isModuleUnificationProject(this.project)) {
if (options.inRepoAddon) {
componentPathName = `${options.inRepoAddon}::${dasherizedModuleName}`;
templateInvocation = `${stringUtil.classify(options.inRepoAddon)}::${classifiedModuleName}`;
} else if (this.project.isEmberCLIAddon()) {
componentPathName = `${this.project.pkg.name}::${dasherizedModuleName}`;
templateInvocation = `${stringUtil.classify(
this.project.pkg.name
)}::${classifiedModuleName}`;
}
}

if (needsCurlyBracketInvocation(options)) {
componentName = componentPathName;
openComponent = descriptor => `{{#${descriptor}}}`;
closeComponent = descriptor => `{{/${descriptor}}}`;
selfCloseComponent = descriptor => `{{${descriptor}}}`;
} else {
componentName = templateInvocation;
openComponent = descriptor => `<${descriptor}>`;
closeComponent = descriptor => `</${descriptor}>`;
selfCloseComponent = descriptor => `<${descriptor} />`;
}
let templateInvocation = invocationFor(options);
let componentName = templateInvocation;
let openComponent = descriptor => `<${descriptor}>`;
let closeComponent = descriptor => `</${descriptor}>`;
let selfCloseComponent = descriptor => `<${descriptor} />`;

return {
path: getPathOption(options),
Expand Down
51 changes: 12 additions & 39 deletions blueprints/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const stringUtil = require('ember-cli-string-utils');
const pathUtil = require('ember-cli-path-utils');
const getPathOption = require('ember-cli-get-component-path-option');
const normalizeEntityName = require('ember-cli-normalize-entity-name');
const { isModuleUnificationProject } = require('../module-unification');
const { EOL } = require('os');
const { has } = require('@ember/edition-utils');

Expand Down Expand Up @@ -96,31 +95,7 @@ module.exports = {
fileMapTokens(options) {
let commandOptions = this.options;

if (isModuleUnificationProject(this.project)) {
return {
__name__: function() {
return 'component';
},
__root__(options) {
if (options.inRepoAddon) {
return path.join('packages', options.inRepoAddon, 'src');
}
if (options.inDummy) {
return path.join('tests', 'dummy', 'src');
}
return 'src';
},
__path__(options) {
return path.join('ui', 'components', options.dasherizedModuleName);
},
__templatepath__(options) {
return path.join('ui', 'components', options.dasherizedModuleName);
},
__templatename__: function() {
return 'template';
},
};
} else if (commandOptions.pod) {
if (commandOptions.pod) {
return {
__path__() {
return path.join(options.podPath, options.locals.path, options.dasherizedModuleName);
Expand Down Expand Up @@ -212,20 +187,18 @@ module.exports = {
let defaultExport = '';
let contents = '';

if (!isModuleUnificationProject(this.project)) {
// if we're in an addon, build import statement
if (options.project.isEmberCLIAddon() || (options.inRepoAddon && !options.inDummy)) {
if (options.pod) {
templatePath = './template';
} else {
templatePath =
pathUtil.getRelativeParentPath(options.entity.name) +
'templates/components/' +
stringUtil.dasherize(options.entity.name);
}
importTemplate = "import layout from '" + templatePath + "';" + EOL;
contents = EOL + ' layout';
// if we're in an addon, build import statement
if (options.project.isEmberCLIAddon() || (options.inRepoAddon && !options.inDummy)) {
if (options.pod) {
templatePath = './template';
} else {
templatePath =
pathUtil.getRelativeParentPath(options.entity.name) +
'templates/components/' +
stringUtil.dasherize(options.entity.name);
}
importTemplate = "import layout from '" + templatePath + "';" + EOL;
contents = EOL + ' layout';
}

let componentClass = this.EMBER_GLIMMER_SET_COMPONENT_TEMPLATE
Expand Down
154 changes: 0 additions & 154 deletions node-tests/blueprints/component-test-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@ const fs = require('fs-extra');
const blueprintHelpers = require('ember-cli-blueprint-test-helpers/helpers');
const setupTestHooks = blueprintHelpers.setupTestHooks;
const emberNew = blueprintHelpers.emberNew;
const emberGenerate = blueprintHelpers.emberGenerate;
const emberGenerateDestroy = blueprintHelpers.emberGenerateDestroy;
const modifyPackages = blueprintHelpers.modifyPackages;
const expectError = require('../helpers/expect-error');

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

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

const setupTestEnvironment = require('../helpers/setup-test-environment');
const enableModuleUnification = setupTestEnvironment.enableModuleUnification;

describe('Blueprint: component-test', function() {
setupTestHooks(this);

Expand Down Expand Up @@ -175,155 +170,6 @@ describe('Blueprint: component-test', function() {
});
});

describe('in app - module unification', function() {
enableModuleUnification();

beforeEach(function() {
return emberNew();
});

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

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

it('component-test x-foo --unit', function() {
return expectError(
emberGenerate(['component-test', 'x-foo', '--unit']),
"The --unit flag isn't supported within a module unification app"
);
});

describe('with usePods=true', function() {
beforeEach(function() {
fs.writeFileSync(
'.ember-cli',
`{
"disableAnalytics": false,
"usePods": true
}`
);
});

it('component-test x-foo', function() {
return expectError(
emberGenerate(['component-test', 'x-foo']),
"Pods aren't supported within a module unification app"
);
});
});
});

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

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

it('component-test x-foo --unit', function() {
return expectError(
emberGenerate(['component-test', 'x-foo', '--unit']),
"The --unit flag isn't supported within a module unification app"
);
});
});

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

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

it('component-test x-foo --unit', function() {
return expectError(
emberGenerate(['component-test', 'x-foo', '--unit']),
"The --unit flag isn't supported within a module unification app"
);
});
});

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

it('component-test x-foo', function() {
return emberGenerateDestroy(['component-test', 'x-foo'], _file => {
expect(_file('src/ui/components/x-foo/component-test.js')).to.equal(
fixture('component-test/mocha-0.12.js')
);
});
});

it('component-test x-foo --unit', function() {
return expectError(
emberGenerate(['component-test', 'x-foo', '--unit']),
"The --unit flag isn't supported within a module unification app"
);
});
});

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

it('component-test x-foo', function() {
return emberGenerateDestroy(['component-test', 'x-foo'], _file => {
expect(_file('src/ui/components/x-foo/component-test.js')).to.equal(
fixture('component-test/mocha-rfc232.js')
);
});
});

it('component-test x-foo --unit', function() {
return expectError(
emberGenerate(['component-test', 'x-foo', '--unit']),
"The --unit flag isn't supported within a module unification app"
);
});
});
});

describe('in addon', function() {
beforeEach(function() {
return emberNew({ target: 'addon' })
Expand Down
Loading

0 comments on commit e1871c8

Please sign in to comment.