Skip to content

Commit

Permalink
fix(stark-core): re-create the last entry in the state history rather…
Browse files Browse the repository at this point in the history
… than add it again when a transition is dynamic.

ISSUES CLOSED: #773
  • Loading branch information
christophercr committed Oct 23, 2018
1 parent 7b6a61d commit 2af2657
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe("Service: StarkRoutingService", () => {
const inheritedParams: { [param: string]: any } = { requestId: "default value", seniority: undefined, onBehalfView: false };
const mockStates: Ng2StateDeclaration[] = [
{
name: "homepage", // the parent is defined in the state's name (contains a dot)
name: "homepage",
url: "/homepage",
params: {
...inheritedParams // ALL states will inherit these params
Expand All @@ -86,7 +86,7 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-01", // the parent is defined in the state's name (contains a dot)
name: "page-01",
url: "/page-01",
params: {}, // no params (inherits the ones from the parent/ancestor)
data: {
Expand All @@ -107,7 +107,7 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-01-01", // the parent is defined in the state's name (contains a dot)
name: "page-01-01",
url: "/page-01-01",
params: {}, // no params (inherits the ones from the parent/ancestor)
data: {
Expand All @@ -123,7 +123,7 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-01-02", // the parent is defined in the state's name (contains a dot)
name: "page-01-02",
url: "/page-01-02",
params: {}, // no params (inherits the ones from the parent/ancestor)
data: {
Expand All @@ -139,7 +139,7 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-01-02-01", // the parent is defined in the state's name (contains a dot)
name: "page-01-02-01",
url: "/page-01-02-01",
params: {}, // no params (inherits the ones from the parent/ancestor)
parent: "page-01-02",
Expand All @@ -150,7 +150,7 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-01-02-02", // the parent is defined in the state's name (contains a dot)
name: "page-01-02-02",
url: "/page-01-02-02",
params: {}, // no params (inherits the ones from the parent/ancestor)
data: {
Expand All @@ -164,7 +164,7 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-01-03", // the parent is defined in the state's name (contains a dot)
name: "page-01-03",
url: "/page-01-03",
params: {}, // no params (inherits the ones from the parent/ancestor)
data: {
Expand All @@ -180,7 +180,7 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-02", // the parent is defined in the state's name (contains a dot)
name: "page-02",
url: "/page-02",
params: {}, // no params (inherits the ones from the parent/ancestor)
data: {
Expand All @@ -196,7 +196,7 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-02-01", // the parent is defined in the state's name (contains a dot)
name: "page-02-01",
url: "/page-02-01",
params: {}, // no params (inherits the ones from the parent/ancestor)
data: {
Expand All @@ -212,7 +212,7 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-02-02", // the parent is defined in the state's name (contains a dot)
name: "page-02-02",
url: "/page-02-02",
params: {}, // no params (inherits the ones from the parent/ancestor)
data: {
Expand All @@ -228,7 +228,7 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-03", // the parent is defined in the state's name (contains a dot)
name: "page-03",
url: "/page-03",
params: {
baseParameter: "inherited"
Expand All @@ -247,9 +247,15 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-03-01", // the parent is defined in the state's name (contains a dot)
name: "page-03-01",
url: "/page-03-01",
params: {}, // no params (inherits the ones from the parent/ancestor)
params: {
// also inherits the ones from the parent/ancestor
dummyParameter: {
// dynamic parameter
dynamic: true
}
},
data: {
translationKey: "PAGE.03.01",
pageTitleColor: "black",
Expand All @@ -263,7 +269,7 @@ describe("Service: StarkRoutingService", () => {
}
},
{
name: "page-03-02", // the parent is defined in the state's name (contains a dot)
name: "page-03-02",
url: "/page-03-02",
params: {}, // no params (inherits the ones from the parent/ancestor)
data: {
Expand Down Expand Up @@ -889,6 +895,51 @@ describe("Service: StarkRoutingService", () => {
)
.subscribe(() => done(), (error: any) => fail(error));
});

it(
"should just be called ONCE in order to navigate to the previous state when the last navigation(s) were dynamic " +
"(same target state different params)",
(done: DoneFn) => {
spyOn($state, "go").and.callThrough();

routingService
.navigateTo("homepage")
.pipe(
catchError((error: any) => {
return throwError("navigateTo homepage " + error);
}),
switchMap(() => routingService.navigateTo("page-03-01", { dummyParameter: "abc" })),
catchError((error: any) => {
return throwError("navigateTo page-03-01 " + error);
}),
switchMap(() => routingService.navigateTo("page-03-01", { dummyParameter: "def" })), // dynamic transition
catchError((error: any) => {
return throwError("navigateTo page-03-01 " + error);
}),
switchMap(() => routingService.navigateTo("page-03-01", { dummyParameter: "xyz" })), // dynamic transition
catchError((error: any) => {
return throwError("navigateTo page-03-01 " + error);
}),
switchMap(() => {
return routingService.navigateToPrevious();
}),
tap((enteredState: StateObject) => {
expect(enteredState).toBeDefined();
expect(enteredState.name).toBe("homepage");
expect($state.go).toHaveBeenCalledTimes(5);
expect((<Spy>$state.go).calls.argsFor(0)).toEqual(["homepage", undefined, undefined]);
expect((<Spy>$state.go).calls.argsFor(1)).toEqual(["page-03-01", { dummyParameter: "abc" }, undefined]);
expect((<Spy>$state.go).calls.argsFor(2)).toEqual(["page-03-01", { dummyParameter: "def" }, undefined]);
expect((<Spy>$state.go).calls.argsFor(3)).toEqual(["page-03-01", { dummyParameter: "xyz" }, undefined]);
expect((<Spy>$state.go).calls.argsFor(4)).toEqual(["homepage", { ...inheritedParams }, undefined]);
}),
catchError((error: any) => {
return throwError("navigateToPrevious " + error);
})
)
.subscribe(() => done(), (error: any) => fail(error));
}
);
});

describe("reload", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,11 @@ export class StarkRoutingServiceImpl implements StarkRoutingService {

this.store.dispatch(new StarkNavigateSuccess(previousStateName, currentState.name(), currentStateParams));

if (transition.dynamic()) {
// for a dynamic transition, the last entry in the stateHistory should be re-created rather than being added again!
// so it should be removed first ;)
this._starkStateHistory.pop();
}
// Add the params of the current state to the _stateTreeParams array
this._starkStateHistory.push({ name: currentState.name(), params: currentStateParams });

Expand Down

0 comments on commit 2af2657

Please sign in to comment.