Skip to content

Commit

Permalink
Merge pull request #588 from Gaurav0/acceptance_tests_for_tests
Browse files Browse the repository at this point in the history
Fix tests in 2.16 and add acceptance tests for tests
  • Loading branch information
Gaurav0 authored Mar 30, 2018
2 parents 2b49f3e + 5309846 commit 1131ad4
Show file tree
Hide file tree
Showing 9 changed files with 458 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/serializers/gist.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default ApplicationSerializer.extend({
},

normalizeArrayResponse(store, primaryModelClass, payload) {
payload.forEach((hash)=> this.normalizeGist(hash, true));
payload.forEach(hash => this.normalizeGist(hash, true));

return this._super(...arguments);
},
Expand Down
46 changes: 40 additions & 6 deletions app/services/ember-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,6 @@ export default Ember.Service.extend({
// avoids security error
appJS += "window.history.pushState = function() {}; window.history.replaceState = function() {}; window.sessionStorage = undefined;";

// Use parent's version of QUnit in Ember.testing mode
if (testing) {
appJS += "window.QUnit = window.parent.QUnit;";
}

// Hide toolbar since it is not working
appCSS += `\n#qunit-testrunner-toolbar, #qunit-tests a[href] { display: none; }\n`;

Expand Down Expand Up @@ -307,6 +302,10 @@ export default Ember.Service.extend({
let EmberENV = twiddleJSON.EmberENV || {};
const isTestingEnabled = testingEnabled(twiddleJSON);

if (testing && !isTestingEnabled) {
depScriptTags += `<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/qunit/2.3.2/qunit.js"></script>`;
}

depScriptTags += `<script type="text/javascript">EmberENV = ${JSON.stringify(EmberENV)};</script>`;
depScriptTags += `<script type="text/javascript" src="${window.assetMap.loader}"></script>`;

Expand All @@ -324,6 +323,41 @@ export default Ember.Service.extend({
}
});

if (isTestingEnabled) {
testStuff += `
<script type="text/javascript">
// Hack around dealing with multiple global QUnits!
jQuery.ajax({
url: 'https://cdnjs.cloudflare.com/ajax/libs/qunit/2.3.2/qunit.js',
dataType: 'text'
}).then(function(script) {
var oldQUnit;
if (window.QUnit) {
oldQUnit = window.QUnit;
}
window.QUnit = {
config: {
autostart: false
}
}
eval(script);
if (!oldQUnit) {
oldQUnit = window.QUnit;
}
if (window.testModule) {
window.require(window.testModule);
}
window.QUnit.start = function() {};
window.QUnit.done(function() {
window.QUnit = oldQUnit;
});
Ember.run(function() {
oldQUnit.start();
});
});
</script>`;
}

depScriptTags += `<script type="text/javascript" src="${window.assetMap.twiddleDeps}"></script>`;

if (isTestingEnabled) {
Expand Down Expand Up @@ -355,7 +389,7 @@ export default Ember.Service.extend({
testJSFiles.forEach(jsFile => {
depScriptTags += `<script type="text/javascript" src="${window.assetMap[jsFile]}"></script>`;
});

testStuff += `<script type="text/javascript">
Ember.Test.adapter = require('ember-qunit').QUnitAdapter.create();
</script>`;
Expand Down
1 change: 0 additions & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"bootstrap-sass": "~3.3.5",
"dom-ruler": "~0.2.4",
"pretender": "~1.4.1",
"ember-inflector": "~1.3.1",
"lodash": "~3.10.1",
"Faker": "~3.1.0",
"file-saver": "1.3.4",
Expand Down
13 changes: 1 addition & 12 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,7 @@ module.exports = function(defaults) {
},

tests: true,
hinting: process.env.EMBER_CLI_TEST_COMMAND || !isProductionLikeBuild,

vendorFiles: {
'ember.js': {
staging: 'bower_components/ember/ember.prod.js'
},
'ember-testing.js': []
}
hinting: process.env.EMBER_CLI_TEST_COMMAND || !isProductionLikeBuild
});

if (isFastboot) {
Expand All @@ -99,10 +92,6 @@ module.exports = function(defaults) {
app.import('vendor/shims/path.js');
app.import('bower_components/file-saver/FileSaver.js');

if (env === "test") {
app.import('bower_components/ember/ember-testing.js', { type: 'test' });
}

if (!isFastboot) {
app.import('vendor/drags.js');
}
Expand Down
171 changes: 171 additions & 0 deletions tests/acceptance/acceptance-application-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
import { test } from 'qunit';
import moduleForAcceptance from 'ember-twiddle/tests/helpers/module-for-acceptance';
import { timeout } from 'ember-concurrency';

moduleForAcceptance('Acceptance | acceptance-application-test', {
beforeEach: function() {
this.cachePrompt = window.prompt;
window.prompt = (text, defaultResponse) => defaultResponse;
},

afterEach: function() {
window.prompt = this.cachePrompt;
}
});

test('An acceptance test for an application works', function(assert) {

const files = [
{
filename: "application.template.hbs",
content: `Welcome to {{appName}}`
},
{
filename: "application.controller.js",
content: `import Ember from "ember";
export default Ember.Controller.extend({
appName: 'Ember Twiddle'
});`
},
{
filename: "twiddle.json",
content: `{
"version": "0.13.0",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": true
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js"
}
}`
},
{
filename: "components/my-component.js",
content: `import Ember from 'ember';
export default Ember.Component.extend({
});`
},
{
filename: "templates/components/my-component.hbs",
content: `{{yield}}`
},
{
filename: "tests/test-helper.js",
content: `import resolver from './helpers/resolver';
import {
setResolver
} from 'ember-qunit';
import jQuery from 'jquery';
setResolver(resolver);
window.testModule = 'twiddle/tests/acceptance/application-test';
`
},
{
filename: "tests/helpers/resolver.js",
content: `import Resolver from '../../resolver';
import config from '../../config/environment';
const resolver = Resolver.create();
resolver.namespace = {
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix
};
export default resolver;`
},
{
filename: "tests/helpers/module-for-acceptance.js",
content: `import { module } from 'qunit';
import Ember from 'ember';
import startApp from '../helpers/start-app';
import destroyApp from '../helpers/destroy-app';
const { RSVP: { Promise } } = Ember;
export default function(name, options = {}) {
module(name, {
beforeEach() {
this.application = startApp();
if (options.beforeEach) {
return options.beforeEach.apply(this, arguments);
}
},
afterEach() {
let afterEach = options.afterEach && options.afterEach.apply(this, arguments);
return Promise.resolve(afterEach).then(() => destroyApp(this.application));
}
});
}`
},
{
filename: "tests/helpers/start-app.js",
content: `import Ember from 'ember';
import Application from '../../app';
import config from '../../config/environment';
const { run } = Ember;
const assign = Ember.assign || Ember.merge;
export default function startApp(attrs) {
let application;
let attributes = assign({rootElement: "#test-root"}, config.APP);
attributes = assign(attributes, attrs); // use defaults, but you can override;
run(() => {
application = Application.create(attributes);
application.setupForTesting();
application.injectTestHelpers();
});
return application;
}
`
},
{
filename: "tests/helpers/destroy-app.js",
content: `import Ember from 'ember';
export default function destroyApp(application) {
Ember.run(application, 'destroy');
}`
},
{
filename: "tests/acceptance/application-test.js",
content: `import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';
moduleForAcceptance('TODO: put something here');
test('visiting /application', function(assert) {
visit('/');
andThen(function() {
assert.equal(currentURL(), '/', 'route loaded correctly');
});
});`
}
];

runGist(files);

andThen(function() {
return timeout(500); // TODO: fix and remove this timing hack
});

andThen(function() {
const outputSpan = 'div#qunit-testresult-display > span.passed';

assert.equal(outputPane().$(outputSpan).text(), '1', 'acceptance test passed');
});
});
Loading

0 comments on commit 1131ad4

Please sign in to comment.