Skip to content

Commit

Permalink
docs(rxApp): added docs/checks for duplicate keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Lamping committed May 5, 2014
1 parent 6c4ab32 commit fd360e0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/rxApp/rxApp.js
Expand Up @@ -6,7 +6,8 @@ angular.module('encore.ui.rxApp', ['encore.ui.rxEnvironment', 'ngSanitize', 'ngR
* @property {string} title Only used on the top level, defines the title to use for all sub-navigation
*
* Common Properties for all 'children' nav items:
* @property {string|object} href String url to use for the menu item or object to passed to rxEnvironmentUrl
* @property {string} [key] ID to use for getter/setter methods by apps. Needs to be unique.
* @property {string|object} href Url to use for the menu item or object to passed to rxEnvironmentUrl
* @property {string} linkText The text displayed for the menu item
* @property {array} children Child menu items for the navigation heirarchy
* @property {string} directive Name of directive to build and show when item is active. For example:
Expand Down Expand Up @@ -195,6 +196,7 @@ angular.module('encore.ui.rxApp', ['encore.ui.rxEnvironment', 'ngSanitize', 'ngR

var getRouteIndex = function (key, routes) {
var routeIndex;
var routeFound = false;

_.forEach(routes, function (route, index) {
if (route.key === key) {
Expand All @@ -206,9 +208,10 @@ angular.module('encore.ui.rxApp', ['encore.ui.rxEnvironment', 'ngSanitize', 'ngR
routeIndex = [index].concat(childIndex);
}
}
if (routeIndex) {
// return false to exit search
return false;
if (routeIndex && !routeFound) {
routeFound = true;
} else {
$log.warn('Duplicate routes found for key: ' + key);
}
});

Expand Down Expand Up @@ -236,7 +239,7 @@ angular.module('encore.ui.rxApp', ['encore.ui.rxEnvironment', 'ngSanitize', 'ngR

return {
/**
* finds the indexes/path to a route
* Finds the indexes/path to a route. Will return last match if duplicate keys exist
* @see setRouteByKey for actual use
* @param {string} key Route Key
* @example
Expand Down
34 changes: 32 additions & 2 deletions src/rxApp/rxApp.spec.js
@@ -1,7 +1,7 @@
/* jshint node: true */

describe('rxAppRoutes', function () {
var appRoutes, myAppRoutes, envSvc, generatedRoutes, location, rootScope;
var appRoutes, myAppRoutes, envSvc, generatedRoutes, location, rootScope, log;

var fakeRoutes = [{
href: { tld: 'example', path: 'myPath' },
Expand Down Expand Up @@ -44,11 +44,12 @@ describe('rxAppRoutes', function () {
});

// Inject in angular constructs
inject(function (rxAppRoutes, Environment, $location, $rootScope) {
inject(function (rxAppRoutes, Environment, $location, $rootScope, $log) {
appRoutes = rxAppRoutes;
envSvc = Environment;
location = $location;
rootScope = $rootScope;
log = $log;
});

// set environment to build from
Expand All @@ -62,6 +63,10 @@ describe('rxAppRoutes', function () {
generatedRoutes = myAppRoutes.getAll();
});

afterEach(function () {
log.reset();
});

it('should build url property from rxEnvironmentUrl', function () {
// first item should have generated URL based on staging href
expect(generatedRoutes[0].url).to.equal('example/myPath');
Expand Down Expand Up @@ -141,6 +146,31 @@ describe('rxAppRoutes', function () {
expect(secondChildIndex, 'second child index').to.eql([0, 1]);
});

it('should warn if duplicate keys found', function () {
var duplicateKeyRoutes = [{
href: '/r/BrokenGifs',
key: 'dupeKey'
}, {
href: '/r/wheredidthesodago',
key: 'nonDupeKey',
children: [{
href: '/r/funny',
key: 'dupeKey'
}]
}];

var routes = new appRoutes(duplicateKeyRoutes);

// find index w/duplicate key
var index = routes.getIndexByKey('dupeKey');

// should return last match
expect(index).to.eql([1, 0]);

// should log a message about duplicate keys
expect(log.warn.logs.length).to.equal(1);
});

describe('setRouteByKey', function () {
it('should allow updating a nav item by key', function () {
var newRouteData = {
Expand Down

0 comments on commit fd360e0

Please sign in to comment.