diff --git a/src/components/router/ActivatedRoute.xml b/src/components/router/ActivatedRoute.xml
index c52187d..b548af3 100644
--- a/src/components/router/ActivatedRoute.xml
+++ b/src/components/router/ActivatedRoute.xml
@@ -9,6 +9,7 @@
+
diff --git a/src/components/router/Router.brs b/src/components/router/Router.brs
index 8b43315..8135ec5 100644
--- a/src/components/router/Router.brs
+++ b/src/components/router/Router.brs
@@ -1,48 +1,48 @@
' @import /components/buildUrl.brs from @dazn/kopytko-utils
' @import /components/getProperty.brs from @dazn/kopytko-utils
' @import /components/NodeUtils.brs from @dazn/kopytko-utils
-' @import /components/ternary.brs from @dazn/kopytko-utils
' @import /components/utils/KopytkoGlobalNode.brs
+
sub init()
_global = KopytkoGlobalNode()
_global.addFields({
router: m.top,
})
- m.top.activatedRoute = CreateObject("roSGNode", "ActivatedRoute")
+ m.top.activatedRoute = _createRoute()
m._history = []
end sub
-sub navigate(data as Object)
- url = buildUrl(data.path, data.params)
+sub navigate(navigateData as Object)
+ url = buildUrl(navigateData.path, navigateData.params)
if (url = m.top.url) then return ' Avoid doubling url
- if (data.skipInHistory = Invalid OR (NOT data.skipInHistory))
+ isBackJourney = getProperty(navigateData, "isBackJourney", false)
+
+ if (NOT getProperty(navigateData, "skipInHistory", false))
_updateHistory()
end if
- if (NOT m.top.activatedRoute.shouldSkip)
- m.top.previousRoute = NodeUtils().cloneNode(m.top.activatedRoute)
- end if
+ ' Needs to be set before activatedRoute as _getPreviousRoute uses the previous value of activatedRoute.
+ m.top.previousRoute = _getPreviousRoute(isBackJourney)
- m.top.activatedRoute.path = data.path
- m.top.activatedRoute.params = ternary(data.params <> Invalid, data.params, {})
- m.top.activatedRoute.backJourneyData = data.backJourneyData
- m.top.activatedRoute.isBackJourney = getProperty(data, "isBackJourney", false)
+ m.top.activatedRoute.path = getProperty(navigateData, "path", "")
+ m.top.activatedRoute.params = getProperty(navigateData, "params", {})
+ m.top.activatedRoute.backJourneyData = navigateData.backJourneyData
+ m.top.activatedRoute.isBackJourney = isBackJourney
m.top.activatedRoute.shouldSkip = false
- m.top.activatedRoute.virtualPath = ""
+ m.top.activatedRoute.virtualPath = getProperty(navigateData, "virtualPath", "")
m.top.url = url
end sub
-function back(data = {} as Object) as Boolean
- previousLocation = m._history.pop()
- if (previousLocation = Invalid)
- return false
- end if
+function back(_backData = {} as Object) as Boolean
+ previousRoute = m._history.pop()
+
+ if (previousRoute = Invalid) then return false
- previousLocation.skipInHistory = true
- previousLocation.isBackJourney = true
- navigate(previousLocation)
+ previousRoute.skipInHistory = true
+ previousRoute.isBackJourney = true
+ navigate(previousRoute)
return true
end function
@@ -51,22 +51,25 @@ sub resetHistory(rootPath = "" as String)
m._history = []
if (rootPath <> "")
- m._history.push({ path: rootPath, params: {} })
+ m._history.push(_createRoute({ path: rootPath }))
end if
end sub
+function _getPreviousRoute(isBackJourney as Boolean) as Object
+ if (isBackJourney OR m.top.activatedRoute.shouldSkip) then return m._history.peek()
+
+ return NodeUtils().cloneNode(m.top.activatedRoute)
+end function
+
sub _updateHistory()
- if (m.top.url = "" OR m.top.activatedRoute.shouldSkip)
- return
- end if
+ if (m.top.url = "" OR m.top.activatedRoute.shouldSkip) then return
- m._history.push(_createHistoryItem(m.top.activatedRoute))
+ m._history.push(NodeUtils().cloneNode(m.top.activatedRoute))
end sub
-function _createHistoryItem(route as Object) as Object
- return {
- path: route.path,
- params: route.params,
- backJourneyData: route.backJourneyData,
- }
+function _createRoute(routeData = {} as Object) as Object
+ route = CreateObject("roSGNode", "ActivatedRoute")
+ route.setFields(routeData)
+
+ return route
end function
diff --git a/src/components/router/_tests/Router.test.brs b/src/components/router/_tests/Router.test.brs
index 72a78de..334ffd1 100644
--- a/src/components/router/_tests/Router.test.brs
+++ b/src/components/router/_tests/Router.test.brs
@@ -1,5 +1,6 @@
' @import /components/KopytkoFrameworkTestSuite.brs from @dazn/kopytko-unit-testing-framework
' @mock /components/utils/KopytkoGlobalNode.brs
+
function TestSuite__Router() as Object
ts = KopytkoFrameworkTestSuite()
ts.name = "Router"
@@ -55,11 +56,11 @@ function TestSuite__Router() as Object
path: "/previous-path",
params: { previousParam: "previousValue" },
backJourneyData: { data: "kopytko" },
- isBackJourney: true,
+ isBackJourney: false,
shouldSkip: false,
virtualPath: "/virtual-path",
}
- m.top.activatedRoute.setFields(data)
+ navigate(data)
' When
navigate({ path: "new-path" })
@@ -160,6 +161,38 @@ function TestSuite__Router() as Object
return ts.assertTrue(result)
end function)
+ ts.addTest("back - sets proper previousRoute", function (ts as Object) as String
+ ' Given
+ data = {
+ path: "/old-path",
+ params: { previousParam: "oldValue" },
+ backJourneyData: { data: "old" },
+ isBackJourney: false,
+ shouldSkip: false,
+ virtualPath: "/virtual-path",
+ }
+ navigate(data)
+ navigate({ path: "new-path" })
+ navigate({ path: "newest-path" })
+
+ ' When
+ back()
+
+ ' Then
+ previousRoute = m.top.previousRoute
+ actual = {
+ backJourneyData: previousRoute.backJourneyData,
+ isBackJourney: previousRoute.isBackJourney,
+ params: previousRoute.params,
+ path: previousRoute.path,
+ shouldSkip: previousRoute.shouldSkip,
+ virtualPath: previousRoute.virtualPath,
+ }
+ expected = data
+
+ return ts.assertEqual(actual, expected)
+ end function)
+
ts.addTest("back - returns false if empty history", function (ts as Object) as String
result = back()
@@ -244,6 +277,7 @@ end function
sub RouterTestSuite__TearDown(ts as Object)
m.top.activatedRoute = CreateObject("roSGNode", "ActivatedRoute")
+ m.top.previousRoute = Invalid
m.top.url = "/"
m._history = []