github.com/letsencrypt/boulder@v0.20251208.0/sfe/forms/fields_test.go (about) 1 package forms 2 3 import ( 4 "testing" 5 6 "github.com/letsencrypt/boulder/test" 7 ) 8 9 func TestInputFieldRenderField(t *testing.T) { 10 cases := []struct { 11 name string 12 required bool 13 }{ 14 {"required", true}, 15 {"optional", false}, 16 } 17 for _, tc := range cases { 18 t.Run(tc.name, func(t *testing.T) { 19 f := NewInputField("Email Address", "email", "Where we can reach you", tc.required) 20 html := string(f.RenderField()) 21 22 test.AssertContains(t, html, `<div class="form-field">`) 23 test.AssertContains(t, html, `<label for="email">Email Address</label>`) 24 test.AssertContains(t, html, `<small class="field-description">Where we can reach you</small>`) 25 test.AssertContains(t, html, `<input type="text" id="email" name="email"`) 26 27 if tc.required { 28 test.AssertContains(t, html, `required="required"`) 29 return 30 } 31 test.AssertNotContains(t, html, `required="required"`) 32 }) 33 } 34 } 35 36 func TestDropdownFieldRenderField(t *testing.T) { 37 cases := []struct { 38 name string 39 required bool 40 }{ 41 {"required", true}, 42 {"optional", false}, 43 } 44 for _, tc := range cases { 45 t.Run(tc.name, func(t *testing.T) { 46 opts := []string{"Small", "Medium", "Large"} 47 f := NewDropdownField("Size", "size", "Pick one", opts, tc.required) 48 49 html := string(f.RenderField()) 50 51 test.AssertContains(t, html, `<div class="form-field">`) 52 test.AssertContains(t, html, `<label for="size">Size</label>`) 53 test.AssertContains(t, html, `<small class="field-description">Pick one</small>`) 54 test.AssertContains(t, html, `<select id="size" name="size"`) 55 test.AssertContains(t, html, `<option value="" selected></option>`) 56 test.AssertContains(t, html, `<option value="Small">Small</option>`) 57 test.AssertContains(t, html, `<option value="Medium">Medium</option>`) 58 test.AssertContains(t, html, `<option value="Large">Large</option>`) 59 60 if tc.required { 61 test.AssertContains(t, html, `required="required"`) 62 return 63 } 64 test.AssertNotContains(t, html, `required="required"`) 65 }) 66 } 67 } 68 69 func TestTextareaFieldRenderField(t *testing.T) { 70 cases := []struct { 71 name string 72 rows int 73 required bool 74 wantRows string 75 }{ 76 {"explicit rows", 7, true, `rows="7"`}, 77 {"default rows when zero", 0, false, `rows="4"`}, 78 {"default rows when negative", -3, true, `rows="4"`}, 79 } 80 for _, tc := range cases { 81 t.Run(tc.name, func(t *testing.T) { 82 f := NewTextareaField("Use Case", "useCase", "Tell us more", tc.rows, tc.required) 83 html := string(f.RenderField()) 84 85 test.AssertContains(t, html, `<div class="form-field">`) 86 test.AssertContains(t, html, `<label for="useCase">Use Case</label>`) 87 test.AssertContains(t, html, `<small class="field-description">Tell us more</small>`) 88 test.AssertContains(t, html, `<textarea id="useCase" name="useCase"`) 89 test.AssertContains(t, html, tc.wantRows) 90 91 if tc.required { 92 test.AssertContains(t, html, `required="required"`) 93 return 94 } 95 test.AssertNotContains(t, html, `required="required"`) 96 }) 97 } 98 } 99 100 func TestCheckboxFieldRenderField(t *testing.T) { 101 cases := []struct { 102 name string 103 required bool 104 }{ 105 {"required", true}, 106 {"optional", false}, 107 } 108 for _, tc := range cases { 109 t.Run(tc.name, func(t *testing.T) { 110 f := NewCheckboxField("Subscriber Agreement", "sa", "I agree to the terms", tc.required) 111 html := string(f.RenderField()) 112 113 test.AssertContains(t, html, `<div class="highlight form-field checkbox-field" id="sa-wrapper">`) 114 test.AssertContains(t, html, `<label for="sa">Subscriber Agreement</label>`) 115 test.AssertContains(t, html, `<div class="checkbox-row">`) 116 test.AssertContains(t, html, `<input type="checkbox" id="sa" name="sa"`) 117 test.AssertContains(t, html, `<span class="checkbox-text">I agree to the terms</span>`) 118 119 if tc.required { 120 test.AssertContains(t, html, `required="required"`) 121 return 122 } 123 test.AssertNotContains(t, html, `required="required"`) 124 }) 125 } 126 }