-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_test.go
368 lines (301 loc) · 11.9 KB
/
form_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package tailush_test
import (
"fmt"
"net/http"
"strings"
"testing"
"github.com/wawandco/tailush"
"github.com/gobuffalo/plush/v4"
"github.com/gobuffalo/tags/v3"
)
func newHctx() plush.HelperContext {
return plush.HelperContext{Context: plush.NewContext()}
}
func TestNew(t *testing.T) {
t.Run("default", func(t *testing.T) {
f := tailush.NewForm(tags.Options{}, newHctx())
if f == nil {
t.Fatalf("f should not be nil")
}
h := f.HTML()
if !strings.Contains(string(h), "form") {
t.Fatalf("f.HTML() should contain 'form'")
}
})
t.Run("method not specified defaults to POST", func(t *testing.T) {
f := tailush.NewForm(tags.Options{}, newHctx())
if f.Options["method"] != "POST" {
t.Fatalf("f.Options[\"method\"] should be 'POST'")
}
})
t.Run("get should be respected", func(t *testing.T) {
f := tailush.NewForm(tags.Options{"method": http.MethodGet}, newHctx())
if f.Options["method"] != http.MethodGet {
t.Fatalf("f.Options[\"method\"] should be 'GET'")
}
h := f.HTML()
if strings.Contains(string(h), "_method") {
t.Fatalf("f.HTML() should contain not '_method' field")
}
})
t.Run("not get/post should be translated to '_method'", func(t *testing.T) {
f := tailush.NewForm(tags.Options{"method": http.MethodPut}, newHctx())
if f.Options["method"] != "POST" {
t.Fatalf("f.Options[\"method\"] should be 'POST'")
}
h := f.HTML()
if !strings.Contains(string(h), `<input name="_method" type="hidden" value="PUT" />`) {
t.Fatalf("f.HTML() should contain '_method' field")
}
})
t.Run("not get/post should be translated to '_method'", func(t *testing.T) {
f := tailush.NewForm(tags.Options{"method": http.MethodPut}, newHctx())
if f.Options["method"] != "POST" {
t.Fatalf("f.Options[\"method\"] should be 'POST'")
}
h := f.HTML()
if !strings.Contains(string(h), `<input name="_method" type="hidden" value="PUT" />`) {
t.Fatalf("f.HTML() should contain '_method' field")
}
})
t.Run("contains multipart", func(t *testing.T) {
f := tailush.NewForm(tags.Options{"multipart": "true"}, newHctx())
if f.Options["enctype"] != "multipart/form-data" {
t.Fatalf(`f.Options["enctype"] should be 'multipart/form-data'`)
}
})
}
func TestLabel(t *testing.T) {
t.Run("form label", func(t *testing.T) {
f := tailush.NewForm(tags.Options{}, newHctx())
l := f.Label("The Label", tags.Options{})
if !strings.Contains(string(l.HTML()), `<label class="block text-sm font-medium text-gray-700">The Label</label>`) {
t.Fatalf("Label should contain 'The Label'")
}
})
t.Run("label with empty value", func(t *testing.T) {
f := tailush.NewForm(tags.Options{}, newHctx())
l := f.Label("", tags.Options{})
if !strings.Contains(string(l.HTML()), `<label class="block text-sm font-medium text-gray-700"></label>`) {
t.Fatalf("form should contain empty label")
}
})
t.Run("using other tag attributes", func(t *testing.T) {
f := tailush.NewForm(tags.Options{}, newHctx())
l := f.Label("something", tags.Options{"id": "label-id"})
if !strings.Contains(string(l.HTML()), `<label class="block text-sm font-medium text-gray-700" id="label-id">something</label>`) {
t.Fatalf("form should contain empty label")
}
})
t.Run("passing class to the label", func(t *testing.T) {
f := tailush.NewForm(tags.Options{}, newHctx())
l := f.Label("something", tags.Options{"id": "label-id", "class": "text-red-500"})
if !strings.Contains(string(l.HTML()), `<label class="block text-sm font-medium text-gray-700 text-red-500" id="label-id">something</label>`) {
t.Fatalf("form should contain mixed classes")
}
})
t.Run("overriding default classes", func(t *testing.T) {
opt := tailush.UseLabelClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
l := f.Label("any", tags.Options{})
if !strings.Contains(string(l.HTML()), `<label class="my-custom-class">any</label>`) {
t.Fatalf("form shouldn't contain mixed classes")
}
})
t.Run("overriding default and passing classes", func(t *testing.T) {
opt := tailush.UseLabelClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
l := f.Label("any", tags.Options{"class": "my-other-class"})
if !strings.Contains(string(l.HTML()), `<label class="my-custom-class my-other-class">any</label>`) {
t.Fatalf("form should contain mixed classes")
}
})
}
func TestFileTag(t *testing.T) {
t.Run("file tag with no options", func(t *testing.T) {
f := tailush.NewForm(tags.Options{}, newHctx())
f.Append(f.FileTag(tags.Options{"name": "file"}))
if !strings.Contains(string(f.HTML()), `<input class="" name="file" type="file" />`) {
fmt.Println(string(f.HTML()))
t.Fatalf("form should contain file input")
}
if !strings.Contains(string(f.HTML()), `enctype="multipart/form-data"`) {
t.Fatalf("enctype should be multipart/form-data")
}
})
t.Run("overriding default classes", func(t *testing.T) {
opt := tailush.UseFileClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
ft := f.FileTag(tags.Options{})
if !strings.Contains(string(ft.HTML()), `<input class="my-custom-class" type="file" />`) {
t.Fatalf("form shouldn't contain mixed classes")
}
})
t.Run("overriding default and passing classes", func(t *testing.T) {
opt := tailush.UseFileClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
ft := f.FileTag(tags.Options{"class": "my-other-class"})
if !strings.Contains(string(ft.HTML()), `<input class="my-custom-class my-other-class" type="file" />`) {
t.Fatalf("form should contain mixed classes")
}
})
}
func TestTextArea(t *testing.T) {
t.Run("text area with no options", func(t *testing.T) {
opt := tailush.UseTextAreaClass("")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
f.Append(f.TextAreaTag(tags.Options{"name": "text"}))
if !strings.Contains(string(f.HTML()), `<textarea class="" name="text"></textarea>`) {
t.Log(string(f.HTML()))
t.Fatalf("form should contain the textarea")
}
})
t.Run("text area with value", func(t *testing.T) {
opt := tailush.UseTextAreaClass("")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
f.Append(f.TextAreaTag(tags.Options{"name": "text", "value": "some text"}))
if !strings.Contains(string(f.HTML()), `<textarea class="" name="text">some text</textarea>`) {
t.Fatalf("textarea should contain `some text`")
}
})
t.Run("text area with encoded content", func(t *testing.T) {
opt := tailush.UseTextAreaClass("")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
f.Append(f.TextAreaTag(tags.Options{"name": "text", "value": "<script>alert</script>"}))
if !strings.Contains(string(f.HTML()), `<textarea class="" name="text"><script>alert</script></textarea>`) {
t.Fatalf("textarea should contain the encoded content")
}
})
t.Run("text area with tag_only", func(t *testing.T) {
opt := tailush.UseTextAreaClass("")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
f.Append(f.TextAreaTag(tags.Options{"name": "text", "tag_only": "true"}))
if !strings.Contains(string(f.HTML()), `<textarea class="" name="text"></textarea>`) {
t.Fatalf("textarea should not contain tag_only")
}
})
t.Run("text area", func(t *testing.T) {
opt := tailush.UseTextAreaClass("")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
f.Append(f.TextArea(tags.Options{"name": "text", "tag_only": "true"}))
if !strings.Contains(string(f.HTML()), `<textarea class="" name="text"></textarea>`) {
t.Fatalf("textarea should not contain tag_only")
}
})
t.Run("overriding default classes", func(t *testing.T) {
opt := tailush.UseTextAreaClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
ta := f.TextAreaTag(tags.Options{})
if !strings.Contains(string(ta.HTML()), `<textarea class="my-custom-class"></textarea>`) {
t.Fatalf("form shouldn't contain mixed classes")
}
})
t.Run("overriding default and passing classes", func(t *testing.T) {
opt := tailush.UseTextAreaClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
ta := f.TextAreaTag(tags.Options{"class": "my-other-class"})
if !strings.Contains(string(ta.HTML()), `<textarea class="my-custom-class my-other-class"></textarea>`) {
t.Fatalf("form should contain mixed classes")
}
})
}
func TestHiddenTag(t *testing.T) {
t.Run("hidden field with tag_only", func(t *testing.T) {
f := tailush.NewForm(tags.Options{}, newHctx())
f.Append(f.HiddenTag(tags.Options{"name": "id", "tag_only": "true", "value": "1"}))
if !strings.Contains(string(f.HTML()), `<input name="id" type="hidden" value="1" />`) {
t.Fatalf("textarea should not contain tag_only")
}
})
}
func TestInputTag(t *testing.T) {
t.Run("overriding default classes", func(t *testing.T) {
opt := tailush.UseInputClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
it := f.InputTag(tags.Options{})
if !strings.Contains(string(it.HTML()), `<input class="my-custom-class" type="text" />`) {
t.Fatalf("form shouldn't contain mixed classes")
}
})
t.Run("overriding default and passing classes", func(t *testing.T) {
opt := tailush.UseInputClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
it := f.InputTag(tags.Options{"class": "my-other-class"})
if !strings.Contains(string(it.HTML()), `<input class="my-custom-class my-other-class" type="text" />`) {
t.Fatalf("form should contain mixed classes")
}
})
}
func TestDateTimeTag(t *testing.T) {
t.Run("overriding default classes", func(t *testing.T) {
opt := tailush.UseDateInputClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
dt := f.DateTimeTag(tags.Options{})
if !strings.Contains(string(dt.HTML()), `<input class="my-custom-class" format="2006-01-02T03:04" type="datetime-local" />`) {
t.Fatalf("form shouldn't contain mixed classes")
}
})
t.Run("overriding default and passing classes", func(t *testing.T) {
opt := tailush.UseDateInputClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
dt := f.DateTimeTag(tags.Options{"class": "my-other-class"})
if !strings.Contains(string(dt.HTML()), `<input class="my-custom-class my-other-class" format="2006-01-02T03:04" type="datetime-local" />`) {
t.Fatalf("form should contain mixed classes")
}
})
}
func TestCheckboxTag(t *testing.T) {
t.Run("overriding default classes", func(t *testing.T) {
opt := tailush.UseCheckboxClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
ct := f.CheckboxTag(tags.Options{})
if !strings.Contains(string(ct.HTML()), `<input class="my-custom-class" type="checkbox" value="true" />`) {
t.Fatalf("form shouldn't contain mixed classes")
}
})
t.Run("overriding default and passing classes", func(t *testing.T) {
opt := tailush.UseCheckboxClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
ct := f.CheckboxTag(tags.Options{"class": "my-other-class"})
if !strings.Contains(string(ct.HTML()), `<input class="my-custom-class my-other-class" type="checkbox" value="true" />`) {
t.Fatalf("form should contain mixed classes")
}
})
}
func TestRadioButtonTag(t *testing.T) {
t.Run("overriding default classes", func(t *testing.T) {
opt := tailush.UseRadioClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
ct := f.RadioButtonTag(tags.Options{})
if !strings.Contains(string(ct.HTML()), `<input class="my-custom-class" type="radio" checked />`) {
t.Fatalf("form shouldn't contain mixed classes")
}
})
t.Run("overriding default and passing classes", func(t *testing.T) {
opt := tailush.UseRadioClass("my-custom-class")
f := tailush.NewForm(tags.Options{}, newHctx())
opt(f)
ct := f.RadioButtonTag(tags.Options{"class": "my-other-class"})
if !strings.Contains(string(ct.HTML()), `<input class="my-custom-class my-other-class" type="radio" checked />`) {
t.Fatalf("form should contain mixed classes")
}
})
}