|
1 | 1 | package scripttests
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - . "github.com/onsi/ginkgo/v2" |
| 4 | + "github.com/gost-dom/browser/html" |
5 | 5 | . "github.com/onsi/gomega"
|
6 | 6 | )
|
7 | 7 |
|
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 | +} |
64 | 11 |
|
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 | +} |
78 | 15 |
|
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 | +} |
85 | 43 |
|
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 | +} |
87 | 49 |
|
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>`)) |
102 | 58 |
|
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 | +} |
107 | 61 |
|
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()") |
111 | 64 |
|
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") |
116 | 68 |
|
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") |
125 | 73 | }
|
0 commit comments