|
| 1 | +package html_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/url" |
| 6 | + "reflect" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/gost-dom/browser/dom/event" |
| 10 | + "github.com/gost-dom/browser/html" |
| 11 | + "github.com/gost-dom/browser/internal/gosthttp" |
| 12 | + "github.com/gost-dom/browser/internal/testing/eventtest" |
| 13 | + . "github.com/gost-dom/browser/internal/testing/exp/fixture" |
| 14 | + . "github.com/gost-dom/browser/internal/testing/gomega-matchers" |
| 15 | + "github.com/gost-dom/browser/internal/testing/htmltest" |
| 16 | + . "github.com/gost-dom/browser/testing/gomega-matchers" |
| 17 | + "github.com/onsi/gomega" |
| 18 | + "github.com/onsi/gomega/types" |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | +) |
| 21 | + |
| 22 | +type InitialHTMLFixture string |
| 23 | +type BaseLocationFixture string |
| 24 | + |
| 25 | +type AssertFixture struct { |
| 26 | + Fixture |
| 27 | + assert *assert.Assertions |
| 28 | + gomega gomega.Gomega |
| 29 | +} |
| 30 | + |
| 31 | +func (f *AssertFixture) Assert() *assert.Assertions { |
| 32 | + if f.assert == nil { |
| 33 | + f.assert = assert.New(f.TB) |
| 34 | + } |
| 35 | + return f.assert |
| 36 | +} |
| 37 | + |
| 38 | +func (f AssertFixture) Expect(actual interface{}, extra ...interface{}) types.Assertion { |
| 39 | + if f.gomega == nil { |
| 40 | + f.gomega = gomega.NewWithT(f.TB) |
| 41 | + } |
| 42 | + return f.gomega.Expect(actual, extra...) |
| 43 | +} |
| 44 | + |
| 45 | +type WindowFixture struct { |
| 46 | + AssertFixture |
| 47 | + BaseLocationFixture |
| 48 | + InitialHTMLFixture |
| 49 | + Window htmltest.WindowHelper |
| 50 | + |
| 51 | + requests []*http.Request |
| 52 | + form html.HTMLFormElement |
| 53 | + actualRequest *http.Request |
| 54 | + submittedForm url.Values |
| 55 | +} |
| 56 | + |
| 57 | +func (f *WindowFixture) Setup() { |
| 58 | + win := html.NewWindow(html.WindowOptions{ |
| 59 | + HttpClient: gosthttp.NewHttpClientFromHandler( |
| 60 | + http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { |
| 61 | + if req.ParseForm() != nil { |
| 62 | + panic("Error parsing form") |
| 63 | + } |
| 64 | + f.actualRequest = req |
| 65 | + f.submittedForm = req.Form |
| 66 | + f.requests = append(f.requests, req) |
| 67 | + }), |
| 68 | + ), |
| 69 | + BaseLocation: string(f.BaseLocationFixture), |
| 70 | + }) |
| 71 | + f.Window = htmltest.NewWindowHelper(f.TB, win) |
| 72 | + |
| 73 | + f.Helper() |
| 74 | + f.Assert().NoError(f.Window.LoadHTML( |
| 75 | + `<body> |
| 76 | + <form> |
| 77 | + <input name="foo" value="bar" /> |
| 78 | + </form> |
| 79 | + </body>`, |
| 80 | + )) |
| 81 | +} |
| 82 | + |
| 83 | +func AssertType[T any](t testing.TB, actual any) (res T) { |
| 84 | + t.Helper() |
| 85 | + var ok bool |
| 86 | + if res, ok = actual.(T); !ok { |
| 87 | + t.Errorf( |
| 88 | + "Value is not expected type: Value - %v. Type: %s", |
| 89 | + actual, reflect.TypeFor[T]().Name(), |
| 90 | + ) |
| 91 | + } |
| 92 | + return |
| 93 | +} |
| 94 | + |
| 95 | +func (f *WindowFixture) Form() html.HTMLFormElement { |
| 96 | + f.Helper() |
| 97 | + e, err := f.Window.HTMLDocument().QuerySelector("form") |
| 98 | + f.Assert().NoError(err) |
| 99 | + return AssertType[html.HTMLFormElement](f, e) |
| 100 | +} |
| 101 | + |
| 102 | +type HTMLFormFixture struct { |
| 103 | + WindowFixture |
| 104 | +} |
| 105 | + |
| 106 | +func TestSubmitForm(t *testing.T) { |
| 107 | + w, setup := InitFixture(t, &HTMLFormFixture{}, BaseLocationFixture( |
| 108 | + "http://example.com/forms/example-form.html?original-query=original-value", |
| 109 | + )) |
| 110 | + setup.Setup() |
| 111 | + |
| 112 | + var submitEventDispatched bool |
| 113 | + w.Form().AddEventListener("submit", eventtest.NewTestHandler(func(e *event.Event) { |
| 114 | + submitEventDispatched = true |
| 115 | + })) |
| 116 | + w.Form().Submit() |
| 117 | + w.Expect(w.actualRequest.Method).To(Equal("GET")) |
| 118 | + w.Expect( |
| 119 | + w.actualRequest.URL.String(), |
| 120 | + ).To(Equal("http://example.com/forms/example-form.html?foo=bar")) |
| 121 | + |
| 122 | + w.Assert().Equal("/forms/example-form.html", w.actualRequest.URL.Path, |
| 123 | + "Form post path") |
| 124 | + w.Assert().Equal("foo=bar", w.actualRequest.URL.RawQuery, "Request query") |
| 125 | + |
| 126 | + w.Assert().False(submitEventDispatched, |
| 127 | + "'submit' event should not be dispatched on form.submit()") |
| 128 | +} |
| 129 | + |
| 130 | +func TestHTMLFormElementSubmitPost(t *testing.T) { |
| 131 | + w, setup := InitFixture(t, &HTMLFormFixture{}, BaseLocationFixture( |
| 132 | + "http://example.com/forms/example-form.html?original-query=original-value", |
| 133 | + )) |
| 134 | + setup.Setup() |
| 135 | + |
| 136 | + form := w.Form() |
| 137 | + form.SetMethod("post") |
| 138 | + form.Submit() |
| 139 | + |
| 140 | + w.Expect(w.actualRequest.Method).To(Equal("POST")) |
| 141 | + w.Expect(w.actualRequest.URL.Path).To(Equal("/forms/example-form.html"), "Requested path") |
| 142 | + w.Expect(w.actualRequest.URL.RawQuery). |
| 143 | + To(Equal("original-query=original-value"), "Requested query") |
| 144 | + w.Expect(w.submittedForm["foo"]).To(Equal([]string{"bar"})) |
| 145 | +} |
| 146 | + |
| 147 | +type HTMLFormSubmitButtonFixture struct { |
| 148 | + HTMLFormFixture |
| 149 | + Submitter html.HTMLButtonElement |
| 150 | +} |
| 151 | + |
| 152 | +func (f *HTMLFormSubmitButtonFixture) Setup() { |
| 153 | + f.Submitter = AssertType[html.HTMLButtonElement]( |
| 154 | + f, |
| 155 | + f.Window.HTMLDocument().CreateHTMLElement("button"), |
| 156 | + ) |
| 157 | + f.Submitter.SetAttribute("type", "submit") |
| 158 | + f.Submitter.SetAttribute("name", "submitter") |
| 159 | + f.Submitter.SetAttribute("value", "submitbtn") |
| 160 | + f.Form().AppendChild(f.Submitter) |
| 161 | +} |
| 162 | + |
| 163 | +func TestHTMLFormElementSubmitWithClickButton(t *testing.T) { |
| 164 | + w, setup := InitFixture(t, &HTMLFormSubmitButtonFixture{}) |
| 165 | + setup.Setup() |
| 166 | + |
| 167 | + w.Submitter.Click() |
| 168 | + w.Assert().NotNil(w.submittedForm, "A form was submitted") |
| 169 | + w.Assert(). |
| 170 | + Equal([]string{"submitbtn"}, w.submittedForm["submitter"], "Submit button added to the form") |
| 171 | + w.Assert().Equal([]string{"bar"}, w.submittedForm["foo"], "Input value from the form") |
| 172 | +} |
| 173 | + |
| 174 | +func TestHTMLFormElementSubmitWithClickButtonAndWeirdCasing(t *testing.T) { |
| 175 | + w, setup := InitFixture(t, &HTMLFormSubmitButtonFixture{}) |
| 176 | + setup.Setup() |
| 177 | + |
| 178 | + w.Submitter.SetType("SuBMit") |
| 179 | + w.Submitter.Click() |
| 180 | + w.Assert().NotNil(w.submittedForm, "A form was submitted") |
| 181 | + w.Assert(). |
| 182 | + Equal([]string{"submitbtn"}, w.submittedForm["submitter"], "Submit button added to the form") |
| 183 | + w.Assert().Equal([]string{"bar"}, w.submittedForm["foo"], "Input value from the form") |
| 184 | +} |
| 185 | + |
| 186 | +func TestHTMLFormElementSubmitWithClickResetButton(t *testing.T) { |
| 187 | + w, setup := InitFixture(t, &HTMLFormSubmitButtonFixture{}) |
| 188 | + setup.Setup() |
| 189 | + |
| 190 | + w.Submitter.SetType("reset") |
| 191 | + w.Submitter.Click() |
| 192 | + w.Assert().Nil(w.submittedForm, "A form was submitted") |
| 193 | +} |
| 194 | + |
| 195 | +func TestHTMLFormElementRequestSubmitWithoutSubmitter(t *testing.T) { |
| 196 | + w, setup := InitFixture(t, &HTMLFormSubmitButtonFixture{}) |
| 197 | + setup.Setup() |
| 198 | + |
| 199 | + w.Assert().NoError(w.Form().RequestSubmit(nil)) |
| 200 | + w.Assert().NotNil(w.submittedForm, "A form was submitted") |
| 201 | + w.Assert().Nil(w.submittedForm["submitter"], "Form submitter value") |
| 202 | + w.Assert().Equal([]string{"bar"}, w.submittedForm["foo"], "Input value from the form") |
| 203 | +} |
| 204 | + |
| 205 | +func TestHTMLFormElementRequestSubmitPreventDefault(t *testing.T) { |
| 206 | + w, setup := InitFixture(t, &HTMLFormSubmitButtonFixture{}) |
| 207 | + setup.Setup() |
| 208 | + |
| 209 | + var submitEventDispatched bool |
| 210 | + w.Form().AddEventListener("submit", eventtest.NewTestHandler(func(e *event.Event) { |
| 211 | + submitEventDispatched = true |
| 212 | + e.PreventDefault() |
| 213 | + })) |
| 214 | + w.Form().RequestSubmit(nil) |
| 215 | + w.Assert().True(submitEventDispatched, "'submit' event dispatched") |
| 216 | + w.Assert().Nil(w.actualRequest, "A request was sent") |
| 217 | +} |
| 218 | + |
| 219 | +func TestHTMLFormElementRequestSubmitWithSubmitter(t *testing.T) { |
| 220 | + w, setup := InitFixture(t, &HTMLFormSubmitButtonFixture{}) |
| 221 | + setup.Setup() |
| 222 | + |
| 223 | + w.Assert().NoError(w.Form().RequestSubmit(w.Submitter)) |
| 224 | + w.Assert().NotNil(w.submittedForm, "A form was submitted") |
| 225 | + w.Assert().NotNil(w.submittedForm["submitter"], "Form submitter value") |
| 226 | + w.Assert().Equal([]string{"bar"}, w.submittedForm["foo"], "Input value from the form") |
| 227 | +} |
| 228 | + |
| 229 | +func TestHTMLFormElementPreventDefaultOnSubmitButton(t *testing.T) { |
| 230 | + w, setup := InitFixture(t, &HTMLFormSubmitButtonFixture{}) |
| 231 | + setup.Setup() |
| 232 | + |
| 233 | + w.Submitter.AddEventListener("click", eventtest.NewTestHandler(func(e *event.Event) { |
| 234 | + e.PreventDefault() |
| 235 | + })) |
| 236 | + w.Submitter.Click() |
| 237 | + w.Assert().Nil(w.actualRequest) |
| 238 | +} |
| 239 | + |
| 240 | +func TestHTMLFormElementFormDataEvent(t *testing.T) { |
| 241 | + w, setup := InitFixture(t, &HTMLFormSubmitButtonFixture{}) |
| 242 | + setup.Setup() |
| 243 | + |
| 244 | + var eventBubbles bool |
| 245 | + w.Form().ParentElement(). |
| 246 | + AddEventListener("formdata", eventtest.NewTestHandler(func(e *event.Event) { |
| 247 | + eventBubbles = true |
| 248 | + })) |
| 249 | + var actualEvent *event.Event |
| 250 | + w.Form().AddEventListener( |
| 251 | + "formdata", |
| 252 | + event.NewEventHandlerFunc(func(e *event.Event) error { |
| 253 | + actualEvent = e |
| 254 | + return nil |
| 255 | + }), |
| 256 | + ) |
| 257 | + w.Form().Submit() |
| 258 | + w.Expect(actualEvent).ToNot(BeNil()) |
| 259 | + w.Expect(eventBubbles).To(BeTrue()) |
| 260 | + formDataEventInit, ok := actualEvent.Data.(html.FormDataEventInit) |
| 261 | + w.Expect(ok).To(BeTrue()) |
| 262 | + w.Expect(formDataEventInit.FormData).ToNot(BeNil()) |
| 263 | + w.Expect(formDataEventInit.FormData).To(HaveFormDataValue("foo", "bar")) |
| 264 | +} |
| 265 | + |
| 266 | +type HTMLFormSubmitInputFixture struct { |
| 267 | + HTMLFormFixture |
| 268 | + Submitter html.HTMLInputElement |
| 269 | +} |
| 270 | + |
| 271 | +func (f *HTMLFormSubmitInputFixture) Setup() { |
| 272 | + f.Submitter = AssertType[html.HTMLInputElement]( |
| 273 | + f, |
| 274 | + f.Window.HTMLDocument().CreateHTMLElement("input"), |
| 275 | + ) |
| 276 | + f.Submitter.SetAttribute("type", "submit") |
| 277 | + f.Submitter.SetAttribute("name", "submitter") |
| 278 | + f.Submitter.SetAttribute("value", "submitinput") |
| 279 | + f.Form().AppendChild(f.Submitter) |
| 280 | +} |
| 281 | + |
| 282 | +func TestHTMLFormElementSubmitButtonWithClickButton(t *testing.T) { |
| 283 | + w, setup := InitFixture(t, &HTMLFormSubmitInputFixture{}) |
| 284 | + setup.Setup() |
| 285 | + |
| 286 | + w.Submitter.Click() |
| 287 | + w.Assert().NotNil(w.submittedForm, "A form was submitted") |
| 288 | + w.Assert(). |
| 289 | + Equal([]string{"submitinput"}, w.submittedForm["submitter"], "Submit input value added to the form") |
| 290 | + w.Assert().Equal([]string{"bar"}, w.submittedForm["foo"], "Input value from the form") |
| 291 | +} |
| 292 | + |
| 293 | +func TestHTMLFormElementSubmitInputWithClickResetButton(t *testing.T) { |
| 294 | + w, setup := InitFixture(t, &HTMLFormSubmitInputFixture{}) |
| 295 | + setup.Setup() |
| 296 | + |
| 297 | + w.Submitter.SetType("reset") |
| 298 | + w.Submitter.Click() |
| 299 | + w.Assert().Nil(w.submittedForm, "A form was submitted") |
| 300 | +} |
0 commit comments