-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_helpers.go
84 lines (70 loc) · 2.07 KB
/
test_helpers.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
package mdctest // import "github.com/vecty-components/material/components/internal/mdctest"
import (
"syscall/js"
"github.com/vecty-components/material/gojs"
"github.com/vecty-components/material/gojs/jsdom"
)
const (
MCW_MODULE = "material-components-web/dist/material-components-web"
)
var (
Dom jsdom.JSDOM
)
func Init() error {
err := LoadMDCModule()
if err != nil {
return err
}
Dom, err = EmulateDOM()
if err != nil {
return err
}
err = ShimHyperform()
if err != nil {
return err
}
return nil
}
// For some reason the material-components-web node module does not come with
// MDCMenu, it only comes with an undocumented MDCSimpleMenu.
func InitMenu() (err error) {
gojs.CatchException(&err)
mdc := js.Global().Get("Object").New()
mdc.Set("menu", js.Global().Call("require", "@material/menu/dist/mdc.menu"))
js.Global().Set("mdc", mdc)
Dom, err = EmulateDOM()
if err != nil {
return err
}
return err
}
func LoadMDCModule() (err error) {
gojs.CatchException(&err)
js.Global().Set("mdc", js.Global().Call("require", MCW_MODULE))
return err
}
func ShimHyperform() (err error) {
gojs.CatchException(&err)
js.Global().Call("require", "hyperform").Invoke(js.Global().Get("window"))
return err
}
// EmulateDOM sets up a fake DOM in Node for "gopherjs test" We emulate a
// browser dom since tests run in Node, and MDC components need a dom element to
// attach to. This is not needed when running in a browser.
func EmulateDOM() (dom jsdom.JSDOM, err error) {
dom, err = jsdom.New(``, &jsdom.M{"pretendToBeVisual": true})
if err != nil {
return nil, err
}
dom.SetHTML(`<html><body></body></html>`)
js.Global().Set("window", dom.Window())
js.Global().Set("document", dom.Window().Get("document"))
js.Global().Set("HTMLElement", dom.Window().Get("HTMLElement"))
raf := dom.Window().Get("requestAnimationFrame")
js.Global().Set("requestAnimationFrame", raf)
caf := dom.Window().Get("cancelAnimationFrame")
js.Global().Set("cancelAnimationFrame", caf)
gcs := dom.Window().Get("getComputedStyle")
js.Global().Set("getComputedStyle", gcs)
return dom, err
}