From 9c4be9f3e73c9229dc371af70a3a2ade3980a75a Mon Sep 17 00:00:00 2001 From: christopherthielen Date: Mon, 8 Jun 2015 21:27:27 -0500 Subject: [PATCH] fix(previous): Allow previous state to track states without URLs. Closes #175 --- src/previous.js | 4 ++-- test/previousSpec.js | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/previous.js b/src/previous.js index 4af2669..ecbfcaa 100644 --- a/src/previous.js +++ b/src/previous.js @@ -8,10 +8,10 @@ angular.module('ct.ui.router.extras.previous', [ 'ct.ui.router.extras.core', 'ct // Check if the fromState is navigable before tracking it. // Root state doesn't get decorated with $$state(). Doh. var fromState = from.state && from.state.$$state && from.state.$$state(); - if (fromState && fromState.navigable) { + if (fromState) { lastPrevious = previous; previous = $transition$.from; - } else { + $transition$.promise.then(commit)['catch'](revert); function commit() { lastPrevious = null; } function revert() { previous = lastPrevious; } diff --git a/test/previousSpec.js b/test/previousSpec.js index a24237e..2424599 100644 --- a/test/previousSpec.js +++ b/test/previousSpec.js @@ -8,7 +8,7 @@ function getPreviousMockStates() { states.push({ name: 'aside2', url: '/aside2'}); // Root of main app states - states.push({ name: 'top', url: '/' }); + states.push({ name: 'top' }); // Personnel tab states.push({ name: 'top.people', url: 'people' }); @@ -59,6 +59,15 @@ describe("$previousState", function () { expect($previousState.get()).toBeNull(); })); + // Test for #175 + it("should not capture root state (or non-navigable states)", inject(function($rootScope) { + testGo("top", { entered: 'top' }); + testGo("top.cust", { entered: 'top.cust' }); + var prev = $previousState.get(); + expect(prev).toBeDefined(); + expect(prev.state.name).toBe("top"); + })); + describe('.go()', function () { it("should transition back to the previous state", function () { @@ -73,15 +82,15 @@ describe("$previousState", function () { describe('.get()', function() { // Test for #120 it("should not return current state, after a transition is cancelled", inject(function($rootScope) { - testGo("top", { entered: 'top' }); - testGo("top.people.managerlist", { entered: ['top.people', 'top.people.managerlist'] }); + testGo("top.people", { entered: [ 'top', 'top.people' ] }); + testGo("top.people.managerlist", { entered: ['top.people.managerlist'] }); var transitionNum = 0; $rootScope.$on("$stateChangeStart", function(evt) { if (transitionNum++ === 0) { evt.preventDefault(); } }); testGo("top.inv.storelist", undefined, { redirect: 'top.people.managerlist'}); // Cancelled, so we're still at original state - expect($previousState.get().state.name).toBe("top"); + expect($previousState.get().state.name).toBe("top.people"); })) });