Skip to content
This repository has been archived by the owner on Jul 12, 2019. It is now read-only.

Commit

Permalink
Setup test-runner & component-test blueprint
Browse files Browse the repository at this point in the history
Shamelessly stolen from implementation in emberjs/ember.js#15934
  • Loading branch information
alexander-alvarez committed Dec 8, 2017
1 parent 029a952 commit ae866b4
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import { module, test } from 'qunit';
import { <% if (testType === 'integration' ) { %> setupRenderingTest <% } else if(testType === 'unit') { %> setupTest <% } %> } from 'ember-qunit';
<% if (testType === 'integration') { %>import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('<%= friendlyTestDescription %>', function(hooks) {
<% if (testType === 'integration' ) { %> setupRenderingTest(hooks); <% } else if(testType === 'unit') { %> setupTest(hooks); <% } %>
setupRenderingTest(hooks);

// use hooks.before/after/beforeEach/afterEach for test setup

test('it renders', async function(assert) {<% if (testType === 'integration' ) { %>
test('it renders', async function(assert) {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.on('myAction', function(val) { ... });
// Handle any actions with this.set('myAction', function(val) { ... });

await this.render(hbs`{{<%= componentPathName %>}}`);
await render(hbs`{{<%= componentPathName %>}}`);

assert.equal(this.$().text().trim(), '');
assert.equal(this.element.textContent.trim(), '');

// Template block usage:
await this.render(hbs`
await render(hbs`
{{#<%= componentPathName %>}}
template block text
{{/<%= componentPathName %>}}
`);

assert.equal(this.$().text().trim(), 'template block text');<% } else if(testType === 'unit') { %>
// Creates the component instance
let Factory = this.owner.factoryFor('component:<%= componentPathName %>');
let subject = Factory.create({});
assert.ok(subject);<% } %>
assert.equal(this.element.textContent.trim(), 'template block text');
});
});<% } else if (testType === 'unit') { %>import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('<%= friendlyTestDescription %>', function(hooks) {
setupTest(hooks);

test('it exists', function(assert) {
let component = this.owner.factoryFor('component:<%= componentPathName %>').create();
assert.ok(component);
});
});
});<% } %>

1 change: 1 addition & 0 deletions config/ember-try.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env node */
module.exports = {
command: 'npm run test:blueprints',
scenarios: [
{
name: 'ember-lts-2.8',
Expand Down
40 changes: 40 additions & 0 deletions node-tests/blueprints/component-test-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

const blueprintHelpers = require('ember-cli-blueprint-test-helpers/helpers');
const setupTestHooks = blueprintHelpers.setupTestHooks;
const emberNew = blueprintHelpers.emberNew;
const emberGenerateDestroy = blueprintHelpers.emberGenerateDestroy;
const modifyPackages = blueprintHelpers.modifyPackages;

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

const fixture = require('../helpers/fixture');

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

describe('in app', function() {
beforeEach(function() {
return emberNew();
});

describe('with [email protected]', function() {

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/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/rfc232-unit.js'));
});
});
});

});
});
11 changes: 11 additions & 0 deletions node-tests/fixtures/component-test/rfc232-unit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';

module('Unit | Component | x foo', function(hooks) {
setupTest(hooks);

test('it exists', function(assert) {
let component = this.owner.factoryFor('component:x-foo').create();
assert.ok(component);
});
});
26 changes: 26 additions & 0 deletions node-tests/fixtures/component-test/rfc232.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Integration | Component | x foo', function(hooks) {
setupRenderingTest(hooks);

test('it renders', async function(assert) {
// Set any properties with this.set('myProperty', 'value');
// Handle any actions with this.set('myAction', function(val) { ... });

await render(hbs`{{x-foo}}`);

assert.equal(this.element.textContent.trim(), '');

// Template block usage:
await render(hbs`
{{#x-foo}}
template block text
{{/x-foo}}
`);

assert.equal(this.element.textContent.trim(), 'template block text');
});
});
8 changes: 8 additions & 0 deletions node-tests/helpers/fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

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

module.exports = function(filePath) {
return file(path.join(__dirname, '../fixtures', filePath));
};
17 changes: 17 additions & 0 deletions node-tests/nodetest-runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

var glob = require('glob');
var Mocha = require('mocha');

var mocha = new Mocha({
timeout: 5000,
reporter: 'spec'
});

mocha.files = glob.sync('node-tests/{blueprints,acceptance,unit}/**/*-test.js');

mocha.run(function(failures) {
process.on('exit', function() {
process.exit(failures);
});
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"build": "ember build",
"start": "ember server",
"test": "ember try:each",
"nodetest": "mocha node-tests --recursive"
"test:blueprints": "node node-tests/nodetest-runner.js"
},
"dependencies": {
"ember-cli-babel": "^6.6.0",
Expand Down

0 comments on commit ae866b4

Please sign in to comment.