Skip to content

Commit de3cc20

Browse files
committed
test: Convert ginkgo test to testify suite
1 parent 5467955 commit de3cc20

File tree

2 files changed

+105
-75
lines changed

2 files changed

+105
-75
lines changed

internal/testing/htmltest/helpers.go

+8
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ func (win WindowHelper) HTMLDocument() HTMLDocumentHelper {
4242
return NewHTMLDocumentHelper(win.t, win.Document())
4343
}
4444

45+
func (h WindowHelper) MustLoadHTML(html string) {
46+
h.t.Helper()
47+
48+
if err := h.Window.LoadHTML(html); err != nil {
49+
h.t.Fatalf("Error loading HTML: %s\nHTML: %s", err.Error(), html)
50+
}
51+
}
52+
4553
// Helper type on top of html.HTMLDocument to provide useful helper functions
4654
// for testing.
4755
type HTMLDocumentHelper struct {

scripting/v8host/class_list_test.go

+97-75
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,112 @@
11
package v8host_test
22

33
import (
4-
. "github.com/onsi/ginkgo/v2"
5-
. "github.com/onsi/gomega"
6-
matchers "github.com/gost-dom/browser/testing/gomega-matchers"
4+
"testing"
5+
6+
"github.com/gost-dom/browser/html"
7+
. "github.com/gost-dom/browser/internal/testing/gomega-matchers"
8+
"github.com/gost-dom/browser/internal/testing/gosttest"
9+
"github.com/gost-dom/browser/internal/testing/htmltest"
10+
"github.com/gost-dom/browser/scripting/v8host"
11+
. "github.com/gost-dom/browser/testing/gomega-matchers"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/suite"
714
)
815

9-
var _ = Describe("V8 Element.classList", func() {
10-
It("Should support .add", func() {
11-
ctx := NewTestContext(LoadHTML("<div id='target' class='c1'></div>"))
12-
ctx.MustRunTestScript(`
13-
const list = document.getElementById('target').classList;
14-
list.add('c2')
15-
`)
16-
element := ctx.Window().Document().GetElementById("target")
17-
Expect(element).To(matchers.HaveAttribute("class", "c1 c2"))
18-
})
16+
type ClassListTestTestSuite struct {
17+
gosttest.GomegaSuite
18+
scriptHost html.ScriptHost
19+
window htmltest.WindowHelper
20+
}
1921

20-
It("Should be an iterable", func() {
21-
ctx := NewTestContext(LoadHTML("<div id='target' class='a b c'></div>"))
22-
Expect(ctx.Eval(`
23-
list = document.getElementById("target").classList
24-
typeof list[Symbol.iterator]`)).To(Equal("function"))
25-
})
22+
func (s *ClassListTestTestSuite) SetupTest() {
23+
s.window = htmltest.NewWindowHelper(s.T(), html.NewWindow(html.WindowOptions{
24+
ScriptHost: s.scriptHost,
25+
}))
26+
}
2627

27-
It("Should be an iterable of class names", func() {
28-
ctx := NewTestContext(LoadHTML("<div id='target' class='a b c'></div>"))
29-
Expect(ctx.Eval(`
28+
func TestClassListTest(t *testing.T) {
29+
t.Parallel()
30+
suite.Run(t, &ClassListTestTestSuite{scriptHost: v8host.New()})
31+
}
32+
33+
func (s *ClassListTestTestSuite) TestAdd() {
34+
s.window.MustLoadHTML("<div id='target' class='c1'></div>")
35+
assert.NoError(s.T(), s.window.Run(`
3036
const list = document.getElementById('target').classList;
31-
Array.from(list).join(",")
32-
`)).To(Equal("a,b,c"))
33-
})
37+
list.add('c2')
38+
`))
39+
element := s.window.HTMLDocument().GetHTMLElementById("target")
40+
s.Expect(element).To(HaveAttribute("class", "c1 c2"))
41+
}
42+
43+
func (s *ClassListTestTestSuite) TestClassListIsIterable() {
44+
s.window.MustLoadHTML("<div id='target' class='a b c'></div>")
45+
s.Expect(s.window.Eval(`
46+
list = document.getElementById("target").classList
47+
typeof list[Symbol.iterator]`)).To(Equal("function"))
48+
}
3449

35-
Describe(".toggle", func() {
36-
scriptContext := InitializeContext(LoadHTML(`<div id="target" class="a b c"></div>`))
50+
func (s *ClassListTestTestSuite) TestIterableIteratesClassNames() {
51+
s.window.MustLoadHTML("<div id='target' class='a b c'></div>")
52+
s.Expect(s.window.Eval(`
53+
const list = document.getElementById('target').classList;
54+
Array.from(list).join(",")
55+
`)).To(Equal("a,b,c"))
56+
}
3757

38-
It("Removes an existing item and returns false", func() {
39-
Expect(scriptContext.Eval(`
40-
document.getElementById("target").classList.toggle("b")
41-
`)).To(BeFalse())
42-
div := scriptContext.Window().Document().GetElementById("target")
43-
Expect(div).To(matchers.HaveAttribute("class", "a c"))
44-
})
58+
func (s *ClassListTestTestSuite) TestToggleExistingClassName() {
59+
s.window.MustLoadHTML(`<div id="target" class="a b c"></div>`)
60+
s.Expect(s.window.Eval(`
61+
document.getElementById("target").classList.toggle("b")
62+
`)).To(BeFalse())
63+
s.Expect(
64+
s.window.HTMLDocument().GetElementById("target"),
65+
).To(HaveAttribute("class", "a c"))
66+
}
4567

46-
It("Removes an adds a non-existing item and returns true", func() {
47-
Expect(scriptContext.Eval(`
48-
document.getElementById("target").classList.toggle("x")
49-
`)).To(BeTrue())
50-
div := scriptContext.Window().Document().GetElementById("target")
51-
Expect(div).To(matchers.HaveAttribute("class", "a b c x"))
52-
})
68+
func (s *ClassListTestTestSuite) TestToggleNonExistingClassName() {
69+
s.window.MustLoadHTML(`<div id="target" class="a b c"></div>`)
70+
s.Expect(s.window.Eval(`
71+
document.getElementById("target").classList.toggle("x")
72+
`)).To(BeTrue())
73+
s.Expect(
74+
s.window.HTMLDocument().GetElementById("target"),
75+
).To(HaveAttribute("class", "a b c x"))
76+
}
5377

54-
Describe("force as true", func() {
55-
It("Should leave an existing item and return true", func() {
56-
Expect(scriptContext.Eval(`
57-
document.getElementById("target").classList.toggle("b", true)
58-
`)).To(BeTrue())
59-
div := scriptContext.Window().Document().GetElementById("target")
60-
Expect(div).To(matchers.HaveAttribute("class", "a b c"))
61-
})
78+
func (s *ClassListTestTestSuite) TestToggleForceExistingItem() {
79+
s.window.MustLoadHTML(`<div id="target" class="a b c"></div>`)
80+
s.Expect(s.window.Eval(`
81+
document.getElementById("target").classList.toggle("b", true)
82+
`)).To(BeTrue(), "Force toggle return value")
83+
div := s.window.Document().GetElementById("target")
84+
s.Expect(div).To(HaveAttribute("class", "a b c"))
85+
}
6286

63-
It("Should add a non-existing item and return true", func() {
64-
Expect(scriptContext.Eval(`
65-
document.getElementById("target").classList.toggle("x", true)
66-
`)).To(BeTrue())
67-
div := scriptContext.Window().Document().GetElementById("target")
68-
Expect(div).To(matchers.HaveAttribute("class", "a b c x"))
69-
})
70-
})
87+
func (s *ClassListTestTestSuite) TestToggleForceNonExistingItem() {
88+
s.window.MustLoadHTML(`<div id="target" class="a b c"></div>`)
89+
s.Expect(s.window.Eval(`
90+
document.getElementById("target").classList.toggle("x", true)
91+
`)).To(BeTrue())
92+
div := s.window.Document().GetElementById("target")
93+
s.Expect(div).To(HaveAttribute("class", "a b c x"))
94+
}
7195

72-
Describe("force as false", func() {
73-
It("Should remove an existing item and return false", func() {
74-
Expect(scriptContext.Eval(`
75-
document.getElementById("target").classList.toggle("b", false)
76-
`)).To(BeFalse())
77-
div := scriptContext.Window().Document().GetElementById("target")
78-
Expect(div).To(matchers.HaveAttribute("class", "a c"))
79-
})
96+
func (s *ClassListTestTestSuite) TestToggleNoForceExistingItem() {
97+
s.window.MustLoadHTML(`<div id="target" class="a b c"></div>`)
98+
s.Expect(s.window.Eval(`
99+
document.getElementById("target").classList.toggle("b", false)
100+
`)).To(BeFalse())
101+
div := s.window.Document().GetElementById("target")
102+
s.Expect(div).To(HaveAttribute("class", "a c"))
103+
}
80104

81-
It("Should ignore a non-existing item and return false", func() {
82-
Expect(scriptContext.Eval(`
83-
document.getElementById("target").classList.toggle("x", false)
84-
`)).To(BeFalse())
85-
div := scriptContext.Window().Document().GetElementById("target")
86-
Expect(div).To(matchers.HaveAttribute("class", "a b c"))
87-
})
88-
})
89-
})
90-
})
105+
func (s *ClassListTestTestSuite) TestToggleNoForceNonExistingItem() {
106+
s.window.MustLoadHTML(`<div id="target" class="a b c"></div>`)
107+
s.Expect(s.window.Eval(`
108+
document.getElementById("target").classList.toggle("x", false)
109+
`)).To(BeFalse())
110+
div := s.window.Document().GetElementById("target")
111+
s.Expect(div).To(HaveAttribute("class", "a b c"))
112+
}

0 commit comments

Comments
 (0)