Skip to content

Commit

Permalink
Add test for Router.notFound
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarmil committed Aug 22, 2023
1 parent 18cc2a1 commit c9f92bf
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 13 deletions.
6 changes: 4 additions & 2 deletions tests/Remoting.Client/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,11 @@ let router : Router<Page, Model, Message> =
| Custom i -> $"/custom/{i}"
setRoute = fun s ->
match s.Trim('/').Split('/') with
| [|""|] -> Some (SetPage Home)
| [|"custom"; i|] -> Some (SetPage (Custom (int i)))
| [|""|] -> Some Home
| [|"custom"; i|] -> Some (Custom (int i))
| _ -> None
makeMessage = SetPage
notFound = None
}

type Tpl = Template<"main.html">
Expand Down
11 changes: 10 additions & 1 deletion tests/Unit.Client/Routing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ open Elmish

type Page =
| [<EndPoint "/">] Home
| [<EndPoint "/not-found">] NotFound
| [<EndPoint "/no-arg">] NoArg
| [<EndPoint "/with-arg">] WithArg of string
| [<EndPoint "/with-args">] WithArgs of string * int
Expand Down Expand Up @@ -80,7 +81,9 @@ let update msg model =
| SetPage p -> { model with page = p }

let router =
try Router.infer SetPage (fun m -> m.page)
try
Router.infer SetPage (fun m -> m.page)
|> Router.withNotFound NotFound
with e ->
eprintfn $"ROUTER ERROR: {e}"
reraise()
Expand All @@ -93,6 +96,7 @@ let innerPageClass = function

let rec pageClass = function
| Home -> "home"
| NotFound -> "notfound"
| NoArg -> "noarg"
| WithArg x -> $"witharg-{x}"
| WithArgs(x, y) -> $"withargs-{x}-{y}"
Expand Down Expand Up @@ -192,6 +196,11 @@ let view model dispatch =
on.click (fun _ -> dispatch (SetPage page))
url
}
a {
attr.``class`` "link-notfound"
attr.href "/invalid-url"
"/not-found"
}
span { attr.``class`` "current-page"; $"{model.page}" }
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Fixture.fs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ and NodeFixture(parent: unit -> IWebElement, by: By) =

/// Assert that the given value is eventually non-null.
member this.EventuallyNotNull(expr: Expr<'T>) =
this.Eventually <@ isNotNull %expr @>
this.Wait(fun () -> eval expr)

[<AutoOpen>]
module Extensions =
Expand Down
14 changes: 7 additions & 7 deletions tests/Unit/Tests/Html.fs
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@ module Html =
inp.Clear()

inp.SendKeys("ab")
elt.EventuallyNotNull <@ elt.ByClass("condBoolIs2") @>
elt.EventuallyNotNull <@ elt.ByClass("condBoolIs2") @> |> ignore
testNull <@ elt.ByClass("condBoolIsNot2") @>

inp.SendKeys("c")
elt.EventuallyNotNull <@ elt.ByClass("condBoolIsNot2") @>
elt.EventuallyNotNull <@ elt.ByClass("condBoolIsNot2") @> |> ignore
testNull <@ elt.ByClass("condBoolIs2") @>

[<Test>]
let ``Union cond reacts to events``() =
let inp = elt.ByClass("condUnionInput")
inp.Clear()

elt.EventuallyNotNull <@ elt.ByClass("condUnionIsEmpty") @>
elt.EventuallyNotNull <@ elt.ByClass("condUnionIsEmpty") @> |> ignore
testNull <@ elt.ByClass("condUnionIsOne") @>
testNull <@ elt.ByClass("condUnionIsMany") @>

inp.SendKeys("a")
elt.EventuallyNotNull <@ elt.ByClass("condUnionIsOne") @>
elt.EventuallyNotNull <@ elt.ByClass("condUnionIsOne") @> |> ignore
testNull <@ elt.ByClass("condUnionIsEmpty") @>
testNull <@ elt.ByClass("condUnionIsMany") @>

inp.SendKeys("b")
elt.EventuallyNotNull <@ elt.ByClass("condUnionIsMany") @>
elt.EventuallyNotNull <@ elt.ByClass("condUnionIsMany") @> |> ignore
testNull <@ elt.ByClass("condUnionIsOne") @>
testNull <@ elt.ByClass("condUnionIsEmpty") @>

Expand All @@ -77,7 +77,7 @@ module Html =
inp.Clear()

inp.SendKeys("ABC")
elt.EventuallyNotNull <@ elt.ByClass("forEachIsA") @>
elt.EventuallyNotNull <@ elt.ByClass("forEachIsA") @> |> ignore
testNotNull <@ elt.ByClass("forEachIsB") @>
testNotNull <@ elt.ByClass("forEachIsC") @>

Expand All @@ -92,7 +92,7 @@ module Html =
inp.Clear()

inp.SendKeys("ABC")
elt.EventuallyNotNull <@ elt.ByClass("forLoopIsA") @>
elt.EventuallyNotNull <@ elt.ByClass("forLoopIsA") @> |> ignore
testNotNull <@ elt.ByClass("forLoopIsB") @>
testNotNull <@ elt.ByClass("forLoopIsC") @>

Expand Down
12 changes: 10 additions & 2 deletions tests/Unit/Tests/Routing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,23 @@ module Routing =
[<Test; TestCaseSource("links"); NonParallelizable>]
let ``Click link``(linkCls: string, url: string, print: string) =
elt.ByClass("link-" + linkCls).Click()
elt.Eventually <@ elt.ByClass("current-page").Text = print @>
let currentPage = elt.EventuallyNotNull <@ elt.ByClass("current-page") @>
elt.Eventually <@ currentPage.Text = print @>
test <@ WebFixture.Driver.Url = WebFixture.Url + url @>

[<Test; TestCaseSource("links"); NonParallelizable>]
let ``Set by model``(linkCls: string, url: string, print: string) =
elt.ByClass("btn-" + linkCls).Click()
elt.Eventually <@ elt.ByClass("current-page").Text = print @>
let currentPage = elt.EventuallyNotNull <@ elt.ByClass("current-page") @>
elt.Eventually <@ currentPage.Text = print @>
test <@ WebFixture.Driver.Url = WebFixture.Url + url @>

[<Test; NonParallelizable>]
let ``Not found``() =
elt.ByClass("link-notfound").Click()
let currentPage = elt.EventuallyNotNull <@ elt.ByClass("current-page") @>
elt.Eventually <@ currentPage.Text = "NotFound" @>

let failingRouter<'T> (expectedError: UnionCaseInfo[] -> InvalidRouterKind) =
TestCaseData(
(fun () -> Router.infer<'T, _, _> id id |> ignore),
Expand Down

0 comments on commit c9f92bf

Please sign in to comment.