Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decouple application_lifecycle_test from global resolver #15328

Merged
merged 1 commit into from Jun 6, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
311 changes: 130 additions & 181 deletions packages/ember/tests/application_lifecycle_test.js
@@ -1,220 +1,169 @@
import {
moduleFor,
AutobootApplicationTestCase
} from 'internal-test-helpers';
import { Application } from 'ember-application';
import { Route, controllerFor } from 'ember-routing';
import { run } from 'ember-metal';
import { Route, Router } from 'ember-routing';
import {
Component,
setTemplates,
getTemplates
} from 'ember-glimmer';
import { jQuery } from 'ember-views';
import { compile } from 'ember-template-compiler';

let App, TEMPLATES, appInstance, router;

function setupApp(klass) {
run(function() {
App = klass.create({
rootElement: '#qunit-fixture'
});
moduleFor('Application Lifecycle - route hooks', class extends AutobootApplicationTestCase {

App.Router = App.Router.extend({
createApplication() {
let application = super.createApplication(...arguments);
this.add('router:main', Router.extend({
location: 'none'
});

App.deferReadiness();

appInstance = App.__deprecatedInstance__;
});
}

QUnit.module('Application Lifecycle', {
setup() {
TEMPLATES = getTemplates();
setupApp(Application.extend());
},

teardown() {
router = null;
run(App, 'destroy');
setTemplates({});
}));
return application;
}
});

function handleURL(path) {
router = appInstance.lookup('router:main');
return run(function() {
return router.handleURL(path).then(function(value) {
ok(true, 'url: `' + path + '` was handled');
return value;
}, function(reason) {
ok(false, reason);
throw reason;
constructor() {
super();
let menuItem = this.menuItem = {};

this.runTask(() => {
this.createApplication();

let SettingRoute = Route.extend({
setupController() {
this.controller.set('selectedMenuItem', menuItem);
},
deactivate() {
this.controller.set('selectedMenuItem', null);
}
});
this.add('route:index', SettingRoute);
this.add('route:application', SettingRoute);
});
});
}

QUnit.test('Resetting the application allows controller properties to be set when a route deactivates', function() {
App.Router.map(function() {
this.route('home', { path: '/' });
});

App.HomeRoute = Route.extend({
setupController() {
this.controllerFor('home').set('selectedMenuItem', 'home');
},
deactivate() {
this.controllerFor('home').set('selectedMenuItem', null);
}
});
App.ApplicationRoute = Route.extend({
setupController() {
this.controllerFor('application').set('selectedMenuItem', 'home');
},
deactivate() {
this.controllerFor('application').set('selectedMenuItem', null);
}
});

appInstance.lookup('router:main');

run(App, 'advanceReadiness');

handleURL('/');

equal(controllerFor(appInstance, 'home').get('selectedMenuItem'), 'home');
equal(controllerFor(appInstance, 'application').get('selectedMenuItem'), 'home');

App.reset();

equal(controllerFor(appInstance, 'home').get('selectedMenuItem'), null);
equal(controllerFor(appInstance, 'application').get('selectedMenuItem'), null);
});
}

QUnit.test('Destroying the application resets the router before the appInstance is destroyed', function() {
App.Router.map(function() {
this.route('home', { path: '/' });
});

App.HomeRoute = Route.extend({
setupController() {
this.controllerFor('home').set('selectedMenuItem', 'home');
},
deactivate() {
this.controllerFor('home').set('selectedMenuItem', null);
}
});
App.ApplicationRoute = Route.extend({
setupController() {
this.controllerFor('application').set('selectedMenuItem', 'home');
},
deactivate() {
this.controllerFor('application').set('selectedMenuItem', null);
}
});

appInstance.lookup('router:main');

run(App, 'advanceReadiness');

handleURL('/');

equal(controllerFor(appInstance, 'home').get('selectedMenuItem'), 'home');
equal(controllerFor(appInstance, 'application').get('selectedMenuItem'), 'home');

run(App, 'destroy');

equal(controllerFor(appInstance, 'home').get('selectedMenuItem'), null);
equal(controllerFor(appInstance, 'application').get('selectedMenuItem'), null);
});
get indexController() {
return this.applicationInstance.lookup('controller:index');
}

QUnit.test('Destroying a route after the router does create an undestroyed `toplevelView`', function() {
App.Router.map(function() {
this.route('home', { path: '/' });
});
get applicationController() {
return this.applicationInstance.lookup('controller:application');
}

setTemplates({
index: compile('Index!'),
application: compile('Application! {{outlet}}')
});
[`@test Resetting the application allows controller properties to be set when a route deactivates`](assert) {
let {
indexController,
applicationController
} = this;
assert.equal(indexController.get('selectedMenuItem'), this.menuItem);
assert.equal(applicationController.get('selectedMenuItem'), this.menuItem);

App.IndexRoute = Route.extend();
run(App, 'advanceReadiness');
this.application.reset();

handleURL('/');
assert.equal(indexController.get('selectedMenuItem'), null);
assert.equal(applicationController.get('selectedMenuItem'), null);
}

let router = appInstance.lookup('router:main');
let route = appInstance.lookup('route:index');
[`@test Destroying the application resets the router before the appInstance is destroyed`](assert) {
let {
indexController,
applicationController
} = this;
assert.equal(indexController.get('selectedMenuItem'), this.menuItem);
assert.equal(applicationController.get('selectedMenuItem'), this.menuItem);

run(router, 'destroy');
equal(router._toplevelView, null, 'the toplevelView was cleared');
this.runTask(() => {
this.application.destroy();
});

run(route, 'destroy');
equal(router._toplevelView, null, 'the toplevelView was not reinitialized');
assert.equal(indexController.get('selectedMenuItem'), null);
assert.equal(applicationController.get('selectedMenuItem'), null);
}

run(App, 'destroy');
equal(router._toplevelView, null, 'the toplevelView was not reinitialized');
});

QUnit.test('initializers can augment an applications customEvents hash', function(assert) {
assert.expect(1);
moduleFor('Application Lifecycle', class extends AutobootApplicationTestCase {

run(App, 'destroy');
createApplication() {
let application = super.createApplication(...arguments);
this.add('router:main', Router.extend({
location: 'none'
}));
return application;
}

let ApplicationSubclass = Application.extend();
[`@test Destroying a route after the router does create an undestroyed 'toplevelView'`](assert) {
this.runTask(() => {
this.createApplication();
this.addTemplate('index', `Index!`);
this.addTemplate('application', `Application! {{outlet}}`);
});

ApplicationSubclass.initializer({
name: 'customize-things',
initialize(application) {
application.customEvents = {
wowza: 'wowza'
};
}
});
let router = this.applicationInstance.lookup('router:main');
let route = this.applicationInstance.lookup('route:index');

setupApp(ApplicationSubclass);
this.runTask(() => router.destroy());
equal(router._toplevelView, null, 'the toplevelView was cleared');

App.FooBarComponent = Component.extend({
wowza() {
assert.ok(true, 'fired the event!');
}
});
this.runTask(() => route.destroy());
equal(router._toplevelView, null, 'the toplevelView was not reinitialized');

TEMPLATES['application'] = compile(`{{foo-bar}}`);
TEMPLATES['components/foo-bar'] = compile(`<div id='wowza-thingy'></div>`);
this.runTask(() => this.application.destroy());
equal(router._toplevelView, null, 'the toplevelView was not reinitialized');
}

run(App, 'advanceReadiness');
[`@test initializers can augment an applications customEvents hash`](assert) {
assert.expect(1);

run(() => jQuery('#wowza-thingy').trigger('wowza'));
});
let MyApplication = Application.extend();

QUnit.test('instanceInitializers can augment an the customEvents hash', function(assert) {
assert.expect(1);
MyApplication.initializer({
name: 'customize-things',
initialize(application) {
application.customEvents = {
wowza: 'wowza'
};
}
});

run(App, 'destroy');
this.runTask(() => {
this.createApplication({}, MyApplication);

let ApplicationSubclass = Application.extend();
this.add('component:foo-bar', Component.extend({
wowza() {
assert.ok(true, 'fired the event!');
}
}));

ApplicationSubclass.instanceInitializer({
name: 'customize-things',
initialize(application) {
application.customEvents = {
herky: 'jerky'
};
}
});
this.addTemplate('application', `{{foo-bar}}`);
this.addTemplate('components/foo-bar', `<div id='wowza-thingy'></div>`);
});

this.$('#wowza-thingy').trigger('wowza');
}

setupApp(ApplicationSubclass);
[`@test instanceInitializers can augment an the customEvents hash`](assert) {
assert.expect(1);

App.FooBarComponent = Component.extend({
jerky() {
assert.ok(true, 'fired the event!');
}
});
let MyApplication = Application.extend();

TEMPLATES['application'] = compile(`{{foo-bar}}`);
TEMPLATES['components/foo-bar'] = compile(`<div id='herky-thingy'></div>`);
MyApplication.instanceInitializer({
name: 'customize-things',
initialize(application) {
application.customEvents = {
herky: 'jerky'
};
}
});
this.runTask(() => {
this.createApplication({}, MyApplication);

run(App, 'advanceReadiness');
this.add('component:foo-bar', Component.extend({
jerky() {
assert.ok(true, 'fired the event!');
}
}));

run(() => jQuery('#herky-thingy').trigger('herky'));
this.addTemplate('application', `{{foo-bar}}`);
this.addTemplate('components/foo-bar', `<div id='herky-thingy'></div>`);
});

this.$('#herky-thingy').trigger('herky');
}
});