-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjest.res
More file actions
193 lines (171 loc) · 4.78 KB
/
jest.res
File metadata and controls
193 lines (171 loc) · 4.78 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
open Ink
@module
external ms: float => string = "ms"
module PQueue = {
type t
type options
@obj external makeOptions: (~concurrency: int=?, unit) => options = ""
@module("p-queue") @new
external make: options => t = "default"
@inline
let make = (~concurrency=?, ()) => make(makeOptions(~concurrency?, ()))
@send
external add: (t, unit => promise<'a>) => unit = "add"
}
let delayFloat = ms => {
Js.Promise2.make((~resolve, ~reject as _reject) => {
Js.Global.setTimeoutFloat(() => {
resolve(. ())
}, ms)->ignore
})
}
let paths = [
"tests/login.js",
"tests/signup.js",
"tests/forgot-password.js",
"tests/reset-password.js",
"tests/view-profile.js",
"tests/edit-profile.js",
"tests/delete-profile.js",
"tests/posts.js",
"tests/post.js",
"tests/comments.js",
]
module Test = {
let getBackgroundColor = status => {
switch status {
| #runs => Some(#yellow)
| #pass => Some(#green)
| #fail => Some(#red)
| _ => None
}
}
@react.component
let make = (~status: [#runs | #pass | #fail], ~path) => {
open Js
<Box>
<Text color=#black backgroundColor=?{getBackgroundColor(status)}>
{React.string(" ")}
{React.string((status :> string)->String2.toUpperCase)}
{React.string(" ")}
</Text>
<Box marginLeft={1}>
<Text dimColor=true>
{React.string(path->String2.split("/")->Belt.Array.getExn(0))}
{React.string("/")}
</Text>
<Text bold=true color=#white>
{React.string(path->String2.split("/")->Belt.Array.getExn(1))}
</Text>
</Box>
</Box>
}
}
module Summary = {
@react.component
let make = (~isFinished, ~passed, ~failed, ~time) => {
<Box flexDirection=#column marginTop={1}>
<Box>
<Box width={#length(14)}>
<Text bold=true> {React.string("Test Suites:")} </Text>
</Box>
{switch failed > 0 {
| true =>
<Text bold=true color=#red>
{React.int(failed)}
{React.string(" failed, ")}
</Text>
| false => React.null
}}
{switch passed > 0 {
| true =>
<Text bold=true color=#green>
{React.int(passed)}
{React.string(" passed, ")}
</Text>
| false => React.null
}}
<Text>
{React.int(passed + failed)}
{React.string(" total")}
</Text>
</Box>
<Box>
<Box width={#length(14)}>
<Text bold=true> {React.string("Time:")} </Text>
</Box>
<Text> {React.string(time)} </Text>
</Box>
{switch isFinished {
| true =>
<Box>
<Text dimColor=true> {React.string("Ran all test suites.")} </Text>
</Box>
| false => React.null
}}
</Box>
}
}
module Jest = {
@react.component
let make = () => {
let (startTime, setStartTime) = React.useState(() => 0.0)
let (completedTests, setCompletedTests) = React.useState(() => [])
let (runningTests, setRunningTests) = React.useState(() => [])
let runTest = async path => {
setRunningTests(previousTests =>
previousTests->Belt.Array.concat([
{
"status": #runs,
"path": path,
},
])
)
await delayFloat(1000.0 *. Js.Math.random())
setRunningTests(previousTests =>
previousTests->Belt.Array.keep(test => test["path"] !== path)
)
setCompletedTests(previousTests => {
previousTests->Belt.Array.concat([
{
"status": Js.Math.random() < 0.5 ? #pass : #fail,
"path": path,
},
])
})
}
React.useEffect0(() => {
setStartTime(_ => Js.Date.now())
let queue = PQueue.make(~concurrency=4, ())
paths->Belt.Array.forEach(path => {
queue->PQueue.add(() => runTest(path))
})
None
})
<Box flexDirection=#column>
<Static
items={completedTests}
renderItems={(test, _) =>
<Test key={test["path"]} status={test["status"]} path={test["path"]} />}
/>
{switch runningTests->Js.Array2.length > 0 {
| true =>
<Box flexDirection=#column marginTop={1}>
{React.array(
runningTests->Belt.Array.map(test => {
<Test key={test["path"]} status={test["status"]} path={test["path"]} />
}),
)}
</Box>
| false => React.null
}}
<Summary
isFinished={runningTests->Js.Array2.length == 0}
passed={completedTests->Belt.Array.keep(test => test["status"] == #pass)->Js.Array2.length}
failed={completedTests->Belt.Array.keep(test => test["status"] == #fail)->Js.Array2.length}
time={ms(Js.Date.now() -. startTime)}
/>
</Box>
}
}
let _ = render(<Jest />, ())