Skip to content

Commit 4f65b22

Browse files
committed
Merge pull request #298 from Gaurav0/testing
Make it easier to do Testing
2 parents 355e840 + 955ff3e commit 4f65b22

File tree

5 files changed

+100
-8
lines changed

5 files changed

+100
-8
lines changed

app/services/ember-cli.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ const requiredDependencies = [
103103
export default Ember.Service.extend({
104104
dependencyResolver: Ember.inject.service(),
105105

106-
init () {
107-
this._super();
106+
init (...args) {
107+
this._super(...args);
108108
this.set('store', this.container.lookup("service:store"));
109109
},
110110

@@ -200,6 +200,12 @@ export default Ember.Service.extend({
200200
appJS += "window.location.hash='" + gist.get('initialRoute') + "';";
201201
}
202202

203+
// avoids security error
204+
appJS += "window.history.pushState = function() {}; window.history.replaceState = function() {};";
205+
206+
// Hide toolbar since it is not working
207+
appCSS += `\n#qunit-testrunner-toolbar, #qunit-tests a[href] { display: none; }\n`;
208+
203209
let index = blueprints['index.html'];
204210
let twiddleJSON = this.getTwiddleJson(gist);
205211
let deps = twiddleJSON.dependencies;
@@ -208,6 +214,7 @@ export default Ember.Service.extend({
208214
let depScriptTags ='';
209215
let appScriptTag = `<script type="text/javascript">${appJS}</script>`;
210216
let appStyleTag = `<style type="text/css">${appCSS}</style>`;
217+
let testStuff = '';
211218

212219
let EmberENV = twiddleJSON.EmberENV || {};
213220
depScriptTags += `<script type="text/javascript">EmberENV = ${JSON.stringify(EmberENV)};</script>`;
@@ -223,8 +230,27 @@ export default Ember.Service.extend({
223230

224231
depScriptTags += `<script type="text/javascript" src="${config.assetsHost}assets/twiddle-deps.js?${config.APP.version}"></script>`;
225232

233+
const testingEnabled = twiddleJSON.options && twiddleJSON.options["enable-testing"];
234+
235+
if (testingEnabled) {
236+
const testJSFiles = ['assets/test-loader.js', 'assets/test-support.js', 'testem.js'];
237+
238+
testJSFiles.forEach(jsFile => {
239+
depScriptTags += `<script type="text/javascript" src="${config.assetsHost}${jsFile}?${config.APP.version}"></script>`;
240+
});
241+
242+
depCssLinkTags += `<link rel="stylesheet" type="text/css" href="${config.assetsHost}assets/test-support.css?${config.APP.version}">`;
243+
244+
testStuff += `
245+
<div id="qunit"></div>
246+
<div id="qunit-fixture"></div>
247+
<div id="ember-testing-container">
248+
<div id="ember-testing"></div>
249+
</div>`;
250+
}
251+
226252
index = index.replace('{{content-for \'head\'}}', `${depCssLinkTags}\n${appStyleTag}`);
227-
index = index.replace('{{content-for \'body\'}}', `${depScriptTags}\n${appScriptTag}`);
253+
index = index.replace('{{content-for \'body\'}}', `${depScriptTags}\n${appScriptTag}\n${testStuff}\n`);
228254

229255
// replace the {{build-timestamp}} placeholder with the number of
230256
// milliseconds since the Unix Epoch:

blueprints/twiddle.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
2-
"version": "0.4.17",
2+
"version": "0.5.0",
33
"EmberENV": {
44
"FEATURES": {}
55
},
6+
"options": {
7+
"enable-testing": false
8+
},
69
"dependencies": {
710
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js",
811
"ember": "https://cdnjs.cloudflare.com/ajax/libs/ember.js/2.2.0/ember.debug.js",

ember-cli-build.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ module.exports = function() {
2323
fingerprint: {
2424
enabled: isProductionLikeBuild,
2525
prepend: prepend,
26-
extensions: ['js', 'css', 'png', 'jpg', 'gif', 'map', 'svg', 'eot', 'ttf', 'woff', 'woff2', 'ico']
26+
extensions: ['js', 'css', 'png', 'jpg', 'gif', 'map', 'svg', 'eot', 'ttf', 'woff', 'woff2', 'ico'],
27+
exclude: ['test-loader', 'test-support', 'testem']
2728
},
2829
codemirror: {
2930
modes: ['xml', 'javascript', 'handlebars', 'htmlmixed', 'css'],
@@ -39,7 +40,7 @@ module.exports = function() {
3940
minifyCSS: { enabled: isProductionLikeBuild },
4041
minifyJS: { enabled: isProductionLikeBuild },
4142

42-
tests: process.env.EMBER_CLI_TEST_COMMAND || !isProductionLikeBuild,
43+
tests: true,
4344
hinting: process.env.EMBER_CLI_TEST_COMMAND || !isProductionLikeBuild,
4445

4546
vendorFiles: {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ember-twiddle",
3-
"version": "0.4.17",
3+
"version": "0.5.0",
44
"description": "Small description for ember-twiddle goes here",
55
"private": true,
66
"directories": {

tests/unit/services/ember-cli-test.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ moduleFor('service:ember-cli', 'Unit | Service | ember cli', {
66
needs: ['model:gist','model:gistFile', 'service:dependency-resolver']
77
});
88

9-
// Replace this with your real tests.
109
test('compiling a gist works', function(assert) {
1110
assert.expect(7);
1211
var service = this.subject();
@@ -164,3 +163,66 @@ test('compileHbs can include backticks', function(assert) {
164163

165164
assert.ok(result.indexOf(template) > -1, 'original template included');
166165
});
166+
167+
test("buildHtml works when testing not enabled", function(assert) {
168+
var service = this.subject();
169+
170+
service.getTwiddleJson = function() {
171+
return {
172+
"version": "0.5.0",
173+
"EmberENV": {
174+
"FEATURES": {}
175+
},
176+
"options": {
177+
"enable-testing": false
178+
},
179+
"dependencies": {
180+
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js"
181+
}
182+
};
183+
};
184+
185+
var gist = Ember.Object.create({
186+
initialRoute: "/"
187+
});
188+
189+
var output = service.buildHtml(gist, '/* app */', '/* styles */');
190+
191+
assert.ok(output.indexOf("window.location.hash='/';") > 0, "output sets initialRoute");
192+
assert.ok(output.indexOf('EmberENV = {"FEATURES":{}}') > 0, "output contains feature flags");
193+
assert.ok(output.indexOf('<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js"></script>') > 0, "output includes dependency");
194+
assert.ok(output.indexOf('<style type="text/css">/* styles */') > 0, "output includes styles");
195+
assert.ok(output.indexOf('<script type="text/javascript">/* app */') > 0, "output includes the app js");
196+
assert.ok(output.indexOf('<div id="ember-testing-container">') === -1, "output does not contain testing container");
197+
});
198+
199+
200+
201+
test("buildHtml works when testing is enabled", function(assert) {
202+
var service = this.subject();
203+
204+
service.getTwiddleJson = function() {
205+
return {
206+
"version": "0.5.0",
207+
"EmberENV": {
208+
"FEATURES": {}
209+
},
210+
"options": {
211+
"enable-testing": true
212+
},
213+
"dependencies": {
214+
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js"
215+
}
216+
};
217+
};
218+
219+
var gist = Ember.Object.create({});
220+
221+
var output = service.buildHtml(gist, '', '');
222+
223+
assert.ok(output.indexOf("window.location.hash='/';") === -1, "output does not set initialRoute if not provided");
224+
assert.ok(output.indexOf('<div id="qunit"></div>') > 0, "output contains qunit div");
225+
assert.ok(output.indexOf('<div id="qunit-fixture"></div>') > 0, "output contains qunit fixture div");
226+
assert.ok(output.indexOf('<div id="ember-testing-container">') > 0, "output contains testing container");
227+
assert.ok(output.indexOf('<div id="ember-testing"></div>') > 0, "output contains testing div");
228+
});

0 commit comments

Comments
 (0)