Xunit -> Expecto #7

Merged
danieljsummers merged 3 commits from 1.8.5.vnext into main 2023-02-28 01:55:17 +00:00
6 changed files with 892 additions and 0 deletions
Showing only changes of commit e113058ffe - Show all commits

View File

@ -15,6 +15,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Htmx.Common", "Comm
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Giraffe.Htmx.Common.Tests", "Common.Tests\Giraffe.Htmx.Common.Tests.fsproj", "{E261A653-68D5-4D7B-99A4-F09282B50F8A}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Tests", "Tests\Tests.fsproj", "{39823773-4311-4E79-9CA0-F9DDC40CAF6A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -48,5 +50,9 @@ Global
{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

33
src/Tests/Common.fs Normal file
View File

@ -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 ]

342
src/Tests/Htmx.fs Normal file
View File

@ -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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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<HttpContext> ()
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 ]

6
src/Tests/Program.fs Normal file
View File

@ -0,0 +1,6 @@
open Expecto
let allTests = testList "Giraffe" [ Common.allTests; Htmx.allTests; ViewEngine.allTests ]
[<EntryPoint>]
let main args = runTestsWithArgs defaultConfig args allTests

25
src/Tests/Tests.fsproj Normal file
View File

@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<Compile Include="Common.fs" />
<Compile Include="Htmx.fs" />
<Compile Include="ViewEngine.fs" />
<Compile Include="Program.fs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Common\Giraffe.Htmx.Common.fsproj" />
<ProjectReference Include="..\Htmx\Giraffe.Htmx.fsproj" />
<ProjectReference Include="..\ViewEngine.Htmx\Giraffe.ViewEngine.Htmx.fsproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Expecto" Version="9.0.4" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
</ItemGroup>
</Project>

480
src/Tests/ViewEngine.fs Normal file
View File

@ -0,0 +1,480 @@
module ViewEngine
open Expecto
open Giraffe.ViewEngine
/// Tests for the HxEncoding module
let hxEncoding =
testList "Encoding" [
test "Form is correct" {
Expect.equal ("application/x-www-form-urlencoded", HxEncoding.Form)
}
test "MultipartForm is correct" {
Expect.equal ("multipart/form-data", HxEncoding.MultipartForm)
}
]
/// Tests for the HxHeaders module
let hxHeaders =
testList "Headers" [
test "From succeeds with an empty list" {
Expect.equal ("{ }", HxHeaders.From [])
}
test "From succeeds and escapes quotes" {
Expect.equal ("{ \"test\": \"one two three\", \"again\": \"four \\\"five\\\" six\" }",
HxHeaders.From [ "test", "one two three"; "again", "four \"five\" six" ])
}
]
/// Tests for the HxParams module
let hxParams =
testList "Params" [
test "All is correct" {
Expect.equal ("*", HxParams.All)
}
test "None is correct" {
Expect.equal ("none", HxParams.None)
}
test "With succeeds with empty list" {
Expect.equal ("", HxParams.With [])
}
test "With succeeds with one list item" {
Expect.equal ("boo", HxParams.With [ "boo" ])
}
test "With succeeds with multiple list items" {
Expect.equal ("foo,bar,baz", HxParams.With [ "foo"; "bar"; "baz" ])
}
test "Except succeeds with empty list" {
Expect.equal ("not ", HxParams.Except [])
}
test "Except succeeds with one list item" {
Expect.equal ("not that", HxParams.Except [ "that" ])
}
test "Except succeeds with multiple list items" {
Expect.equal ("not blue,green", HxParams.Except [ "blue"; "green" ])
}
]
/// Tests for the HxRequest module
let hxRequest =
testList "Request" [
test "Configure succeeds with an empty list" {
Expect.equal ("{ }", HxRequest.Configure [])
}
test "Configure succeeds with a non-empty list" {
Expect.equal ("{ \"a\": \"b\", \"c\": \"d\" }", HxRequest.Configure [ "\"a\": \"b\""; "\"c\": \"d\"" ])
}
test "Configure succeeds with all known params configured" {
Expect.equal ("{ \"timeout\": 1000, \"credentials\": false, \"noHeaders\": true }",
HxRequest.Configure [ HxRequest.Timeout 1000; HxRequest.Credentials false; HxRequest.NoHeaders true ])
}
test "Timeout succeeds" {
Expect.equal ("\"timeout\": 50", HxRequest.Timeout 50)
}
test "Credentials succeeds when set to true" {
Expect.equal ("\"credentials\": true", HxRequest.Credentials true)
}
test "Credentials succeeds when set to false" {
Expect.equal ("\"credentials\": false", HxRequest.Credentials false)
}
test "NoHeaders succeeds when set to true" {
Expect.equal ("\"noHeaders\": true", HxRequest.NoHeaders true)
}
test "NoHeaders succeeds when set to false" {
Expect.equal ("\"noHeaders\": false", HxRequest.NoHeaders false)
}
]
/// Tests for the HxTrigger module
let hxTrigger =
testList "Trigger" [
test "Click is correct" {
Expect.equal ("click", HxTrigger.Click)
}
test "Load is correct" {
Expect.equal ("load", HxTrigger.Load)
}
test "Revealed is correct" {
Expect.equal ("revealed", HxTrigger.Revealed)
}
test "Every succeeds" {
Expect.equal ("every 3s", HxTrigger.Every "3s")
}
test "Filter.Alt succeeds" {
Expect.equal ("click[altKey]", HxTrigger.Filter.Alt HxTrigger.Click)
}
test "Filter.Ctrl succeeds" {
Expect.equal ("click[ctrlKey]", HxTrigger.Filter.Ctrl HxTrigger.Click)
}
test "Filter.Shift succeeds" {
Expect.equal ("click[shiftKey]", HxTrigger.Filter.Shift HxTrigger.Click)
}
test "Filter.CtrlAlt succeeds" {
Expect.equal ("click[ctrlKey&&altKey]", HxTrigger.Filter.CtrlAlt HxTrigger.Click)
}
test "Filter.CtrlShift succeeds" {
Expect.equal ("click[ctrlKey&&shiftKey]", HxTrigger.Filter.CtrlShift HxTrigger.Click)
}
test "Filter.CtrlAltShift succeeds" {
Expect.equal ("click[ctrlKey&&altKey&&shiftKey]", HxTrigger.Filter.CtrlAltShift HxTrigger.Click)
}
test "Filter.AltShift succeeds" {
Expect.equal ("click[altKey&&shiftKey]", HxTrigger.Filter.AltShift HxTrigger.Click)
}
test "Once succeeds when it is the first modifier" {
Expect.equal ("once", HxTrigger.Once "")
}
test "Once succeeds when it is not the first modifier" {
Expect.equal ("click once", HxTrigger.Once "click")
}
test "Changed succeeds when it is the first modifier" {
Expect.equal ("changed", HxTrigger.Changed "")
}
test "Changed succeeds when it is not the first modifier" {
Expect.equal ("click changed", HxTrigger.Changed "click")
}
test "Delay succeeds when it is the first modifier" {
Expect.equal ("delay:1s", HxTrigger.Delay "1s" "")
}
test "Delay succeeds when it is not the first modifier" {
Expect.equal ("click delay:2s", HxTrigger.Delay "2s" "click")
}
test "Throttle succeeds when it is the first modifier" {
Expect.equal ("throttle:4s", HxTrigger.Throttle "4s" "")
}
test "Throttle succeeds when it is not the first modifier" {
Expect.equal ("click throttle:7s", HxTrigger.Throttle "7s" "click")
}
test "From succeeds when it is the first modifier" {
Expect.equal ("from:.nav", HxTrigger.From ".nav" "")
}
test "From succeeds when it is not the first modifier" {
Expect.equal ("click from:#somewhere", HxTrigger.From "#somewhere" "click")
}
test "FromDocument succeeds when it is the first modifier" {
Expect.equal ("from:document", HxTrigger.FromDocument "")
}
test "FromDocument succeeds when it is not the first modifier" {
Expect.equal ("click from:document", HxTrigger.FromDocument "click")
}
test "FromWindow succeeds when it is the first modifier" {
Expect.equal ("from:window", HxTrigger.FromWindow "")
}
test "FromWindow succeeds when it is not the first modifier" {
Expect.equal ("click from:window", HxTrigger.FromWindow "click")
}
test "FromClosest succeeds when it is the first modifier" {
Expect.equal ("from:closest div", HxTrigger.FromClosest "div" "")
}
test "FromClosest succeeds when it is not the first modifier" {
Expect.equal ("click from:closest p", HxTrigger.FromClosest "p" "click")
}
test "FromFind succeeds when it is the first modifier" {
Expect.equal ("from:find li", HxTrigger.FromFind "li" "")
}
test "FromFind succeeds when it is not the first modifier" {
Expect.equal ("click from:find .spot", HxTrigger.FromFind ".spot" "click")
}
test "Target succeeds when it is the first modifier" {
Expect.equal ("target:main", HxTrigger.Target "main" "")
}
test "Target succeeds when it is not the first modifier" {
Expect.equal ("click target:footer", HxTrigger.Target "footer" "click")
}
test "Consume succeeds when it is the first modifier" {
Expect.equal ("consume", HxTrigger.Consume "")
}
test "Consume succeeds when it is not the first modifier" {
Expect.equal ("click consume", HxTrigger.Consume "click")
}
test "Queue succeeds when it is the first modifier" {
Expect.equal ("queue:abc", HxTrigger.Queue "abc" "")
}
test "Queue succeeds when it is not the first modifier" {
Expect.equal ("click queue:def", HxTrigger.Queue "def" "click")
}
test "QueueFirst succeeds when it is the first modifier" {
Expect.equal ("queue:first", HxTrigger.QueueFirst "")
}
test "QueueFirst succeeds when it is not the first modifier" {
Expect.equal ("click queue:first", HxTrigger.QueueFirst "click")
}
test "QueueLast succeeds when it is the first modifier" {
Expect.equal ("queue:last", HxTrigger.QueueLast "")
}
test "QueueLast succeeds when it is not the first modifier" {
Expect.equal ("click queue:last", HxTrigger.QueueLast "click")
}
test "QueueAll succeeds when it is the first modifier" {
Expect.equal ("queue:all", HxTrigger.QueueAll "")
}
test "QueueAll succeeds when it is not the first modifier" {
Expect.equal ("click queue:all", HxTrigger.QueueAll "click")
}
test "QueueNone succeeds when it is the first modifier" {
Expect.equal ("queue:none", HxTrigger.QueueNone "")
}
test "QueueNone succeeds when it is not the first modifier" {
Expect.equal ("click queue:none", HxTrigger.QueueNone "click")
}
]
/// Tests for the HxVals module
let hxVals =
testList "Vals" [
test "From succeeds with an empty list" {
Expect.equal ("{ }", HxVals.From [])
}
test "From succeeds and escapes quotes" {
Expect.equal ("{ \"test\": \"a \\\"b\\\" c\", \"2\": \"d e f\" }",
HxVals.From [ "test", "a \"b\" c"; "2", "d e f" ])
}
]
/// Tests for the HtmxAttrs module
let attributes =
testList "Attributes" [
/// Pipe-able assertion for a rendered node
let shouldRender expected node = Expect.equal (expected, RenderView.AsString.htmlNode node)
test "_hxBoost succeeds" {
div [ _hxBoost ] [] |> shouldRender """<div hx-boost="true"></div>"""
}
test "_hxConfirm succeeds" {
button [ _hxConfirm "REALLY?!?" ] [] |> shouldRender """<button hx-confirm="REALLY?!?"></button>"""
}
test "_hxDelete succeeds" {
span [ _hxDelete "/this-endpoint" ] [] |> shouldRender """<span hx-delete="/this-endpoint"></span>"""
}
test "_hxDisable succeeds" {
p [ _hxDisable ] [] |> shouldRender """<p hx-disable></p>"""
}
test "_hxDisinherit succeeds" {
strong [ _hxDisinherit "*" ] [] |> shouldRender """<strong hx-disinherit="*"></strong>"""
}
test "_hxEncoding succeeds" {
form [ _hxEncoding "utf-7" ] [] |> shouldRender """<form hx-encoding="utf-7"></form>"""
}
test "_hxExt succeeds" {
section [ _hxExt "extendme" ] [] |> shouldRender """<section hx-ext="extendme"></section>"""
}
test "_hxGet succeeds" {
article [ _hxGet "/the-text" ] [] |> shouldRender """<article hx-get="/the-text"></article>"""
}
test "_hxHeaders succeeds" {
figure [ _hxHeaders """{ "X-Special-Header": "some-header" }""" ] []
|> shouldRender """<figure hx-headers="{ &quot;X-Special-Header&quot;: &quot;some-header&quot; }"></figure>"""
}
test "_hxHistory succeeds" {
span [ _hxHistory "false" ] [] |> shouldRender """<span hx-history="false"></span>"""
}
test "_hxHistoryElt succeeds" {
table [ _hxHistoryElt ] [] |> shouldRender """<table hx-history-elt></table>"""
}
test "_hxInclude succeeds" {
a [ _hxInclude ".extra-stuff" ] [] |> shouldRender """<a hx-include=".extra-stuff"></a>"""
}
test "_hxIndicator succeeds" {
aside [ _hxIndicator "#spinner" ] [] |> shouldRender """<aside hx-indicator="#spinner"></aside>"""
}
test "_hxNoBoost succeeds" {
td [ _hxNoBoost ] [] |> shouldRender """<td hx-boost="false"></td>"""
}
test "_hxParams succeeds" {
br [ _hxParams "[p1,p2]" ] |> shouldRender """<br hx-params="[p1,p2]">"""
}
test "_hxPatch succeeds" {
div [ _hxPatch "/arrrgh" ] [] |> shouldRender """<div hx-patch="/arrrgh"></div>"""
}
test "_hxPost succeeds" {
hr [ _hxPost "/hear-ye-hear-ye" ] |> shouldRender """<hr hx-post="/hear-ye-hear-ye">"""
}
test "_hxPreserve succeeds" {
img [ _hxPreserve ] |> shouldRender """<img hx-preserve="true">"""
}
test "_hxPrompt succeeds" {
strong [ _hxPrompt "Who goes there?" ] [] |> shouldRender """<strong hx-prompt="Who goes there?"></strong>"""
}
test "_hxPushUrl succeeds" {
dl [ _hxPushUrl "/a-b-c" ] [] |> shouldRender """<dl hx-push-url="/a-b-c"></dl>"""
}
test "_hxPut succeeds" {
s [ _hxPut "/take-this" ] [] |> shouldRender """<s hx-put="/take-this"></s>"""
}
test "_hxReplaceUrl succeeds" {
p [ _hxReplaceUrl "/something-else" ] [] |> shouldRender """<p hx-replace-url="/something-else"></p>"""
}
test "_hxRequest succeeds" {
u [ _hxRequest "noHeaders" ] [] |> shouldRender """<u hx-request="noHeaders"></u>"""
}
test "_hxSelect succeeds" {
nav [ _hxSelect "#navbar" ] [] |> shouldRender """<nav hx-select="#navbar"></nav>"""
}
test "_hxSelectOob succeeds" {
section [ _hxSelectOob "#oob" ] [] |> shouldRender """<section hx-select-oob="#oob"></section>"""
}
test "_hxSse succeeds" {
footer [ _hxSse "connect:/my-events" ] [] |> shouldRender """<footer hx-sse="connect:/my-events"></footer>"""
}
test "_hxSwap succeeds" {
del [ _hxSwap "innerHTML" ] [] |> shouldRender """<del hx-swap="innerHTML"></del>"""
}
test "_hxSwapOob succeeds" {
li [ _hxSwapOob "true" ] [] |> shouldRender """<li hx-swap-oob="true"></li>"""
}
test "_hxSync succeeds" {
nav [ _hxSync "closest form:abort" ] [] |> shouldRender """<nav hx-sync="closest form:abort"></nav>"""
}
test "_hxTarget succeeds" {
header [ _hxTarget "#somewhereElse" ] [] |> shouldRender """<header hx-target="#somewhereElse"></header>"""
}
test "_hxTrigger succeeds" {
figcaption [ _hxTrigger "load" ] [] |> shouldRender """<figcaption hx-trigger="load"></figcaption>"""
}
test "_hxVals succeeds" {
dt [ _hxVals """{ "extra": "values" }""" ] []
|> shouldRender """<dt hx-vals="{ &quot;extra&quot;: &quot;values&quot; }"></dt>"""
}
test "_hxWs succeeds" {
ul [ _hxWs "connect:/web-socket" ] [] |> shouldRender """<ul hx-ws="connect:/web-socket"></ul>"""
}
]
/// Tests for the Script module
let script =
testList "Script" [
test "Script.minified succeeds" {
let html = RenderView.AsString.htmlNode Script.minified
Expect.equal
("""<script src="https://unpkg.com/htmx.org@1.8.5" integrity="sha384-7aHh9lqPYGYZ7sTHvzP1t3BAfLhYSTy9ArHdP3Xsr9/3TlGurYgcPBoFmXX2TX/w" crossorigin="anonymous"></script>""",
html)
}
test "Script.unminified succeeds" {
let html = RenderView.AsString.htmlNode Script.unminified
Expect.equal
("""<script src="https://unpkg.com/htmx.org@1.8.5/dist/htmx.js" integrity="sha384-VgGOQitu5eD5qAdh1QPLvPeTt1X4/Iw9B2sfYw+p3xtTumxaRv+onip7FX+P6q30" crossorigin="anonymous"></script>""",
html)
}
]
open System.Text
/// Tests for the RenderFragment module
let renderFragment =
testList "RenderFragment" [
test "RenderFragment.findIdNode fails with a Text node" {
Expect.isFalse (Option.isSome (RenderFragment.findIdNode "blue" (Text "")))
}
test "RenderFragment.findIdNode fails with a VoidElement without a matching ID" {
Expect.isFalse (Option.isSome (RenderFragment.findIdNode "purple" (br [ _id "mauve" ])))
}
test "RenderFragment.findIdNode fails with a ParentNode with no children with a matching ID" {
Expect.isFalse (Option.isSome (RenderFragment.findIdNode "green" (p [] [ str "howdy"; span [] [ str "huh" ] ])))
}
test "RenderFragment.findIdNode succeeds with a VoidElement with a matching ID" {
let leNode = hr [ _id "groovy" ]
let foundNode = RenderFragment.findIdNode "groovy" leNode
Expect.isTrue (Option.isSome foundNode)
Assert.Same (leNode, foundNode.Value)
}
test "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" ])
Expect.isTrue (Option.isSome foundNode)
Assert.Same (leNode, foundNode.Value)
}
/// Generate a message if the requested ID node is not found
let private nodeNotFound (nodeId : string) =
$"<em>&ndash; ID {nodeId} not found &ndash;</em>"
/// Tests for the AsString module
testList "AsString" [
test "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" ]]
Expect.equal ("""<span id="needle">ouch</span>""", html)
}
test "RenderFragment.AsString.htmlFromNodes fails when an ID is not matched" {
Expect.equal (nodeNotFound "oops", RenderFragment.AsString.htmlFromNodes "oops" [])
}
test "RenderFragment.AsString.htmlFromNode succeeds when ID is matched at top level" {
let html = RenderFragment.AsString.htmlFromNode "wow" (p [ _id "wow" ] [ str "found it" ])
Expect.equal ("""<p id="wow">found it</p>""", html)
}
test "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"
Expect.equal ("""<p id="hey">ta-da</p>""", html)
}
test "RenderFragment.AsString.htmlFromNode fails when an ID is not matched" {
Expect.equal (nodeNotFound "me", RenderFragment.AsString.htmlFromNode "me" (hr []))
}
]
/// Tests for the AsBytes module
testList "AsBytes" [
/// Alias for UTF-8 encoding
let private utf8 = Encoding.UTF8
test "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" ]]
Expect.equal<byte> (utf8.GetBytes """<span id="found">boo</span>""", bytes)
}
test "RenderFragment.AsBytes.htmlFromNodes fails when an ID is not matched" {
Expect.equal<byte> (utf8.GetBytes (nodeNotFound "whiff"), RenderFragment.AsBytes.htmlFromNodes "whiff" [])
}
test "RenderFragment.AsBytes.htmlFromNode succeeds when ID is matched at top level" {
let bytes = RenderFragment.AsBytes.htmlFromNode "first" (p [ _id "first" ] [ str "!!!" ])
Expect.equal<byte> (utf8.GetBytes """<p id="first">!!!</p>""", bytes)
}
test "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"
Expect.equal<byte> (utf8.GetBytes """<p id="child">node</p>""", bytes)
}
test "RenderFragment.AsBytes.htmlFromNode fails when an ID is not matched" {
Expect.equal<byte> (utf8.GetBytes (nodeNotFound "foo"), RenderFragment.AsBytes.htmlFromNode "foo" (hr []))
}
]
/// Tests for the IntoStringBuilder module
testList "IntoStringBuilder" [
test "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 ";)" ] ]]
Expect.equal ("""<span id="find-me">;)</span>""", string sb)
}
test "RenderFragment.IntoStringBuilder.htmlFromNodes fails when an ID is not matched" {
let sb = StringBuilder ()
RenderFragment.IntoStringBuilder.htmlFromNodes sb "missing" []
Expect.equal (nodeNotFound "missing", string sb)
}
test "RenderFragment.IntoStringBuilder.htmlFromNode succeeds when ID is matched at top level" {
let sb = StringBuilder ()
RenderFragment.IntoStringBuilder.htmlFromNode sb "top" (p [ _id "top" ] [ str "pinnacle" ])
Expect.equal ("""<p id="top">pinnacle</p>""", string sb)
}
test "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"
Expect.equal ("""<p id="it">is here</p>""", string sb)
}
test "RenderFragment.IntoStringBuilder.htmlFromNode fails when an ID is not matched" {
let sb = StringBuilder ()
RenderFragment.IntoStringBuilder.htmlFromNode sb "bar" (hr [])
Expect.equal (nodeNotFound "bar", string sb)
}
]
]
/// All tests in this module
let allTests =
testList "ViewEngine.Htmx"
[ hxEncoding; hxHeaders; hxParams; hxRequest; hxTrigger; hxVals; attributes; script; renderFragment ]