Skip to content

Commit

Permalink
Adding isActive
Browse files Browse the repository at this point in the history
  • Loading branch information
scalvert authored and rwjblue committed Jun 26, 2017
1 parent e7df1fd commit 5314c87
Show file tree
Hide file tree
Showing 5 changed files with 324 additions and 17 deletions.
64 changes: 64 additions & 0 deletions packages/ember-routing/lib/services/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@ import {
Service,
readOnly
} from 'ember-runtime';
import {
get,
isEmpty
} from 'ember-metal';
import { assign } from 'ember-utils';
import RouterDSL from '../system/dsl';


function shallowEqual(a, b) {
let k;
for (k in a) {
if (a.hasOwnProperty(k) && a[k] !== b[k]) { return false; }
}
for (k in b) {
if (b.hasOwnProperty(k) && a[k] !== b[k]) { return false; }
}
return true;
}

/**
The Router service is the public API that provides component/view layer
Expand All @@ -19,6 +37,7 @@ import {
const RouterService = Service.extend({
currentRouteName: readOnly('router.currentRouteName'),
currentURL: readOnly('router.currentURL'),
currentState: readOnly('router.currentState'),
location: readOnly('router.location'),
rootURL: readOnly('router.rootURL'),
router: null,
Expand Down Expand Up @@ -79,6 +98,51 @@ const RouterService = Service.extend({
*/
urlFor(/* routeName, ...models, options */) {
return this.router.generate(...arguments);
},

/**
Determines whether a route is active.
@method urlFor
@param {String} routeName the name of the route
@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 {String} the string representing the generated URL
@public
*/
isActive(/* routeName, ...models, options */) {
if (!this.router.isActive(...arguments)) { return false; }
debugger;
let { routeName, models, queryParams } = this._extractArguments(...arguments);
let emptyQueryParams = Object.keys(queryParams).length;

if (!emptyQueryParams) {
let visibleQueryParams = {};
assign(visibleQueryParams, queryParams);

this.router._prepareQueryParams(routeName, models, visibleQueryParams);
return shallowEqual(visibleQueryParams, queryParams);
}

return true;
},

_extractArguments(...args) {
let routeName;
let models;
let possibleQueryParams = args[args.length - 1];
let queryParams = {};

if (possibleQueryParams && possibleQueryParams.hasOwnProperty('queryParams')) {
queryParams = args.pop().queryParams;
}

routeName = args.shift();
models = args;

return { routeName, models, queryParams };
}
});

Expand Down
1 change: 1 addition & 0 deletions packages/ember-routing/lib/system/router_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default EmberObject.extend({
routerJsState: null,

isActiveIntent(routeName, models, queryParams, queryParamsMustMatch) {
debugger;
let state = this.routerJsState;
if (!this.routerJs.isActiveIntent(routeName, models, null, state)) { return false; }

Expand Down
243 changes: 243 additions & 0 deletions packages/ember/tests/routing/router_service_test/isActive_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
import {
Controller,
inject,
String
} from 'ember-runtime';
import { Component } from 'ember-glimmer';
import { Route, NoneLocation } from 'ember-routing';
import {
get,
set
} from 'ember-metal';
import {
RouterTestCase,
moduleFor
} from 'internal-test-helpers';

import { EMBER_ROUTING_ROUTER_SERVICE } from 'ember/features';

if (EMBER_ROUTING_ROUTER_SERVICE) {
moduleFor('Router Service - isActive', class extends RouterTestCase {
['@test RouterService#isActive returns true for simple route'](assert) {
assert.expect(1);

return this.visit('/')
.then(() => {
return this.routerService.transitionTo('parent.child');
})
.then(() => {
return this.routerService.transitionTo('parent.sister');
})
.then(() => {
assert.ok(this.routerService.isActive('parent.sister'));
});
}

['@test RouterService#isActive returns true for simple route with dynamic segments'](assert) {
assert.expect(1);

let dynamicModel = { id: 1 };

return this.visit('/')
.then(() => {
return this.routerService.transitionTo('dynamic', dynamicModel);
})
.then(() => {
assert.ok(this.routerService.isActive('dynamic', dynamicModel));
});
}

['@test RouterService#urlFor returns URL for simple route with basic query params'](assert) {
assert.expect(2);

let queryParams = this.buildQueryParams({ sort: 'ASC' });

this.registerController('parent.child', Controller.extend({
queryParams: ['sort'],
sort: 'ASC'
})
);
debugger;
return this.visit('/')
.then(() => {
return this.routerService.transitionTo('parent.child', queryParams);
})
.then(() => {
assert.ok(this.routerService.isActive('parent.child', queryParams));
assert.notOk(this.routerService.isActive('parent.child', this.buildQueryParams({ sort: 'DESC' })));
});
}

['@test RouterService#urlFor returns URL for simple route with array as query params'](assert) {
assert.expect(1);

let queryParams = this.buildQueryParams({ sort: ['ascending'] });

return this.visit('/')
.then(() => {
return this.routerService.transitionTo('parent.child', queryParams);
})
.then(() => {
assert.ok(this.routerService.isActive('parent.child', this.buildQueryParams({ sort: 'descending' })));
});
}

// ['@test RouterService#urlFor returns URL for simple route with null query params'](assert) {
// assert.expect(1);

// let queryParams = buildQueryParams({ foo: null });

// return this.visit('/').then(() => {
// let expectedURL = this.routerService.urlFor('parent.child', queryParams);

// assert.equal('/child', expectedURL);
// });
// }

// ['@test RouterService#urlFor returns URL for simple route with undefined query params'](assert) {
// assert.expect(1);

// let queryParams = buildQueryParams({ foo: undefined });

// return this.visit('/').then(() => {
// let expectedURL = this.routerService.urlFor('parent.child', queryParams);

// assert.equal('/child', expectedURL);
// });
// }

// ['@test RouterService#urlFor returns URL for simple route with dynamic segments and basic query params'](assert) {
// assert.expect(1);

// let queryParams = buildQueryParams({ foo: 'bar' });

// return this.visit('/').then(() => {
// let expectedURL = this.routerService.urlFor('dynamic', { id: 1 }, queryParams);

// assert.equal('/dynamic/1?foo=bar', expectedURL);
// });
// }

// ['@test RouterService#urlFor returns URL for simple route with dynamic segments and array as query params'](assert) {
// assert.expect(1);

// let queryParams = buildQueryParams({ selectedItems: ['a', 'b', 'c'] });

// return this.visit('/').then(() => {
// let expectedURL = this.routerService.urlFor('dynamic', { id: 1 }, queryParams);

// assert.equal('/dynamic/1?selectedItems[]=a&selectedItems[]=b&selectedItems[]=c', expectedURL);
// });
// }

// ['@test RouterService#urlFor returns URL for simple route with dynamic segments and null query params'](assert) {
// assert.expect(1);

// let queryParams = buildQueryParams({ foo: null });

// return this.visit('/').then(() => {
// let expectedURL = this.routerService.urlFor('dynamic', { id: 1 }, queryParams);

// assert.equal('/dynamic/1', expectedURL);
// });
// }

// ['@test RouterService#urlFor returns URL for simple route with dynamic segments and undefined query params'](assert) {
// assert.expect(1);

// let queryParams = buildQueryParams({ foo: undefined });

// return this.visit('/').then(() => {
// let expectedURL = this.routerService.urlFor('dynamic', { id: 1 }, queryParams);

// assert.equal('/dynamic/1', expectedURL);
// });
// }

// ['@test RouterService#urlFor correctly transitions to route via generated path'](assert) {
// assert.expect(1);

// let expectedURL;

// return this.visit('/')
// .then(() => {
// expectedURL = this.routerService.urlFor('parent.child');

// return this.routerService.transitionTo(expectedURL);
// })
// .then(() => {
// assert.equal(expectedURL, this.routerService.get('currentURL'));
// });
// }

// ['@test RouterService#urlFor correctly transitions to route via generated path with dynamic segments'](assert) {
// assert.expect(1);

// let expectedURL;
// let dynamicModel = { id: 1 };

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

// return this.visit('/')
// .then(() => {
// expectedURL = this.routerService.urlFor('dynamic', dynamicModel);

// return this.routerService.transitionTo(expectedURL);
// })
// .then(() => {
// assert.equal(expectedURL, this.routerService.get('currentURL'));
// });
// }

// ['@test RouterService#urlFor correctly transitions to route via generated path with query params'](assert) {
// assert.expect(1);

// let expectedURL;
// let actualURL;
// let queryParams = buildQueryParams({ foo: 'bar' });

// return this.visit('/')
// .then(() => {
// expectedURL = this.routerService.urlFor('parent.child', queryParams);

// return this.routerService.transitionTo(expectedURL);
// })
// .then(() => {
// actualURL = `${this.routerService.get('currentURL')}?foo=bar`;

// assert.equal(expectedURL, actualURL);
// });
// }

// ['@test RouterService#urlFor correctly transitions to route via generated path with dynamic segments and query params'](assert) {
// assert.expect(1);

// let expectedURL;
// let actualURL;
// let queryParams = buildQueryParams({ foo: 'bar' });
// let dynamicModel = { id: 1 };

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

// return this.visit('/')
// .then(() => {
// expectedURL = this.routerService.urlFor('dynamic', dynamicModel, queryParams);

// return this.routerService.transitionTo(expectedURL);
// })
// .then(() => {
// actualURL = `${this.routerService.get('currentURL')}?foo=bar`;

// assert.equal(expectedURL, actualURL);
// });
// }
});
}
Loading

0 comments on commit 5314c87

Please sign in to comment.