From 0f3c59ab1f4a85347652b4be344b2eb9d33605c8 Mon Sep 17 00:00:00 2001 From: Aaron VonderHaar Date: Mon, 2 Mar 2020 17:25:12 -0800 Subject: [PATCH] WIP: add forward --- src/ProgramTest.elm | 33 +++++++++++++++---- src/SimulatedEffect.elm | 1 + src/SimulatedEffect/Cmd.elm | 3 ++ src/SimulatedEffect/Navigation.elm | 11 +++++-- .../SimulatedEffects/NavigationTest.elm | 23 +++++++++++++ 5 files changed, 63 insertions(+), 8 deletions(-) diff --git a/src/ProgramTest.elm b/src/ProgramTest.elm index e37a59b..3a8b4ab 100644 --- a/src/ProgramTest.elm +++ b/src/ProgramTest.elm @@ -235,6 +235,7 @@ type ProgramTest model msg effect Maybe { currentLocation : Url , browserHistory : List Url + , browserForward : List Url } , effectSimulation : Maybe (EffectSimulation msg effect) } @@ -320,6 +321,7 @@ createHelper program options = Just { currentLocation = baseUrl , browserHistory = [] + , browserForward = [] } , effectSimulation = Maybe.map EffectSimulation.init options.deconstructEffect } @@ -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 = @@ -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 @@ -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 diff --git a/src/SimulatedEffect.elm b/src/SimulatedEffect.elm index 30d157e..ba5d08a 100644 --- a/src/SimulatedEffect.elm +++ b/src/SimulatedEffect.elm @@ -14,6 +14,7 @@ type SimulatedEffect msg | PushUrl String | ReplaceUrl String | Back Int + | Forward Int type SimulatedTask x a diff --git a/src/SimulatedEffect/Cmd.elm b/src/SimulatedEffect/Cmd.elm index cbcfff2..7105cd6 100644 --- a/src/SimulatedEffect/Cmd.elm +++ b/src/SimulatedEffect/Cmd.elm @@ -60,3 +60,6 @@ map f effect = SimulatedEffect.Back n -> SimulatedEffect.Back n + + SimulatedEffect.Forward n -> + SimulatedEffect.Forward n diff --git a/src/SimulatedEffect/Navigation.elm b/src/SimulatedEffect/Navigation.elm index fe9e146..8a1b49a 100644 --- a/src/SimulatedEffect/Navigation.elm +++ b/src/SimulatedEffect/Navigation.elm @@ -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._ @@ -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 -} @@ -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 diff --git a/tests/ProgramTestTests/SimulatedEffects/NavigationTest.elm b/tests/ProgramTestTests/SimulatedEffects/NavigationTest.elm index 7027e97..5571d0c 100644 --- a/tests/ProgramTestTests/SimulatedEffects/NavigationTest.elm +++ b/tests/ProgramTestTests/SimulatedEffects/NavigationTest.elm @@ -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?" ]