Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions src/ProgramTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ type ProgramTest model msg effect
Maybe
{ currentLocation : Url
, browserHistory : List Url
, browserForward : List Url
}
, effectSimulation : Maybe (EffectSimulation msg effect)
}
Expand Down Expand Up @@ -320,6 +321,7 @@ createHelper program options =
Just
{ currentLocation = baseUrl
, browserHistory = []
, browserForward = []
}
, effectSimulation = Maybe.map EffectSimulation.init options.deconstructEffect
}
Expand Down Expand Up @@ -1467,6 +1469,21 @@ queueSimulatedEffect effect programTest =
programTest
|> routeChangeHelper ("simulating effect: SimulatedEffect.Navigation.Back " ++ String.fromInt n) 2 (Url.toString first)

SimulatedEffect.Forward n ->
case state.navigation of
Nothing ->
programTest

Just navigation ->
case List.head navigation.browserForward of
Nothing ->
-- there's nothing to go forward to
programTest

Just first ->
programTest
|> routeChangeHelper ("simulating effect: SimulatedEffect.Navigation.Forward " ++ String.fromInt n) 0 (Url.toString first)


drain : ProgramTest model msg effect -> ProgramTest model msg effect
drain =
Expand Down Expand Up @@ -2045,10 +2062,10 @@ routeChangeHelper functionName removeFromBackStack url programTest =
Nothing ->
Finished (ProgramDoesNotSupportNavigation functionName)

Just { currentLocation, browserHistory } ->
Just navigation ->
let
newLocation =
Url.Extra.resolve currentLocation url
Url.Extra.resolve navigation.currentLocation url

processRouteChange =
case state.program.onRouteChange newLocation of
Expand All @@ -2063,10 +2080,14 @@ routeChangeHelper functionName removeFromBackStack url programTest =
{ state
| navigation =
Just
{ currentLocation = newLocation
, browserHistory =
(currentLocation :: browserHistory)
|> List.drop removeFromBackStack
{ navigation
| currentLocation = newLocation
, browserHistory =
(navigation.currentLocation :: navigation.browserHistory)
|> List.drop removeFromBackStack
, browserForward =
(navigation.currentLocation :: navigation.browserHistory)
|> List.take 1
}
}
|> processRouteChange
Expand Down
1 change: 1 addition & 0 deletions src/SimulatedEffect.elm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type SimulatedEffect msg
| PushUrl String
| ReplaceUrl String
| Back Int
| Forward Int


type SimulatedTask x a
Expand Down
3 changes: 3 additions & 0 deletions src/SimulatedEffect/Cmd.elm
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,6 @@ map f effect =

SimulatedEffect.Back n ->
SimulatedEffect.Back n

SimulatedEffect.Forward n ->
SimulatedEffect.Forward n
11 changes: 9 additions & 2 deletions src/SimulatedEffect/Navigation.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module SimulatedEffect.Navigation exposing (pushUrl, replaceUrl, back)
module SimulatedEffect.Navigation exposing (pushUrl, replaceUrl, back, forward)

{-| This module parallels [elm/browsers's `Browser.Navigation` module](https://package.elm-lang.org/packages/elm/browser/1.0.1/Browser-Navigation).
_Pull requests are welcome to add any functions that are missing._
Expand All @@ -9,7 +9,7 @@ to help you implement the function to provide when using [`ProgramTest.withSimul

# Navigate within Page

@docs pushUrl, replaceUrl, back
@docs pushUrl, replaceUrl, back, forward

-}

Expand Down Expand Up @@ -37,3 +37,10 @@ replaceUrl =
back : Int -> SimulatedEffect msg
back =
SimulatedEffect.Back


{-| Go forward some number of pages.
-}
forward : Int -> SimulatedEffect msg
forward =
SimulatedEffect.Forward
23 changes: 23 additions & 0 deletions tests/ProgramTestTests/SimulatedEffects/NavigationTest.elm
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,27 @@ all =
TestingProgram.application (SimulatedEffect.Navigation.pushUrl "https://example.com/new")
|> ProgramTest.update (ProduceEffects (SimulatedEffect.Navigation.back 0))
|> ProgramTest.expectBrowserUrl (Expect.equal "https://example.com/new")
, test "simulating forward undoes a back" <|
\() ->
TestingProgram.application (SimulatedEffect.Navigation.pushUrl "https://example.com/new")
|> ProgramTest.update (ProduceEffects (SimulatedEffect.Navigation.back 1))
|> ProgramTest.update Clear
|> ProgramTest.update (ProduceEffects (SimulatedEffect.Navigation.forward 1))
|> ProgramTest.expectModel (Expect.equal [ "OnUrlChange: https://example.com/new" ])
, test "simulating forward 2 only does one URL change" <|
\() ->
TestingProgram.application (SimulatedEffect.Navigation.pushUrl "https://example.com/new")
|> ProgramTest.update (ProduceEffects (SimulatedEffect.Navigation.pushUrl "https://example.com/new2"))
-- back: [new, path], current: new2, forward: []
|> Debug.log "A"
|> ProgramTest.update (ProduceEffects (SimulatedEffect.Navigation.back 2))
-- back: [], current: path, forward: [new, new2]
|> Debug.log "B"
|> ProgramTest.update Clear
|> ProgramTest.update (ProduceEffects (SimulatedEffect.Navigation.forward 2))
-- back: [new, path], current: new2, forward: []
|> Debug.log "C"
|> ProgramTest.expectModel (Expect.equal [ "OnUrlChange: https://example.com/new2" ])
, todo "forward 1 after back 2?"
, todo "what happens for real with invalid n?"
]