forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromise_test.go
127 lines (107 loc) · 3.31 KB
/
promise_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
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
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package v8go_test
import (
"testing"
"rogchap.com/v8go"
)
func TestPromiseFulfilled(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
ctx, _ := v8go.NewContext(iso)
if _, err := v8go.NewPromiseResolver(nil); err == nil {
t.Error("expected error with <nil> Context")
}
res1, _ := v8go.NewPromiseResolver(ctx)
prom1 := res1.GetPromise()
if s := prom1.State(); s != v8go.Pending {
t.Errorf("unexpected state for Promise, want Pending (0) got: %v", s)
}
var thenInfo *v8go.FunctionCallbackInfo
prom1thenVal := prom1.Then(func(info *v8go.FunctionCallbackInfo) *v8go.Value {
thenInfo = info
return nil
})
prom1then, _ := prom1thenVal.AsPromise()
if prom1then.State() != v8go.Pending {
t.Errorf("unexpected state for dependent Promise, want Pending got: %v", prom1then.State())
}
if thenInfo != nil {
t.Error("unexpected call of Then prior to resolving the promise")
}
val1, _ := v8go.NewValue(iso, "foo")
res1.Resolve(val1)
if s := prom1.State(); s != v8go.Fulfilled {
t.Fatalf("unexpected state for Promise, want Fulfilled (1) got: %v", s)
}
if result := prom1.Result(); result.String() != val1.String() {
t.Errorf("expected the Promise result to match the resolve value, but got: %s", result)
}
if thenInfo == nil {
t.Errorf("expected Then to be called, was not")
}
if len(thenInfo.Args()) != 1 || thenInfo.Args()[0].String() != "foo" {
t.Errorf("expected promise to be called with [foo] args, was: %+v", thenInfo.Args())
}
}
func TestPromiseRejected(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
ctx, _ := v8go.NewContext(iso)
res2, _ := v8go.NewPromiseResolver(ctx)
val2, _ := v8go.NewValue(iso, "Bad Foo")
res2.Reject(val2)
prom2 := res2.GetPromise()
if s := prom2.State(); s != v8go.Rejected {
t.Fatalf("unexpected state for Promise, want Rejected (2) got: %v", s)
}
var thenInfo *v8go.FunctionCallbackInfo
var then2Fulfilled, then2Rejected bool
prom2.
Catch(func(info *v8go.FunctionCallbackInfo) *v8go.Value {
thenInfo = info
return nil
}).
Then(
func(_ *v8go.FunctionCallbackInfo) *v8go.Value {
then2Fulfilled = true
return nil
},
func(_ *v8go.FunctionCallbackInfo) *v8go.Value {
then2Rejected = true
return nil
},
)
ctx.PerformMicrotaskCheckpoint()
if thenInfo == nil {
t.Fatalf("expected Then to be called on already-resolved promise, but was not")
}
if len(thenInfo.Args()) != 1 || thenInfo.Args()[0].String() != val2.String() {
t.Fatalf("expected [%v], was: %+v", val2, thenInfo.Args())
}
if then2Fulfilled {
t.Fatalf("unexpectedly called onFulfilled")
}
if !then2Rejected {
t.Fatalf("expected call to onRejected, got none")
}
}
func TestPromiseThenPanic(t *testing.T) {
t.Parallel()
iso, _ := v8go.NewIsolate()
ctx, _ := v8go.NewContext(iso)
res, _ := v8go.NewPromiseResolver(ctx)
prom := res.GetPromise()
t.Run("no callbacks", func(t *testing.T) {
defer func() { recover() }()
prom.Then()
t.Errorf("expected a panic")
})
t.Run("3 callbacks", func(t *testing.T) {
defer func() { recover() }()
fn := func(_ *v8go.FunctionCallbackInfo) *v8go.Value { return nil }
prom.Then(fn, fn, fn)
t.Errorf("expected a panic")
})
}