Skip to content

Commit d9d03a7

Browse files
committed
test(dom): Convert ginkgo tests for Element
Converts ginkgo tests to testify suites and naked Go tests
1 parent 08eafdd commit d9d03a7

File tree

1 file changed

+150
-149
lines changed

1 file changed

+150
-149
lines changed

dom/element_test.go

+150-149
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
. "github.com/gost-dom/browser/testing/gomega-matchers"
1111
"github.com/stretchr/testify/suite"
1212

13-
. "github.com/onsi/ginkgo/v2"
1413
"github.com/onsi/gomega"
1514
)
1615

@@ -163,220 +162,222 @@ func TestElement(t *testing.T) {
163162
suite.Run(t, new(ElementTestSuite))
164163
}
165164

166-
var _ = Describe("Element", func() {
167-
Describe("InsertAdjacentHTML", func() {
168-
It("Should insert correctly 'beforeBegin'", func() {
169-
doc := ParseHtmlString(`<body>
165+
func TestElementInsertAdjacentHTMLBeforeBegin(t *testing.T) {
166+
gomega := gomega.NewWithT(t)
167+
doc := ParseHtmlString(`<body>
170168
<div id="1">El 1</div>
171169
<div id="2">El 2
172170
<div>El 2-a</div>
173171
<div>El 2-b</div>
174172
</div>
175173
<div id="3">El 1</div>
176174
</body>`)
177-
el := doc.GetElementById("2")
178-
gomega.Expect(el.InsertAdjacentHTML(
179-
"beforebegin",
180-
"<div>1st new child</div><div>2nd new child</div>",
181-
)).To(Succeed())
182-
gomega.Expect(doc.Body()).To(HaveOuterHTML(`<body>
175+
el := doc.GetElementById("2")
176+
gomega.Expect(el.InsertAdjacentHTML(
177+
"beforebegin",
178+
"<div>1st new child</div><div>2nd new child</div>",
179+
)).To(Succeed())
180+
gomega.Expect(doc.Body()).To(HaveOuterHTML(`<body>
183181
<div id="1">El 1</div>
184182
<div>1st new child</div><div>2nd new child</div><div id="2">El 2
185183
<div>El 2-a</div>
186184
<div>El 2-b</div>
187185
</div>
188186
<div id="3">El 1</div>
189187
</body>`))
188+
}
190189

191-
})
192-
193-
It("Should insert correctly 'afterBegin'", func() {
194-
doc := ParseHtmlString(`<body>
190+
func TestElementInsertAdjacentHTMLAfterBegin(t *testing.T) {
191+
gomega := gomega.NewWithT(t)
192+
doc := ParseHtmlString(`<body>
195193
<div id="1">El 1</div>
196194
<div id="2">El 2
197195
<div>El 2-a</div>
198196
<div>El 2-b</div>
199197
</div>
200198
<div id="3">El 1</div>
201199
</body>`)
202-
el, err := (doc.QuerySelector("[id='2']"))
203-
gomega.Expect(err).ToNot(HaveOccurred())
204-
gomega.Expect(
205-
el.InsertAdjacentHTML(
206-
"afterbegin",
207-
"<div>1st new child</div><div>2nd new child</div>",
208-
),
209-
).To(Succeed())
210-
gomega.Expect(doc.Body()).To(HaveOuterHTML(`<body>
200+
el, err := (doc.QuerySelector("[id='2']"))
201+
gomega.Expect(err).ToNot(HaveOccurred())
202+
gomega.Expect(
203+
el.InsertAdjacentHTML(
204+
"afterbegin",
205+
"<div>1st new child</div><div>2nd new child</div>",
206+
),
207+
).To(Succeed())
208+
gomega.Expect(doc.Body()).To(HaveOuterHTML(`<body>
211209
<div id="1">El 1</div>
212210
<div id="2"><div>1st new child</div><div>2nd new child</div>El 2
213211
<div>El 2-a</div>
214212
<div>El 2-b</div>
215213
</div>
216214
<div id="3">El 1</div>
217215
</body>`))
216+
}
218217

219-
})
220-
221-
It("Should insert correctly 'beforeEnd'", func() {
222-
doc := ParseHtmlString(`<body>
218+
func TestElementInsertAdjacentHTMLBeforeEnd(t *testing.T) {
219+
gomega := gomega.NewWithT(t)
220+
doc := ParseHtmlString(`<body>
223221
<div id="1">El 1</div>
224222
<div id="2">El 2
225223
<div>El 2-a</div>
226224
<div>El 2-b</div>
227225
</div>
228226
<div id="3">El 1</div>
229227
</body>`)
230-
el, err := (doc.QuerySelector("[id='2']"))
231-
gomega.Expect(err).ToNot(HaveOccurred())
232-
gomega.Expect(
233-
el.InsertAdjacentHTML(
234-
"beforeend",
235-
"<div>1st new child</div><div>2nd new child</div>",
236-
),
237-
).To(Succeed())
238-
gomega.Expect(doc.Body()).To(HaveOuterHTML(`<body>
228+
el, err := (doc.QuerySelector("[id='2']"))
229+
gomega.Expect(err).ToNot(HaveOccurred())
230+
gomega.Expect(
231+
el.InsertAdjacentHTML(
232+
"beforeend",
233+
"<div>1st new child</div><div>2nd new child</div>",
234+
),
235+
).To(Succeed())
236+
gomega.Expect(doc.Body()).To(HaveOuterHTML(`<body>
239237
<div id="1">El 1</div>
240238
<div id="2">El 2
241239
<div>El 2-a</div>
242240
<div>El 2-b</div>
243241
<div>1st new child</div><div>2nd new child</div></div>
244242
<div id="3">El 1</div>
245243
</body>`))
244+
}
246245

247-
})
248-
249-
It("Should insert correctly 'afterend'", func() {
250-
doc := ParseHtmlString(`<body>
246+
func TestElementInsertAdjacentHTMLAfterEnd(t *testing.T) {
247+
gomega := gomega.NewWithT(t)
248+
doc := ParseHtmlString(`<body>
251249
<div id="1">El 1</div>
252250
<div id="2">El 2
253251
<div>El 2-a</div>
254252
<div>El 2-b</div>
255253
</div>
256254
<div id="3">El 1</div>
257255
</body>`)
258-
el, err := (doc.QuerySelector("[id='2']"))
259-
gomega.Expect(err).ToNot(HaveOccurred())
260-
gomega.Expect(
261-
el.InsertAdjacentHTML(
262-
"afterend",
263-
"<div>1st new child</div><div>2nd new child</div>",
264-
),
265-
).To(Succeed())
266-
gomega.Expect(doc.Body()).To(HaveOuterHTML(`<body>
256+
el, err := (doc.QuerySelector("[id='2']"))
257+
gomega.Expect(err).ToNot(HaveOccurred())
258+
gomega.Expect(
259+
el.InsertAdjacentHTML(
260+
"afterend",
261+
"<div>1st new child</div><div>2nd new child</div>",
262+
),
263+
).To(Succeed())
264+
gomega.Expect(doc.Body()).To(HaveOuterHTML(`<body>
267265
<div id="1">El 1</div>
268266
<div id="2">El 2
269267
<div>El 2-a</div>
270268
<div>El 2-b</div>
271269
</div><div>1st new child</div><div>2nd new child</div>
272270
<div id="3">El 1</div>
273271
</body>`))
274-
})
275-
})
272+
}
276273

277-
Describe("HTML Rendering", func() {
278-
It("Should support OuterHTML", func() {
279-
doc := ParseHtmlString(`<body><div id="2">El 2
274+
func TestElementOuterHTML(t *testing.T) {
275+
gomega := gomega.NewWithT(t)
276+
// NOTE: Whitespace is part of the parsed HTML as #text nodes
277+
doc := ParseHtmlString(`<body><div id="2">El 2
280278
<div>El 2-a</div>
281279
<div>El 2-b</div>
282280
</div></body>`)
283-
gomega.Expect(doc.Body().OuterHTML()).To(Equal(`<body><div id="2">El 2
281+
gomega.Expect(doc.Body().OuterHTML()).To(Equal(`<body><div id="2">El 2
284282
<div>El 2-a</div>
285283
<div>El 2-b</div>
286284
</div></body>`))
287-
gomega.Expect(doc.Body().InnerHTML()).To(Equal(`<div id="2">El 2
285+
gomega.Expect(doc.Body().InnerHTML()).To(Equal(`<div id="2">El 2
288286
<div>El 2-a</div>
289287
<div>El 2-b</div>
290288
</div>`))
291-
})
292-
})
293-
294-
Describe("Append/Prepend/ReplaceChildren/Children", func() {
295-
// These should ideaaly be tested on both Element, Document, and
296-
// DocumentFragment. The functions are defined in the ParentNode
297-
// mixin interface that all 3 share.
298-
299-
It("Should add elements in 'Append'", func() {
300-
doc := ParseHtmlString(`<body>a<div>b</div>c</body>`)
301-
b := doc.Body()
302-
divE := doc.CreateElement("div")
303-
divE.SetTextContent("e")
304-
b.Append(
305-
doc.CreateText("d"),
306-
divE,
307-
doc.CreateText("f"),
308-
)
309-
gomega.Expect(b).To(HaveOuterHTML(`<body>a<div>b</div>cd<div>e</div>f</body>`))
310-
})
311-
312-
It("Should add elements first in 'Prepend'", func() {
313-
doc := ParseHtmlString(`<body>a<div>b</div>c</body>`)
314-
b := doc.Body()
315-
divE := doc.CreateElement("div")
316-
divE.SetTextContent("e")
317-
b.Prepend(
318-
doc.CreateText("d"),
319-
divE,
320-
doc.CreateText("f"),
321-
)
322-
gomega.Expect(b).To(HaveOuterHTML(`<body>d<div>e</div>fa<div>b</div>c</body>`))
323-
})
324-
325-
It("Should replace elements first in 'ReplaceChildren'", func() {
326-
doc := ParseHtmlString(`<body>a<div>b</div>c</body>`)
327-
b := doc.Body()
328-
divE := doc.CreateElement("div")
329-
divE.SetTextContent("e")
330-
b.ReplaceChildren(
331-
doc.CreateText("d"),
332-
divE,
333-
doc.CreateText("f"),
334-
)
335-
gomega.Expect(b).To(HaveOuterHTML(`<body>d<div>e</div>f</body>`))
336-
})
337-
338-
It("Should iterate elements in 'Children'", func() {
339-
doc := ParseHtmlString(
340-
`<body>a<div id="el-1">b</div>c<div id="el-2">d</div>e<div name="el-3">f</div>g</body>`,
341-
)
342-
b := doc.Body()
343-
c := b.Children()
344-
345-
gomega.Expect(c.Length()).To(Equal(3))
346-
gomega.Expect(c.Item(0)).To(HaveOuterHTML(`<div id="el-1">b</div>`))
347-
gomega.Expect(c.Item(1)).To(HaveAttribute("id", "el-2"))
348-
gomega.Expect(c.Item(2)).To(HaveAttribute("name", "el-3"))
349-
350-
gomega.Expect(c.Item(-1)).To(BeNil())
351-
gomega.Expect(c.Item(3)).To(BeNil())
352-
353-
gomega.Expect(c.NamedItem("el-1")).To(HaveAttribute("id", "el-1"))
354-
gomega.Expect(c.NamedItem("el-2")).To(HaveAttribute("id", "el-2"))
355-
gomega.Expect(c.NamedItem("el-3")).To(HaveAttribute("name", "el-3"))
356-
})
357-
358-
Describe("First/Last element", func() {
359-
It("Should return elements when they exist", func() {
360-
doc := ParseHtmlString(
361-
`<body>a<div id="el-1">b</div>c<div id="el-2">d</div>e<div name="el-3">f</div>g</body>`,
362-
)
363-
b := doc.Body()
364-
gomega.Expect(b.FirstElementChild()).To(HaveAttribute("id", "el-1"))
365-
gomega.Expect(b.LastElementChild()).To(HaveOuterHTML(`<div name="el-3">f</div>`))
366-
})
367-
368-
It("Should nil when there are only non-element children", func() {
369-
doc := ParseHtmlString(
370-
`<body>body text</body>`,
371-
)
372-
b := doc.Body()
373-
gomega.Expect(b.FirstElementChild()).To(BeNil())
374-
gomega.Expect(b.LastElementChild()).To(BeNil())
375-
})
376-
})
377-
378-
It("Should handle empty string correctly in 'NamedItem'", func() {
379-
Skip("Need to research")
380-
})
381-
})
382-
})
289+
}
290+
291+
// ParentElementTestSuite describes functionality in the ParentNode IDL
292+
// interface mixin, which is used by both Element, Document, and
293+
// DocumentFragment.
294+
//
295+
// Ideally, these tests should be executed on all 3 types
296+
type ParentElementTestSuite struct {
297+
gosttest.GomegaSuite
298+
}
299+
300+
func TestParentElement(t *testing.T) {
301+
suite.Run(t, new(ParentElementTestSuite))
302+
}
303+
304+
func (s *ParentElementTestSuite) TestAppend() {
305+
doc := ParseHtmlString(`<body>a<div>b</div>c</body>`)
306+
b := doc.Body()
307+
divE := doc.CreateElement("div")
308+
divE.SetTextContent("e")
309+
b.Append(
310+
doc.CreateText("d"),
311+
divE,
312+
doc.CreateText("f"),
313+
)
314+
s.Expect(b).To(HaveOuterHTML(`<body>a<div>b</div>cd<div>e</div>f</body>`))
315+
}
316+
317+
func (s *ParentElementTestSuite) TestPrepend() {
318+
doc := ParseHtmlString(`<body>a<div>b</div>c</body>`)
319+
b := doc.Body()
320+
divE := doc.CreateElement("div")
321+
divE.SetTextContent("e")
322+
b.Prepend(
323+
doc.CreateText("d"),
324+
divE,
325+
doc.CreateText("f"),
326+
)
327+
s.Expect(b).To(HaveOuterHTML(`<body>d<div>e</div>fa<div>b</div>c</body>`))
328+
}
329+
330+
func (s *ParentElementTestSuite) TestReplaceChildren() {
331+
doc := ParseHtmlString(`<body>a<div>b</div>c</body>`)
332+
b := doc.Body()
333+
divE := doc.CreateElement("div")
334+
divE.SetTextContent("e")
335+
b.ReplaceChildren(
336+
doc.CreateText("d"),
337+
divE,
338+
doc.CreateText("f"),
339+
)
340+
s.Expect(b).To(HaveOuterHTML(`<body>d<div>e</div>f</body>`))
341+
}
342+
343+
func (s *ParentElementTestSuite) TestIterateChildren() {
344+
doc := ParseHtmlString(
345+
`<body>a<div id="el-1">b</div>c<div id="el-2">d</div>e<div name="el-3">f</div>g</body>`,
346+
)
347+
b := doc.Body()
348+
c := b.Children()
349+
350+
s.Expect(c.Length()).To(Equal(3))
351+
s.Expect(c.Item(0)).To(HaveOuterHTML(`<div id="el-1">b</div>`))
352+
s.Expect(c.Item(1)).To(HaveAttribute("id", "el-2"))
353+
s.Expect(c.Item(2)).To(HaveAttribute("name", "el-3"))
354+
355+
s.Expect(c.Item(-1)).To(BeNil())
356+
s.Expect(c.Item(3)).To(BeNil())
357+
358+
s.Expect(c.NamedItem("el-1")).To(HaveAttribute("id", "el-1"))
359+
s.Expect(c.NamedItem("el-2")).To(HaveAttribute("id", "el-2"))
360+
s.Expect(c.NamedItem("el-3")).To(HaveAttribute("name", "el-3"))
361+
}
362+
363+
func (s *ParentElementTestSuite) TestFirstLastOnExistingElement() {
364+
doc := ParseHtmlString(
365+
`<body>a<div id="el-1">b</div>c<div id="el-2">d</div>e<div name="el-3">f</div>g</body>`,
366+
)
367+
b := doc.Body()
368+
s.Expect(b.FirstElementChild()).To(HaveAttribute("id", "el-1"))
369+
s.Expect(b.LastElementChild()).To(HaveOuterHTML(`<div name="el-3">f</div>`))
370+
}
371+
372+
func (s *ParentElementTestSuite) TestFirstLastWithNonElement() {
373+
doc := ParseHtmlString(
374+
`<body>body text</body>`,
375+
)
376+
b := doc.Body()
377+
s.Expect(b.FirstElementChild()).To(BeNil())
378+
s.Expect(b.LastElementChild()).To(BeNil())
379+
}
380+
381+
func (s *ParentElementTestSuite) TestEmptyString() {
382+
s.T().Skip("How to handle empty string in NamedItem")
383+
}

0 commit comments

Comments
 (0)