Skip to content

Commit

Permalink
Add Scenario.forward/Scenario.back
Browse files Browse the repository at this point in the history
  • Loading branch information
arowM committed Jul 17, 2023
1 parent 513c147 commit 72b7d40
Showing 1 changed file with 170 additions and 0 deletions.
170 changes: 170 additions & 0 deletions src/Tepa/Scenario.elm
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ module Tepa.Scenario exposing
, HttpRequestBody(..)
, portResponse
, randomResponse
, forward
, back
, fromJust
, fromOk
, RenderConfig
Expand Down Expand Up @@ -134,6 +136,12 @@ module Tepa.Scenario exposing
@docs randomResponse
## Browser Simulators
@docs forward
@docs back
# Conditions
@docs fromJust
Expand Down Expand Up @@ -1772,6 +1780,168 @@ randomResponse (Session session) markup param =
}


{-| Simulate borwser forward event.
If there are no pages to forward, the test fails.
-}
forward :
Session
-> Markup
-> Scenario flags m
forward (Session session) markup =
let
description =
"[" ++ session.uniqueName ++ "] " ++ stringifyInlineItems markup.content
in
Scenario
{ test =
\_ context ->
case Dict.get session.uniqueName context.sessions of
Nothing ->
SeqTest.fail description <|
\_ ->
Expect.fail
"forward: The application is not active on the session. Use `loadApp` beforehand."

Just sessionContext ->
case History.forward 1 sessionContext.history of
Nothing ->
SeqTest.fail description <|
\_ ->
Expect.fail
"forward: No pages to forward."

Just newHistory ->
let
updateResult =
update context
(Core.UrlChange <| History.current newHistory)
{ sessionContext
| history = newHistory
}
in
case updateResult of
SessionExpired ->
SeqTest.pass
{ context
| sessions =
Dict.remove session.uniqueName
context.sessions
}

SessionUpdateFailed err ->
SeqTest.fail description <|
\_ -> Expect.fail err

SessionUpdated nextSessionContext ->
SeqTest.pass
{ context
| sessions =
Dict.insert session.uniqueName
nextSessionContext
context.sessions
}
, markup =
\config ->
let
markup_ =
config.processSystemScenario
{ uniqueSessionName = session.uniqueName }
markup
in
if markup_.appear then
MdBuilder.appendListItem markup_.content
>> MdBuilder.appendBlocks markup_.detail
>> MdBuilder.break
>> Ok

else
Ok
}


{-| Simulate borwser back event.
If there are no pages to back, the test fails.
-}
back :
Session
-> Markup
-> Scenario flags m
back (Session session) markup =
let
description =
"[" ++ session.uniqueName ++ "] " ++ stringifyInlineItems markup.content
in
Scenario
{ test =
\_ context ->
case Dict.get session.uniqueName context.sessions of
Nothing ->
SeqTest.fail description <|
\_ ->
Expect.fail
"back: The application is not active on the session. Use `loadApp` beforehand."

Just sessionContext ->
case History.back 1 sessionContext.history of
Nothing ->
SeqTest.fail description <|
\_ ->
Expect.fail
"back: No pages to back."

Just newHistory ->
let
updateResult =
update context
(Core.UrlChange <| History.current newHistory)
{ sessionContext
| history = newHistory
}
in
case updateResult of
SessionExpired ->
SeqTest.pass
{ context
| sessions =
Dict.remove session.uniqueName
context.sessions
}

SessionUpdateFailed err ->
SeqTest.fail description <|
\_ -> Expect.fail err

SessionUpdated nextSessionContext ->
SeqTest.pass
{ context
| sessions =
Dict.insert session.uniqueName
nextSessionContext
context.sessions
}
, markup =
\config ->
let
markup_ =
config.processSystemScenario
{ uniqueSessionName = session.uniqueName }
markup
in
if markup_.appear then
MdBuilder.appendListItem markup_.content
>> MdBuilder.appendBlocks markup_.detail
>> MdBuilder.break
>> Ok

else
Ok
}


wrapBySpec : Random.Spec a -> a -> Result String Core.RandomValue
wrapBySpec spec =
case spec of
Expand Down

0 comments on commit 72b7d40

Please sign in to comment.