-
Notifications
You must be signed in to change notification settings - Fork 192
/
CSSFrameworkSpec.hs
725 lines (603 loc) · 53.9 KB
/
CSSFrameworkSpec.hs
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
{-|
Module: Test.View.CSSFrameworkSpec
Copyright: (c) digitally induced GmbH, 2020
-}
module Test.View.CSSFrameworkSpec where
import Test.Hspec
import IHP.Prelude
import IHP.Controller.Context
import IHP.FrameworkConfig as FrameworkConfig
import Control.Exception
import IHP.Controller.RequestContext
import IHP.View.Types
import IHP.View.CSSFramework
import IHP.FlashMessages.Types
import IHP.Controller.Session
import qualified Text.Blaze.Renderer.Text as Blaze
import qualified Text.Blaze.Html5 as H
import IHP.HSX.QQ (hsx)
import IHP.ModelSupport
import IHP.Breadcrumb.Types
import IHP.Breadcrumb.ViewFunctions (breadcrumbLink, breadcrumbLinkExternal, breadcrumbText, renderBreadcrumb)
import IHP.Pagination.Types
import qualified IHP.Prelude as Text (isInfixOf)
import qualified Data.TMap as TypeMap
import qualified Network.Wai as Wai
import IHP.Pagination.ViewFunctions (renderPagination)
tests = do
describe "CSS Framework" do
describe "bootstrap5" do
let cssFramework = bootstrap
it "should render a successful flash message" do
let flashMessage = SuccessFlashMessage "You have successfully registered for an account"
styledFlashMessage cssFramework cssFramework flashMessage `shouldRenderTo` "<div class=\"alert alert-success\">You have successfully registered for an account</div>"
it "should render a error flash message" do
let flashMessage = ErrorFlashMessage "You have successfully registered for an account"
styledFlashMessage cssFramework cssFramework flashMessage `shouldRenderTo` "<div class=\"alert alert-danger\">You have successfully registered for an account</div>"
describe "submit button" do
let submitButton = SubmitButton { label = "Save Project" , buttonClass = "my-custom-button" , cssFramework, buttonDisabled = False }
it "should render" do
styledSubmitButton cssFramework cssFramework submitButton `shouldRenderTo` "<button class=\"btn btn-primary my-custom-button\" type=\"submit\">Save Project</button>"
it "should render with empty class" do
styledSubmitButton cssFramework cssFramework (submitButton { buttonClass = "" }) `shouldRenderTo` "<button class=\"btn btn-primary\" type=\"submit\">Save Project</button>"
it "should render with disabled button" do
styledSubmitButton cssFramework cssFramework (submitButton { buttonClass = "", buttonDisabled = True }) `shouldRenderTo` "<button class=\"btn btn-primary\" disabled=\"disabled\" type=\"submit\">Save Project</button>"
describe "text field" do
let baseTextField = FormField
{ fieldType = TextInput
, fieldName = "firstname"
, fieldLabel = "First name:"
, fieldValue = ""
, fieldInputId = "fname"
, validatorResult = Nothing
, additionalAttributes = []
, fieldClass = ""
, labelClass = ""
, disabled = False
, disableLabel = False
, disableGroup = False
, disableValidationResult = False
, cssFramework = cssFramework
, helpText = ""
, placeholder = "Your firstname"
, required = False
, autofocus = False
}
it "should render" do
let textField = baseTextField
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-fname\"><label class=\"form-label\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\"> </div>"
it "should render with disabled" do
let textField = baseTextField { disabled = True }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-fname\"><label class=\"form-label\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\" disabled=\"disabled\"> </div>"
it "should render a validation error" do
let textField = baseTextField { validatorResult = Just (TextViolation "should not be empty") }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-fname\"><label class=\"form-label\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control is-invalid\"><div class=\"invalid-feedback\">should not be empty</div> </div>"
it "should render with disableLabel = True" do
let textField = baseTextField { disableLabel = True }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-fname\"><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\"> </div>"
it "should render with disableGroup = True" do
let textField = baseTextField { disableGroup = True }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<label class=\"form-label\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\"> "
it "should render with help text" do
let textField = baseTextField { helpText = "Enter your first name" }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-fname\"><label class=\"form-label\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\"> <small class=\"form-text\">Enter your first name</small></div>"
it "should render with additional attributes" do
let textField = baseTextField { additionalAttributes = [ ("min", "1"), ("max", "100") ] }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-fname\"><label class=\"form-label\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\" min=\"1\" max=\"100\"> </div>"
describe "hidden field" do
let baseTextField = FormField
{ fieldType = HiddenInput
, fieldName = "workerProfileId"
, fieldLabel = "Worker Profile Id"
, fieldValue = ""
, fieldInputId = "award_workerProfileId"
, validatorResult = Nothing
, additionalAttributes = []
, fieldClass = ""
, labelClass = ""
, disabled = False
, disableLabel = True
, disableGroup = True
, disableValidationResult = True
, cssFramework = cssFramework
, helpText = ""
, placeholder = ""
, required = False
, autofocus = False
}
it "should render without a group and without validation" do
let textField = baseTextField
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<input type=\"hidden\" name=\"workerProfileId\" placeholder=\"\" id=\"award_workerProfileId\" class=\"form-control\"> "
describe "checkbox" do
let baseCheckbox = FormField
{ fieldType = CheckboxInput
, fieldName = "is_active"
, fieldLabel = "Is Active"
, fieldValue = "yes"
, fieldInputId = "user_is_active"
, validatorResult = Nothing
, fieldClass = ""
, labelClass = ""
, disabled = False
, disableLabel = False
, disableGroup = False
, disableValidationResult = False
, additionalAttributes = []
, cssFramework = cssFramework
, helpText = ""
, placeholder = ""
, required = False
, autofocus = False
}
it "should render" do
let checkbox = baseCheckbox
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-user_is_active\"><div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> </div></div>"
it "should render with disabled" do
let checkbox = baseCheckbox { disabled = True }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-user_is_active\"><div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\" disabled=\"disabled\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> </div></div>"
it "should render without checked" do
let checkbox = baseCheckbox { fieldValue = "no" }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-user_is_active\"><div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> </div></div>"
it "should render with disableLabel = True" do
let checkbox = baseCheckbox { disableLabel = True }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-user_is_active\"><div class=\"form-check\"><div><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\"><input type=\"hidden\" name=\"is_active\" value=\"off\"> </div></div></div>"
it "should render with disableGroup = True" do
let checkbox = baseCheckbox { disableGroup = True }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> </div>"
it "should render with help text" do
let checkbox = baseCheckbox { helpText = "Is the user active?" }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-user_is_active\"><div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> <small class=\"form-text\">Is the user active?</small></div></div>"
it "should render with additional attributes" do
let checkbox = baseCheckbox { additionalAttributes = [ ("data-x", "true") ] }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-user_is_active\"><div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\" data-x=\"true\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> </div></div>"
describe "select" do
let baseSelect = FormField
{ fieldType = SelectInput [("First Value", "a"), ("Second Value", "b")]
, fieldName = "user_id"
, fieldLabel = "User"
, fieldValue = ""
, fieldInputId = "project_user_id"
, validatorResult = Nothing
, fieldClass = ""
, labelClass = ""
, disabled = False
, disableLabel = False
, disableGroup = False
, disableValidationResult = False
, additionalAttributes = []
, cssFramework = cssFramework
, helpText = ""
, placeholder = "Please select"
, required = False
, autofocus = False
}
it "should render" do
let select = baseSelect
styledFormField cssFramework cssFramework select `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-project_user_id\"><label class=\"form-label\" for=\"project_user_id\">User</label><select name=\"user_id\" id=\"project_user_id\" class=\"form-control\" value=\"\"><option selected=\"selected\" disabled=\"disabled\">Please select</option> <option value=\"a\">First Value</option><option value=\"b\">Second Value</option></select> </div>"
it "should render with disabled" do
let select = baseSelect { disabled = True }
styledFormField cssFramework cssFramework select `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-project_user_id\"><label class=\"form-label\" for=\"project_user_id\">User</label><select name=\"user_id\" id=\"project_user_id\" class=\"form-control\" value=\"\" disabled=\"disabled\"><option selected=\"selected\" disabled=\"disabled\">Please select</option> <option value=\"a\">First Value</option><option value=\"b\">Second Value</option></select> </div>"
it "should render with selected" do
let select = baseSelect { fieldValue = "b" }
styledFormField cssFramework cssFramework select `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-project_user_id\"><label class=\"form-label\" for=\"project_user_id\">User</label><select name=\"user_id\" id=\"project_user_id\" class=\"form-control\" value=\"b\"><option disabled=\"disabled\">Please select</option> <option value=\"a\">First Value</option><option value=\"b\" selected=\"selected\">Second Value</option></select> </div>"
it "should render with custom placeholder" do
let select = baseSelect { placeholder = "Pick something" }
styledFormField cssFramework cssFramework select `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-project_user_id\"><label class=\"form-label\" for=\"project_user_id\">User</label><select name=\"user_id\" id=\"project_user_id\" class=\"form-control\" value=\"\"><option selected=\"selected\" disabled=\"disabled\">Pick something</option> <option value=\"a\">First Value</option><option value=\"b\">Second Value</option></select> </div>"
it "should render with additional attributes" do
let select = baseSelect { additionalAttributes = [ ("data-x", "true") ] }
styledFormField cssFramework cssFramework select `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-project_user_id\"><label class=\"form-label\" for=\"project_user_id\">User</label><select name=\"user_id\" id=\"project_user_id\" class=\"form-control\" value=\"\" data-x=\"true\"><option selected=\"selected\" disabled=\"disabled\">Please select</option> <option value=\"a\">First Value</option><option value=\"b\">Second Value</option></select> </div>"
describe "radio" do
let baseRadio = FormField
{ fieldType = RadioInput [("First Value", "a"), ("Second Value", "b")]
, fieldName = "user_id"
, fieldLabel = "User"
, fieldValue = ""
, fieldInputId = "project_user_id"
, validatorResult = Nothing
, fieldClass = ""
, labelClass = ""
, disabled = False
, disableLabel = False
, disableGroup = False
, disableValidationResult = False
, additionalAttributes = []
, cssFramework = cssFramework
, helpText = ""
, placeholder = ""
, required = False
, autofocus = False
}
it "should render" do
let radio = baseRadio
styledFormField cssFramework cssFramework radio `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-project_user_id\"><label class=\"form-label\" for=\"project_user_id\">User</label><fieldset><div class=\"form-check\"><input class=\"form-check-input\" type=\"radio\" id=\"project_user_id_a\" name=\"user_id\" value=\"a\"><label class=\"form-check-label\" for=\"project_user_id_a\">First Value</label></div><div class=\"form-check\"><input class=\"form-check-input\" type=\"radio\" id=\"project_user_id_b\" name=\"user_id\" value=\"b\"><label class=\"form-check-label\" for=\"project_user_id_b\">Second Value</label></div></fieldset> </div>"
it "should render with disabled" do
let radio = baseRadio { disabled = True }
styledFormField cssFramework cssFramework radio `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-project_user_id\"><label class=\"form-label\" for=\"project_user_id\">User</label><fieldset><div class=\"form-check\"><input class=\"form-check-input\" type=\"radio\" id=\"project_user_id_a\" name=\"user_id\" value=\"a\" disabled=\"disabled\"><label class=\"form-check-label\" for=\"project_user_id_a\">First Value</label></div><div class=\"form-check\"><input class=\"form-check-input\" type=\"radio\" id=\"project_user_id_b\" name=\"user_id\" value=\"b\" disabled=\"disabled\"><label class=\"form-check-label\" for=\"project_user_id_b\">Second Value</label></div></fieldset> </div>"
it "should render with checked" do
let radio = baseRadio { fieldValue = "b" }
styledFormField cssFramework cssFramework radio `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-project_user_id\"><label class=\"form-label\" for=\"project_user_id\">User</label><fieldset><div class=\"form-check\"><input class=\"form-check-input\" type=\"radio\" id=\"project_user_id_a\" name=\"user_id\" value=\"a\"><label class=\"form-check-label\" for=\"project_user_id_a\">First Value</label></div><div class=\"form-check\"><input class=\"form-check-input\" type=\"radio\" id=\"project_user_id_b\" name=\"user_id\" value=\"b\" checked=\"checked\"><label class=\"form-check-label\" for=\"project_user_id_b\">Second Value</label></div></fieldset> </div>"
it "should render with additional attributes" do
let radio = baseRadio { additionalAttributes = [ ("data-x", "true") ] }
styledFormField cssFramework cssFramework radio `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-project_user_id\"><label class=\"form-label\" for=\"project_user_id\">User</label><fieldset><div class=\"form-check\"><input class=\"form-check-input\" type=\"radio\" id=\"project_user_id_a\" name=\"user_id\" value=\"a\" data-x=\"true\"><label class=\"form-check-label\" for=\"project_user_id_a\">First Value</label></div><div class=\"form-check\"><input class=\"form-check-input\" type=\"radio\" id=\"project_user_id_b\" name=\"user_id\" value=\"b\" data-x=\"true\"><label class=\"form-check-label\" for=\"project_user_id_b\">Second Value</label></div></fieldset> </div>"
describe "textarea field" do
let baseTextField = FormField
{ fieldType = TextareaInput
, fieldName = "body"
, fieldLabel = "Body:"
, fieldValue = "Hello\nWorld!"
, fieldInputId = "body"
, validatorResult = Nothing
, additionalAttributes = []
, fieldClass = ""
, labelClass = ""
, disabled = False
, disableLabel = False
, disableGroup = False
, disableValidationResult = False
, cssFramework = cssFramework
, helpText = ""
, placeholder = "Describe your issue"
, required = False
, autofocus = False
}
it "should render" do
let textField = baseTextField
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-body\"><label class=\"form-label\" for=\"body\">Body:</label><textarea name=\"body\" placeholder=\"Describe your issue\" id=\"body\" class=\"form-control\">Hello\nWorld!</textarea></div>"
it "should render with additional attributes" do
let textField = baseTextField { additionalAttributes = [ ("data-x", "true") ] }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"mb-3\" id=\"form-group-body\"><label class=\"form-label\" for=\"body\">Body:</label><textarea name=\"body\" placeholder=\"Describe your issue\" id=\"body\" class=\"form-control\" data-x=\"true\">Hello\nWorld!</textarea></div>"
describe "pagination" do
let basePagination = Pagination
{
pageSize = 3
, totalItems = 12
, currentPage = 2
, window = 3
}
it "should render previous link" do
let pagination = basePagination
styledPaginationLinkPrevious cssFramework cssFramework pagination "#" `shouldRenderTo` "<li class=\"page-item\"><a class=\"page-link\" href=\"#\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span><span class=\"sr-only\">Previous</span></a></li>"
it "should render previous link disabled on the first page" do
let pagination = basePagination { currentPage = 1}
styledPaginationLinkPrevious cssFramework cssFramework pagination "#" `shouldRenderTo` "<li class=\"page-item disabled\"><a class=\"page-link\" href=\"#\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span><span class=\"sr-only\">Previous</span></a></li>"
it "should render next link" do
let pagination = basePagination
styledPaginationLinkNext cssFramework cssFramework pagination "#" `shouldRenderTo` "<li class=\"page-item\"><a class=\"page-link\" href=\"#\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span><span class=\"sr-only\">Next</span></a></li>"
it "should render next link disabled on the last page" do
let pagination = basePagination { currentPage = 4}
styledPaginationLinkNext cssFramework cssFramework pagination "#" `shouldRenderTo` "<li class=\"page-item disabled\"><a class=\"page-link\" href=\"#\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span><span class=\"sr-only\">Next</span></a></li>"
it "should render items per page selector" do
let pagination = basePagination
styledPaginationItemsPerPageSelector cssFramework cssFramework pagination (\n -> cs $ "https://example.com?maxItems=" <> (show n)) `shouldRenderTo` "<option value=\"10\" data-url=\"https://example.com?maxItems=10\">10 items per page</option><option value=\"20\" data-url=\"https://example.com?maxItems=20\">20 items per page</option><option value=\"50\" data-url=\"https://example.com?maxItems=50\">50 items per page</option><option value=\"100\" data-url=\"https://example.com?maxItems=100\">100 items per page</option><option value=\"200\" data-url=\"https://example.com?maxItems=200\">200 items per page</option>"
it "should render the wrapping pagination" do
let pagination = basePagination
let paginationView = PaginationView
{ cssFramework = cssFramework
, pagination = pagination
, pageUrl = const ""
, linkPrevious = mempty
, linkNext = mempty
, pageDotDotItems = mempty
, itemsPerPageSelector = mempty
}
let render = Blaze.renderMarkup $ styledPagination cssFramework cssFramework paginationView
Text.isInfixOf "<div class=\"d-flex justify-content-md-center\">" (cs render) `shouldBe` True
it "should render the pagination if there are enough elements" do
let pagination = Pagination
{ pageSize = 5
, totalItems = 15
, currentPage = 1
, window = 3
}
context <- createControllerContextWithCSSFramework cssFramework
let ?context = context
let render = Blaze.renderMarkup $ renderPagination pagination
Text.isInfixOf "<nav aria-label=\"Page Navigator\"" (cs render) `shouldBe` True
it "should not render the pagination if there aren't enough elements" do
let pagination = Pagination
{ pageSize = 10
, totalItems = 5
, currentPage = 1
, window = 3
}
context <- createControllerContextWithCSSFramework cssFramework
let ?context = context
renderPagination pagination `shouldRenderTo` mempty
describe "breadcrumbs" do
it "should render a breadcrumb item with no link" do
let breadcrumbItem = breadcrumbText "First item"
let breadcrumbs = [breadcrumbItem]
styledBreadcrumbItem cssFramework cssFramework breadcrumbs breadcrumbItem True `shouldRenderTo` "<li class=\"breadcrumb-item active\">First item</li>"
it "should render a breadcrumb item with external link" do
let breadcrumbItem = breadcrumbLinkExternal "First item" "https://example.com"
let breadcrumbs = [breadcrumbItem]
styledBreadcrumbItem cssFramework cssFramework breadcrumbs breadcrumbItem True `shouldRenderTo` "<li class=\"breadcrumb-item active\"><a href=\"https://example.com\">First item</a></li>"
it "should render a breadcrumb item as non-active" do
let breadcrumbItem = breadcrumbText "First item"
let breadcrumbs = [breadcrumbItem]
styledBreadcrumbItem cssFramework cssFramework breadcrumbs breadcrumbItem False `shouldRenderTo` "<li class=\"breadcrumb-item\">First item</li>"
it "should render the wrapping breadcrumb and last item as active" do
let breadcrumbs = [breadcrumbText "First item", breadcrumbText "Last item"]
context <- createControllerContextWithCSSFramework cssFramework
let ?context = context
renderBreadcrumb breadcrumbs `shouldRenderTo` "<nav><ol class=\"breadcrumb\"><li class=\"breadcrumb-item\">First item</li><li class=\"breadcrumb-item active\">Last item</li></ol></nav>"
it "should support show of BreadcrumbItem" do
let breadcrumbItem = breadcrumbText "First item"
show breadcrumbItem `shouldBe` "{ breadcrumbLabel = \"First item\", url = Nothing }"
describe "bootstrap4" do
let cssFramework = bootstrap4
it "should render a successful flash message" do
let flashMessage = SuccessFlashMessage "You have successfully registered for an account"
styledFlashMessage cssFramework cssFramework flashMessage `shouldRenderTo` "<div class=\"alert alert-success\">You have successfully registered for an account</div>"
it "should render a error flash message" do
let flashMessage = ErrorFlashMessage "You have successfully registered for an account"
styledFlashMessage cssFramework cssFramework flashMessage `shouldRenderTo` "<div class=\"alert alert-danger\">You have successfully registered for an account</div>"
describe "submit button" do
let submitButton = SubmitButton { label = "Save Project" , buttonClass = "my-custom-button" , cssFramework, buttonDisabled = False }
it "should render" do
styledSubmitButton cssFramework cssFramework submitButton `shouldRenderTo` "<button class=\"btn btn-primary my-custom-button\" type=\"submit\">Save Project</button>"
it "should render with empty class" do
styledSubmitButton cssFramework cssFramework (submitButton { buttonClass = "" }) `shouldRenderTo` "<button class=\"btn btn-primary\" type=\"submit\">Save Project</button>"
it "should render with disabled button" do
styledSubmitButton cssFramework cssFramework (submitButton { buttonClass = "", buttonDisabled = True }) `shouldRenderTo` "<button class=\"btn btn-primary\" disabled=\"disabled\" type=\"submit\">Save Project</button>"
describe "text field" do
let baseTextField = FormField
{ fieldType = TextInput
, fieldName = "firstname"
, fieldLabel = "First name:"
, fieldValue = ""
, fieldInputId = "fname"
, validatorResult = Nothing
, additionalAttributes = []
, fieldClass = ""
, labelClass = ""
, disabled = False
, disableLabel = False
, disableGroup = False
, disableValidationResult = False
, cssFramework = cssFramework
, helpText = ""
, placeholder = "Your firstname"
, required = False
, autofocus = False
}
it "should render" do
let textField = baseTextField
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-fname\"><label class=\"\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\"> </div>"
it "should render with disabled" do
let textField = baseTextField { disabled = True }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-fname\"><label class=\"\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\" disabled=\"disabled\"> </div>"
it "should render a validation error" do
let textField = baseTextField { validatorResult = Just (TextViolation "should not be empty") }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-fname\"><label class=\"\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control is-invalid\"><div class=\"invalid-feedback\">should not be empty</div> </div>"
it "should render with disableLabel = True" do
let textField = baseTextField { disableLabel = True }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-fname\"><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\"> </div>"
it "should render with disableGroup = True" do
let textField = baseTextField { disableGroup = True }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<label class=\"\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\"> "
it "should render with help text" do
let textField = baseTextField { helpText = "Enter your first name" }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-fname\"><label class=\"\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\"> <small class=\"form-text text-muted\">Enter your first name</small></div>"
it "should render with additional attributes" do
let textField = baseTextField { additionalAttributes = [ ("min", "1"), ("max", "100") ] }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-fname\"><label class=\"\" for=\"fname\">First name:</label><input type=\"text\" name=\"firstname\" placeholder=\"Your firstname\" id=\"fname\" class=\"form-control\" min=\"1\" max=\"100\"> </div>"
describe "checkbox" do
let baseCheckbox = FormField
{ fieldType = CheckboxInput
, fieldName = "is_active"
, fieldLabel = "Is Active"
, fieldValue = "yes"
, fieldInputId = "user_is_active"
, validatorResult = Nothing
, fieldClass = ""
, labelClass = ""
, disabled = False
, disableLabel = False
, disableGroup = False
, disableValidationResult = False
, additionalAttributes = []
, cssFramework = cssFramework
, helpText = ""
, placeholder = ""
, required = False
, autofocus = False
}
it "should render" do
let checkbox = baseCheckbox
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-user_is_active\"><div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> </div></div>"
it "should render with disabled" do
let checkbox = baseCheckbox { disabled = True }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-user_is_active\"><div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\" disabled=\"disabled\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> </div></div>"
it "should render without checked" do
let checkbox = baseCheckbox { fieldValue = "no" }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-user_is_active\"><div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> </div></div>"
it "should render with disableLabel = True" do
let checkbox = baseCheckbox { disableLabel = True }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-user_is_active\"><div class=\"form-check\"><div><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\"><input type=\"hidden\" name=\"is_active\" value=\"off\"> </div></div></div>"
it "should render with disableGroup = True" do
let checkbox = baseCheckbox { disableGroup = True }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> </div>"
it "should render with help text" do
let checkbox = baseCheckbox { helpText = "Is the user active?" }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-user_is_active\"><div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> <small class=\"form-text text-muted\">Is the user active?</small></div></div>"
it "should render with additional attributes" do
let checkbox = baseCheckbox { additionalAttributes = [ ("data-x", "true") ] }
styledFormField cssFramework cssFramework checkbox `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-user_is_active\"><div class=\"form-check\"><input type=\"checkbox\" name=\"is_active\" class=\"form-check-input\" id=\"user_is_active\" checked=\"checked\" data-x=\"true\"><input type=\"hidden\" name=\"is_active\" value=\"off\"><label class=\"form-check-label\" for=\"user_is_active\">Is Active</label> </div></div>"
describe "select" do
let baseSelect = FormField
{ fieldType = SelectInput [("First Value", "a"), ("Second Value", "b")]
, fieldName = "user_id"
, fieldLabel = "User"
, fieldValue = ""
, fieldInputId = "project_user_id"
, validatorResult = Nothing
, fieldClass = ""
, labelClass = ""
, disabled = False
, disableLabel = False
, disableGroup = False
, disableValidationResult = False
, additionalAttributes = []
, cssFramework = cssFramework
, helpText = ""
, placeholder = "Please select"
, required = False
, autofocus = False
}
it "should render" do
let select = baseSelect
styledFormField cssFramework cssFramework select `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-project_user_id\"><label class=\"\" for=\"project_user_id\">User</label><select name=\"user_id\" id=\"project_user_id\" class=\"form-control\" value=\"\"><option selected=\"selected\" disabled=\"disabled\">Please select</option> <option value=\"a\">First Value</option><option value=\"b\">Second Value</option></select> </div>"
it "should render with disabled" do
let select = baseSelect { disabled = True }
styledFormField cssFramework cssFramework select `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-project_user_id\"><label class=\"\" for=\"project_user_id\">User</label><select name=\"user_id\" id=\"project_user_id\" class=\"form-control\" value=\"\" disabled=\"disabled\"><option selected=\"selected\" disabled=\"disabled\">Please select</option> <option value=\"a\">First Value</option><option value=\"b\">Second Value</option></select> </div>"
it "should render with selected" do
let select = baseSelect { fieldValue = "b" }
styledFormField cssFramework cssFramework select `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-project_user_id\"><label class=\"\" for=\"project_user_id\">User</label><select name=\"user_id\" id=\"project_user_id\" class=\"form-control\" value=\"b\"><option disabled=\"disabled\">Please select</option> <option value=\"a\">First Value</option><option value=\"b\" selected=\"selected\">Second Value</option></select> </div>"
it "should render with custom placeholder" do
let select = baseSelect { placeholder = "Pick something" }
styledFormField cssFramework cssFramework select `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-project_user_id\"><label class=\"\" for=\"project_user_id\">User</label><select name=\"user_id\" id=\"project_user_id\" class=\"form-control\" value=\"\"><option selected=\"selected\" disabled=\"disabled\">Pick something</option> <option value=\"a\">First Value</option><option value=\"b\">Second Value</option></select> </div>"
it "should render with additional attributes" do
let select = baseSelect { additionalAttributes = [ ("data-x", "true") ] }
styledFormField cssFramework cssFramework select `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-project_user_id\"><label class=\"\" for=\"project_user_id\">User</label><select name=\"user_id\" id=\"project_user_id\" class=\"form-control\" value=\"\" data-x=\"true\"><option selected=\"selected\" disabled=\"disabled\">Please select</option> <option value=\"a\">First Value</option><option value=\"b\">Second Value</option></select> </div>"
describe "textarea field" do
let baseTextField = FormField
{ fieldType = TextareaInput
, fieldName = "body"
, fieldLabel = "Body:"
, fieldValue = "Hello\nWorld!"
, fieldInputId = "body"
, validatorResult = Nothing
, additionalAttributes = []
, fieldClass = ""
, labelClass = ""
, disabled = False
, disableLabel = False
, disableGroup = False
, disableValidationResult = False
, cssFramework = cssFramework
, helpText = ""
, placeholder = "Describe your issue"
, required = False
, autofocus = False
}
it "should render" do
let textField = baseTextField
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-body\"><label class=\"\" for=\"body\">Body:</label><textarea name=\"body\" placeholder=\"Describe your issue\" id=\"body\" class=\"form-control\">Hello\nWorld!</textarea></div>"
it "should render with additional attributes" do
let textField = baseTextField { additionalAttributes = [ ("data-x", "true") ] }
styledFormField cssFramework cssFramework textField `shouldRenderTo` "<div class=\"form-group\" id=\"form-group-body\"><label class=\"\" for=\"body\">Body:</label><textarea name=\"body\" placeholder=\"Describe your issue\" id=\"body\" class=\"form-control\" data-x=\"true\">Hello\nWorld!</textarea></div>"
describe "pagination" do
let basePagination = Pagination
{
pageSize = 3
, totalItems = 12
, currentPage = 2
, window = 3
}
it "should render previous link" do
let pagination = basePagination
styledPaginationLinkPrevious cssFramework cssFramework pagination "#" `shouldRenderTo` "<li class=\"page-item\"><a class=\"page-link\" href=\"#\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span><span class=\"sr-only\">Previous</span></a></li>"
it "should render previous link disabled on the first page" do
let pagination = basePagination { currentPage = 1}
styledPaginationLinkPrevious cssFramework cssFramework pagination "#" `shouldRenderTo` "<li class=\"page-item disabled\"><a class=\"page-link\" href=\"#\" aria-label=\"Previous\"><span aria-hidden=\"true\">«</span><span class=\"sr-only\">Previous</span></a></li>"
it "should render next link" do
let pagination = basePagination
styledPaginationLinkNext cssFramework cssFramework pagination "#" `shouldRenderTo` "<li class=\"page-item\"><a class=\"page-link\" href=\"#\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span><span class=\"sr-only\">Next</span></a></li>"
it "should render next link disabled on the last page" do
let pagination = basePagination { currentPage = 4}
styledPaginationLinkNext cssFramework cssFramework pagination "#" `shouldRenderTo` "<li class=\"page-item disabled\"><a class=\"page-link\" href=\"#\" aria-label=\"Next\"><span aria-hidden=\"true\">»</span><span class=\"sr-only\">Next</span></a></li>"
it "should render items per page selector" do
let pagination = basePagination
styledPaginationItemsPerPageSelector cssFramework cssFramework pagination (\n -> cs $ "https://example.com?maxItems=" <> (show n)) `shouldRenderTo` "<option value=\"10\" data-url=\"https://example.com?maxItems=10\">10 items per page</option><option value=\"20\" data-url=\"https://example.com?maxItems=20\">20 items per page</option><option value=\"50\" data-url=\"https://example.com?maxItems=50\">50 items per page</option><option value=\"100\" data-url=\"https://example.com?maxItems=100\">100 items per page</option><option value=\"200\" data-url=\"https://example.com?maxItems=200\">200 items per page</option>"
it "should render the wrapping pagination" do
let pagination = basePagination
let paginationView = PaginationView
{ cssFramework = cssFramework
, pagination = pagination
, pageUrl = const ""
, linkPrevious = mempty
, linkNext = mempty
, pageDotDotItems = mempty
, itemsPerPageSelector = mempty
}
let render = Blaze.renderMarkup $ styledPagination cssFramework cssFramework paginationView
Text.isInfixOf "<div class=\"d-flex justify-content-md-center\">" (cs render) `shouldBe` True
it "should render the pagination if there are enough elements" do
let pagination = Pagination
{ pageSize = 5
, totalItems = 15
, currentPage = 1
, window = 3
}
context <- createControllerContextWithCSSFramework cssFramework
let ?context = context
let render = Blaze.renderMarkup $ renderPagination pagination
Text.isInfixOf "<nav aria-label=\"Page Navigator\"" (cs render) `shouldBe` True
it "should not render the pagination if there aren't enough elements" do
let pagination = Pagination
{ pageSize = 10
, totalItems = 5
, currentPage = 1
, window = 3
}
context <- createControllerContextWithCSSFramework cssFramework
let ?context = context
renderPagination pagination `shouldRenderTo` mempty
describe "breadcrumbs" do
it "should render a breadcrumb item with no link" do
let breadcrumbItem = breadcrumbText "First item"
let breadcrumbs = [breadcrumbItem]
styledBreadcrumbItem cssFramework cssFramework breadcrumbs breadcrumbItem True `shouldRenderTo` "<li class=\"breadcrumb-item active\">First item</li>"
it "should render a breadcrumb item with external link" do
let breadcrumbItem = breadcrumbLinkExternal "First item" "https://example.com"
let breadcrumbs = [breadcrumbItem]
styledBreadcrumbItem cssFramework cssFramework breadcrumbs breadcrumbItem True `shouldRenderTo` "<li class=\"breadcrumb-item active\"><a href=\"https://example.com\">First item</a></li>"
it "should render a breadcrumb item as non-active" do
let breadcrumbItem = breadcrumbText "First item"
let breadcrumbs = [breadcrumbItem]
styledBreadcrumbItem cssFramework cssFramework breadcrumbs breadcrumbItem False `shouldRenderTo` "<li class=\"breadcrumb-item\">First item</li>"
it "should render the wrapping breadcrumb and last item as active" do
let breadcrumbs = [breadcrumbText "First item", breadcrumbText "Last item"]
context <- createControllerContextWithCSSFramework cssFramework
let ?context = context
renderBreadcrumb breadcrumbs `shouldRenderTo` "<nav><ol class=\"breadcrumb\"><li class=\"breadcrumb-item\">First item</li><li class=\"breadcrumb-item active\">Last item</li></ol></nav>"
it "should support show of BreadcrumbItem" do
let breadcrumbItem = breadcrumbText "First item"
show breadcrumbItem `shouldBe` "{ breadcrumbLabel = \"First item\", url = Nothing }"
describe "tailwind" do
let cssFramework = tailwind
describe "textareaField" do
let baseTextField = FormField
{ fieldType = TextareaInput
, fieldName = "body"
, fieldLabel = "Body:"
, fieldValue = "Hello\nWorld!"
, fieldInputId = "body"
, validatorResult = Nothing
, additionalAttributes = []
, fieldClass = ""
, labelClass = ""
, disabled = False
, disableLabel = False
, disableGroup = False
, disableValidationResult = False
, cssFramework = cssFramework
, helpText = ""
, placeholder = "Describe your issue"
, required = False
, autofocus = False
}
it "should render tailwind label classes" do
styledFormField cssFramework cssFramework baseTextField `shouldRenderTo` "<div class=\"flex flex-col my-6 space-y-2\" id=\"form-group-body\"><label class=\"font-medium text-gray-700\" for=\"body\">Body:</label><textarea name=\"body\" placeholder=\"Describe your issue\" id=\"body\" class=\"focus:ring-blue-500 focus:border-blue-500 block w-full border-gray-300 rounded-md\">Hello\nWorld!</textarea></div>"
shouldRenderTo renderFunction expectedHtml = Blaze.renderMarkup renderFunction `shouldBe` expectedHtml
{-| Mock a Controller context with CSSFramework.
-}
createControllerContextWithCSSFramework :: Typeable option => option -> IO ControllerContext
createControllerContextWithCSSFramework cssFramework = do
frameworkConfig <- FrameworkConfig.buildFrameworkConfig do
option cssFramework
let requestBody = FormBody { params = [], files = [] }
let request = Wai.defaultRequest
let requestContext = RequestContext { request, respond = error "respond", requestBody, frameworkConfig = frameworkConfig }
pure FrozenControllerContext { requestContext, customFields = TypeMap.empty }