From acd144f3799f7b71a934b4a908dfb0f50ea7a1ca Mon Sep 17 00:00:00 2001 From: Brian Runnells Date: Sat, 4 Aug 2018 19:41:52 -0500 Subject: [PATCH 1/2] Add RFC 232 variant detector for mocha & component tests --- .../__testType__/__path__/__test__.js | 38 ++++++++++++++ blueprints/test-framework-detector.js | 10 +++- node-tests/blueprints/component-test-test.js | 51 +++++++++++++++++++ .../component-test/mocha-rfc232-unit.js | 12 +++++ .../fixtures/component-test/mocha-rfc232.js | 27 ++++++++++ 5 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 blueprints/component-test/mocha-rfc-232-files/__root__/__testType__/__path__/__test__.js create mode 100644 node-tests/fixtures/component-test/mocha-rfc232-unit.js create mode 100644 node-tests/fixtures/component-test/mocha-rfc232.js diff --git a/blueprints/component-test/mocha-rfc-232-files/__root__/__testType__/__path__/__test__.js b/blueprints/component-test/mocha-rfc-232-files/__root__/__testType__/__path__/__test__.js new file mode 100644 index 00000000000..e3afbedf9bb --- /dev/null +++ b/blueprints/component-test/mocha-rfc-232-files/__root__/__testType__/__path__/__test__.js @@ -0,0 +1,38 @@ +<% if (testType === 'integration') { %>import { expect } from 'chai'; +import { describe, it } from 'mocha'; +import { setupRenderingTest } from 'ember-mocha'; +import { render } from '@ember/test-helpers'; +import hbs from 'htmlbars-inline-precompile'; + +describe('<%= friendlyTestDescription %>', function() { + setupRenderingTest(); + + it('renders', async function() { + // Set any properties with this.set('myProperty', 'value'); + // Handle any actions with this.set('myAction', function(val) { ... }); + + await render(hbs`{{<%= componentPathName %>}}`); + + expect(this.element.textContent.trim()).to.equal(''); + + // Template block usage: + await render(hbs` + {{#<%= componentPathName %>}} + template block text + {{/<%= componentPathName %>}} + `); + + expect(this.element.textContent.trim()).to.equal('template block text'); + }); +});<% } else if (testType === 'unit') { %>import { expect } from 'chai'; +import { describe, it } from 'mocha'; +import { setupTest } from 'ember-mocha'; + +describe('<%= friendlyTestDescription %>', function() { + setupTest(); + + it('exists', function() { + let component = this.owner.factoryFor('component:<%= componentPathName %>').create(); + expect(component).to.be.ok; + }); +});<% } %> diff --git a/blueprints/test-framework-detector.js b/blueprints/test-framework-detector.js index ce46022345d..eedef9e3049 100644 --- a/blueprints/test-framework-detector.js +++ b/blueprints/test-framework-detector.js @@ -26,7 +26,15 @@ module.exports = function(blueprint) { type = 'qunit'; } } else if ('ember-mocha' in dependencies) { - type = 'mocha-0.12'; + let checker = new VersionChecker(this.project); + if ( + fs.existsSync(this.path + '/mocha-rfc-232-files') && + checker.for('ember-mocha', 'npm').gte('0.14.0') + ) { + type = 'mocha-rfc-232'; + } else { + type = 'mocha-0.12'; + } } else if ('ember-cli-mocha' in dependencies) { let checker = new VersionChecker(this.project); if ( diff --git a/node-tests/blueprints/component-test-test.js b/node-tests/blueprints/component-test-test.js index b0444147171..df4065b5968 100644 --- a/node-tests/blueprints/component-test-test.js +++ b/node-tests/blueprints/component-test-test.js @@ -133,6 +133,32 @@ describe('Blueprint: component-test', function() { }); }); }); + + describe('with ember-mocha@0.14.0', function() { + beforeEach(function() { + modifyPackages([ + { name: 'ember-cli-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('tests/integration/components/x-foo-test.js')).to.equal( + fixture('component-test/mocha-rfc232.js') + ); + }); + }); + + it('component-test x-foo --unit', function() { + return emberGenerateDestroy(['component-test', 'x-foo', '--unit'], _file => { + expect(_file('tests/unit/components/x-foo-test.js')).to.equal( + fixture('component-test/mocha-rfc232-unit.js') + ); + }); + }); + }); }); describe('in app - module unification', function() { @@ -247,6 +273,31 @@ describe('Blueprint: component-test', function() { ); }); }); + + describe('with ember-mocha@0.14.0', function() { + beforeEach(function() { + modifyPackages([ + { name: 'ember-cli-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() { diff --git a/node-tests/fixtures/component-test/mocha-rfc232-unit.js b/node-tests/fixtures/component-test/mocha-rfc232-unit.js new file mode 100644 index 00000000000..72f1b87eb6d --- /dev/null +++ b/node-tests/fixtures/component-test/mocha-rfc232-unit.js @@ -0,0 +1,12 @@ +import { expect } from 'chai'; +import { describe, it } from 'mocha'; +import { setupTest } from 'ember-mocha'; + +describe('Unit | Component | x-foo', function() { + setupTest(); + + it('exists', function() { + let component = this.owner.factoryFor('component:x-foo').create(); + expect(component).to.be.ok; + }); +}); diff --git a/node-tests/fixtures/component-test/mocha-rfc232.js b/node-tests/fixtures/component-test/mocha-rfc232.js new file mode 100644 index 00000000000..fa2c2aa9224 --- /dev/null +++ b/node-tests/fixtures/component-test/mocha-rfc232.js @@ -0,0 +1,27 @@ +import { expect } from 'chai'; +import { describe, it } from 'mocha'; +import { setupRenderingTest } from 'ember-mocha'; +import { render } from '@ember/test-helpers'; +import hbs from 'htmlbars-inline-precompile'; + +describe('Integration | Component | x-foo', function() { + setupRenderingTest(); + + it('renders', async function() { + // Set any properties with this.set('myProperty', 'value'); + // Handle any actions with this.set('myAction', function(val) { ... }); + + await render(hbs`{{x-foo}}`); + + expect(this.element.textContent.trim()).to.equal(''); + + // Template block usage: + await render(hbs` + {{#x-foo}} + template block text + {{/x-foo}} + `); + + expect(this.element.textContent.trim()).to.equal('template block text'); + }); +}); From a47ff0933376f30cc6076f9750666f843114e796 Mon Sep 17 00:00:00 2001 From: Brian Runnells Date: Sat, 18 Aug 2018 07:46:20 -0500 Subject: [PATCH 2/2] add mocha rfc 268 acceptance tests --- .../tests/acceptance/__name__-test.js | 13 +++++++++++++ node-tests/blueprints/acceptance-test-test.js | 18 ++++++++++++++++++ .../fixtures/acceptance-test/mocha-rfc268.js | 13 +++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 blueprints/acceptance-test/mocha-rfc-232-files/tests/acceptance/__name__-test.js create mode 100644 node-tests/fixtures/acceptance-test/mocha-rfc268.js diff --git a/blueprints/acceptance-test/mocha-rfc-232-files/tests/acceptance/__name__-test.js b/blueprints/acceptance-test/mocha-rfc-232-files/tests/acceptance/__name__-test.js new file mode 100644 index 00000000000..e26de7ca265 --- /dev/null +++ b/blueprints/acceptance-test/mocha-rfc-232-files/tests/acceptance/__name__-test.js @@ -0,0 +1,13 @@ +import { describe, it } from 'mocha'; +import { expect } from 'chai'; +import { setupApplicationTest } from 'ember-mocha'; +import { visit, currentURL } from '@ember/test-helpers'; + +describe('<%= friendlyTestName %>', function() { + setupApplicationTest(); + + it('can visit /<%= dasherizedModuleName %>', async function() { + await visit('/<%= dasherizedModuleName %>'); + expect(currentURL()).to.equal('/<%= dasherizedModuleName %>'); + }); +}); diff --git a/node-tests/blueprints/acceptance-test-test.js b/node-tests/blueprints/acceptance-test-test.js index 3cbeb06441f..a452a93e9db 100644 --- a/node-tests/blueprints/acceptance-test-test.js +++ b/node-tests/blueprints/acceptance-test-test.js @@ -58,6 +58,24 @@ describe('Blueprint: acceptance-test', function() { }); }); }); + + describe('with ember-mocha@0.14.0', function() { + beforeEach(function() { + modifyPackages([ + { name: 'ember-cli-qunit', delete: true }, + { name: 'ember-mocha', dev: true }, + ]); + generateFakePackageManifest('ember-mocha', '0.14.0'); + }); + + it('acceptance-test foo', function() { + return emberGenerateDestroy(['acceptance-test', 'foo'], _file => { + expect(_file('tests/acceptance/foo-test.js')).to.equal( + fixture('acceptance-test/mocha-rfc268.js') + ); + }); + }); + }); }); describe('in addon', function() { diff --git a/node-tests/fixtures/acceptance-test/mocha-rfc268.js b/node-tests/fixtures/acceptance-test/mocha-rfc268.js new file mode 100644 index 00000000000..9f5c93d21f1 --- /dev/null +++ b/node-tests/fixtures/acceptance-test/mocha-rfc268.js @@ -0,0 +1,13 @@ +import { describe, it } from 'mocha'; +import { expect } from 'chai'; +import { setupApplicationTest } from 'ember-mocha'; +import { visit, currentURL } from '@ember/test-helpers'; + +describe('Acceptance | foo', function() { + setupApplicationTest(); + + it('can visit /foo', async function() { + await visit('/foo'); + expect(currentURL()).to.equal('/foo'); + }); +});