Skip to content

Commit

Permalink
fix: wrong previousRoute after back journey (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
bchelkowski authored Dec 5, 2023
1 parent ba91baa commit cdd6652
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/components/router/ActivatedRoute.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<field id="renderedUrl" type="string" />
<field id="routeConfig" type="assocarray" />
<field id="shouldSkip" type="boolean" />
<field id="skipInHistory" type="boolean" />
<field id="virtualPath" type="string" />
</interface>
</component>
67 changes: 35 additions & 32 deletions src/components/router/Router.brs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
38 changes: 36 additions & 2 deletions src/components/router/_tests/Router.test.brs
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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" })
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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 = []
Expand Down

0 comments on commit cdd6652

Please sign in to comment.