Skip to content

Commit

Permalink
Fix failing specs
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Jul 17, 2014
1 parent b42bd9a commit 1a28f94
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 44 deletions.
34 changes: 1 addition & 33 deletions modules/mixins/ActiveState.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,5 @@
var ActiveStore = require('../stores/ActiveStore');

function routeIsActive(activeRoutes, routeName) {
return activeRoutes.some(function (route) {
return route.props.name === routeName;
});
}

function paramsAreActive(activeParams, params) {
for (var property in params) {
if (activeParams[property] !== String(params[property]))
return false;
}

return true;
}

function queryIsActive(activeQuery, query) {
for (var property in query) {
if (activeQuery[property] !== String(query[property]))
return false;
}

return true;
}

/**
* A mixin for components that need to know about the routes, params,
* and query that are currently active. Components that use it get two
Expand Down Expand Up @@ -62,15 +38,7 @@ var ActiveState = {
* Returns true if the route with the given name, URL parameters, and query
* are all currently active.
*/
isActive: function (routeName, params, query) {
var state = ActiveStore.getState();
var isActive = routeIsActive(state.routes, routeName) && paramsAreActive(state.params, params);

if (query)
return isActive && queryIsActive(state.query, query);

return isActive;
}
isActive: ActiveStore.isActive

},

Expand Down
41 changes: 33 additions & 8 deletions modules/stores/ActiveStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ var _activeRoutes = [];
var _activeParams = {};
var _activeQuery = {};

function routeIsActive(routeName) {
return _activeRoutes.some(function (route) {
return route.props.name === routeName;
});
}

function paramsAreActive(params) {
for (var property in params) {
if (_activeParams[property] !== String(params[property]))
return false;
}

return true;
}

function queryIsActive(query) {
for (var property in query) {
if (_activeQuery[property] !== String(query[property]))
return false;
}

return true;
}

var EventEmitter = require('event-emitter');
var _events = EventEmitter();

Expand Down Expand Up @@ -45,15 +69,16 @@ var ActiveStore = {
},

/**
* Returns an object with the currently active `routes`, `params`,
* and `query`.
* Returns true if the route with the given name, URL parameters, and query
* are all currently active.
*/
getState: function () {
return {
routes: _activeRoutes,
params: _activeParams,
query: _activeQuery
};
isActive: function (routeName, params, query) {
var isActive = routeIsActive(routeName) && paramsAreActive(params);

if (query)
return isActive && queryIsActive(query);

return isActive;
}

};
Expand Down
6 changes: 3 additions & 3 deletions specs/ActiveStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('when a Route is active', function () {

describe('and it has no params', function () {
beforeEach(function () {
ActiveStore.update({
ActiveStore.updateState({
activeRoutes: [ route ]
});
});
Expand All @@ -29,7 +29,7 @@ describe('when a Route is active', function () {

describe('and the right params are given', function () {
beforeEach(function () {
ActiveStore.update({
ActiveStore.updateState({
activeRoutes: [ route ],
activeParams: { id: '123', show: 'true' },
activeQuery: { search: 'abc' }
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('when a Route is active', function () {

describe('and the wrong params are given', function () {
beforeEach(function () {
ActiveStore.update({
ActiveStore.updateState({
activeRoutes: [ route ],
activeParams: { id: 123 }
});
Expand Down

0 comments on commit 1a28f94

Please sign in to comment.