Skip to content
This repository has been archived by the owner on Sep 20, 2020. It is now read-only.

Commit

Permalink
fix(previous): Allow previous state to track states without URLs.
Browse files Browse the repository at this point in the history
Closes #175
  • Loading branch information
christopherthielen committed Jun 9, 2015
1 parent b53c1ef commit 9c4be9f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/previous.js
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
17 changes: 13 additions & 4 deletions test/previousSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
Expand Down Expand Up @@ -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 () {
Expand All @@ -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");
}))
});

Expand Down

0 comments on commit 9c4be9f

Please sign in to comment.