Skip to content

Commit

Permalink
Merge pull request #14861 from scalvert/router-service
Browse files Browse the repository at this point in the history
Phase 2 of public Router service - adding replaceWith
  • Loading branch information
rwjblue authored Jan 22, 2017
2 parents 13c26ed + ae016ea commit 9e24191
Show file tree
Hide file tree
Showing 7 changed files with 376 additions and 311 deletions.
23 changes: 22 additions & 1 deletion packages/ember-routing/lib/services/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,28 @@ const RouterService = Service.extend({
@public
*/
transitionTo() {
this.router.transitionTo(...arguments);
return this.router.transitionTo(...arguments);
},

/**
Transition into another route while replacing the current URL, if possible.
The route may be either a single route or route path:
See [Route.replaceWith](http://emberjs.com/api/classes/Ember.Route.html#method_replaceWith) for more info.
@method replaceWith
@category ember-routing-router-service
@param {String} name the name of the route or a URL
@param {...Object} models the model(s) or identifier(s) to be used while
transitioning to the route.
@param {Object} [options] optional hash with a queryParams property
containing a mapping of query parameters
@return {Transition} the transition object associated with this
attempted transition
@public
*/
replaceWith() {
return this.router.replaceWith(...arguments);
}
});

Expand Down
256 changes: 18 additions & 238 deletions packages/ember/tests/routing/router_service_test/basic_test.js
Original file line number Diff line number Diff line change
@@ -1,317 +1,97 @@
import Logger from 'ember-console';
import {
Controller,
inject
} from 'ember-runtime';
import { inject } from 'ember-runtime';
import { Component } from 'ember-glimmer';
import { Route, NoneLocation } from 'ember-routing';
import {
run,
get,
set
} from 'ember-metal';
import { jQuery } from 'ember-views';
import {
ApplicationTestCase,
RouterTestCase,
moduleFor
} from 'internal-test-helpers';

import { isFeatureEnabled } from 'ember-metal';

if (isFeatureEnabled('ember-routing-router-service')) {
moduleFor('Router Service - main', class extends ApplicationTestCase {
constructor() {
super();

this.router.map(function() {
this.route('parent', { path: '/' }, function() {
this.route('child');
this.route('sister');
this.route('brother');
});
this.route('dynamic', { path: '/dynamic/:post_id' });
});
}

moduleFor('Router Service - main', class extends RouterTestCase {
['@test RouterService#currentRouteName is correctly set for top level route'](assert) {
assert.expect(1);

let routerService;

this.registerRoute('parent.index', Route.extend({
routerService: inject.service('router'),
init() {
this._super();
routerService = get(this, 'routerService');
}
}));

return this.visit('/').then(() => {
assert.equal(routerService.get('currentRouteName'), 'parent.index');
assert.equal(this.routerService.get('currentRouteName'), 'parent.index');
});
}

['@test RouterService#currentRouteName is correctly set for child route'](assert) {
assert.expect(1);

let routerService;

this.registerRoute('parent.child', Route.extend({
routerService: inject.service('router'),
init() {
this._super();
routerService = get(this, 'routerService');
}
}));

return this.visit('/child').then(() => {
assert.equal(routerService.get('currentRouteName'), 'parent.child');
assert.equal(this.routerService.get('currentRouteName'), 'parent.child');
});
}

['@test RouterService#currentRouteName is correctly set after transition'](assert) {
assert.expect(1);

let routerService;

this.registerRoute('parent.child', Route.extend({
routerService: inject.service('router'),
init() {
this._super();
routerService = get(this, 'routerService');
},

afterModel() {
this.transitionTo('parent.sister');
}
}));

return this.visit('/child').then(() => {
assert.equal(routerService.get('currentRouteName'), 'parent.sister');
});
return this.visit('/child')
.then(() => {
return this.routerService.transitionTo('parent.sister');
})
.then(() => {
assert.equal(this.routerService.get('currentRouteName'), 'parent.sister');
});
}

['@test RouterService#currentRouteName is correctly set on each transition'](assert) {
assert.expect(3);

let routerService;

this.registerRoute('parent.child', Route.extend({
routerService: inject.service('router'),
init() {
this._super();
routerService = get(this, 'routerService');
}
}));

return this.visit('/child')
.then(() => {
assert.equal(routerService.get('currentRouteName'), 'parent.child');
assert.equal(this.routerService.get('currentRouteName'), 'parent.child');

return this.visit('/sister');
})
.then(() => {
assert.equal(routerService.get('currentRouteName'), 'parent.sister');
assert.equal(this.routerService.get('currentRouteName'), 'parent.sister');

return this.visit('/brother');
})
.then(() => {
assert.equal(routerService.get('currentRouteName'), 'parent.brother');
assert.equal(this.routerService.get('currentRouteName'), 'parent.brother');
});
}

['@test RouterService#rootURL is correctly set to the default value'](assert) {
assert.expect(1);

let routerService;

this.registerRoute('parent.index', Route.extend({
routerService: inject.service('router'),
init() {
this._super();
routerService = get(this, 'routerService');
}
}));

return this.visit('/').then(() => {
assert.equal(routerService.get('rootURL'), '/');
assert.equal(this.routerService.get('rootURL'), '/');
});
}

['@test RouterService#rootURL is correctly set to a custom value'](assert) {
assert.expect(1);

let routerService;

this.registerRoute('parent.index', Route.extend({
routerService: inject.service('router'),
init() {
this._super();
set(this.router, 'rootURL', '/homepage');
routerService = get(this, 'routerService');
}
}));

return this.visit('/').then(() => {
assert.equal(routerService.get('rootURL'), '/homepage');
assert.equal(this.routerService.get('rootURL'), '/homepage');
});
}

['@test RouterService#location is correctly delegated from router:main'](assert) {
assert.expect(2);

let routerService;

this.registerRoute('parent.index', Route.extend({
routerService: inject.service('router'),
init() {
this._super();
routerService = get(this, 'routerService');
}
}));

return this.visit('/').then(() => {
let location = routerService.get('location');
let location = this.routerService.get('location');
assert.ok(location);
assert.ok(location instanceof NoneLocation);
});
}

['@test RouterService#transitionTo with basic route'](assert) {
assert.expect(1);

let routerService;
let componentInstance;

this.registerRoute('parent.index', Route.extend({
routerService: inject.service('router'),
init() {
this._super();
routerService = get(this, 'routerService');
}
}));

this.registerTemplate('parent.index', '{{foo-bar}}');

this.registerComponent('foo-bar', {
ComponentClass: Component.extend({
routerService: inject.service('router'),
init() {
this._super();
componentInstance = this;
},
actions: {
transitionToSister() {
get(this, 'routerService').transitionTo('parent.sister');
}
}
}),
template: `foo-bar`
});

return this.visit('/').then(() => {
run(function() {
componentInstance.send('transitionToSister');
});

assert.equal(routerService.get('currentRouteName'), 'parent.sister');
});
}

['@test RouterService#transitionTo with dynamic segment'](assert) {
assert.expect(3);

let routerService;
let componentInstance;
let dynamicModel = { id: 1, contents: 'much dynamicism' };

this.registerRoute('parent.index', Route.extend({
routerService: inject.service('router'),
init() {
this._super();
routerService = get(this, 'routerService');
}
}));

this.registerTemplate('parent.index', '{{foo-bar}}');
this.registerTemplate('dynamic', '{{model.contents}}');

this.registerComponent('foo-bar', {
ComponentClass: Component.extend({
routerService: inject.service('router'),
init() {
this._super();
componentInstance = this;
},
actions: {
transitionToDynamic() {
get(this, 'routerService').transitionTo('dynamic', dynamicModel);
}
}
}),
template: `foo-bar`
});

return this.visit('/').then(() => {
run(function() {
componentInstance.send('transitionToDynamic');
});

assert.equal(routerService.get('currentRouteName'), 'dynamic');
assert.equal(routerService.get('currentURL'), '/dynamic/1');
this.assertText('much dynamicism');
});
}

['@test RouterService#transitionTo with dynamic segment and model hook'](assert) {
assert.expect(3);

let routerService;
let componentInstance;
let dynamicModel = { id: 1, contents: 'much dynamicism' };

this.registerRoute('parent.index', Route.extend({
routerService: inject.service('router'),
init() {
this._super();
routerService = get(this, 'routerService');
}
}));

this.registerRoute('dynamic', Route.extend({
model() {
return dynamicModel;
}
}));

this.registerTemplate('parent.index', '{{foo-bar}}');
this.registerTemplate('dynamic', '{{model.contents}}');

this.registerComponent('foo-bar', {
ComponentClass: Component.extend({
routerService: inject.service('router'),
init() {
this._super();
componentInstance = this;
},
actions: {
transitionToDynamic() {
get(this, 'routerService').transitionTo('dynamic', 1);
}
}
}),
template: `foo-bar`
});

return this.visit('/').then(() => {
run(function() {
componentInstance.send('transitionToDynamic');
});

assert.equal(routerService.get('currentRouteName'), 'dynamic');
assert.equal(routerService.get('currentURL'), '/dynamic/1');
this.assertText('much dynamicism');
});
}
});
}
Loading

0 comments on commit 9e24191

Please sign in to comment.