|
1 | 1 | package html_test
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
4 | 5 | "net/http"
|
5 | 6 | "net/url"
|
| 7 | + "testing" |
6 | 8 |
|
7 | 9 | "github.com/gost-dom/browser/dom"
|
8 | 10 | "github.com/gost-dom/browser/dom/event"
|
9 | 11 | "github.com/gost-dom/browser/html"
|
10 | 12 | . "github.com/gost-dom/browser/html"
|
11 | 13 | "github.com/gost-dom/browser/internal/gosthttp"
|
12 | 14 | "github.com/gost-dom/browser/internal/testing/eventtest"
|
| 15 | + "github.com/gost-dom/browser/internal/testing/gosttest" |
13 | 16 | . "github.com/gost-dom/browser/internal/testing/htmltest"
|
14 | 17 | . "github.com/gost-dom/browser/testing/gomega-matchers"
|
| 18 | + "github.com/stretchr/testify/suite" |
15 | 19 |
|
16 | 20 | . "github.com/onsi/ginkgo/v2"
|
17 | 21 | . "github.com/onsi/gomega"
|
18 | 22 | )
|
19 | 23 |
|
20 |
| -var _ = Describe("HTML Form", func() { |
21 |
| - Describe("Method property", func() { |
22 |
| - var form HTMLFormElement |
23 |
| - BeforeEach(func() { |
24 |
| - doc := NewHTMLDocument(nil) |
25 |
| - form = doc.CreateElement("form").(HTMLFormElement) |
26 |
| - }) |
27 |
| - |
28 |
| - Describe("Setting the value", func() { |
29 |
| - It("Should update the attribute", func() { |
30 |
| - form.SetMethod("new value") |
31 |
| - Expect(form).To(HaveAttribute("method", "new value")) |
32 |
| - Expect(form).ToNot(BeNil()) |
33 |
| - }) |
34 |
| - }) |
35 |
| - |
36 |
| - Describe("Getting the value", func() { |
37 |
| - It("Should return 'get' by default", func() { |
38 |
| - Expect(form.Method()).To(Equal("get")) |
39 |
| - }) |
40 |
| - |
41 |
| - It("Should return 'post' when set to 'post', 'POST', 'PoSt', etc.", func() { |
42 |
| - for _, value := range []string{"post", "POST", "PoSt"} { |
43 |
| - form.SetMethod(value) |
44 |
| - Expect(form.Method()).To(Equal("post")) |
45 |
| - } |
46 |
| - }) |
47 |
| - |
48 |
| - It("Should return 'get' when assigned an invalid value", func() { |
49 |
| - form.SetMethod("post") |
50 |
| - Expect(form.Method()).To(Equal("post")) |
51 |
| - form.SetMethod("invalid") |
52 |
| - Expect(form.Method()).To(Equal("get")) |
53 |
| - }) |
54 |
| - }) |
55 |
| - }) |
| 24 | +type HTMLFormElementTestSuite struct { |
| 25 | + gosttest.GomegaSuite |
| 26 | + win html.Window |
| 27 | + doc html.HTMLDocument |
| 28 | + form html.HTMLFormElement |
| 29 | +} |
| 30 | + |
| 31 | +func TestHTMLFormElement(t *testing.T) { |
| 32 | + suite.Run(t, new(HTMLFormElementTestSuite)) |
| 33 | +} |
| 34 | + |
| 35 | +func (s *HTMLFormElementTestSuite) SetupTest() { |
| 36 | + s.win = NewWindowHelper(s.T(), NewWindow(WindowOptions{ |
| 37 | + BaseLocation: "http://example.com/forms/example-form.html?original-query=original-value", |
| 38 | + })) |
| 39 | + |
| 40 | + s.doc = NewHTMLDocument(s.win) |
| 41 | + s.form = s.doc.CreateElement("form").(HTMLFormElement) |
| 42 | +} |
| 43 | + |
| 44 | +func (s *HTMLFormElementTestSuite) createForm() HTMLFormElement { |
| 45 | + return s.doc.CreateElement("form").(HTMLFormElement) |
| 46 | +} |
| 47 | + |
| 48 | +func (s *HTMLFormElementTestSuite) TestMethodIDLAttribute() { |
| 49 | + form := s.createForm() |
| 50 | + |
| 51 | + s.Assert().Equal("get", form.Method(), "Default value for HTMLFormElement.method") |
| 52 | + |
| 53 | + form.SetMethod("new value") |
| 54 | + s.Expect(form).To(HaveAttribute("method", "new value"), |
| 55 | + "Content attribute when setting IDL to invalid value") |
| 56 | + s.Expect(form.Method()).To(Equal("get"), |
| 57 | + "IDL method attribute when set to an invalid value") |
| 58 | + |
| 59 | + for _, value := range []string{"post", "POST", "PoSt"} { |
| 60 | + form.SetMethod(value) |
| 61 | + s.Expect(form.Method()).To(Equal("post"), |
| 62 | + fmt.Sprintf("IDL method attribute, set to %s", value), |
| 63 | + ) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func (s *HTMLFormElementTestSuite) TestActionIDLAttribute() { |
| 68 | + form := s.createForm() |
| 69 | + |
| 70 | + s.Assert().Equal(s.win.LocationHREF(), form.Action(), "Default value for action IDL attribute") |
| 71 | + |
| 72 | + form.SetAction("/foo-bar") |
| 73 | + s.Expect(form). |
| 74 | + To(HaveAttribute("action", "/foo-bar"), "Action content attribute with absolute path") |
| 75 | + s.Expect(form.Action()). |
| 76 | + To(Equal("http://example.com/foo-bar"), "Action IDL attribute when set to an absolute path") |
| 77 | + |
| 78 | + form.SetAttribute("action", "submit-target") |
| 79 | + s.Expect(form).To( |
| 80 | + HaveAttribute("action", "submit-target"), |
| 81 | + "Action content attribute with relative path") |
| 82 | + |
| 83 | + s.Expect(form.Action()).To( |
| 84 | + Equal("http://example.com/forms/submit-target"), |
| 85 | + "Action IDL attribute when set to a relative path") |
| 86 | +} |
56 | 87 |
|
| 88 | +var _ = Describe("HTML Form", func() { |
57 | 89 | Describe("Submit behaviour", func() {
|
58 | 90 | var window WindowHelper
|
59 | 91 | var requests []*http.Request
|
@@ -103,28 +135,6 @@ var _ = Describe("HTML Form", func() {
|
103 | 135 | })
|
104 | 136 |
|
105 | 137 | Describe("Method and action behaviour", func() {
|
106 |
| - |
107 |
| - Describe("Action", func() { |
108 |
| - Describe("Get", func() { |
109 |
| - It("Should return the document location when not set", func() { |
110 |
| - Expect(form.Action()).To(Equal(window.Location().Href())) |
111 |
| - }) |
112 |
| - }) |
113 |
| - |
114 |
| - Describe("Set", func() { |
115 |
| - It( |
116 |
| - "Should update the action attribute, and update action property to relative url", |
117 |
| - func() { |
118 |
| - form.SetAction("/foo-bar") |
119 |
| - Expect( |
120 |
| - form, |
121 |
| - ).To(HaveAttribute("action", "/foo-bar")) |
122 |
| - Expect(form.Action()).To(Equal("http://example.com/foo-bar")) |
123 |
| - }, |
124 |
| - ) |
125 |
| - }) |
126 |
| - }) |
127 |
| - |
128 | 138 | Describe("No method of action specified", func() {
|
129 | 139 | It("Should make a GET request to the original location", func() {
|
130 | 140 | form.Submit()
|
|
0 commit comments