-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular_test.go
More file actions
328 lines (292 loc) · 9.93 KB
/
angular_test.go
File metadata and controls
328 lines (292 loc) · 9.93 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// SPDX-License-Identifier: EUPL-1.2
package webview
import (
"context"
"strings"
"testing"
"time"
)
func newAngularTestHarness(t *testing.T, onMessage func(*fakeCDPTarget, cdpMessage)) (*AngularHelper, *fakeCDPTarget, *CDPClient) {
t.Helper()
server := newFakeCDPServer(t)
target := server.primaryTarget()
target.onMessage = onMessage
client := newConnectedCDPClient(t, target)
wv := &Webview{
client: client,
ctx: context.Background(),
timeout: time.Second,
consoleLogs: make([]ConsoleMessage, 0),
consoleLimit: 10,
}
return NewAngularHelper(wv), target, client
}
func TestAngular_SetTimeout_Good(t *testing.T) {
ah := NewAngularHelper(&Webview{})
ah.SetTimeout(5 * time.Second)
if ah.timeout != 5*time.Second {
t.Fatalf("SetTimeout = %v, want 5s", ah.timeout)
}
}
func TestAngular_WaitForAngular_Bad_NotAngular(t *testing.T) {
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
target.replyValue(msg.ID, false)
})
if err := ah.WaitForAngular(); err == nil {
t.Fatal("WaitForAngular succeeded for a non-Angular page")
}
}
func TestAngular_WaitForAngular_Good(t *testing.T) {
var evaluateCount int
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
evaluateCount++
expr, _ := msg.Params["expression"].(string)
if strings.Contains(expr, "getAllAngularRootElements") || strings.Contains(expr, "[ng-version]") {
target.replyValue(msg.ID, true)
return
}
target.replyValue(msg.ID, true)
})
if err := ah.WaitForAngular(); err != nil {
t.Fatalf("WaitForAngular returned error: %v", err)
}
if evaluateCount < 2 {
t.Fatalf("WaitForAngular made %d evaluate calls, want at least 2", evaluateCount)
}
}
func TestAngular_waitForZoneStability_Good_FallsBackToPolling(t *testing.T) {
var calls int
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
calls++
expr, _ := msg.Params["expression"].(string)
switch {
case strings.Contains(expr, "new Promise"):
target.replyError(msg.ID, "zone probe failed")
default:
target.replyValue(msg.ID, true)
}
})
if err := ah.waitForZoneStability(context.Background()); err != nil {
t.Fatalf("waitForZoneStability returned error: %v", err)
}
if calls < 2 {
t.Fatalf("waitForZoneStability made %d evaluate calls, want at least 2", calls)
}
}
func TestAngular_NavigateByRouter_Good(t *testing.T) {
var expressions []string
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
expr, _ := msg.Params["expression"].(string)
expressions = append(expressions, expr)
if strings.Contains(expr, "navigateByUrl") {
target.replyValue(msg.ID, true)
return
}
target.replyValue(msg.ID, true)
})
if err := ah.NavigateByRouter("/dashboard"); err != nil {
t.Fatalf("NavigateByRouter returned error: %v", err)
}
if len(expressions) < 2 {
t.Fatalf("NavigateByRouter made %d evaluate calls, want at least 2", len(expressions))
}
}
func TestAngular_NavigateByRouter_Bad(t *testing.T) {
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
target.replyError(msg.ID, "could not find router")
})
if err := ah.NavigateByRouter("/dashboard"); err == nil {
t.Fatal("NavigateByRouter succeeded despite evaluation error")
}
}
func TestAngular_GetComponentProperty_Good(t *testing.T) {
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
expr, _ := msg.Params["expression"].(string)
if !strings.Contains(expr, `const selector = "app-user";`) {
t.Fatalf("expression did not quote selector: %s", expr)
}
if !strings.Contains(expr, `const propertyName = "displayName";`) {
t.Fatalf("expression did not quote property name: %s", expr)
}
target.replyValue(msg.ID, "Ada")
})
got, err := ah.GetComponentProperty("app-user", "displayName")
if err != nil {
t.Fatalf("GetComponentProperty returned error: %v", err)
}
if got != "Ada" {
t.Fatalf("GetComponentProperty = %v, want Ada", got)
}
}
func TestAngular_SetComponentProperty_Good(t *testing.T) {
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
expr, _ := msg.Params["expression"].(string)
if !strings.Contains(expr, `component[propertyName] = true;`) {
t.Fatalf("expression did not set the component property: %s", expr)
}
target.replyValue(msg.ID, true)
})
if err := ah.SetComponentProperty("app-user", "active", true); err != nil {
t.Fatalf("SetComponentProperty returned error: %v", err)
}
}
func TestAngular_CallComponentMethod_Good(t *testing.T) {
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
expr, _ := msg.Params["expression"].(string)
if !strings.Contains(expr, `component[methodName](1, "two")`) {
t.Fatalf("expression did not marshal method args: %s", expr)
}
target.replyValue(msg.ID, map[string]any{"ok": true})
})
got, err := ah.CallComponentMethod("app-user", "save", 1, "two")
if err != nil {
t.Fatalf("CallComponentMethod returned error: %v", err)
}
if gotMap, ok := got.(map[string]any); !ok || gotMap["ok"] != true {
t.Fatalf("CallComponentMethod = %#v, want ok=true", got)
}
}
func TestAngular_TriggerChangeDetection_Good(t *testing.T) {
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
target.replyValue(msg.ID, true)
})
if err := ah.TriggerChangeDetection(); err != nil {
t.Fatalf("TriggerChangeDetection returned error: %v", err)
}
}
func TestAngular_GetService_Good(t *testing.T) {
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
target.replyValue(msg.ID, map[string]any{"name": "session"})
})
got, err := ah.GetService("SessionService")
if err != nil {
t.Fatalf("GetService returned error: %v", err)
}
if gotMap, ok := got.(map[string]any); !ok || gotMap["name"] != "session" {
t.Fatalf("GetService = %#v, want session map", got)
}
}
func TestAngular_WaitForComponent_Good(t *testing.T) {
var calls int
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
calls++
if calls == 1 {
target.replyValue(msg.ID, false)
return
}
target.replyValue(msg.ID, true)
})
if err := ah.WaitForComponent("app-user"); err != nil {
t.Fatalf("WaitForComponent returned error: %v", err)
}
if calls < 2 {
t.Fatalf("WaitForComponent calls = %d, want at least 2", calls)
}
}
func TestAngular_DispatchEvent_Good(t *testing.T) {
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
expr, _ := msg.Params["expression"].(string)
if !strings.Contains(expr, `new CustomEvent(eventName, { bubbles: true, detail: {"count":1} })`) && !strings.Contains(expr, `new CustomEvent(eventName, { bubbles: true, detail: {\"count\":1} })`) {
t.Fatalf("expression did not dispatch custom event with detail: %s", expr)
}
target.replyValue(msg.ID, true)
})
if err := ah.DispatchEvent("app-user", "count-change", map[string]any{"count": 1}); err != nil {
t.Fatalf("DispatchEvent returned error: %v", err)
}
}
func TestAngular_GetNgModel_Good(t *testing.T) {
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
target.replyValue(msg.ID, "hello")
})
got, err := ah.GetNgModel("input[name=email]")
if err != nil {
t.Fatalf("GetNgModel returned error: %v", err)
}
if got != "hello" {
t.Fatalf("GetNgModel = %v, want hello", got)
}
}
func TestAngular_SetNgModel_Good(t *testing.T) {
ah, _, _ := newAngularTestHarness(t, func(target *fakeCDPTarget, msg cdpMessage) {
if msg.Method != "Runtime.evaluate" {
t.Fatalf("unexpected method %q", msg.Method)
}
target.replyValue(msg.ID, true)
})
if err := ah.SetNgModel(`input[name="x"]`, `";window.hacked=true;//`); err != nil {
t.Fatalf("SetNgModel returned error: %v", err)
}
}
func TestAngular_copyStringOnlyMap_Good(t *testing.T) {
tests := []struct {
name string
in any
want map[string]string
}{
{name: "map any", in: map[string]any{"a": "1", "b": 2}, want: map[string]string{"a": "1"}},
{name: "map string", in: map[string]string{"c": "3"}, want: map[string]string{"c": "3"}},
{name: "nil", in: nil, want: nil},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got := copyStringOnlyMap(tc.in)
if len(got) != len(tc.want) {
t.Fatalf("copyStringOnlyMap len = %d, want %d", len(got), len(tc.want))
}
for k, want := range tc.want {
if got[k] != want {
t.Fatalf("copyStringOnlyMap[%q] = %q, want %q", k, got[k], want)
}
}
})
}
}
func TestAngular_formatJSValue_Ugly_FallsBackToSprint(t *testing.T) {
got := formatJSValue(make(chan int))
if got == "null" {
t.Fatalf("formatJSValue fallback returned %q, want quoted sprint output", got)
}
if !strings.HasPrefix(got, "\"") || !strings.HasSuffix(got, "\"") {
t.Fatalf("formatJSValue fallback = %q, want quoted string output", got)
}
}