Skip to content

Commit

Permalink
Begin removing jQuery usage in application tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Dec 22, 2017
1 parent 9bc6697 commit 022b1ec
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 85 deletions.
24 changes: 14 additions & 10 deletions packages/ember-application/tests/system/application_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ import {

moduleFor('Ember.Application, autobooting multiple apps', class extends ApplicationTestCase {
constructor() {
jQuery('#qunit-fixture').html(`
let fixture = document.querySelector('#qunit-fixture');
fixture.innerHTML = `
<div id="one">
<div id="one-child">HI</div>
</div>
<div id="two">HI</div>
`);
`;
super();
}

Expand Down Expand Up @@ -213,7 +214,6 @@ moduleFor('Ember.Application, default resolver with autoboot', class extends Def

[`@test Minimal Application initialized with just an application template`]() {
this.setupFixture('<script type="text/x-handlebars">Hello World</script>');

this.runTask(() => this.createApplication());
this.assertInnerHTML('Hello World');
}
Expand All @@ -236,14 +236,14 @@ moduleFor('Ember.Application, autobooting', class extends AutobootApplicationTes
super.teardown();
}

[`@test initialized application goes to initial route`](assert) {
[`@test initialized application goes to initial route`]() {
this.runTask(() => {
this.createApplication();
this.addTemplate('application', '{{outlet}}');
this.addTemplate('index', '<h1>Hi from index</h1>');
});

assert.equal(this.$('h1').text(), 'Hi from index');
this.assertText('Hi from index');
}

[`@test ready hook is called before routing begins`](assert) {
Expand Down Expand Up @@ -290,18 +290,18 @@ moduleFor('Ember.Application, autobooting', class extends AutobootApplicationTes
// need to make some assertions about the created router
let router = this.application.__deprecatedInstance__.lookup('router:main');
assert.equal(router instanceof Router, true, 'Router was set from initialize call');
assert.equal(this.$('h1').text(), 'Hello!');
this.assertText('Hello!');
}

[`@test Application Controller backs the appplication template`](assert) {
[`@test Application Controller backs the appplication template`]() {
this.runTask(() => {
this.createApplication();
this.addTemplate('application', '<h1>{{greeting}}</h1>');
this.add('controller:application', Controller.extend({
greeting: 'Hello!'
}));
});
assert.equal(this.$('h1').text(), 'Hello!');
this.assertText('Hello!');
}

[`@test enable log of libraries with an ENV var`](assert) {
Expand All @@ -321,8 +321,12 @@ moduleFor('Ember.Application, autobooting', class extends AutobootApplicationTes
this.runTask(() => this.createApplication());

assert.equal(messages[1], 'Ember : ' + VERSION);
assert.equal(messages[2], 'jQuery : ' + jQuery().jquery);
assert.equal(messages[3], 'my-lib : ' + '2.0.0a');
if (jQuery) {
assert.equal(messages[2], 'jQuery : ' + jQuery().jquery);
assert.equal(messages[3], 'my-lib : ' + '2.0.0a');
} else {
assert.equal(messages[2], 'my-lib : ' + '2.0.0a');
}

libraries.deRegister('my-lib');
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-application/tests/system/bootstrap-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ moduleFor('Ember.Application with default resolver and autoboot', class extends

['@test templates in script tags are extracted at application creation'](assert) {
this.runTask(() => this.createApplication());
assert.equal(this.$('#app').text(), 'Hello World!');
assert.equal(document.querySelector('#app').textContent, 'Hello World!');
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ moduleFor('Ember.Application with extended default resolver and autoboot', class
});
}

[`@test a resolver can be supplied to application`](assert) {
[`@test a resolver can be supplied to application`]() {
this.runTask(() => this.createApplication());
assert.equal(this.$('h1').text(), 'Fallback');
this.assertText('Fallback');
}

});
9 changes: 3 additions & 6 deletions packages/ember-application/tests/system/initializers_test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { assign } from 'ember-utils';
import { moduleFor, AutobootApplicationTestCase } from 'internal-test-helpers';
import { Application } from 'ember-application';
import { jQuery } from 'ember-views';

moduleFor('Ember.Application initializers', class extends AutobootApplicationTestCase {
constructor() {
jQuery('#qunit-fixture').html(`
<div id="one">ONE</div>
get fixture() {
return `<div id="one">ONE</div>
<div id="two">TWO</div>
`);
super();
`;
}

get applicationOptions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import { assign } from 'ember-utils';
import { moduleFor, AutobootApplicationTestCase } from 'internal-test-helpers';
import { Application, ApplicationInstance } from 'ember-application';
import { jQuery } from 'ember-views';

moduleFor('Ember.Application instance initializers', class extends AutobootApplicationTestCase {
constructor() {
jQuery('#qunit-fixture').html(`
<div id="one">ONE</div>
get fixture() {
return `<div id="one">ONE</div>
<div id="two">TWO</div>
`);
super();
`;
}

get applicationOptions() {
Expand Down
102 changes: 43 additions & 59 deletions packages/ember-application/tests/system/visit_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import Engine from '../../system/engine';
import { Route } from 'ember-routing';
import { Component, helper } from 'ember-glimmer';
import { compile } from 'ember-template-compiler';
import { jQuery } from 'ember-views';

function expectAsyncError() {
RSVP.off('error');
Expand All @@ -29,6 +28,13 @@ moduleFor('Ember.Application - visit()', class extends ApplicationTestCase {
return super.createApplication(options, Application.extend());
}

assertEmptyFixture(message) {
this.assert.strictEqual(
document.querySelector('#qunit-fixture').children.length, 0,
`there are no elements in the fixture element ${message ? message : ''}`
);
}

// This tests whether the application is "autobooted" by registering an
// instance initializer and asserting it never gets run. Since this is
// inherently testing that async behavior *doesn't* happen, we set a
Expand Down Expand Up @@ -274,18 +280,15 @@ moduleFor('Ember.Application - visit()', class extends ApplicationTestCase {
[`@test visit() returns a promise that resolves when the view has rendered`](assert) {
this.addTemplate('application', `<h1>Hello world</h1>`);

assert.strictEqual(
this.$().children().length, 0,
'there are no elements in the fixture element'
);
this.assertEmptyFixture();

return this.visit('/').then(instance => {
assert.ok(
instance instanceof ApplicationInstance,
'promise is resolved with an ApplicationInstance'
);
assert.equal(
this.$('h1').text(), 'Hello world',
this.element.textContent, 'Hello world',
'the application was rendered once the promise resolves'
);
});
Expand All @@ -296,20 +299,15 @@ moduleFor('Ember.Application - visit()', class extends ApplicationTestCase {

this.addTemplate('application', '<h1>Hello world</h1>');

assert.strictEqual(
this.$().children().length, 0,
'there are no elements in the fixture element'
);
this.assertEmptyFixture();

return this.visit('/', { shouldRender: false }).then(instance => {
assert.ok(
instance instanceof ApplicationInstance,
'promise is resolved with an ApplicationInstance'
);
assert.strictEqual(
this.$().children().length, 0,
'there are still no elements in the fixture element after visit'
);

this.assertEmptyFixture('after visit');
});
}

Expand All @@ -318,18 +316,15 @@ moduleFor('Ember.Application - visit()', class extends ApplicationTestCase {

this.addTemplate('application', '<h1>Hello world</h1>');

assert.strictEqual(
this.$('#qunit-fixture').children().length, 0,
'there are no elements in the fixture element'
);
this.assertEmptyFixture();

return this.visit('/', { shouldRender: true }).then(instance => {
assert.ok(
instance instanceof ApplicationInstance,
'promise is resolved with an ApplicationInstance'
);
assert.strictEqual(
this.$().children().length, 1,
document.querySelector('#qunit-fixture').children.length, 1,
'there is 1 element in the fixture element after visit'
);
});
Expand All @@ -352,20 +347,15 @@ moduleFor('Ember.Application - visit()', class extends ApplicationTestCase {
let BlogMap = function() {};
this.add('route-map:blog', BlogMap);

assert.strictEqual(
this.$('#qunit-fixture').children().length, 0,
'there are no elements in the fixture element'
);
this.assertEmptyFixture();

return this.visit('/blog', { shouldRender: false }).then(instance => {
assert.ok(
instance instanceof ApplicationInstance,
'promise is resolved with an ApplicationInstance'
);
assert.strictEqual(
this.$().children().length, 0,
'there are still no elements in the fixture element after visit'
);

this.assertEmptyFixture('after visit');
});
}

Expand Down Expand Up @@ -398,18 +388,15 @@ moduleFor('Ember.Application - visit()', class extends ApplicationTestCase {
let BlogMap = function() {};
this.add('route-map:blog', BlogMap);

assert.strictEqual(
this.$('#qunit-fixture').children().length, 0,
'there are no elements in the fixture element'
);
this.assertEmptyFixture();

return this.visit('/blog', { isInteractive: false }).then(instance => {
assert.ok(
instance instanceof ApplicationInstance,
'promise is resolved with an ApplicationInstance'
);
assert.strictEqual(
this.$().find('p').text(), 'Dis cache money',
this.element.querySelector('p').textContent, 'Dis cache money',
'Engine component is resolved'
);
});
Expand Down Expand Up @@ -439,14 +426,11 @@ moduleFor('Ember.Application - visit()', class extends ApplicationTestCase {
let BlogMap = function() {};
this.add('route-map:blog', BlogMap);

assert.strictEqual(
this.$().children().length, 0,
'there are no elements in the fixture element'
);
this.assertEmptyFixture();

return this.visit('/blog', { shouldRender: true }).then(() => {
assert.strictEqual(
this.$().find('p').text(), 'Dis cache money',
this.element.querySelector('p').textContent, 'Dis cache money',
'Engine component is resolved'
);
});
Expand Down Expand Up @@ -475,14 +459,11 @@ moduleFor('Ember.Application - visit()', class extends ApplicationTestCase {
let BlogMap = function() {};
this.add('route-map:blog', BlogMap);

assert.strictEqual(
this.$().children().length, 0,
'there are no elements in the fixture element'
);
this.assertEmptyFixture();

return this.visit('/blog', { shouldRender: true }).then(() => {
assert.strictEqual(
this.$().text(), 'turnt up',
this.element.textContent, 'turnt up',
'Engine component is resolved'
);
});
Expand Down Expand Up @@ -576,18 +557,21 @@ moduleFor('Ember.Application - visit()', class extends ApplicationTestCase {
}
}));

let $foo = jQuery('<div />').appendTo('#qunit-fixture');
let $bar = jQuery('<div />').appendTo('#qunit-fixture');
let fixtureElement = document.querySelector('#qunit-fixture');
let foo = document.createElement('div');
let bar = document.createElement('div');
fixtureElement.appendChild(foo);
fixtureElement.appendChild(bar);

let data = encodeURIComponent(JSON.stringify({ name: 'Godfrey' }));
let instances = [];

return RSVP.all([
this.runTask(() => {
return this.application.visit(`/x-foo?data=${data}`, { rootElement: $foo[0] });
return this.application.visit(`/x-foo?data=${data}`, { rootElement: foo });
}),
this.runTask(() => {
return this.application.visit('/x-bar', { rootElement: $bar[0] });
return this.application.visit('/x-bar', { rootElement: bar });
})
]).then(_instances => {
instances = _instances;
Expand All @@ -598,28 +582,28 @@ moduleFor('Ember.Application - visit()', class extends ApplicationTestCase {
assert.ok(xBarInitCalled);
assert.ok(xBarDidInsertElementCalled);

assert.equal($foo.find('h1').text(), 'X-Foo');
assert.equal($foo.find('p').text(), 'Hello Godfrey, I have been clicked 0 times (0 times combined)!');
assert.ok($foo.text().indexOf('X-Bar') === -1);
assert.equal(foo.querySelector('h1').textContent, 'X-Foo');
assert.equal(foo.querySelector('p').textContent, 'Hello Godfrey, I have been clicked 0 times (0 times combined)!');
assert.ok(foo.textContent.indexOf('X-Bar') === -1);

assert.equal($bar.find('h1').text(), 'X-Bar');
assert.equal($bar.find('button').text(), 'Join 0 others in clicking me!');
assert.ok($bar.text().indexOf('X-Foo') === -1);
assert.equal(bar.querySelector('h1').textContent, 'X-Bar');
assert.equal(bar.querySelector('button').textContent, 'Join 0 others in clicking me!');
assert.ok(bar.textContent.indexOf('X-Foo') === -1);

this.runTask(() => {
$foo.find('x-foo').click();
this.click(foo.querySelector('x-foo'));
});

assert.equal($foo.find('p').text(), 'Hello Godfrey, I have been clicked 1 times (1 times combined)!');
assert.equal($bar.find('button').text(), 'Join 1 others in clicking me!');
assert.equal(foo.querySelector('p').textContent, 'Hello Godfrey, I have been clicked 1 times (1 times combined)!');
assert.equal(bar.querySelector('button').textContent, 'Join 1 others in clicking me!');

this.runTask(() => {
$bar.find('button').click();
$bar.find('button').click();
this.click(bar.querySelector('button'));
this.click(bar.querySelector('button'));
});

assert.equal($foo.find('p').text(), 'Hello Godfrey, I have been clicked 1 times (3 times combined)!');
assert.equal($bar.find('button').text(), 'Join 3 others in clicking me!');
assert.equal(foo.querySelector('p').textContent, 'Hello Godfrey, I have been clicked 1 times (3 times combined)!');
assert.equal(bar.querySelector('button').textContent, 'Join 3 others in clicking me!');

}).finally(() => {
this.runTask(() => {
Expand Down

0 comments on commit 022b1ec

Please sign in to comment.