Skip to content

Commit 0b75f47

Browse files
kmmrajDivineDominion
authored andcommitted
Fix change action when the old route is longer than the new route
Fixes #88
1 parent 86af78f commit 0b75f47

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

ReSwiftRouter/Router.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,21 @@ open class Router<State: StateType>: StoreSubscriber {
156156
routeBuildingIndex -= 1
157157
}
158158

159+
// This is the 3. case:
160+
// "The new route has a different element after the commonSubroute, we need to replace
161+
// the old route element with the new one"
162+
if oldRoute.count > (commonSubroute + 1) && newRoute.count > (commonSubroute + 1) {
163+
let changeAction = RoutingActions.change(
164+
responsibleRoutableIndex: routableIndex(for: commonSubroute),
165+
segmentToBeReplaced: oldRoute[commonSubroute + 1],
166+
newSegment: newRoute[commonSubroute + 1])
167+
168+
routingActions.append(changeAction)
169+
}
159170
// This is the 1. case:
160171
// "The old route had an element after the commonSubroute and the new route does not
161172
// we need to pop the route segment after the commonSubroute"
162-
if oldRoute.count > newRoute.count {
173+
else if oldRoute.count > newRoute.count {
163174
let popAction = RoutingActions.pop(
164175
responsibleRoutableIndex: routableIndex(for: routeBuildingIndex - 1),
165176
segmentToBePopped: oldRoute[routeBuildingIndex]
@@ -168,17 +179,7 @@ open class Router<State: StateType>: StoreSubscriber {
168179
routingActions.append(popAction)
169180
routeBuildingIndex -= 1
170181
}
171-
// This is the 3. case:
172-
// "The new route has a different element after the commonSubroute, we need to replace
173-
// the old route element with the new one"
174-
else if oldRoute.count > (commonSubroute + 1) && newRoute.count > (commonSubroute + 1) {
175-
let changeAction = RoutingActions.change(
176-
responsibleRoutableIndex: routableIndex(for: commonSubroute),
177-
segmentToBeReplaced: oldRoute[commonSubroute + 1],
178-
newSegment: newRoute[commonSubroute + 1])
179-
180-
routingActions.append(changeAction)
181-
}
182+
182183

183184
// Push remainder of elements in new Route that weren't in old Route, this covers
184185
// the 2. case:

ReSwiftRouterTests/ReSwiftRouterTestsUnitTests.swift

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ class ReSwiftRouterUnitTests: QuickSpec {
5858
expect(action2Correct).to(beTrue())
5959
}
6060

61-
it("generates a Change action on the last common subroute") {
61+
it("generates a Change on the last common subroute for routes of same length") {
62+
6263
let oldRoute = [tabBarViewControllerIdentifier, counterViewControllerIdentifier]
6364
let newRoute = [tabBarViewControllerIdentifier, statsViewControllerIdentifier]
6465

@@ -83,7 +84,7 @@ class ReSwiftRouterUnitTests: QuickSpec {
8384
expect(new).to(equal(statsViewControllerIdentifier))
8485
}
8586

86-
it("generates a Change action on the last common subroute, also for routes of different length") {
87+
it("generates a Change on the last common subroute when new route is longer than the old route") {
8788
let oldRoute = [tabBarViewControllerIdentifier, counterViewControllerIdentifier]
8889
let newRoute = [tabBarViewControllerIdentifier, statsViewControllerIdentifier,
8990
infoViewControllerIdentifier]
@@ -119,6 +120,42 @@ class ReSwiftRouterUnitTests: QuickSpec {
119120
expect(action1Correct).to(beTrue())
120121
expect(action2Correct).to(beTrue())
121122
}
123+
124+
it("generates a Change on the last common subroute when the new route is shorter than the old route") {
125+
let oldRoute = [tabBarViewControllerIdentifier, counterViewControllerIdentifier,infoViewControllerIdentifier]
126+
let newRoute = [tabBarViewControllerIdentifier, statsViewControllerIdentifier]
127+
128+
let routingActions = Router<AppState>.routingActionsForTransition(from: oldRoute,
129+
to: newRoute)
130+
131+
var action1Correct: Bool?
132+
var action2Correct: Bool?
133+
134+
if case let RoutingActions.pop(responsibleRoutableIndex, segmentToBePopped)
135+
= routingActions[0] {
136+
137+
if responsibleRoutableIndex == 2
138+
&& segmentToBePopped == infoViewControllerIdentifier {
139+
140+
action1Correct = true
141+
}
142+
}
143+
144+
if case let RoutingActions.change(responsibleRoutableIndex, segmentToBeReplaced,
145+
newSegment)
146+
= routingActions[1] {
147+
148+
if responsibleRoutableIndex == 1
149+
&& segmentToBeReplaced == counterViewControllerIdentifier
150+
&& newSegment == statsViewControllerIdentifier{
151+
action2Correct = true
152+
}
153+
}
154+
155+
expect(routingActions).to(haveCount(2))
156+
expect(action1Correct).to(beTrue())
157+
expect(action2Correct).to(beTrue())
158+
}
122159

123160
it("generates a Change action on root when root element changes") {
124161
let oldRoute = [tabBarViewControllerIdentifier]

0 commit comments

Comments
 (0)