Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
maddalax committed Oct 31, 2024
1 parent 8a00828 commit f42351e
Show file tree
Hide file tree
Showing 8 changed files with 546 additions and 0 deletions.
160 changes: 160 additions & 0 deletions framework/h/attribute_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package h

import (
"github.com/maddalax/htmgo/framework/hx"
"github.com/stretchr/testify/assert"
"testing"
)

func TestAttributes(t *testing.T) {
tests := []struct {
name string
attribute *AttributeR
expectedKey string
expectedValue string
}{
{"NoSwap", NoSwap(), "hx-swap", "none"},
{"Checked", Checked().(*AttributeR), "checked", ""},
{"Id", Id("myID").(*AttributeR), "id", "myID"},
{"Disabled", Disabled(), "disabled", ""},
{"HxTarget", HxTarget("#myTarget").(*AttributeR), "hx-target", "#myTarget"},
{"Name", Name("myName").(*AttributeR), "name", "myName"},
{"HxConfirm", HxConfirm("Are you sure?").(*AttributeR), "hx-confirm", "Are you sure?"},
{"Class", Class("class1", "class2"), "class", "class1 class2 "},
{"ReadOnly", ReadOnly(), "readonly", ""},
{"Required", Required(), "required", ""},
{"Multiple", Multiple(), "multiple", ""},
{"Selected", Selected(), "selected", ""},
{"MaxLength", MaxLength(10), "maxlength", "10"},
{"MinLength", MinLength(5), "minlength", "5"},
{"Size", Size(3), "size", "3"},
{"Width", Width(100), "width", "100"},
{"Height", Height(200), "height", "200"},
{"Download", Download(true), "download", "true"},
{"Rel", Rel("noopener"), "rel", "noopener"},
{"Pattern", Pattern("[A-Za-z]+"), "pattern", "[A-Za-z]+"},
{"Action", Action("/submit"), "action", "/submit"},
{"Method", Method("POST"), "method", "POST"},
{"Enctype", Enctype("multipart/form-data"), "enctype", "multipart/form-data"},
{"AutoComplete", AutoComplete("on"), "autocomplete", "on"},
{"AutoFocus", AutoFocus(), "autofocus", ""},
{"NoValidate", NoValidate(), "novalidate", ""},
{"Step", Step("0.1"), "step", "0.1"},
{"Max", Max("100"), "max", "100"},
{"Min", Min("0"), "min", "0"},
{"Cols", Cols(30), "cols", "30"},
{"Rows", Rows(10), "rows", "10"},
{"Wrap", Wrap("soft"), "wrap", "soft"},
{"Role", Role("button"), "role", "button"},
{"AriaLabel", AriaLabel("Close Dialog"), "aria-label", "Close Dialog"},
{"AriaHidden", AriaHidden(true), "aria-hidden", "true"},
{"TabIndex", TabIndex(1), "tabindex", "1"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expectedKey, tt.attribute.Name)
assert.Equal(t, tt.expectedValue, tt.attribute.Value)
})
}
}

func TestClassF(t *testing.T) {
attribute := ClassF("class-%d", 123)
assert.Equal(t, "class", attribute.Name)
assert.Equal(t, "class-123", attribute.Value)
}

func TestClassX(t *testing.T) {
classMap := ClassMap{"visible": true, "hidden": false}
attribute := ClassX("base", classMap).(*AttributeR)
assert.Equal(t, "class", attribute.Name)
assert.Equal(t, "base visible ", attribute.Value)
}

func TestJoinAttributes(t *testing.T) {
attr1 := Attribute("data-attr", "one")
attr2 := Attribute("data-attr", "two")
joined := JoinAttributes(", ", attr1, attr2)
assert.Equal(t, "data-attr", joined.Name)
assert.Equal(t, "one, two", joined.Value)
}

func TestTarget(t *testing.T) {
attr := Target("_blank")
assert.Equal(t, "target", attr.(*AttributeR).Name)
assert.Equal(t, "_blank", attr.(*AttributeR).Value)
}

func TestD(t *testing.T) {
attr := D("M10 10 H 90 V 90 H 10 Z")
assert.Equal(t, "d", attr.(*AttributeR).Name)
assert.Equal(t, "M10 10 H 90 V 90 H 10 Z", attr.(*AttributeR).Value)
}

func TestHxExtension(t *testing.T) {
attr := HxExtension("trigger-children")
assert.Equal(t, "hx-ext", attr.Name)
assert.Equal(t, "trigger-children", attr.Value)
}

func TestHxExtensions(t *testing.T) {
attr := HxExtensions("foo", "bar")
assert.Equal(t, "hx-ext", attr.(*AttributeR).Name)
assert.Equal(t, "foo,bar", attr.(*AttributeR).Value)
}

func TestHxTrigger(t *testing.T) {
trigger := hx.NewTrigger(hx.OnClick()) // This assumes hx.NewTrigger is a correct call
attr := HxTrigger(hx.OnClick())
assert.Equal(t, "hx-trigger", attr.Name)
assert.Equal(t, trigger.ToString(), attr.Value)
}

func TestHxTriggerClick(t *testing.T) {
attr := HxTriggerClick() // Assuming no options for simplicity
assert.Equal(t, "hx-trigger", attr.Name)
assert.Equal(t, "click", attr.Value)
}

func TestTriggerChildren(t *testing.T) {
attr := TriggerChildren()
assert.Equal(t, "hx-ext", attr.Name)
assert.Equal(t, "trigger-children", attr.Value)
}

func TestHxInclude(t *testing.T) {
attr := HxInclude(".include-selector")
assert.Equal(t, "hx-include", attr.(*AttributeR).Name)
assert.Equal(t, ".include-selector", attr.(*AttributeR).Value)
}

func TestHxIndicator(t *testing.T) {
attr := HxIndicator("#my-indicator")
assert.Equal(t, "hx-indicator", attr.Name)
assert.Equal(t, "#my-indicator", attr.Value)
}

func TestHidden(t *testing.T) {
attr := Hidden()
assert.Equal(t, "style", attr.(*AttributeR).Name)
assert.Equal(t, "display:none", attr.(*AttributeR).Value)
}

func TestControls(t *testing.T) {
attr := Controls()
assert.Equal(t, "controls", attr.(*AttributeR).Name)
assert.Equal(t, "", attr.(*AttributeR).Value)
}

func TestPlaceholder(t *testing.T) {
attr := Placeholder("Enter text")
assert.Equal(t, "placeholder", attr.(*AttributeR).Name)
assert.Equal(t, "Enter text", attr.(*AttributeR).Value)
}

func TestBoost(t *testing.T) {
attr := Boost()
assert.Equal(t, "hx-boost", attr.(*AttributeR).Name)
assert.Equal(t, "true", attr.(*AttributeR).Value)
}
15 changes: 15 additions & 0 deletions framework/h/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,3 +381,18 @@ func TestToggleClassOnSibling(t *testing.T) {
}
`))
}

func TestPreventDefault(t *testing.T) {
t.Parallel()
compareIgnoreSpaces(t, renderJs(t, PreventDefault()), "event.preventDefault();")
}

func TestConsoleLog(t *testing.T) {
t.Parallel()
compareIgnoreSpaces(t, renderJs(t, ConsoleLog("Log Message")), "console.log('Log Message');")
}

func TestSetValue(t *testing.T) {
t.Parallel()
compareIgnoreSpaces(t, renderJs(t, SetValue("New Value")), "this.value = 'New Value';")
}
57 changes: 57 additions & 0 deletions framework/h/header_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package h

import (
"github.com/maddalax/htmgo/framework/hx"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestReplaceUrlHeader(t *testing.T) {
headers := ReplaceUrlHeader("/new-url")
assert.Contains(t, *headers, hx.ReplaceUrlHeader)
assert.Equal(t, "/new-url", (*headers)[hx.ReplaceUrlHeader])
}

func TestPushUrlHeader(t *testing.T) {
headers := PushUrlHeader("/push-url")
assert.Contains(t, *headers, hx.PushUrlHeader)
assert.Equal(t, "/push-url", (*headers)[hx.PushUrlHeader])
}

func TestPushQsHeader(t *testing.T) {
ctx := &RequestContext{currentBrowserUrl: "https://example.com/path"}
qs := NewQs("a", "b", "c", "d")
headers := PushQsHeader(ctx, qs)
expectedURL := "/path?a=b&c=d"
assert.Contains(t, *headers, hx.ReplaceUrlHeader)
assert.Equal(t, expectedURL, (*headers)[hx.ReplaceUrlHeader])
}

func TestCombineHeaders(t *testing.T) {
h1 := NewHeaders("Content-Type", "application/json")
h2 := NewHeaders("Authorization", "Bearer token")
combined := CombineHeaders(h1, h2)
assert.Equal(t, "application/json", (*combined)["Content-Type"])
assert.Equal(t, "Bearer token", (*combined)["Authorization"])
}

func TestCurrentPath(t *testing.T) {
req, _ := http.NewRequest("GET", "https://example.com", nil)
req.Header.Set(hx.CurrentUrlHeader, "https://example.com/current-path")
ctx := &RequestContext{Request: req}
path := CurrentPath(ctx)
assert.Equal(t, "/current-path", path)
}

func TestNewHeaders(t *testing.T) {
headers := NewHeaders("X-Custom", "value", "X-Another", "another-value")
require.NotNil(t, headers)
assert.Equal(t, "value", (*headers)["X-Custom"])
assert.Equal(t, "another-value", (*headers)["X-Another"])

invalidHeaders := NewHeaders("X-Custom")
assert.Empty(t, *invalidHeaders) // Check incorrect pair length handling
}
27 changes: 27 additions & 0 deletions framework/h/qs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package h

import (
"github.com/stretchr/testify/assert"
"net/http"
"net/url"
"testing"
)

Expand Down Expand Up @@ -47,3 +49,28 @@ func TestSetQsOnUrlWithDelete(t *testing.T) {
set := SetQueryParams("https://example.com/path?a=b&c=d", qs)
assert.Equal(t, "https://example.com/path?a=b2", set)
}

func TestGetQueryParam(t *testing.T) {
t.Parallel()
req, _ := http.NewRequest("GET", "http://localhost/?foo=bar&baz=qux", nil)
ctx := &RequestContext{Request: req}

result := GetQueryParam(ctx, "foo")
assert.Equal(t, "bar", result)

result = GetQueryParam(ctx, "baz")
assert.Equal(t, "qux", result)

result = GetQueryParam(ctx, "missing")
assert.Equal(t, "", result)

ctx.currentBrowserUrl = "http://localhost/?current=value"

result = GetQueryParam(ctx, "current")
assert.Equal(t, "value", result)

// url params should override browser url
req.URL, _ = url.Parse("http://localhost/?foo=override")
result = GetQueryParam(ctx, "foo")
assert.Equal(t, "override", result)
}
3 changes: 3 additions & 0 deletions framework/h/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (

// JsonSerializeOrEmpty serializes the given data as JSON, or returns an empty string if the serialization fails.
func JsonSerializeOrEmpty(data any) string {
if data == nil {
return ""
}
serialized, err := json.Marshal(data)
if err != nil {
return ""
Expand Down
28 changes: 28 additions & 0 deletions framework/h/serialize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package h

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestSerialize(t *testing.T) {
t.Parallel()
data := map[string]any{
"hello": "world",
"foo": "bar",
}
serialized := JsonSerializeOrEmpty(data)
assert.Equal(t, `{"foo":"bar","hello":"world"}`, serialized)
}

func TestSerializeNil(t *testing.T) {
t.Parallel()
serialized := JsonSerializeOrEmpty(nil)
assert.Equal(t, "", serialized)
}

func TestSerializeInvalid(t *testing.T) {
t.Parallel()
serialized := JsonSerializeOrEmpty(func() {})
assert.Equal(t, "", serialized)
}
Loading

0 comments on commit f42351e

Please sign in to comment.