-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheerio_test.go
60 lines (47 loc) · 1.1 KB
/
cheerio_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package cheerio
import (
"fmt"
"testing"
duktape "gopkg.in/olebedev/go-duktape.v3"
)
func TestAddTextAndClass(t *testing.T) {
ctx := duktape.New()
Define(ctx)
defer ctx.DestroyHeap()
js := `
$ = cheerio.load('<h2 class = "title">Hello world</h2>');
$('h2.title').text('Hello there!');
$('h2').addClass('welcome');
$.html();
`
if err := ctx.PevalString(js); err != nil {
t.Fatal(err)
}
respString := ctx.SafeToString(-1)
expect := `<html><head></head><body><h2 class="title welcome">Hello there!</h2></body></html>`
if respString != expect {
t.Fatalf("Expected %s, got %s", expect, respString)
}
}
func TestGlobals(t *testing.T) {
testCases := []struct {
js string
typeOf string
}{
{`typeof cheerio;`, "function"},
}
for idx, tc := range testCases {
t.Run(fmt.Sprintf("#%d", idx), func(t *testing.T) {
ctx := duktape.New()
Define(ctx)
defer ctx.Destroy()
if err := ctx.PevalString(tc.js); err != nil {
t.Fatal(err)
}
got := ctx.SafeToString(-1)
if got != tc.typeOf {
t.Fatalf("Expected typeOf of %s, got %s", tc.typeOf, got)
}
})
}
}