-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
546 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.