From f0aa1a41d7fb225f885a05fc197b8f4721b018c6 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sat, 30 Oct 2021 13:02:10 -0400 Subject: [PATCH] Add tests for attribute rendering --- .gitignore | 4 +- .../Giraffe.ViewEngine.Htmx.Tests.fsproj | 5 + src/ViewEngine.Htmx.Tests/Tests.fs | 127 +++++++++++++++++- src/ViewEngine.Htmx/Htmx.fs | 14 +- 4 files changed, 138 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index 8462afe..1592c8d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,6 @@ **/obj .fake -.ionide \ No newline at end of file +.ionide +.idea +*.user diff --git a/src/ViewEngine.Htmx.Tests/Giraffe.ViewEngine.Htmx.Tests.fsproj b/src/ViewEngine.Htmx.Tests/Giraffe.ViewEngine.Htmx.Tests.fsproj index 768b74c..2d275f3 100644 --- a/src/ViewEngine.Htmx.Tests/Giraffe.ViewEngine.Htmx.Tests.fsproj +++ b/src/ViewEngine.Htmx.Tests/Giraffe.ViewEngine.Htmx.Tests.fsproj @@ -13,6 +13,7 @@ + @@ -25,4 +26,8 @@ + + + + diff --git a/src/ViewEngine.Htmx.Tests/Tests.fs b/src/ViewEngine.Htmx.Tests/Tests.fs index b908e37..f2f5ba5 100644 --- a/src/ViewEngine.Htmx.Tests/Tests.fs +++ b/src/ViewEngine.Htmx.Tests/Tests.fs @@ -1,8 +1,125 @@ -module Tests +module Giraffe.ViewEngine.Htmx.Tests -open System +open Giraffe.ViewEngine open Xunit -[] -let ``My test`` () = - Assert.True(true) +/// 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 ``_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 ``_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 ] [] |> shouldRender """
""" + + [] + let ``_hxPut succeeds`` () = + s [ _hxPut "/take-this" ] [] |> shouldRender """""" + + [] + let ``_hxRequest succeeds`` () = + u [ _hxRequest "noHeaders" ] [] |> shouldRender """""" + + [] + let ``_hxSelect succeeds`` () = + nav [ _hxSelect "#navbar" ] [] |> 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 ``_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 """
      """ + \ No newline at end of file diff --git a/src/ViewEngine.Htmx/Htmx.fs b/src/ViewEngine.Htmx/Htmx.fs index 5c8aa40..63b71de 100644 --- a/src/ViewEngine.Htmx/Htmx.fs +++ b/src/ViewEngine.Htmx/Htmx.fs @@ -50,8 +50,8 @@ module HxTrigger = match trigger.Contains "[" with | true -> let parts = trigger.Split ('[', ']') - sprintf "%s[%s&&%s]" parts.[0] parts.[1] filter - | false -> sprintf "%s[%s]" trigger filter + $"{parts.[0]}[{parts.[1]}&&{filter}]" + | false -> $"{trigger}[{filter}]" /// Trigger the event on a click let Click = "click" /// Trigger the event on page load @@ -81,9 +81,9 @@ module HxTrigger = /// Attributes and flags for HTMX [] module HtmxAttrs = - /// Progressively enhances anchors and forms to use AJAX requests + /// Progressively enhances anchors and forms to use AJAX requests (use `_hxNoBoost` to set to false) let _hxBoost = attr "hx-boost" "true" - /// Shows a confim() dialog before issuing a request + /// Shows a confirm() dialog before issuing a request let _hxConfirm = attr "hx-confirm" /// Issues a DELETE to the specified URL let _hxDelete = attr "hx-delete" @@ -103,6 +103,8 @@ module HtmxAttrs = let _hxInclude = attr "hx-include" /// The element to put the htmx-request class on during the AJAX request let _hxIndicator = attr "hx-indicator" + /// Overrides a previous `hx-boost` + let _hxNoBoost = attr "hx-boost" "false" /// Filters the parameters that will be submitted with a request let _hxParams = attr "hx-params" /// Issues a PATCH to the specified URL @@ -123,10 +125,10 @@ module HtmxAttrs = let _hxSelect = attr "hx-select" /// Establishes and listens to Server Sent Event (SSE) sources for events let _hxSse = attr "hx-sse" - /// Marks content in a response as being "Out of Band", i.e. swapped somewhere other than the target - let _hxSwapOob = attr "hx-swap-oob" /// Controls how the response content is swapped into the DOM (e.g. 'outerHTML' or 'beforeEnd') let _hxSwap = attr "hx-swap" + /// Marks content in a response as being "Out of Band", i.e. swapped somewhere other than the target + let _hxSwapOob = attr "hx-swap-oob" /// Specifies the target element to be swapped let _hxTarget = attr "hx-target" /// Specifies the event that triggers the request