Skip to content

Commit 6945725

Browse files
committed
test: Convert script Document tests
Convert from ginkgo to testify suites
1 parent de3cc20 commit 6945725

File tree

5 files changed

+77
-112
lines changed

5 files changed

+77
-112
lines changed
+58-110
Original file line numberDiff line numberDiff line change
@@ -1,125 +1,73 @@
11
package scripttests
22

33
import (
4-
. "github.com/onsi/ginkgo/v2"
4+
"github.com/gost-dom/browser/html"
55
. "github.com/onsi/gomega"
66
)
77

8-
func (suite *ScriptTestSuite) CreateDocumentTests() {
9-
prefix := suite.Prefix
10-
11-
Describe(prefix+"Document", func() {
12-
var ctx *ScriptTestContext
13-
14-
BeforeEach(func() {
15-
ctx = suite.NewContext()
16-
})
17-
18-
Describe("`createElement`", func() {
19-
It("Should return an HTMLElement", func() {
20-
Expect(`document.createElement("base")`).To(ctx.BeInstanceOf("HTMLElement"))
21-
})
22-
23-
It("Should exist on the prototype", func() {
24-
Expect(ctx.Eval(`
25-
Object.getOwnPropertyNames(Document.prototype).includes("createElement")
26-
`)).To(BeTrue())
27-
Expect(ctx.Eval(`
28-
Object.getOwnPropertyNames(Document).includes("createElement")
29-
`)).To(BeFalse())
30-
})
31-
})
32-
33-
Describe("`location`", func() {
34-
It("Should be an own property of the document, not the prototype", func() {
35-
Expect(
36-
ctx.Eval("Object.getOwnPropertyNames(document)"),
37-
).To(ContainElements("location"))
38-
Expect(
39-
ctx.Eval("Object.getOwnPropertyNames(Document.prototype)"),
40-
).ToNot(ContainElements("location"))
41-
})
42-
})
43-
44-
Describe("getElementById", func() {
45-
It("Should return the right element", func() {
46-
ctx := suite.LoadHTML(
47-
`<body><div id='elm-1'>Elm: 1</div><div id='elm-2'>Elm: 2</div></body>`,
48-
)
49-
Expect(ctx.Eval(`
50-
const e = document.getElementById("elm-2")
51-
e.outerHTML
52-
`)).To(Equal(`<div id="elm-2">Elm: 2</div>`))
53-
Expect(
54-
ctx.Eval(`Object.getPrototypeOf(e).constructor.name`),
55-
).To(Equal("HTMLDivElement"))
56-
})
57-
})
58-
59-
itShouldBeADocument := func() {
60-
It("Should be an instance of Document", func() {
61-
Expect(ctx.Eval("actual instanceof Document")).To(BeTrue())
62-
Expect("actual").To(ctx.BeInstanceOf("Document"))
63-
})
8+
type DocumentTestSuite struct {
9+
ScriptHostSuite
10+
}
6411

65-
It("Should have nodeType 9", func() {
66-
Expect(ctx.Eval("actual.nodeType")).To(BeEquivalentTo(9))
67-
})
68-
It("Should be an instance of Node", func() {
69-
Expect(ctx.Eval("actual instanceof Node")).To(BeTrue())
70-
})
71-
It("Should be an instance of EventTarget", func() {
72-
Expect(ctx.Eval("actual instanceof EventTarget")).To(BeTrue())
73-
})
74-
It("Should be an instance of Object", func() {
75-
Expect(ctx.Eval("actual instanceof Object")).To(BeTrue())
76-
})
77-
}
12+
func NewDocumentSuite(h html.ScriptHost) *DocumentTestSuite {
13+
return &DocumentTestSuite{ScriptHostSuite: *NewScriptHostSuite(h)}
14+
}
7815

79-
itShouldBeAnHTMLDocument := func(expectHTMLDocument bool) {
80-
It("Should be an instance of HTMLDocument", func() {
81-
Expect(
82-
ctx.Eval("actual instanceof HTMLDocument"),
83-
).To(Equal(expectHTMLDocument))
84-
})
16+
func (s *DocumentTestSuite) TestPrototype() {
17+
s.Assert().Equal("HTMLDocument",
18+
s.mustEval("Object.getPrototypeOf(document).constructor.name"),
19+
"Global document instance")
20+
21+
s.Assert().Equal(true, s.mustEval(
22+
`Object.getOwnPropertyNames(Document.prototype).includes("createElement")`),
23+
"createElement exists on Document.prototype")
24+
25+
s.Assert().Equal("Node",
26+
s.mustEval(`Object.getPrototypeOf(Document.prototype).constructor.name`),
27+
"Document inherits from Node")
28+
s.Assert().Equal("Document",
29+
s.mustEval(`Object.getPrototypeOf(HTMLDocument.prototype).constructor.name`),
30+
"HTMLDocument inherits from Document")
31+
32+
s.Assert().Equal(false, s.mustEval(
33+
`Object.getOwnPropertyNames(Document).includes("createElement")`),
34+
"createElement exist on Document (static method)")
35+
36+
s.Assert().Contains(
37+
s.mustEval("Object.getOwnPropertyNames(document)"), "location",
38+
"location should exist on document instance")
39+
s.Assert().NotContains(
40+
s.mustEval("Object.getOwnPropertyNames(Document.prototype)"), "location",
41+
"location should not exist on Document prototype")
42+
}
8543

86-
itShouldBeADocument()
44+
func (s *DocumentTestSuite) TestCreateElement() {
45+
s.Assert().Equal(true,
46+
s.mustEval(`document.createElement("base") instanceof HTMLElement`),
47+
"Element is an HTMLElement instance")
48+
}
8749

88-
It("Should have a class hierarchy of 5 classes", func() {
89-
expectedBaseClassCount := 4
90-
if expectHTMLDocument {
91-
expectedBaseClassCount++
92-
}
93-
Expect(ctx.Eval(`
94-
let baseClassCount = 0
95-
let current = actual
96-
while(current = Object.getPrototypeOf(current))
97-
baseClassCount++
98-
baseClassCount;
99-
`)).To(BeEquivalentTo(expectedBaseClassCount))
100-
})
101-
}
50+
func (s *DocumentTestSuite) TestGetElementByID() {
51+
s.mustLoadHTML(
52+
`<body><div id='elm-1'>Elm: 1</div><div id='elm-2'>Elm: 2</div></body>`,
53+
)
54+
s.Expect(s.eval(`
55+
const e = document.getElementById("elm-2")
56+
e.outerHTML
57+
`)).To(Equal(`<div id="elm-2">Elm: 2</div>`))
10258

103-
Describe("new Document()", func() {
104-
BeforeEach(func() {
105-
Expect(ctx.Run("const actual = new Document()")).To(Succeed())
106-
})
59+
s.Expect(s.eval(`Object.getPrototypeOf(e).constructor.name`)).To(Equal("HTMLDivElement"))
60+
}
10761

108-
It("Should now be the same as window.document", func() {
109-
Expect(ctx.Eval("actual === window.document")).To(BeFalse())
110-
})
62+
func (s *DocumentTestSuite) TestNewDocument() {
63+
s.mustRun("const actual = new Document()")
11164

112-
Describe("Class hierarchy", func() {
113-
itShouldBeAnHTMLDocument(false)
114-
})
115-
})
65+
s.Assert().Equal(false,
66+
s.mustEval(`actual === document`),
67+
"New document must not equal global document")
11668

117-
Describe("Class Hierarchy of `window.document`", func() {
118-
BeforeEach(func() {
119-
ctx = suite.LoadHTML("<html></html>")
120-
ctx.Run("const actual = window.document")
121-
})
122-
itShouldBeAnHTMLDocument(true)
123-
})
124-
})
69+
s.Assert().EqualValues(9, s.mustEval("actual.nodeType"), "new Document().nodeType")
70+
s.Assert().Equal("Document",
71+
s.mustEval("Object.getPrototypeOf(actual).constructor.name"),
72+
"Actual constructor")
12573
}

internal/test/scripttests/script_host_suite.go

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ type ScriptHostSuite struct {
1212
window htmltest.WindowHelper
1313
}
1414

15+
func (s *ScriptHostSuite) mustLoadHTML(html string) {
16+
s.T().Helper()
17+
s.Assert().NoError(s.window.LoadHTML(html))
18+
}
19+
1520
func (s *ScriptHostSuite) SetupTest() {
1621
s.window = htmltest.NewWindowHelper(s.T(), html.NewWindow(html.WindowOptions{
1722
ScriptHost: s.scriptHost,
@@ -40,6 +45,18 @@ func (s *ScriptHostSuite) eval(script string) (any, error) {
4045
return s.window.Eval(script)
4146
}
4247

48+
func (s *ScriptHostSuite) mustRun(script string) {
49+
s.T().Helper()
50+
s.Assert().NoError(s.run(script))
51+
}
52+
53+
func (s *ScriptHostSuite) mustEval(script string) any {
54+
s.T().Helper()
55+
res, err := s.eval(script)
56+
s.Assert().NoError(err)
57+
return res
58+
}
59+
4360
// Runs a script, and discards the returned value.
4461
//
4562
// Returns an error if the script code throws.

internal/test/scripttests/script_test_suite.go

-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,5 @@ func (suite *ScriptTestSuite) NewWindow() html.Window {
7979
}
8080

8181
func (suite *ScriptTestSuite) CreateAllGinkgoTests() {
82-
suite.CreateDocumentTests()
8382
suite.CreateEventTargetTests()
8483
}

internal/test/scripttests/suites.go

+1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ func RunSuites(t *testing.T, h html.ScriptHost) {
1616
t.Run("EventLoop", runSuite(NewEventLoopTestSuite(h)))
1717
t.Run("Window", runSuite(NewWindowTestSuite(h)))
1818
t.Run("UIEvents", runSuite(NewUIEventTestSuite(h)))
19+
t.Run("Document", runSuite(NewDocumentSuite(h)))
1920
}

internal/testing/htmltest/helpers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (h WindowHelper) MustLoadHTML(html string) {
4646
h.t.Helper()
4747

4848
if err := h.Window.LoadHTML(html); err != nil {
49-
h.t.Fatalf("Error loading HTML: %s\nHTML: %s", err.Error(), html)
49+
h.t.Errorf("Error loading HTML: %s\nHTML: %s", err.Error(), html)
5050
}
5151
}
5252

0 commit comments

Comments
 (0)