Skip to content

Commit a17cb2a

Browse files
committed
test(html): Extreact window location tests
Convert ginkgo tests to testify
1 parent 952f99c commit a17cb2a

File tree

2 files changed

+87
-78
lines changed

2 files changed

+87
-78
lines changed

html/window_location_test.go

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package html_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/suite"
7+
8+
"github.com/gost-dom/browser/dom/event"
9+
"github.com/gost-dom/browser/html"
10+
. "github.com/gost-dom/browser/internal/testing/gomega-matchers"
11+
"github.com/gost-dom/browser/internal/testing/gosttest"
12+
. "github.com/gost-dom/browser/testing/gomega-matchers"
13+
"github.com/gost-dom/browser/testing/testservers"
14+
)
15+
16+
type WindowLocationTestSuite struct {
17+
gosttest.GomegaSuite
18+
window html.Window
19+
}
20+
21+
func (s *WindowLocationTestSuite) SetupTest() {
22+
server := testservers.NewAnchorTagNavigationServer()
23+
s.window = NewWindowFromHandler(server)
24+
25+
}
26+
27+
func TestWindowLocation(t *testing.T) {
28+
suite.Run(t, new(WindowLocationTestSuite))
29+
}
30+
31+
func (s *WindowLocationTestSuite) TestEmptyWindow() {
32+
s.Expect(s.window.Location().Href()).To(Equal("about:blank"))
33+
}
34+
35+
func (s *WindowLocationTestSuite) TestPathnam() {
36+
s.Expect(s.window.Navigate("/index")).To(Succeed())
37+
s.Expect(s.window.Location().Pathname()).To(Equal("/index"))
38+
}
39+
40+
func (s *WindowLocationTestSuite) TestNavigateToAboutBlank() {
41+
s.Expect(s.window.Navigate("about:blank")).To(Succeed())
42+
s.Expect(s.window.Document()).To(HaveH1("Gost-DOM"))
43+
}
44+
45+
func (s *WindowLocationTestSuite) TestNavigateClearsEventHandlers() {
46+
count := 0
47+
s.Expect(s.window.Navigate("about:blank")).To(Succeed())
48+
s.window.AddEventListener(
49+
"gost-event",
50+
event.NewEventHandlerFunc(func(e *event.Event) error {
51+
count++
52+
return nil
53+
}))
54+
55+
s.Expect(s.window.Navigate("/index")).To(Succeed())
56+
s.window.DispatchEvent(event.New("gost-event", nil))
57+
s.Expect(count).To(Equal(0))
58+
}
59+
60+
func (s *WindowLocationTestSuite) GetLink(text string) html.HTMLElement {
61+
s.T().Helper()
62+
s.Expect(s.window.Navigate("/index")).To(Succeed())
63+
nodes, err := s.window.Document().QuerySelectorAll("a")
64+
s.Expect(err).ToNot(HaveOccurred())
65+
for _, n := range nodes.All() {
66+
if n.TextContent() == text {
67+
return n.(html.HTMLElement)
68+
}
69+
}
70+
s.T().Fatalf("Link not found with text: %s", text)
71+
return nil
72+
}
73+
74+
func (s *WindowLocationTestSuite) TestClickAbsoluteURL() {
75+
link := s.GetLink("Products from absolute url")
76+
link.Click()
77+
s.Expect(s.window.Location().Pathname()).To(Equal("/products"))
78+
79+
}
80+
81+
func (s *WindowLocationTestSuite) TestClickRelativeURL() {
82+
link := s.GetLink("Products from relative url")
83+
link.Click()
84+
s.Expect(s.window.Location().Pathname()).To(Equal("/products"))
85+
86+
}

html/window_test.go

+1-78
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@ import (
44
"strings"
55
"testing"
66

7-
. "github.com/onsi/ginkgo/v2"
8-
. "github.com/onsi/gomega"
97
"github.com/stretchr/testify/suite"
108

119
"github.com/gost-dom/browser/dom"
12-
"github.com/gost-dom/browser/dom/event"
13-
"github.com/gost-dom/browser/html"
1410
. "github.com/gost-dom/browser/html"
15-
"github.com/gost-dom/browser/internal/domslices"
11+
. "github.com/gost-dom/browser/internal/testing/gomega-matchers"
1612
"github.com/gost-dom/browser/internal/testing/gosttest"
1713
. "github.com/gost-dom/browser/testing/gomega-matchers"
18-
"github.com/gost-dom/browser/testing/testservers"
1914
)
2015

2116
type WindowTestSuite struct {
@@ -37,75 +32,3 @@ func (s *WindowTestSuite) TestDocumentWithDOCTYPE() {
3732
func TestWindow(t *testing.T) {
3833
suite.Run(t, new(WindowTestSuite))
3934
}
40-
41-
var _ = Describe("Window", func() {
42-
Describe("Location()", func() {
43-
var window Window
44-
45-
BeforeEach(func() {
46-
server := testservers.NewAnchorTagNavigationServer()
47-
DeferCleanup(func() { server = nil })
48-
window = NewWindowFromHandler(server)
49-
})
50-
51-
It("Should be about:blank", func() {
52-
Expect(window.Location().Href()).To(Equal("about:blank"))
53-
})
54-
55-
It("Should return the path loaded from", func() {
56-
Expect(window.Navigate("/index")).To(Succeed())
57-
Expect(window.Location().Pathname()).To(Equal("/index"))
58-
})
59-
60-
Describe("Navigate", func() {
61-
It("Should load a blank page when loading about:blank", func() {
62-
Expect(window.Navigate("about:blank")).To(Succeed())
63-
Expect(window.Document()).To(HaveH1("Gost-DOM"))
64-
})
65-
66-
It("Should clear event handlers", func() {
67-
count := 0
68-
Expect(window.Navigate("about:blank")).To(Succeed())
69-
window.AddEventListener(
70-
"gost-event",
71-
event.NewEventHandlerFunc(func(e *event.Event) error {
72-
count++
73-
return nil
74-
}))
75-
76-
Expect(window.Navigate("/index")).To(Succeed())
77-
window.DispatchEvent(event.New("gost-event", nil))
78-
Expect(count).To(Equal(0))
79-
})
80-
})
81-
82-
Describe("User navigation (clicking links)", func() {
83-
var links []dom.Node
84-
85-
BeforeEach(func() {
86-
Expect(window.Navigate("/index")).To(Succeed())
87-
nodes, err := window.Document().QuerySelectorAll("a")
88-
Expect(err).ToNot(HaveOccurred())
89-
links = nodes.All()
90-
})
91-
92-
It("Should update when using a link with absolute url", func() {
93-
link, ok := domslices.SliceFindFunc(links, func(n dom.Node) bool {
94-
return n.TextContent() == "Products from absolute url"
95-
})
96-
Expect(ok).To(BeTrue())
97-
link.(html.HTMLElement).Click()
98-
Expect(window.Location().Pathname()).To(Equal("/products"))
99-
})
100-
101-
It("Should update when using a link with relative url", func() {
102-
link, ok := domslices.SliceFindFunc(links, func(n dom.Node) bool {
103-
return n.TextContent() == "Products from relative url"
104-
})
105-
Expect(ok).To(BeTrue())
106-
link.(html.HTMLElement).Click()
107-
Expect(window.Location().Pathname()).To(Equal("/products"))
108-
})
109-
})
110-
})
111-
})

0 commit comments

Comments
 (0)