utils_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package presets
  2. import (
  3. "context"
  4. "testing"
  5. "github.com/google/go-cmp/cmp"
  6. v "github.com/qor5/ui/vuetify"
  7. "github.com/qor5/web"
  8. h "github.com/theplant/htmlgo"
  9. )
  10. func TestInputWithDefaults(t *testing.T) {
  11. type Person struct {
  12. Name string
  13. }
  14. ctx := context.Background()
  15. obj := Person{Name: "Tom"}
  16. field := &FieldContext{
  17. Name: "Name",
  18. FormKey: "NameFormKey",
  19. Label: "NameLabel",
  20. }
  21. // HTML component
  22. t.Run("Input", func(t *testing.T) {
  23. expect, _ := h.Input("").Attr(web.VFieldName(field.FormKey)...).Value(field.StringValue(obj)).MarshalHTML(ctx)
  24. result, _ := InputWithDefaults(h.Input(""), obj, field).MarshalHTML(ctx)
  25. if diff := cmp.Diff(expect, result); diff != "" {
  26. t.Fatal(diff)
  27. }
  28. })
  29. // Vuetify component
  30. t.Run("VSelect", func(t *testing.T) {
  31. expect, _ := v.VSelect().FieldName(field.FormKey).Value(field.Value(obj)).Label(field.Label).Outlined(true).MarshalHTML(ctx)
  32. result, _ := InputWithDefaults(v.VSelect(), obj, field).Outlined(true).MarshalHTML(ctx)
  33. if diff := cmp.Diff(expect, result); diff != "" {
  34. t.Fatal(diff)
  35. }
  36. })
  37. t.Run("VTextField", func(t *testing.T) {
  38. expect, _ := v.VTextField().FieldName(field.FormKey).Value(field.Value(obj)).Label(field.Label).MarshalHTML(ctx)
  39. result, _ := InputWithDefaults(v.VTextField(), obj, field).MarshalHTML(ctx)
  40. if diff := cmp.Diff(expect, result); diff != "" {
  41. t.Fatal(diff)
  42. }
  43. })
  44. }