-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise_test.go
101 lines (78 loc) · 2.05 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
//go:build js
package goji
import (
"fmt"
"syscall/js"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPromiseNew(t *testing.T) {
var (
res []js.Value
executor js.Func
)
executor = js.FuncOf(func(this js.Value, args []js.Value) any {
executor.Release()
res = args
return js.Undefined()
})
value := Promise.New(executor)
assert.NotNil(t, value)
require.Len(t, res, 2)
assert.Equal(t, js.TypeFunction, res[0].Type())
assert.Equal(t, js.TypeFunction, res[1].Type())
}
func TestPromiseResolveThenAwait(t *testing.T) {
value := js.ValueOf(1)
prom := Promise.Resolve(value)
res, err := Await(prom)
require.NoError(t, err)
require.Len(t, res, 1)
assert.Equal(t, value, res[0])
}
func TestPromiseRejectThenAwait(t *testing.T) {
value := Error.New("test")
prom := Promise.Reject(js.Value(value))
res, err := Await(prom)
assert.Len(t, res, 0)
assert.Equal(t, value, err)
}
func TestPromiseOfThenAwaitResolve(t *testing.T) {
value := js.ValueOf(true)
prom := PromiseOf(func(resolve, reject func(value js.Value)) {
resolve(value)
})
res, err := Await(prom)
require.NoError(t, err)
require.Len(t, res, 1)
assert.Equal(t, value, res[0])
}
func TestPromiseOfThenAwaitReject(t *testing.T) {
value := Error.New("test reject")
prom := PromiseOf(func(resolve, reject func(value js.Value)) {
reject(js.Value(value))
})
res, err := Await(prom)
assert.Len(t, res, 0)
assert.Equal(t, value, err)
}
func TestPromiseAsyncResolve(t *testing.T) {
fn := Async(func(this js.Value, args []js.Value) (js.Value, error) {
return js.ValueOf(1), nil
})
prom := PromiseValue(fn.Invoke())
res, err := Await(prom)
require.NoError(t, err)
require.Len(t, res, 1)
assert.Equal(t, js.ValueOf(1), res[0])
}
func TestPromiseAsyncReject(t *testing.T) {
fn := Async(func(this js.Value, args []js.Value) (js.Value, error) {
return js.Undefined(), fmt.Errorf("rejected")
})
prom := PromiseValue(fn.Invoke())
_, err := Await(prom)
require.NotNil(t, err)
assert.Equal(t, "Error: rejected", err.Error())
}