From e3db0bced096f14691c518867a48a33f9d9ca9d8 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Mon, 27 Feb 2023 20:55:16 -0500 Subject: [PATCH] Xunit -> Expecto (#7) --- .../Giraffe.Htmx.Common.Tests.fsproj | 30 - src/Common.Tests/Program.fs | 1 - src/Common.Tests/Tests.fs | 35 -- src/Giraffe.Htmx.sln | 22 +- src/Htmx.Tests/Giraffe.Htmx.Tests.fsproj | 32 - src/Htmx.Tests/Program.fs | 1 - src/Htmx.Tests/Tests.fs | 372 ----------- src/Tests/Common.fs | 33 + src/Tests/Htmx.fs | 342 ++++++++++ src/Tests/Program.fs | 6 + src/Tests/Tests.fsproj | 25 + src/Tests/ViewEngine.fs | 578 +++++++++++++++++ .../Giraffe.ViewEngine.Htmx.Tests.fsproj | 31 - src/ViewEngine.Htmx.Tests/Program.fs | 1 - src/ViewEngine.Htmx.Tests/Tests.fs | 583 ------------------ 15 files changed, 989 insertions(+), 1103 deletions(-) delete mode 100644 src/Common.Tests/Giraffe.Htmx.Common.Tests.fsproj delete mode 100644 src/Common.Tests/Program.fs delete mode 100644 src/Common.Tests/Tests.fs delete mode 100644 src/Htmx.Tests/Giraffe.Htmx.Tests.fsproj delete mode 100644 src/Htmx.Tests/Program.fs delete mode 100644 src/Htmx.Tests/Tests.fs create mode 100644 src/Tests/Common.fs create mode 100644 src/Tests/Htmx.fs create mode 100644 src/Tests/Program.fs create mode 100644 src/Tests/Tests.fsproj create mode 100644 src/Tests/ViewEngine.fs delete mode 100644 src/ViewEngine.Htmx.Tests/Giraffe.ViewEngine.Htmx.Tests.fsproj delete mode 100644 src/ViewEngine.Htmx.Tests/Program.fs delete mode 100644 src/ViewEngine.Htmx.Tests/Tests.fs diff --git a/src/Common.Tests/Giraffe.Htmx.Common.Tests.fsproj b/src/Common.Tests/Giraffe.Htmx.Common.Tests.fsproj deleted file mode 100644 index 58aa294..0000000 --- a/src/Common.Tests/Giraffe.Htmx.Common.Tests.fsproj +++ /dev/null @@ -1,30 +0,0 @@ - - - - false - false - - - - - - - - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - diff --git a/src/Common.Tests/Program.fs b/src/Common.Tests/Program.fs deleted file mode 100644 index fdc31cd..0000000 --- a/src/Common.Tests/Program.fs +++ /dev/null @@ -1 +0,0 @@ -module Program = let [] main _ = 0 diff --git a/src/Common.Tests/Tests.fs b/src/Common.Tests/Tests.fs deleted file mode 100644 index 4172698..0000000 --- a/src/Common.Tests/Tests.fs +++ /dev/null @@ -1,35 +0,0 @@ -module Tests - -open Giraffe.Htmx -open Xunit - -/// Tests for the HxSwap module -module Swap = - - [] - let ``InnerHtml is correct`` () = - Assert.Equal ("innerHTML", HxSwap.InnerHtml) - - [] - let ``OuterHtml is correct`` () = - Assert.Equal ("outerHTML", HxSwap.OuterHtml) - - [] - let ``BeforeBegin is correct`` () = - Assert.Equal ("beforebegin", HxSwap.BeforeBegin) - - [] - let ``BeforeEnd is correct`` () = - Assert.Equal ("beforeend", HxSwap.BeforeEnd) - - [] - let ``AfterBegin is correct`` () = - Assert.Equal ("afterbegin", HxSwap.AfterBegin) - - [] - let ``AfterEnd is correct`` () = - Assert.Equal ("afterend", HxSwap.AfterEnd) - - [] - let ``None is correct`` () = - Assert.Equal ("none", HxSwap.None) diff --git a/src/Giraffe.Htmx.sln b/src/Giraffe.Htmx.sln index c1ebafb..3e73efa 100644 --- a/src/Giraffe.Htmx.sln +++ b/src/Giraffe.Htmx.sln @@ -5,15 +5,11 @@ VisualStudioVersion = 16.0.30114.105 MinimumVisualStudioVersion = 10.0.40219.1 Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Htmx", "Htmx\Giraffe.Htmx.fsproj", "{8AB3085C-5236-485A-8565-A09106E72E1E}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Htmx.Tests", "Htmx.Tests\Giraffe.Htmx.Tests.fsproj", "{D7CDD578-7A6F-4EF6-846A-80A55037E049}" -EndProject Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.ViewEngine.Htmx", "ViewEngine.Htmx\Giraffe.ViewEngine.Htmx.fsproj", "{F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.ViewEngine.Htmx.Tests", "ViewEngine.Htmx.Tests\Giraffe.ViewEngine.Htmx.Tests.fsproj", "{F21C28CE-1F18-4CB0-B2F7-10DABE84FB78}" -EndProject Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Htmx.Common", "Common\Giraffe.Htmx.Common.fsproj", "{75D66845-F93A-4463-AD29-A8B16E4D4BA9}" EndProject -Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Htmx.Common.Tests", "Common.Tests\Giraffe.Htmx.Common.Tests.fsproj", "{E261A653-68D5-4D7B-99A4-F09282B50F8A}" +Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Tests", "Tests\Tests.fsproj", "{39823773-4311-4E79-9CA0-F9DDC40CAF6A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -28,25 +24,17 @@ Global {8AB3085C-5236-485A-8565-A09106E72E1E}.Debug|Any CPU.Build.0 = Debug|Any CPU {8AB3085C-5236-485A-8565-A09106E72E1E}.Release|Any CPU.ActiveCfg = Release|Any CPU {8AB3085C-5236-485A-8565-A09106E72E1E}.Release|Any CPU.Build.0 = Release|Any CPU - {D7CDD578-7A6F-4EF6-846A-80A55037E049}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D7CDD578-7A6F-4EF6-846A-80A55037E049}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D7CDD578-7A6F-4EF6-846A-80A55037E049}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D7CDD578-7A6F-4EF6-846A-80A55037E049}.Release|Any CPU.Build.0 = Release|Any CPU {F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Debug|Any CPU.Build.0 = Debug|Any CPU {F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Release|Any CPU.ActiveCfg = Release|Any CPU {F718B3C1-EE01-4F04-ABCE-BF2AE700FDA9}.Release|Any CPU.Build.0 = Release|Any CPU - {F21C28CE-1F18-4CB0-B2F7-10DABE84FB78}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F21C28CE-1F18-4CB0-B2F7-10DABE84FB78}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F21C28CE-1F18-4CB0-B2F7-10DABE84FB78}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F21C28CE-1F18-4CB0-B2F7-10DABE84FB78}.Release|Any CPU.Build.0 = Release|Any CPU {75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Debug|Any CPU.Build.0 = Debug|Any CPU {75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Release|Any CPU.ActiveCfg = Release|Any CPU {75D66845-F93A-4463-AD29-A8B16E4D4BA9}.Release|Any CPU.Build.0 = Release|Any CPU - {E261A653-68D5-4D7B-99A4-F09282B50F8A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {E261A653-68D5-4D7B-99A4-F09282B50F8A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {E261A653-68D5-4D7B-99A4-F09282B50F8A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {E261A653-68D5-4D7B-99A4-F09282B50F8A}.Release|Any CPU.Build.0 = Release|Any CPU + {39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {39823773-4311-4E79-9CA0-F9DDC40CAF6A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/src/Htmx.Tests/Giraffe.Htmx.Tests.fsproj b/src/Htmx.Tests/Giraffe.Htmx.Tests.fsproj deleted file mode 100644 index 056f57c..0000000 --- a/src/Htmx.Tests/Giraffe.Htmx.Tests.fsproj +++ /dev/null @@ -1,32 +0,0 @@ - - - - false - false - - - - - - - - - - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - diff --git a/src/Htmx.Tests/Program.fs b/src/Htmx.Tests/Program.fs deleted file mode 100644 index fdc31cd..0000000 --- a/src/Htmx.Tests/Program.fs +++ /dev/null @@ -1 +0,0 @@ -module Program = let [] main _ = 0 diff --git a/src/Htmx.Tests/Tests.fs b/src/Htmx.Tests/Tests.fs deleted file mode 100644 index 213ac70..0000000 --- a/src/Htmx.Tests/Tests.fs +++ /dev/null @@ -1,372 +0,0 @@ -module Giraffe.Htmx.Tests - -open System -open Giraffe.Htmx -open Microsoft.AspNetCore.Http -open NSubstitute -open Xunit - -/// Tests for the IHeaderDictionary extension properties -module IHeaderDictionaryExtensions = - - [] - let ``HxBoosted succeeds when the header is not present`` () = - let ctx = Substitute.For () - ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore - Option.isNone ctx.Request.Headers.HxBoosted |> Assert.True - - [] - let ``HxBoosted succeeds when the header is present and true`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Boosted", "true") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Option.isSome ctx.Request.Headers.HxBoosted |> Assert.True - Option.get ctx.Request.Headers.HxBoosted |> Assert.True - - [] - let ``HxBoosted succeeds when the header is present and false`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Boosted", "false") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Option.isSome ctx.Request.Headers.HxBoosted |> Assert.True - Option.get ctx.Request.Headers.HxBoosted |> Assert.False - - [] - let ``HxCurrentUrl succeeds when the header is not present`` () = - let ctx = Substitute.For () - ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore - Option.isNone ctx.Request.Headers.HxCurrentUrl |> Assert.True - - [] - let ``HxCurrentUrl succeeds when the header is present`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Current-URL", "http://localhost/test.htm") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Option.isSome ctx.Request.Headers.HxCurrentUrl |> Assert.True - Assert.Equal (Uri "http://localhost/test.htm", Option.get ctx.Request.Headers.HxCurrentUrl) - - [] - let ``HxHistoryRestoreRequest succeeds when the header is not present`` () = - let ctx = Substitute.For () - ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore - Option.isNone ctx.Request.Headers.HxHistoryRestoreRequest |> Assert.True - - [] - let ``HxHistoryRestoreRequest succeeds when the header is present and true`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-History-Restore-Request", "true") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Option.isSome ctx.Request.Headers.HxHistoryRestoreRequest |> Assert.True - Option.get ctx.Request.Headers.HxHistoryRestoreRequest |> Assert.True - - [] - let ``HxHistoryRestoreRequest succeeds when the header is present and false`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-History-Restore-Request", "false") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Option.isSome ctx.Request.Headers.HxHistoryRestoreRequest |> Assert.True - Option.get ctx.Request.Headers.HxHistoryRestoreRequest |> Assert.False - - [] - let ``HxPrompt succeeds when the header is not present`` () = - let ctx = Substitute.For () - ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore - Option.isNone ctx.Request.Headers.HxPrompt |> Assert.True - - [] - let ``HxPrompt succeeds when the header is present`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Prompt", "of course") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Option.isSome ctx.Request.Headers.HxPrompt |> Assert.True - Assert.Equal("of course", Option.get ctx.Request.Headers.HxPrompt) - - [] - let ``HxRequest succeeds when the header is not present`` () = - let ctx = Substitute.For () - ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore - Option.isNone ctx.Request.Headers.HxRequest |> Assert.True - - [] - let ``HxRequest succeeds when the header is present and true`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Request", "true") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Option.isSome ctx.Request.Headers.HxRequest |> Assert.True - Option.get ctx.Request.Headers.HxRequest |> Assert.True - - [] - let ``HxRequest succeeds when the header is present and false`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Request", "false") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Option.isSome ctx.Request.Headers.HxRequest |> Assert.True - Option.get ctx.Request.Headers.HxRequest |> Assert.False - - [] - let ``HxTarget succeeds when the header is not present`` () = - let ctx = Substitute.For () - ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore - Option.isNone ctx.Request.Headers.HxTarget |> Assert.True - - [] - let ``HxTarget succeeds when the header is present`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Target", "#leItem") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Option.isSome ctx.Request.Headers.HxTarget |> Assert.True - Assert.Equal("#leItem", Option.get ctx.Request.Headers.HxTarget) - - [] - let ``HxTrigger succeeds when the header is not present`` () = - let ctx = Substitute.For () - ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore - Option.isNone ctx.Request.Headers.HxTrigger |> Assert.True - - [] - let ``HxTrigger succeeds when the header is present`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Trigger", "#trig") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Option.isSome ctx.Request.Headers.HxTrigger |> Assert.True - Assert.Equal("#trig", Option.get ctx.Request.Headers.HxTrigger) - - [] - let ``HxTriggerName succeeds when the header is not present`` () = - let ctx = Substitute.For () - ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore - Option.isNone ctx.Request.Headers.HxTriggerName |> Assert.True - - [] - let ``HxTriggerName succeeds when the header is present`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Trigger-Name", "click") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Option.isSome ctx.Request.Headers.HxTriggerName |> Assert.True - Assert.Equal("click", Option.get ctx.Request.Headers.HxTriggerName) - - -/// Tests for the HttpRequest extension properties -module HttpRequestExtensions = - - [] - let ``IsHtmx succeeds when request is not from htmx`` () = - let ctx = Substitute.For () - ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore - Assert.False ctx.Request.IsHtmx - - [] - let ``IsHtmx succeeds when request is from htmx`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Request", "true") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Assert.True ctx.Request.IsHtmx - - [] - let ``IsHtmxRefresh succeeds when request is not from htmx`` () = - let ctx = Substitute.For () - ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore - Assert.False ctx.Request.IsHtmxRefresh - - [] - let ``IsHtmxRefresh succeeds when request is from htmx, but not a refresh`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Request", "true") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Assert.False ctx.Request.IsHtmxRefresh - - [] - let ``IsHtmxRefresh succeeds when request is from htmx and is a refresh`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - dic.Add ("HX-Request", "true") - dic.Add ("HX-History-Restore-Request", "true") - ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore - Assert.True ctx.Request.IsHtmxRefresh - - -/// Tests for the HttpHandler functions provided in the Handlers module -module HandlerTests = - - open System.Threading.Tasks - - /// Dummy "next" parameter to get the pipeline to execute/terminate - let next (ctx : HttpContext) = Task.FromResult (Some ctx) - - [] - let ``withHxPushUrl succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxPushUrl "/a-new-url" next ctx - Assert.True (dic.ContainsKey "HX-Push-Url") - Assert.Equal ("/a-new-url", dic["HX-Push-Url"][0]) - } - - [] - let ``withHxNoPushUrl succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxNoPushUrl next ctx - Assert.True (dic.ContainsKey "HX-Push-Url") - Assert.Equal ("false", dic["HX-Push-Url"][0]) - } - - [] - let ``withHxRedirect succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxRedirect "/somewhere-else" next ctx - Assert.True (dic.ContainsKey "HX-Redirect") - Assert.Equal ("/somewhere-else", dic["HX-Redirect"][0]) - } - - [] - let ``withHxRefresh succeeds when set to true`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxRefresh true next ctx - Assert.True (dic.ContainsKey "HX-Refresh") - Assert.Equal ("true", dic["HX-Refresh"][0]) - } - - [] - let ``withHxRefresh succeeds when set to false`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxRefresh false next ctx - Assert.True (dic.ContainsKey "HX-Refresh") - Assert.Equal ("false", dic["HX-Refresh"][0]) - } - - [] - let ``withHxReplaceUrl succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxReplaceUrl "/a-substitute-url" next ctx - Assert.True (dic.ContainsKey "HX-Replace-Url") - Assert.Equal ("/a-substitute-url", dic["HX-Replace-Url"][0]) - } - - [] - let ``withHxNoReplaceUrl succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxNoReplaceUrl next ctx - Assert.True (dic.ContainsKey "HX-Replace-Url") - Assert.Equal ("false", dic["HX-Replace-Url"][0]) - } - - [] - let ``withHxReswap succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxReswap HxSwap.BeforeEnd next ctx - Assert.True (dic.ContainsKey "HX-Reswap") - Assert.Equal (HxSwap.BeforeEnd, dic["HX-Reswap"][0]) - } - - [] - let ``withHxRetarget succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxRetarget "#somewhereElse" next ctx - Assert.True (dic.ContainsKey "HX-Retarget") - Assert.Equal ("#somewhereElse", dic["HX-Retarget"][0]) - } - - [] - let ``withHxTrigger succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxTrigger "doSomething" next ctx - Assert.True (dic.ContainsKey "HX-Trigger") - Assert.Equal ("doSomething", dic["HX-Trigger"][0]) - } - - [] - let ``withHxTriggerMany succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxTriggerMany [ "blah", "foo"; "bleh", "bar" ] next ctx - Assert.True (dic.ContainsKey "HX-Trigger") - Assert.Equal ("""{ "blah": "foo", "bleh": "bar" }""", dic["HX-Trigger"][0]) - } - - [] - let ``withHxTriggerAfterSettle succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxTriggerAfterSettle "byTheWay" next ctx - Assert.True (dic.ContainsKey "HX-Trigger-After-Settle") - Assert.Equal ("byTheWay", dic["HX-Trigger-After-Settle"][0]) - } - - [] - let ``withHxTriggerManyAfterSettle succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxTriggerManyAfterSettle [ "oof", "ouch"; "hmm", "uh" ] next ctx - Assert.True (dic.ContainsKey "HX-Trigger-After-Settle") - Assert.Equal ("""{ "oof": "ouch", "hmm": "uh" }""", dic["HX-Trigger-After-Settle"][0]) - } - - [] - let ``withHxTriggerAfterSwap succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxTriggerAfterSwap "justASec" next ctx - Assert.True (dic.ContainsKey "HX-Trigger-After-Swap") - Assert.Equal ("justASec", dic["HX-Trigger-After-Swap"][0]) - } - - [] - let ``withHxTriggerManyAfterSwap succeeds`` () = - let ctx = Substitute.For () - let dic = HeaderDictionary () - ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore - task { - let! _ = withHxTriggerManyAfterSwap [ "this", "1"; "that", "2" ] next ctx - Assert.True (dic.ContainsKey "HX-Trigger-After-Swap") - Assert.Equal ("""{ "this": "1", "that": "2" }""", dic["HX-Trigger-After-Swap"][0]) - } diff --git a/src/Tests/Common.fs b/src/Tests/Common.fs new file mode 100644 index 0000000..db4cc68 --- /dev/null +++ b/src/Tests/Common.fs @@ -0,0 +1,33 @@ +module Common + +open Expecto +open Giraffe.Htmx + +/// Tests for the HxSwap module +let swap = + testList "HxSwap" [ + test "InnerHtml is correct" { + Expect.equal HxSwap.InnerHtml "innerHTML" "Inner HTML swap value incorrect" + } + test "OuterHtml is correct" { + Expect.equal HxSwap.OuterHtml "outerHTML" "Outer HTML swap value incorrect" + } + test "BeforeBegin is correct" { + Expect.equal HxSwap.BeforeBegin "beforebegin" "Before Begin swap value incorrect" + } + test "BeforeEnd is correct" { + Expect.equal HxSwap.BeforeEnd "beforeend" "Before End swap value incorrect" + } + test "AfterBegin is correct" { + Expect.equal HxSwap.AfterBegin "afterbegin" "After Begin swap value incorrect" + } + test "AfterEnd is correct" { + Expect.equal HxSwap.AfterEnd "afterend" "After End swap value incorrect" + } + test "None is correct" { + Expect.equal HxSwap.None "none" "None swap value incorrect" + } + ] + +/// All tests for this module +let allTests = testList "Htmx.Common" [ swap ] diff --git a/src/Tests/Htmx.fs b/src/Tests/Htmx.fs new file mode 100644 index 0000000..338e488 --- /dev/null +++ b/src/Tests/Htmx.fs @@ -0,0 +1,342 @@ +module Htmx + +open System +open Expecto +open Giraffe.Htmx +open Microsoft.AspNetCore.Http +open NSubstitute + +/// Tests for the IHeaderDictionary extension properties +let dictExtensions = + testList "IHeaderDictionaryExtensions" [ + testList "HxBoosted" [ + test "succeeds when the header is not present" { + let ctx = Substitute.For () + ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore + Expect.isNone ctx.Request.Headers.HxBoosted "There should not have been a header returned" + } + test "succeeds when the header is present and true" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Boosted", "true") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isSome ctx.Request.Headers.HxBoosted "There should be a header present" + Expect.isTrue ctx.Request.Headers.HxBoosted.Value "The header value should have been true" + } + test "succeeds when the header is present and false" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Boosted", "false") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isSome ctx.Request.Headers.HxBoosted "There should be a header present" + Expect.isFalse ctx.Request.Headers.HxBoosted.Value "The header value should have been false" + } + ] + testList "HxCurrentUrl" [ + test "succeeds when the header is not present" { + let ctx = Substitute.For () + ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore + Expect.isNone ctx.Request.Headers.HxCurrentUrl "There should not have been a header returned" + } + test "succeeds when the header is present" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Current-URL", "http://localhost/test.htm") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isSome ctx.Request.Headers.HxCurrentUrl "There should be a header present" + Expect.equal + ctx.Request.Headers.HxCurrentUrl.Value (Uri "http://localhost/test.htm") + "The header value was not correct" + } + ] + testList "HxHistoryRestoreRequest" [ + test "succeeds when the header is not present" { + let ctx = Substitute.For () + ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore + Expect.isNone ctx.Request.Headers.HxHistoryRestoreRequest "There should not have been a header returned" + } + test "succeeds when the header is present and true" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-History-Restore-Request", "true") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isSome ctx.Request.Headers.HxHistoryRestoreRequest "There should be a header present" + Expect.isTrue ctx.Request.Headers.HxHistoryRestoreRequest.Value "The header value should have been true" + } + test "succeeds when the header is present and false" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-History-Restore-Request", "false") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isSome ctx.Request.Headers.HxHistoryRestoreRequest "There should be a header present" + Expect.isFalse + ctx.Request.Headers.HxHistoryRestoreRequest.Value "The header should have been false" + } + ] + testList "HxPrompt" [ + test "succeeds when the header is not present" { + let ctx = Substitute.For () + ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore + Expect.isNone ctx.Request.Headers.HxPrompt "There should not have been a header returned" + } + test "succeeds when the header is present" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Prompt", "of course") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isSome ctx.Request.Headers.HxPrompt "There should be a header present" + Expect.equal ctx.Request.Headers.HxPrompt.Value "of course" "The header value was incorrect" + } + ] + testList "HxRequest" [ + test "succeeds when the header is not present" { + let ctx = Substitute.For () + ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore + Expect.isNone ctx.Request.Headers.HxRequest "There should not have been a header returned" + } + test "succeeds when the header is present and true" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Request", "true") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isSome ctx.Request.Headers.HxRequest "There should be a header present" + Expect.isTrue ctx.Request.Headers.HxRequest.Value "The header should have been true" + } + test "succeeds when the header is present and false" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Request", "false") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isSome ctx.Request.Headers.HxRequest "There should be a header present" + Expect.isFalse ctx.Request.Headers.HxRequest.Value "The header should have been false" + } + ] + testList "HxTarget" [ + test "succeeds when the header is not present" { + let ctx = Substitute.For () + ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore + Expect.isNone ctx.Request.Headers.HxTarget "There should not have been a header returned" + } + test "succeeds when the header is present" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Target", "#leItem") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isSome ctx.Request.Headers.HxTarget "There should be a header present" + Expect.equal ctx.Request.Headers.HxTarget.Value "#leItem" "The header value was incorrect" + } + ] + testList "HxTrigger" [ + test "succeeds when the header is not present" { + let ctx = Substitute.For () + ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore + Expect.isNone ctx.Request.Headers.HxTrigger "There should not have been a header returned" + } + test "succeeds when the header is present" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Trigger", "#trig") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isSome ctx.Request.Headers.HxTrigger "There should be a header present" + Expect.equal ctx.Request.Headers.HxTrigger.Value "#trig" "The header value was incorrect" + } + ] + testList "HxTriggerName" [ + test "succeeds when the header is not present" { + let ctx = Substitute.For () + ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore + Expect.isNone ctx.Request.Headers.HxTriggerName "There should not have been a header returned" + } + test "HxTriggerName succeeds when the header is present" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Trigger-Name", "click") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isSome ctx.Request.Headers.HxTriggerName "There should be a header present" + Expect.equal ctx.Request.Headers.HxTriggerName.Value "click" "The header value was incorrect" + } + ] + ] + +/// Tests for the HttpRequest extension properties +let reqExtensions = + testList "HttpRequestExtensions" [ + testList "IsHtmx" [ + test "succeeds when request is not from htmx" { + let ctx = Substitute.For () + ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore + Expect.isFalse ctx.Request.IsHtmx "The request should not be an htmx request" + } + test "succeeds when request is from htmx" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Request", "true") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isTrue ctx.Request.IsHtmx "The request should have been an htmx request" + } + ] + testList "IsHtmxRefresh" [ + test "succeeds when request is not from htmx" { + let ctx = Substitute.For () + ctx.Request.Headers.ReturnsForAnyArgs (HeaderDictionary ()) |> ignore + Expect.isFalse ctx.Request.IsHtmxRefresh "The request should not have been an htmx refresh" + } + test "succeeds when request is from htmx, but not a refresh" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Request", "true") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isFalse ctx.Request.IsHtmxRefresh "The request should not have been an htmx refresh" + } + test "IsHtmxRefresh succeeds when request is from htmx and is a refresh" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + dic.Add ("HX-Request", "true") + dic.Add ("HX-History-Restore-Request", "true") + ctx.Request.Headers.ReturnsForAnyArgs dic |> ignore + Expect.isTrue ctx.Request.IsHtmxRefresh "The request should have been an htmx refresh" + } + ] + ] + +open System.Threading.Tasks + +/// Dummy "next" parameter to get the pipeline to execute/terminate +let next (ctx : HttpContext) = Task.FromResult (Some ctx) + +/// Tests for the HttpHandler functions provided in the Handlers module +let handlers = + testList "HandlerTests" [ + testTask "withHxPushUrl succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxPushUrl "/a-new-url" next ctx + Expect.isTrue (dic.ContainsKey "HX-Push-Url") "The HX-Push-Url header should be present" + Expect.equal dic["HX-Push-Url"].[0] "/a-new-url" "The HX-Push-Url value was incorrect" + } + testTask "withHxNoPushUrl succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxNoPushUrl next ctx + Expect.isTrue (dic.ContainsKey "HX-Push-Url") "The HX-Push-Url header should be present" + Expect.equal dic["HX-Push-Url"].[0] "false" "The HX-Push-Url value was incorrect" + } + testTask "withHxRedirect succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxRedirect "/somewhere-else" next ctx + Expect.isTrue (dic.ContainsKey "HX-Redirect") "The HX-Redirect header should be present" + Expect.equal dic["HX-Redirect"].[0] "/somewhere-else" "The HX-Redirect value was incorrect" + } + testList "withHxRefresh" [ + testTask "succeeds when set to true" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxRefresh true next ctx + Expect.isTrue (dic.ContainsKey "HX-Refresh") "The HX-Refresh header should be present" + Expect.equal dic["HX-Refresh"].[0] "true" "The HX-Refresh value was incorrect" + } + testTask "succeeds when set to false" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxRefresh false next ctx + Expect.isTrue (dic.ContainsKey "HX-Refresh") "The HX-Refresh header should be present" + Expect.equal dic["HX-Refresh"].[0] "false" "The HX-Refresh value was incorrect" + } + ] + testTask "withHxReplaceUrl succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxReplaceUrl "/a-substitute-url" next ctx + Expect.isTrue (dic.ContainsKey "HX-Replace-Url") "The HX-Replace-Url header should be present" + Expect.equal dic["HX-Replace-Url"].[0] "/a-substitute-url" "The HX-Replace-Url value was incorrect" + } + testTask "withHxNoReplaceUrl succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxNoReplaceUrl next ctx + Expect.isTrue (dic.ContainsKey "HX-Replace-Url") "The HX-Replace-Url header should be present" + Expect.equal dic["HX-Replace-Url"].[0] "false" "The HX-Replace-Url value was incorrect" + } + testTask "withHxReswap succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxReswap HxSwap.BeforeEnd next ctx + Expect.isTrue (dic.ContainsKey "HX-Reswap") "The HX-Reswap header should be present" + Expect.equal dic["HX-Reswap"].[0] HxSwap.BeforeEnd "The HX-Reswap value was incorrect" + } + testTask "withHxRetarget succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxRetarget "#somewhereElse" next ctx + Expect.isTrue (dic.ContainsKey "HX-Retarget") "The HX-Retarget header should be present" + Expect.equal dic["HX-Retarget"].[0] "#somewhereElse" "The HX-Retarget value was incorrect" + } + testTask "withHxTrigger succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxTrigger "doSomething" next ctx + Expect.isTrue (dic.ContainsKey "HX-Trigger") "The HX-Trigger header should be present" + Expect.equal dic["HX-Trigger"].[0] "doSomething" "The HX-Trigger value was incorrect" + } + testTask "withHxTriggerMany succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxTriggerMany [ "blah", "foo"; "bleh", "bar" ] next ctx + Expect.isTrue (dic.ContainsKey "HX-Trigger") "The HX-Trigger header should be present" + Expect.equal + dic["HX-Trigger"].[0] """{ "blah": "foo", "bleh": "bar" }""" "The HX-Trigger value was incorrect" + } + testTask "withHxTriggerAfterSettle succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxTriggerAfterSettle "byTheWay" next ctx + Expect.isTrue + (dic.ContainsKey "HX-Trigger-After-Settle") "The HX-Trigger-After-Settle header should be present" + Expect.equal dic["HX-Trigger-After-Settle"].[0] "byTheWay" "The HX-Trigger-After-Settle value was incorrect" + } + testTask "withHxTriggerManyAfterSettle succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxTriggerManyAfterSettle [ "oof", "ouch"; "hmm", "uh" ] next ctx + Expect.isTrue + (dic.ContainsKey "HX-Trigger-After-Settle") "The HX-Trigger-After-Settle header should be present" + Expect.equal + dic["HX-Trigger-After-Settle"].[0] """{ "oof": "ouch", "hmm": "uh" }""" + "The HX-Trigger-After-Settle value was incorrect" + } + testTask "withHxTriggerAfterSwap succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxTriggerAfterSwap "justASec" next ctx + Expect.isTrue (dic.ContainsKey "HX-Trigger-After-Swap") "The HX-Trigger-After-Swap header should be present" + Expect.equal dic["HX-Trigger-After-Swap"].[0] "justASec" "The HX-Trigger-After-Swap value was incorrect" + } + testTask "withHxTriggerManyAfterSwap succeeds" { + let ctx = Substitute.For () + let dic = HeaderDictionary () + ctx.Response.Headers.ReturnsForAnyArgs dic |> ignore + let! _ = withHxTriggerManyAfterSwap [ "this", "1"; "that", "2" ] next ctx + Expect.isTrue (dic.ContainsKey "HX-Trigger-After-Swap") "The HX-Trigger-After-Swap header should be present" + Expect.equal + dic["HX-Trigger-After-Swap"].[0] """{ "this": "1", "that": "2" }""" + "The HX-Trigger-After-Swap value was incorrect" + } + ] + +/// All tests for this module +let allTests = testList "Htmx" [ dictExtensions; reqExtensions; handlers ] diff --git a/src/Tests/Program.fs b/src/Tests/Program.fs new file mode 100644 index 0000000..cc9fe1a --- /dev/null +++ b/src/Tests/Program.fs @@ -0,0 +1,6 @@ +open Expecto + +let allTests = testList "Giraffe" [ Common.allTests; Htmx.allTests; ViewEngine.allTests ] + +[] +let main args = runTestsWithArgs defaultConfig args allTests diff --git a/src/Tests/Tests.fsproj b/src/Tests/Tests.fsproj new file mode 100644 index 0000000..70e0920 --- /dev/null +++ b/src/Tests/Tests.fsproj @@ -0,0 +1,25 @@ + + + + Exe + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Tests/ViewEngine.fs b/src/Tests/ViewEngine.fs new file mode 100644 index 0000000..aeabf8e --- /dev/null +++ b/src/Tests/ViewEngine.fs @@ -0,0 +1,578 @@ +module ViewEngine + +open Expecto +open Giraffe.ViewEngine +open Giraffe.ViewEngine.Htmx + +/// Tests for the HxEncoding module +let hxEncoding = + testList "HxEncoding" [ + test "Form is correct" { + Expect.equal HxEncoding.Form "application/x-www-form-urlencoded" "Form encoding not correct" + } + test "MultipartForm is correct" { + Expect.equal HxEncoding.MultipartForm "multipart/form-data" "Multipart form encoding not correct" + } + ] + +/// Tests for the HxHeaders module +let hxHeaders = + testList "HxHeaders" [ + testList "From" [ + test "succeeds with an empty list" { + Expect.equal (HxHeaders.From []) "{ }" "Empty headers not correct" + } + test "succeeds and escapes quotes" { + Expect.equal + (HxHeaders.From [ "test", "one two three"; "again", """four "five" six""" ]) + """{ "test": "one two three", "again": "four \"five\" six" }""" "Headers not correct" + } + ] + ] + +/// Tests for the HxParams module +let hxParams = + testList "HxParams" [ + test "All is correct" { + Expect.equal HxParams.All "*" "All is not correct" + } + test "None is correct" { + Expect.equal HxParams.None "none" "None is not correct" + } + testList "With" [ + test "succeeds with empty list" { + Expect.equal (HxParams.With []) "" "With with empty list should have been blank" + } + test "succeeds with one list item" { + Expect.equal (HxParams.With [ "boo" ]) "boo" "With single item incorrect" + } + test "succeeds with multiple list items" { + Expect.equal (HxParams.With [ "foo"; "bar"; "baz" ]) "foo,bar,baz" "With multiple items incorrect" + } + ] + testList "Except" [ + test "succeeds with empty list" { + Expect.equal (HxParams.Except []) "not " "Except with empty list incorrect" + } + test "succeeds with one list item" { + Expect.equal (HxParams.Except [ "that" ]) "not that" "Except single item incorrect" + } + test "succeeds with multiple list items" { + Expect.equal (HxParams.Except [ "blue"; "green" ]) "not blue,green" "Except multiple items incorrect" + } + ] + ] + +/// Tests for the HxRequest module +let hxRequest = + testList "HxRequest" [ + testList "Configure" [ + test "succeeds with an empty list" { + Expect.equal (HxRequest.Configure []) "{ }" "Configure with empty list incorrect" + } + test "succeeds with a non-empty list" { + Expect.equal + (HxRequest.Configure [ "\"a\": \"b\""; "\"c\": \"d\"" ]) """{ "a": "b", "c": "d" }""" + "Configure with a non-empty list incorrect" + } + test "succeeds with all known params configured" { + Expect.equal + (HxRequest.Configure + [ HxRequest.Timeout 1000; HxRequest.Credentials false; HxRequest.NoHeaders true ]) + """{ "timeout": 1000, "credentials": false, "noHeaders": true }""" + "Configure with all known params incorrect" + } + ] + test "Timeout succeeds" { + Expect.equal (HxRequest.Timeout 50) "\"timeout\": 50" "Timeout value incorrect" + } + testList "Credentials" [ + test "succeeds when set to true" { + Expect.equal (HxRequest.Credentials true) "\"credentials\": true" "Credentials value incorrect" + } + test "succeeds when set to false" { + Expect.equal (HxRequest.Credentials false) "\"credentials\": false" "Credentials value incorrect" + } + ] + testList "NoHeaders" [ + test "succeeds when set to true" { + Expect.equal (HxRequest.NoHeaders true) "\"noHeaders\": true" "NoHeaders value incorrect" + } + test "succeeds when set to false" { + Expect.equal (HxRequest.NoHeaders false) "\"noHeaders\": false" "NoHeaders value incorrect" + } + ] + ] + +/// Tests for the HxTrigger module +let hxTrigger = + testList "HxTrigger" [ + test "Click is correct" { + Expect.equal HxTrigger.Click "click" "Click is incorrect" + } + test "Load is correct" { + Expect.equal HxTrigger.Load "load" "Load is incorrect" + } + test "Revealed is correct" { + Expect.equal HxTrigger.Revealed "revealed" "Revealed is incorrect" + } + test "Every succeeds" { + Expect.equal (HxTrigger.Every "3s") "every 3s" "Every is incorrect" + } + testList "Filter" [ + test "Alt succeeds" { + Expect.equal (HxTrigger.Filter.Alt HxTrigger.Click) "click[altKey]" "Alt filter incorrect" + } + test "Ctrl succeeds" { + Expect.equal (HxTrigger.Filter.Ctrl HxTrigger.Click) "click[ctrlKey]" "Ctrl filter incorrect" + } + test "Shift succeeds" { + Expect.equal (HxTrigger.Filter.Shift HxTrigger.Click) "click[shiftKey]" "Shift filter incorrect" + } + test "CtrlAlt succeeds" { + Expect.equal + (HxTrigger.Filter.CtrlAlt HxTrigger.Click) "click[ctrlKey&&altKey]" "Ctrl+Alt filter incorrect" + } + test "CtrlShift succeeds" { + Expect.equal + (HxTrigger.Filter.CtrlShift HxTrigger.Click) "click[ctrlKey&&shiftKey]" + "Ctrl+Shift filter incorrect" + } + test "CtrlAltShift succeeds" { + Expect.equal + (HxTrigger.Filter.CtrlAltShift HxTrigger.Click) "click[ctrlKey&&altKey&&shiftKey]" + "Ctrl+Alt+Shift filter incorrect" + } + test "AltShift succeeds" { + Expect.equal + (HxTrigger.Filter.AltShift HxTrigger.Click) "click[altKey&&shiftKey]" "Alt+Shift filter incorrect" + } + ] + testList "Once" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.Once "") "once" "Once modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.Once "click") "click once" "Once modifier incorrect" + } + ] + testList "Changed" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.Changed "") "changed" "Changed modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.Changed "click") "click changed" "Changed modifier incorrect" + } + ] + testList "Delay" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.Delay "1s" "") "delay:1s" "Delay modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.Delay "2s" "click") "click delay:2s" "Delay modifier incorrect" + } + ] + testList "Throttle" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.Throttle "4s" "") "throttle:4s" "Throttle modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.Throttle "7s" "click") "click throttle:7s" "Throttle modifier incorrect" + } + ] + testList "From" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.From ".nav" "") "from:.nav" "From modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.From "#somewhere" "click") "click from:#somewhere" "From modifier incorrect" + } + ] + testList "FromDocument" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.FromDocument "") "from:document" "FromDocument modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.FromDocument "click") "click from:document" "FromDocument modifier incorrect" + } + ] + testList "FromWindow" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.FromWindow "") "from:window" "FromWindow modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.FromWindow "click") "click from:window" "FromWindow modifier incorrect" + } + ] + testList "FromClosest" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.FromClosest "div" "") "from:closest div" "FromClosest modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.FromClosest "p" "click") "click from:closest p" "FromClosest modifier incorrect" + } + ] + testList "FromFind" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.FromFind "li" "") "from:find li" "FromFind modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.FromFind ".spot" "click") "click from:find .spot" "FromFind modifier incorrect" + } + ] + testList "Target" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.Target "main" "") "target:main" "Target modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.Target "footer" "click") "click target:footer" "Target modifier incorrect" + } + ] + testList "Consume" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.Consume "") "consume" "Consume modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.Consume "click") "click consume" "Consume modifier incorrect" + } + ] + testList "Queue" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.Queue "abc" "") "queue:abc" "Queue modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.Queue "def" "click") "click queue:def" "Queue modifier incorrect" + } + ] + testList "QueueFirst" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.QueueFirst "") "queue:first" "QueueFirst modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.QueueFirst "click") "click queue:first" "QueueFirst modifier incorrect" + } + ] + testList "QueueLast" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.QueueLast "") "queue:last" "QueueLast modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.QueueLast "click") "click queue:last" "QueueLast modifier incorrect" + } + ] + testList "QueueAll" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.QueueAll "") "queue:all" "QueueAll modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.QueueAll "click") "click queue:all" "QueueAll modifier incorrect" + } + ] + testList "QueueNone" [ + test "succeeds when it is the first modifier" { + Expect.equal (HxTrigger.QueueNone "") "queue:none" "QueueNone modifier incorrect" + } + test "succeeds when it is not the first modifier" { + Expect.equal (HxTrigger.QueueNone "click") "click queue:none" "QueueNone modifier incorrect" + } + ] + ] + +/// Tests for the HxVals module +let hxVals = + testList "HxVals" [ + testList "From" [ + test "succeeds with an empty list" { + Expect.equal (HxVals.From []) "{ }" "From with an empty list is incorrect" + } + test "succeeds and escapes quotes" { + Expect.equal + (HxVals.From [ "test", """a "b" c"""; "2", "d e f" ]) + """{ "test": "a \"b\" c", "2": "d e f" }""" "From value is incorrect" + } + ] + ] + +/// Tests for the HtmxAttrs module +let attributes = + testList "Attributes" [ + + /// Pipe-able assertion for a rendered node + let shouldRender expected node = + Expect.equal (RenderView.AsString.htmlNode node) expected "Rendered HTML incorrect" + + test "_hxBoost succeeds" { + div [ _hxBoost ] [] |> shouldRender """
""" + } + test "_hxConfirm succeeds" { + button [ _hxConfirm "REALLY?!?" ] [] |> shouldRender """""" + } + test "_hxDelete succeeds" { + span [ _hxDelete "/this-endpoint" ] [] |> shouldRender """""" + } + test "_hxDisable succeeds" { + p [ _hxDisable ] [] |> shouldRender """

""" + } + test "_hxDisinherit succeeds" { + strong [ _hxDisinherit "*" ] [] |> shouldRender """""" + } + test "_hxEncoding succeeds" { + form [ _hxEncoding "utf-7" ] [] |> shouldRender """
""" + } + test "_hxExt succeeds" { + section [ _hxExt "extendme" ] [] |> shouldRender """
""" + } + test "_hxGet succeeds" { + article [ _hxGet "/the-text" ] [] |> shouldRender """
""" + } + test "_hxHeaders succeeds" { + figure [ _hxHeaders """{ "X-Special-Header": "some-header" }""" ] [] + |> shouldRender """
""" + } + test "_hxHistory succeeds" { + span [ _hxHistory "false" ] [] |> shouldRender """""" + } + test "_hxHistoryElt succeeds" { + table [ _hxHistoryElt ] [] |> shouldRender """
""" + } + test "_hxInclude succeeds" { + a [ _hxInclude ".extra-stuff" ] [] |> shouldRender """""" + } + test "_hxIndicator succeeds" { + aside [ _hxIndicator "#spinner" ] [] |> shouldRender """""" + } + test "_hxNoBoost succeeds" { + td [ _hxNoBoost ] [] |> shouldRender """""" + } + test "_hxParams succeeds" { + br [ _hxParams "[p1,p2]" ] |> shouldRender """
""" + } + test "_hxPatch succeeds" { + div [ _hxPatch "/arrrgh" ] [] |> shouldRender """
""" + } + test "_hxPost succeeds" { + hr [ _hxPost "/hear-ye-hear-ye" ] |> shouldRender """
""" + } + test "_hxPreserve succeeds" { + img [ _hxPreserve ] |> shouldRender """""" + } + test "_hxPrompt succeeds" { + strong [ _hxPrompt "Who goes there?" ] [] + |> shouldRender """""" + } + test "_hxPushUrl succeeds" { + dl [ _hxPushUrl "/a-b-c" ] [] |> shouldRender """
""" + } + test "_hxPut succeeds" { + s [ _hxPut "/take-this" ] [] |> shouldRender """""" + } + test "_hxReplaceUrl succeeds" { + p [ _hxReplaceUrl "/something-else" ] [] |> shouldRender """

""" + } + test "_hxRequest succeeds" { + u [ _hxRequest "noHeaders" ] [] |> shouldRender """""" + } + test "_hxSelect succeeds" { + nav [ _hxSelect "#navbar" ] [] |> shouldRender """""" + } + test "_hxSelectOob succeeds" { + section [ _hxSelectOob "#oob" ] [] |> shouldRender """
""" + } + test "_hxSse succeeds" { + footer [ _hxSse "connect:/my-events" ] [] + |> shouldRender """
""" + } + test "_hxSwap succeeds" { + del [ _hxSwap "innerHTML" ] [] |> shouldRender """""" + } + test "_hxSwapOob succeeds" { + li [ _hxSwapOob "true" ] [] |> shouldRender """
  • """ + } + test "_hxSync succeeds" { + nav [ _hxSync "closest form:abort" ] [] |> shouldRender """""" + } + test "_hxTarget succeeds" { + header [ _hxTarget "#somewhereElse" ] [] |> shouldRender """
    """ + } + test "_hxTrigger succeeds" { + figcaption [ _hxTrigger "load" ] [] |> shouldRender """
    """ + } + test "_hxVals succeeds" { + dt [ _hxVals """{ "extra": "values" }""" ] [] + |> shouldRender """
    """ + } + test "_hxWs succeeds" { + ul [ _hxWs "connect:/web-socket" ] [] |> shouldRender """
      """ + } + ] + +/// Tests for the Script module +let script = + testList "Script" [ + test "minified succeeds" { + let html = RenderView.AsString.htmlNode Script.minified + Expect.equal + html + """""" + "Minified script tag is incorrect" + } + test "unminified succeeds" { + let html = RenderView.AsString.htmlNode Script.unminified + Expect.equal + html + """""" + "Unminified script tag is incorrect" + } + ] + +open System.Text + +/// Tests for the RenderFragment module +let renderFragment = + testList "RenderFragment" [ + + /// Validate that the two object references are the same object + let isSame obj1 obj2 message = + Expect.isTrue (obj.ReferenceEquals (obj1, obj2)) message + + testList "findIdNode" [ + test "fails with a Text node" { + Expect.isNone (RenderFragment.findIdNode "blue" (Text "")) "There should not have been a node found" + } + test "fails with a VoidElement without a matching ID" { + Expect.isNone + (RenderFragment.findIdNode "purple" (br [ _id "mauve" ])) "There should not have been a node found" + } + test "fails with a ParentNode with no children with a matching ID" { + Expect.isNone + (RenderFragment.findIdNode "green" (p [] [ str "howdy"; span [] [ str "huh" ] ])) + "There should not have been a node found" + } + test "succeeds with a VoidElement with a matching ID" { + let leNode = hr [ _id "groovy" ] + let foundNode = RenderFragment.findIdNode "groovy" leNode + Expect.isSome foundNode "There should have been a node found" + isSame leNode foundNode.Value "The node should have been the same object" + } + test "succeeds with a ParentNode with a child with a matching ID" { + let leNode = span [ _id "its-me" ] [ str "Mario" ] + let foundNode = + RenderFragment.findIdNode "its-me" (p [] [ str "test"; str "again"; leNode; str "un mas" ]) + Expect.isSome foundNode "There should have been a node found" + isSame leNode foundNode.Value "The node should have been the same object" + } + ] + + /// Generate a message if the requested ID node is not found + let nodeNotFound (nodeId : string) = + $"– ID {nodeId} not found –" + + testList "AsString" [ + testList "htmlFromNodes" [ + test "succeeds when an ID is matched" { + let html = + RenderFragment.AsString.htmlFromNodes "needle" + [ p [] [] + p [ _id "haystack" ] [ str "hay"; span [ _id "needle" ] [ str "ouch" ]; str "hay" ] + ] + Expect.equal html """ouch""" "HTML is incorrect" + } + test "fails when an ID is not matched" { + Expect.equal + (RenderFragment.AsString.htmlFromNodes "oops" []) (nodeNotFound "oops") "HTML is incorrect" + } + ] + testList "htmlFromNode" [ + test "succeeds when ID is matched at top level" { + let html = RenderFragment.AsString.htmlFromNode "wow" (p [ _id "wow" ] [ str "found it" ]) + Expect.equal html """

      found it

      """ "HTML is incorrect" + } + test "succeeds when ID is matched in child element" { + let html = + div [] [ p [] [ str "not it" ]; p [ _id "hey" ] [ str "ta-da" ]] + |> RenderFragment.AsString.htmlFromNode "hey" + Expect.equal html """

      ta-da

      """ "HTML is incorrect" + } + test "fails when an ID is not matched" { + Expect.equal + (RenderFragment.AsString.htmlFromNode "me" (hr [])) (nodeNotFound "me") "HTML is incorrect" + } + ] + ] + testList "AsBytes" [ + + /// Alias for UTF-8 encoding + let utf8 = Encoding.UTF8 + + testList "htmlFromNodes" [ + test "succeeds when an ID is matched" { + let bytes = + RenderFragment.AsBytes.htmlFromNodes "found" + [ p [] [] + p [ _id "not-it" ] [ str "nope"; span [ _id "found" ] [ str "boo" ]; str "nope" ] + ] + Expect.equal bytes (utf8.GetBytes """boo""") "HTML bytes are incorrect" + } + test "fails when an ID is not matched" { + Expect.equal + (RenderFragment.AsBytes.htmlFromNodes "whiff" []) (utf8.GetBytes (nodeNotFound "whiff")) + "HTML bytes are incorrect" + } + ] + testList "htmlFromNode" [ + test "succeeds when ID is matched at top level" { + let bytes = RenderFragment.AsBytes.htmlFromNode "first" (p [ _id "first" ] [ str "!!!" ]) + Expect.equal bytes (utf8.GetBytes """

      !!!

      """) "HTML bytes are incorrect" + } + test "succeeds when ID is matched in child element" { + let bytes = + div [] [ p [] [ str "not me" ]; p [ _id "child" ] [ str "node" ]] + |> RenderFragment.AsBytes.htmlFromNode "child" + Expect.equal bytes (utf8.GetBytes """

      node

      """) "HTML bytes are incorrect" + } + test "fails when an ID is not matched" { + Expect.equal + (RenderFragment.AsBytes.htmlFromNode "foo" (hr [])) (utf8.GetBytes (nodeNotFound "foo")) + "HTML bytes are incorrect" + } + ] + ] + testList "IntoStringBuilder" [ + testList "htmlFromNodes" [ + test "succeeds when an ID is matched" { + let sb = StringBuilder () + RenderFragment.IntoStringBuilder.htmlFromNodes sb "find-me" + [ p [] []; p [ _id "peekaboo" ] [ str "bzz"; str "nope"; span [ _id "find-me" ] [ str ";)" ] ]] + Expect.equal (string sb) """;)""" "HTML is incorrect" + } + test "fails when an ID is not matched" { + let sb = StringBuilder () + RenderFragment.IntoStringBuilder.htmlFromNodes sb "missing" [] + Expect.equal (string sb) (nodeNotFound "missing") "HTML is incorrect" + } + ] + testList "htmlFromNode" [ + test "succeeds when ID is matched at top level" { + let sb = StringBuilder () + RenderFragment.IntoStringBuilder.htmlFromNode sb "top" (p [ _id "top" ] [ str "pinnacle" ]) + Expect.equal (string sb) """

      pinnacle

      """ "HTML is incorrect" + } + test "succeeds when ID is matched in child element" { + let sb = StringBuilder () + div [] [ p [] [ str "nada" ]; p [ _id "it" ] [ str "is here" ]] + |> RenderFragment.IntoStringBuilder.htmlFromNode sb "it" + Expect.equal (string sb) """

      is here

      """ "HTML is incorrect" + } + test "fails when an ID is not matched" { + let sb = StringBuilder () + RenderFragment.IntoStringBuilder.htmlFromNode sb "bar" (hr []) + Expect.equal (string sb) (nodeNotFound "bar") "HTML is incorrect" + } + ] + ] + ] + +/// All tests in this module +let allTests = + testList "ViewEngine.Htmx" + [ hxEncoding; hxHeaders; hxParams; hxRequest; hxTrigger; hxVals; attributes; script; renderFragment ] diff --git a/src/ViewEngine.Htmx.Tests/Giraffe.ViewEngine.Htmx.Tests.fsproj b/src/ViewEngine.Htmx.Tests/Giraffe.ViewEngine.Htmx.Tests.fsproj deleted file mode 100644 index 9d9d736..0000000 --- a/src/ViewEngine.Htmx.Tests/Giraffe.ViewEngine.Htmx.Tests.fsproj +++ /dev/null @@ -1,31 +0,0 @@ - - - - false - false - - - - - - - - - - - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - runtime; build; native; contentfiles; analyzers; buildtransitive - all - - - - - - - - diff --git a/src/ViewEngine.Htmx.Tests/Program.fs b/src/ViewEngine.Htmx.Tests/Program.fs deleted file mode 100644 index fdc31cd..0000000 --- a/src/ViewEngine.Htmx.Tests/Program.fs +++ /dev/null @@ -1 +0,0 @@ -module Program = let [] main _ = 0 diff --git a/src/ViewEngine.Htmx.Tests/Tests.fs b/src/ViewEngine.Htmx.Tests/Tests.fs deleted file mode 100644 index c48cbf2..0000000 --- a/src/ViewEngine.Htmx.Tests/Tests.fs +++ /dev/null @@ -1,583 +0,0 @@ -module Giraffe.ViewEngine.Htmx.Tests - -open Giraffe.ViewEngine -open Xunit - -/// Tests for the HxEncoding module -module Encoding = - - [] - let ``Form is correct`` () = - Assert.Equal ("application/x-www-form-urlencoded", HxEncoding.Form) - - [] - let ``MultipartForm is correct`` () = - Assert.Equal ("multipart/form-data", HxEncoding.MultipartForm) - - -/// Tests for the HxHeaders module -module Headers = - - [] - let ``From succeeds with an empty list`` () = - Assert.Equal ("{ }", HxHeaders.From []) - - [] - let ``From succeeds and escapes quotes`` () = - Assert.Equal ("{ \"test\": \"one two three\", \"again\": \"four \\\"five\\\" six\" }", - HxHeaders.From [ "test", "one two three"; "again", "four \"five\" six" ]) - - -/// Tests for the HxParams module -module Params = - - [] - let ``All is correct`` () = - Assert.Equal ("*", HxParams.All) - - [] - let ``None is correct`` () = - Assert.Equal ("none", HxParams.None) - - [] - let ``With succeeds with empty list`` () = - Assert.Equal ("", HxParams.With []) - - [] - let ``With succeeds with one list item`` () = - Assert.Equal ("boo", HxParams.With [ "boo" ]) - - [] - let ``With succeeds with multiple list items`` () = - Assert.Equal ("foo,bar,baz", HxParams.With [ "foo"; "bar"; "baz" ]) - - [] - let ``Except succeeds with empty list`` () = - Assert.Equal ("not ", HxParams.Except []) - - [] - let ``Except succeeds with one list item`` () = - Assert.Equal ("not that", HxParams.Except [ "that" ]) - - [] - let ``Except succeeds with multiple list items`` () = - Assert.Equal ("not blue,green", HxParams.Except [ "blue"; "green" ]) - - -/// Tests for the HxRequest module -module Request = - - [] - let ``Configure succeeds with an empty list`` () = - Assert.Equal ("{ }", HxRequest.Configure []) - - [] - let ``Configure succeeds with a non-empty list`` () = - Assert.Equal ("{ \"a\": \"b\", \"c\": \"d\" }", HxRequest.Configure [ "\"a\": \"b\""; "\"c\": \"d\"" ]) - - [] - let ``Configure succeeds with all known params configured`` () = - Assert.Equal ("{ \"timeout\": 1000, \"credentials\": false, \"noHeaders\": true }", - HxRequest.Configure [ HxRequest.Timeout 1000; HxRequest.Credentials false; HxRequest.NoHeaders true ]) - - [] - let ``Timeout succeeds`` () = - Assert.Equal ("\"timeout\": 50", HxRequest.Timeout 50) - - [] - let ``Credentials succeeds when set to true`` () = - Assert.Equal ("\"credentials\": true", HxRequest.Credentials true) - - [] - let ``Credentials succeeds when set to false`` () = - Assert.Equal ("\"credentials\": false", HxRequest.Credentials false) - - [] - let ``NoHeaders succeeds when set to true`` () = - Assert.Equal ("\"noHeaders\": true", HxRequest.NoHeaders true) - - [] - let ``NoHeaders succeeds when set to false`` () = - Assert.Equal ("\"noHeaders\": false", HxRequest.NoHeaders false) - - -/// Tests for the HxTrigger module -module Trigger = - - [] - let ``Click is correct`` () = - Assert.Equal ("click", HxTrigger.Click) - - [] - let ``Load is correct`` () = - Assert.Equal ("load", HxTrigger.Load) - - [] - let ``Revealed is correct`` () = - Assert.Equal ("revealed", HxTrigger.Revealed) - - [] - let ``Every succeeds`` () = - Assert.Equal ("every 3s", HxTrigger.Every "3s") - - [] - let ``Filter.Alt succeeds`` () = - Assert.Equal ("click[altKey]", HxTrigger.Filter.Alt HxTrigger.Click) - - [] - let ``Filter.Ctrl succeeds`` () = - Assert.Equal ("click[ctrlKey]", HxTrigger.Filter.Ctrl HxTrigger.Click) - - [] - let ``Filter.Shift succeeds`` () = - Assert.Equal ("click[shiftKey]", HxTrigger.Filter.Shift HxTrigger.Click) - - [] - let ``Filter.CtrlAlt succeeds`` () = - Assert.Equal ("click[ctrlKey&&altKey]", HxTrigger.Filter.CtrlAlt HxTrigger.Click) - - [] - let ``Filter.CtrlShift succeeds`` () = - Assert.Equal ("click[ctrlKey&&shiftKey]", HxTrigger.Filter.CtrlShift HxTrigger.Click) - - [] - let ``Filter.CtrlAltShift succeeds`` () = - Assert.Equal ("click[ctrlKey&&altKey&&shiftKey]", HxTrigger.Filter.CtrlAltShift HxTrigger.Click) - - [] - let ``Filter.AltShift succeeds`` () = - Assert.Equal ("click[altKey&&shiftKey]", HxTrigger.Filter.AltShift HxTrigger.Click) - - [] - let ``Once succeeds when it is the first modifier`` () = - Assert.Equal ("once", HxTrigger.Once "") - - [] - let ``Once succeeds when it is not the first modifier`` () = - Assert.Equal ("click once", HxTrigger.Once "click") - - [] - let ``Changed succeeds when it is the first modifier`` () = - Assert.Equal ("changed", HxTrigger.Changed "") - - [] - let ``Changed succeeds when it is not the first modifier`` () = - Assert.Equal ("click changed", HxTrigger.Changed "click") - - [] - let ``Delay succeeds when it is the first modifier`` () = - Assert.Equal ("delay:1s", HxTrigger.Delay "1s" "") - - [] - let ``Delay succeeds when it is not the first modifier`` () = - Assert.Equal ("click delay:2s", HxTrigger.Delay "2s" "click") - - [] - let ``Throttle succeeds when it is the first modifier`` () = - Assert.Equal ("throttle:4s", HxTrigger.Throttle "4s" "") - - [] - let ``Throttle succeeds when it is not the first modifier`` () = - Assert.Equal ("click throttle:7s", HxTrigger.Throttle "7s" "click") - - [] - let ``From succeeds when it is the first modifier`` () = - Assert.Equal ("from:.nav", HxTrigger.From ".nav" "") - - [] - let ``From succeeds when it is not the first modifier`` () = - Assert.Equal ("click from:#somewhere", HxTrigger.From "#somewhere" "click") - - [] - let ``FromDocument succeeds when it is the first modifier`` () = - Assert.Equal ("from:document", HxTrigger.FromDocument "") - - [] - let ``FromDocument succeeds when it is not the first modifier`` () = - Assert.Equal ("click from:document", HxTrigger.FromDocument "click") - - [] - let ``FromWindow succeeds when it is the first modifier`` () = - Assert.Equal ("from:window", HxTrigger.FromWindow "") - - [] - let ``FromWindow succeeds when it is not the first modifier`` () = - Assert.Equal ("click from:window", HxTrigger.FromWindow "click") - - [] - let ``FromClosest succeeds when it is the first modifier`` () = - Assert.Equal ("from:closest div", HxTrigger.FromClosest "div" "") - - [] - let ``FromClosest succeeds when it is not the first modifier`` () = - Assert.Equal ("click from:closest p", HxTrigger.FromClosest "p" "click") - - [] - let ``FromFind succeeds when it is the first modifier`` () = - Assert.Equal ("from:find li", HxTrigger.FromFind "li" "") - - [] - let ``FromFind succeeds when it is not the first modifier`` () = - Assert.Equal ("click from:find .spot", HxTrigger.FromFind ".spot" "click") - - [] - let ``Target succeeds when it is the first modifier`` () = - Assert.Equal ("target:main", HxTrigger.Target "main" "") - - [] - let ``Target succeeds when it is not the first modifier`` () = - Assert.Equal ("click target:footer", HxTrigger.Target "footer" "click") - - [] - let ``Consume succeeds when it is the first modifier`` () = - Assert.Equal ("consume", HxTrigger.Consume "") - - [] - let ``Consume succeeds when it is not the first modifier`` () = - Assert.Equal ("click consume", HxTrigger.Consume "click") - - [] - let ``Queue succeeds when it is the first modifier`` () = - Assert.Equal ("queue:abc", HxTrigger.Queue "abc" "") - - [] - let ``Queue succeeds when it is not the first modifier`` () = - Assert.Equal ("click queue:def", HxTrigger.Queue "def" "click") - - [] - let ``QueueFirst succeeds when it is the first modifier`` () = - Assert.Equal ("queue:first", HxTrigger.QueueFirst "") - - [] - let ``QueueFirst succeeds when it is not the first modifier`` () = - Assert.Equal ("click queue:first", HxTrigger.QueueFirst "click") - - [] - let ``QueueLast succeeds when it is the first modifier`` () = - Assert.Equal ("queue:last", HxTrigger.QueueLast "") - - [] - let ``QueueLast succeeds when it is not the first modifier`` () = - Assert.Equal ("click queue:last", HxTrigger.QueueLast "click") - - [] - let ``QueueAll succeeds when it is the first modifier`` () = - Assert.Equal ("queue:all", HxTrigger.QueueAll "") - - [] - let ``QueueAll succeeds when it is not the first modifier`` () = - Assert.Equal ("click queue:all", HxTrigger.QueueAll "click") - - [] - let ``QueueNone succeeds when it is the first modifier`` () = - Assert.Equal ("queue:none", HxTrigger.QueueNone "") - - [] - let ``QueueNone succeeds when it is not the first modifier`` () = - Assert.Equal ("click queue:none", HxTrigger.QueueNone "click") - - -/// Tests for the HxVals module -module Vals = - - [] - let ``From succeeds with an empty list`` () = - Assert.Equal ("{ }", HxVals.From []) - - [] - let ``From succeeds and escapes quotes`` () = - Assert.Equal ("{ \"test\": \"a \\\"b\\\" c\", \"2\": \"d e f\" }", - HxVals.From [ "test", "a \"b\" c"; "2", "d e f" ]) - - -/// Tests for the HtmxAttrs module -module Attributes = - - /// Pipe-able assertion for a rendered node - let shouldRender expected node = Assert.Equal (expected, RenderView.AsString.htmlNode node) - - [] - let ``_hxBoost succeeds`` () = - div [ _hxBoost ] [] |> shouldRender """
      """ - - [] - let ``_hxConfirm succeeds`` () = - button [ _hxConfirm "REALLY?!?" ] [] |> shouldRender """""" - - [] - let ``_hxDelete succeeds`` () = - span [ _hxDelete "/this-endpoint" ] [] |> shouldRender """""" - - [] - let ``_hxDisable succeeds`` () = - p [ _hxDisable ] [] |> shouldRender """

      """ - - [] - let ``_hxDisinherit succeeds`` () = - strong [ _hxDisinherit "*" ] [] |> shouldRender """""" - - [] - let ``_hxEncoding succeeds`` () = - form [ _hxEncoding "utf-7" ] [] |> shouldRender """
      """ - - [] - let ``_hxExt succeeds`` () = - section [ _hxExt "extendme" ] [] |> shouldRender """
      """ - - [] - let ``_hxGet succeeds`` () = - article [ _hxGet "/the-text" ] [] |> shouldRender """
      """ - - [] - let ``_hxHeaders succeeds`` () = - figure [ _hxHeaders """{ "X-Special-Header": "some-header" }""" ] [] - |> shouldRender """
      """ - - [] - let ``_hxHistory succeeds`` () = - span [ _hxHistory "false" ] [] |> shouldRender """""" - - [] - let ``_hxHistoryElt succeeds`` () = - table [ _hxHistoryElt ] [] |> shouldRender """
      """ - - [] - let ``_hxInclude succeeds`` () = - a [ _hxInclude ".extra-stuff" ] [] |> shouldRender """""" - - [] - let ``_hxIndicator succeeds`` () = - aside [ _hxIndicator "#spinner" ] [] |> shouldRender """""" - - [] - let ``_hxNoBoost succeeds`` () = - td [ _hxNoBoost ] [] |> shouldRender """""" - - [] - let ``_hxParams succeeds`` () = - br [ _hxParams "[p1,p2]" ] |> shouldRender """
      """ - - [] - let ``_hxPatch succeeds`` () = - div [ _hxPatch "/arrrgh" ] [] |> shouldRender """
      """ - - [] - let ``_hxPost succeeds`` () = - hr [ _hxPost "/hear-ye-hear-ye" ] |> shouldRender """
      """ - - [] - let ``_hxPreserve succeeds`` () = - img [ _hxPreserve ] |> shouldRender """""" - - [] - let ``_hxPrompt succeeds`` () = - strong [ _hxPrompt "Who goes there?" ] [] |> shouldRender """""" - - [] - let ``_hxPushUrl succeeds`` () = - dl [ _hxPushUrl "/a-b-c" ] [] |> shouldRender """
      """ - - [] - let ``_hxPut succeeds`` () = - s [ _hxPut "/take-this" ] [] |> shouldRender """""" - - [] - let ``_hxReplaceUrl succeeds`` () = - p [ _hxReplaceUrl "/something-else" ] [] |> shouldRender """

      """ - - [] - let ``_hxRequest succeeds`` () = - u [ _hxRequest "noHeaders" ] [] |> shouldRender """""" - - [] - let ``_hxSelect succeeds`` () = - nav [ _hxSelect "#navbar" ] [] |> shouldRender """""" - - [] - let ``_hxSelectOob succeeds`` () = - section [ _hxSelectOob "#oob" ] [] |> shouldRender """
      """ - - [] - let ``_hxSse succeeds`` () = - footer [ _hxSse "connect:/my-events" ] [] |> shouldRender """
      """ - - [] - let ``_hxSwap succeeds`` () = - del [ _hxSwap "innerHTML" ] [] |> shouldRender """""" - - [] - let ``_hxSwapOob succeeds`` () = - li [ _hxSwapOob "true" ] [] |> shouldRender """
    • """ - - [] - let ``_hxSync succeeds`` () = - nav [ _hxSync "closest form:abort" ] [] |> shouldRender """""" - - [] - let ``_hxTarget succeeds`` () = - header [ _hxTarget "#somewhereElse" ] [] |> shouldRender """
      """ - - [] - let ``_hxTrigger succeeds`` () = - figcaption [ _hxTrigger "load" ] [] |> shouldRender """
      """ - - [] - let ``_hxVals succeeds`` () = - dt [ _hxVals """{ "extra": "values" }""" ] [] - |> shouldRender """
      """ - - [] - let ``_hxWs succeeds`` () = - ul [ _hxWs "connect:/web-socket" ] [] |> shouldRender """
        """ - - -/// Tests for the Script module -module Script = - - [] - let ``Script.minified succeeds`` () = - let html = RenderView.AsString.htmlNode Script.minified - Assert.Equal - ("""""", - html) - - [] - let ``Script.unminified succeeds`` () = - let html = RenderView.AsString.htmlNode Script.unminified - Assert.Equal - ("""""", - html) - - -/// Tests for the RenderFragment module -module RenderFragment = - - open System.Text - - [] - let ``RenderFragment.findIdNode fails with a Text node`` () = - Assert.False (Option.isSome (RenderFragment.findIdNode "blue" (Text ""))) - - [] - let ``RenderFragment.findIdNode fails with a VoidElement without a matching ID`` () = - Assert.False (Option.isSome (RenderFragment.findIdNode "purple" (br [ _id "mauve" ]))) - - [] - let ``RenderFragment.findIdNode fails with a ParentNode with no children with a matching ID`` () = - Assert.False (Option.isSome (RenderFragment.findIdNode "green" (p [] [ str "howdy"; span [] [ str "huh" ] ]))) - - [] - let ``RenderFragment.findIdNode succeeds with a VoidElement with a matching ID`` () = - let leNode = hr [ _id "groovy" ] - let foundNode = RenderFragment.findIdNode "groovy" leNode - Assert.True (Option.isSome foundNode) - Assert.Same (leNode, foundNode.Value) - - [] - let ``RenderFragment.findIdNode succeeds with a ParentNode with a child with a matching ID`` () = - let leNode = span [ _id "its-me" ] [ str "Mario" ] - let foundNode = RenderFragment.findIdNode "its-me" (p [] [ str "test"; str "again"; leNode; str "un mas" ]) - Assert.True (Option.isSome foundNode) - Assert.Same (leNode, foundNode.Value) - - /// Generate a message if the requested ID node is not found - let private nodeNotFound (nodeId : string) = - $"– ID {nodeId} not found –" - - /// Tests for the AsString module - module AsString = - - [] - let ``RenderFragment.AsString.htmlFromNodes succeeds when an ID is matched`` () = - let html = - RenderFragment.AsString.htmlFromNodes "needle" - [ p [] []; p [ _id "haystack" ] [ span [ _id "needle" ] [ str "ouch" ]; str "hay"; str "hay" ]] - Assert.Equal ("""ouch""", html) - - [] - let ``RenderFragment.AsString.htmlFromNodes fails when an ID is not matched`` () = - Assert.Equal (nodeNotFound "oops", RenderFragment.AsString.htmlFromNodes "oops" []) - - [] - let ``RenderFragment.AsString.htmlFromNode succeeds when ID is matched at top level`` () = - let html = RenderFragment.AsString.htmlFromNode "wow" (p [ _id "wow" ] [ str "found it" ]) - Assert.Equal ("""

        found it

        """, html) - - [] - let ``RenderFragment.AsString.htmlFromNode succeeds when ID is matched in child element`` () = - let html = - div [] [ p [] [ str "not it" ]; p [ _id "hey" ] [ str "ta-da" ]] - |> RenderFragment.AsString.htmlFromNode "hey" - Assert.Equal ("""

        ta-da

        """, html) - - [] - let ``RenderFragment.AsString.htmlFromNode fails when an ID is not matched`` () = - Assert.Equal (nodeNotFound "me", RenderFragment.AsString.htmlFromNode "me" (hr [])) - - /// Tests for the AsBytes module - module AsBytes = - - /// Alias for UTF-8 encoding - let private utf8 = Encoding.UTF8 - - [] - let ``RenderFragment.AsBytes.htmlFromNodes succeeds when an ID is matched`` () = - let bytes = - RenderFragment.AsBytes.htmlFromNodes "found" - [ p [] []; p [ _id "not-it" ] [ str "nope"; span [ _id "found" ] [ str "boo" ]; str "nope" ]] - Assert.Equal (utf8.GetBytes """boo""", bytes) - - [] - let ``RenderFragment.AsBytes.htmlFromNodes fails when an ID is not matched`` () = - Assert.Equal (utf8.GetBytes (nodeNotFound "whiff"), RenderFragment.AsBytes.htmlFromNodes "whiff" []) - - [] - let ``RenderFragment.AsBytes.htmlFromNode succeeds when ID is matched at top level`` () = - let bytes = RenderFragment.AsBytes.htmlFromNode "first" (p [ _id "first" ] [ str "!!!" ]) - Assert.Equal (utf8.GetBytes """

        !!!

        """, bytes) - - [] - let ``RenderFragment.AsBytes.htmlFromNode succeeds when ID is matched in child element`` () = - let bytes = - div [] [ p [] [ str "not me" ]; p [ _id "child" ] [ str "node" ]] - |> RenderFragment.AsBytes.htmlFromNode "child" - Assert.Equal (utf8.GetBytes """

        node

        """, bytes) - - [] - let ``RenderFragment.AsBytes.htmlFromNode fails when an ID is not matched`` () = - Assert.Equal (utf8.GetBytes (nodeNotFound "foo"), RenderFragment.AsBytes.htmlFromNode "foo" (hr [])) - - /// Tests for the IntoStringBuilder module - module IntoStringBuilder = - - [] - let ``RenderFragment.IntoStringBuilder.htmlFromNodes succeeds when an ID is matched`` () = - let sb = StringBuilder () - RenderFragment.IntoStringBuilder.htmlFromNodes sb "find-me" - [ p [] []; p [ _id "peekaboo" ] [ str "bzz"; str "nope"; span [ _id "find-me" ] [ str ";)" ] ]] - Assert.Equal (""";)""", string sb) - - [] - let ``RenderFragment.IntoStringBuilder.htmlFromNodes fails when an ID is not matched`` () = - let sb = StringBuilder () - RenderFragment.IntoStringBuilder.htmlFromNodes sb "missing" [] - Assert.Equal (nodeNotFound "missing", string sb) - - [] - let ``RenderFragment.IntoStringBuilder.htmlFromNode succeeds when ID is matched at top level`` () = - let sb = StringBuilder () - RenderFragment.IntoStringBuilder.htmlFromNode sb "top" (p [ _id "top" ] [ str "pinnacle" ]) - Assert.Equal ("""

        pinnacle

        """, string sb) - - [] - let ``RenderFragment.IntoStringBuilder.htmlFromNode succeeds when ID is matched in child element`` () = - let sb = StringBuilder () - div [] [ p [] [ str "nada" ]; p [ _id "it" ] [ str "is here" ]] - |> RenderFragment.IntoStringBuilder.htmlFromNode sb "it" - Assert.Equal ("""

        is here

        """, string sb) - - [] - let ``RenderFragment.IntoStringBuilder.htmlFromNode fails when an ID is not matched`` () = - let sb = StringBuilder () - RenderFragment.IntoStringBuilder.htmlFromNode sb "bar" (hr []) - Assert.Equal (nodeNotFound "bar", string sb)