-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathjs_test.go
95 lines (83 loc) · 1.87 KB
/
js_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package js
import (
"context"
"fmt"
"github.com/psilva261/mycel"
"github.com/psilva261/mycel/browser/fs"
"github.com/psilva261/mycel/logger"
"github.com/psilva261/mycel/nodes"
"github.com/psilva261/mycel/style"
"golang.org/x/net/html"
"io/ioutil"
"net/url"
"strings"
"testing"
"time"
)
const simpleHTML = `
<html>
<body>
<h1 id="title">Hello</h1>
</body>
</html>
`
func init() {
log.Debug = true
SetFetcher(&TestFetcher{})
go fs.Srv9p()
<-time.After(2*time.Second)
}
type TestFetcher struct {}
func (tf *TestFetcher) Ctx() context.Context {
return context.Background()
}
func (tf *TestFetcher) Origin() (u *url.URL) {
u, _ = url.Parse("https://example.com")
return
}
func (tf *TestFetcher) LinkedUrl(string) (*url.URL, error) {
return nil, fmt.Errorf("not implemented")
}
func (tf *TestFetcher) Get(*url.URL) ([]byte, mycel.ContentType, error) {
return nil, mycel.ContentType{}, fmt.Errorf("not implemented")
}
func TestJQueryHide(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
buf, err := ioutil.ReadFile("jquery-3.5.1.js")
if err != nil {
t.Fatalf("%v", err)
}
script := `
$(document).ready(function() {
$('h1').hide();
});
`
r := strings.NewReader(simpleHTML)
doc, err := html.Parse(r)
if err != nil {
t.Fatalf(err.Error())
}
nt := nodes.NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
fs.SetDOM(nt)
fs.Update("", simpleHTML, nil, []string{string(buf), script})
resHtm, changed, err := Start(string(buf), script)
if err != nil {
t.Fatalf("%v", err)
}
if !changed {
t.Fatalf("changed=%v", changed)
}
t.Logf("resHtm=%v", resHtm)
r = strings.NewReader(resHtm)
doc, err = html.Parse(r)
if err != nil {
t.Fatalf(err.Error())
}
nt = nodes.NewNodeTree(doc, style.Map{}, make(map[*html.Node]style.Map), nil)
if v := nt.Find("h1").Css("display"); v != "none" {
t.Fatalf("%v", v)
}
Stop()
}