Repository navigation
Expand file tree
/
Copy pathevent_wasm_test.go
More file actions
150 lines (131 loc) · 3.74 KB
/
Copy pathevent_wasm_test.go
File metadata and controls
150 lines (131 loc) · 3.74 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
//go:build wasm
package dom
import (
"syscall/js"
"testing"
)
// TestOnKeyDownMovesFocus is the consumer-shaped proof the publication rule
// demands: it goes through the real stack a consumer uses — a real element
// tree, OnKeyDown on its container, a real KeyboardEvent dispatched via
// syscall/js — with no doubles. Step 4 is the exact thing calendarslider
// must do in components: a roving-focus implementation written against
// OnKeyDown moves document.activeElement. If this is awkward to write here,
// the API is awkward to use.
func TestOnKeyDownMovesFocus(t *testing.T) {
ids := []string{"key-day-0", "key-day-1", "key-day-2"}
var days []*Element
for _, id := range ids {
days = append(days, NewElement("button").ID(id).Text(id))
}
var got Key
current := 0
container := NewElement("div").ID("key-strip").
OnKeyDown(func(e KeyEvent) {
got = e.Key()
switch e.Key() {
case KeyArrowRight:
if current+1 < len(days) {
current++
}
case KeyArrowLeft:
if current > 0 {
current--
}
case KeyHome:
current = 0
case KeyEnd:
current = len(days) - 1
default:
return
}
if ref, ok := days[current].Ref(); ok {
ref.Focus()
}
})
for _, d := range days {
container.Child(d)
}
if err := Render("app", container); err != nil {
t.Fatalf("Render failed: %v", err)
}
// focus the first day, like a Tab stop landing on the strip
if ref, ok := days[0].Ref(); ok {
ref.Focus()
} else {
t.Fatal("day 0 has no live reference after Render")
}
dispatchKey := func(targetID, key string) {
ref, ok := Get(targetID)
if !ok {
t.Fatalf("no live reference for %s", targetID)
}
opts := js.Global().Get("Object").New()
opts.Set("key", key)
opts.Set("bubbles", true)
evt := js.Global().Get("KeyboardEvent").New("keydown", opts)
ref.(*elementWasm).val.Call("dispatchEvent", evt)
}
activeID := func() string {
active := js.Global().Get("document").Get("activeElement")
if active.IsNull() || active.IsUndefined() {
return ""
}
return active.Get("id").String()
}
// ArrowRight on day 0: handler sees the typed key, focus moves to day 1
dispatchKey(ids[0], "ArrowRight")
if got != KeyArrowRight {
t.Errorf("handler received %q, want %q", string(got), string(KeyArrowRight))
}
if id := activeID(); id != ids[1] {
t.Errorf("after ArrowRight activeElement is %q, want %q", id, ids[1])
}
// Home on day 1: focus returns to day 0
dispatchKey(ids[1], "Home")
if got != KeyHome {
t.Errorf("handler received %q, want %q", string(got), string(KeyHome))
}
if id := activeID(); id != ids[0] {
t.Errorf("after Home activeElement is %q, want %q", id, ids[0])
}
}
func TestPointerAndMouseEventsWasm(t *testing.T) {
pointerDownFired := false
pointerEnterFired := false
buttonsVal := 0
btn := NewElement("button").ID("test-pointer-btn").
OnPointerDown(func(e Event) {
pointerDownFired = true
buttonsVal = e.Buttons()
e.ReleasePointerCapture()
}).
OnPointerEnter(func(e Event) {
pointerEnterFired = true
})
if err := Render("app", btn); err != nil {
t.Fatalf("Render failed: %v", err)
}
ref, ok := Get("test-pointer-btn")
if !ok {
t.Fatal("button has no live reference")
}
rawEl := ref.(*elementWasm).val
// Dispatch pointerdown
opts := js.Global().Get("Object").New()
opts.Set("bubbles", true)
opts.Set("buttons", 1)
evtDown := js.Global().Get("Event").New("pointerdown", opts)
rawEl.Call("dispatchEvent", evtDown)
if !pointerDownFired {
t.Error("expected pointerdown to fire")
}
if buttonsVal < 0 {
t.Error("expected non-negative buttons")
}
// Dispatch pointerenter
evtEnter := js.Global().Get("Event").New("pointerenter", opts)
rawEl.Call("dispatchEvent", evtEnter)
if !pointerEnterFired {
t.Error("expected pointerenter to fire")
}
}