-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathworklet-tests.ts
More file actions
251 lines (250 loc) · 7.32 KB
/
worklet-tests.ts
File metadata and controls
251 lines (250 loc) · 7.32 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
import { worklet, Worklets } from "react-native-worklets-core";
import { ExpectException, ExpectValue, getWorkletInfo } from "./utils";
export const worklet_tests = {
check_is_not_worklet: () => {
const fn = Worklets.defaultContext.createRunAsync((a: number) => {
return a * 200;
});
return ExpectValue(typeof fn, "function");
},
check_worklet_closure: () => {
const x = 100;
const w = (a: number) => {
"worklet";
return a + x;
};
const { closure } = getWorkletInfo(w);
return ExpectValue(closure, { x: 100 });
},
call_nested_worklet: () => {
const rootWorklet = () => {
"worklet";
const cl = { x: 100 };
const nestedWorklet = (c: number) => {
"worklet";
return c + cl.x;
};
const nestedWorkletFn =
Worklets.defaultContext.createRunAsync(nestedWorklet);
return nestedWorkletFn(100);
};
const fw = () => {
"worklet";
return rootWorklet();
};
let wf = Worklets.defaultContext.createRunAsync(fw);
return ExpectValue(wf(), 200);
},
check_worklet_closure_shared_value: () => {
const x = Worklets.createSharedValue(1000);
const w = (a: number) => {
"worklet";
return a + x.value;
};
const { closure } = getWorkletInfo(w);
return ExpectValue(closure, { x: { value: 1000 } });
},
check_worklet_code: () => {
const w = (a: number) => {
"worklet";
return a;
};
const { code } = getWorkletInfo(w);
return ExpectValue(code, "function anonymous(a) {\n return a;\n}");
},
check_share_object_with_js_function_fails_on_call: () => {
const api = {
someFunc: (a: number) => a + 100,
};
const f = () => {
"worklet";
return api.someFunc(100);
};
const w = Worklets.defaultContext.createRunAsync(f);
return ExpectException(w);
},
check_js_promise_resolves: () => {
const f = (a: number) => {
"worklet";
return Promise.resolve(a + 100);
};
return ExpectValue(f(100), 200);
},
check_c_promise_resolves_from_context: () => {
const f = (a: number) => {
"worklet";
return a + 100;
};
const w = Worklets.defaultContext.createRunAsync(f);
return ExpectValue(
new Promise((resolve) => {
w(100).then(resolve);
}),
200
);
},
check_finally_is_called: () => {
const f = (a: number) => {
"worklet";
return a + 100;
};
const w = Worklets.defaultContext.createRunAsync(f);
return ExpectValue(
new Promise<void>((resolve) => {
w(100).finally(resolve);
}),
undefined
);
},
check_then_with_empty_args: () => {
const f = (a: number) => {
"worklet";
return a + 100;
};
const w = Worklets.defaultContext.createRunAsync(f);
return ExpectValue(
new Promise<void>((resolve) => {
w(100).then().finally(resolve);
}),
undefined
);
},
check_pure_array_is_passed_as_array: () => {
const array = [0, 1, 2, 3, 4];
const f = () => {
"worklet";
return Array.isArray(array);
};
const w = Worklets.defaultContext.createRunAsync(f);
return ExpectValue(w(), true);
},
check_thread_id_exists: () => {
const threadId = Worklets.getCurrentThreadId();
return ExpectValue(Number.isSafeInteger(threadId), true);
},
check_thread_id_consecutive_calls_are_equal: () => {
const first = Worklets.getCurrentThreadId();
const second = Worklets.getCurrentThreadId();
return ExpectValue(first, second);
},
check_thread_id_consecutive_calls_in_worklet_are_equal: async () => {
const [first, second] = await Worklets.defaultContext.runAsync(() => {
"worklet";
const firstId = Worklets.getCurrentThreadId();
const secondId = Worklets.getCurrentThreadId();
return [firstId, secondId];
});
return ExpectValue(first, second);
},
check_thread_ids_are_different: async () => {
const jsThreadId = Worklets.getCurrentThreadId();
const workletThreadId = await Worklets.defaultContext.runAsync(() => {
"worklet";
return Worklets.getCurrentThreadId();
});
return await ExpectValue(jsThreadId !== workletThreadId, true);
},
check_pure_array_is_passed_as_jsi_array: () => {
const array = [0, 1, 2, 3, 4];
const f = () => {
"worklet";
return Worklets.__jsi_is_array(array);
};
const w = Worklets.defaultContext.createRunAsync(f);
return ExpectValue(w(), true);
},
check_pure_array_inside_object_is_passed_as_jsi_array: () => {
const obj = { a: [0, 1, 2, 3, 4] };
const f = () => {
"worklet";
return Worklets.__jsi_is_array(obj.a);
};
const w = Worklets.defaultContext.createRunAsync(f);
return ExpectValue(w(), true);
},
check_pure_array_nested_argument_is_passed_as_jsi_array: () => {
const obj = { a: [0, 1, 2, 3, 4] };
const f = (t: typeof obj) => {
"worklet";
return Worklets.__jsi_is_array(t.a);
};
const w = Worklets.defaultContext.createRunAsync(f);
return ExpectValue(w(obj), true);
},
check_shared_value_array_is_not_passed_as_jsi_array: () => {
const obj = Worklets.createSharedValue([0, 1, 2, 3, 4]);
const f = () => {
"worklet";
return Worklets.__jsi_is_array(obj.value);
};
const w = Worklets.defaultContext.createRunAsync(f);
return ExpectValue(w(), false);
},
check_shared_value_nested_array_is_not_passed_as_jsi_array: () => {
const obj = Worklets.createSharedValue({ a: [0, 1, 2, 3, 4] });
const f = () => {
"worklet";
return Worklets.__jsi_is_array(obj.value.a);
};
const w = Worklets.defaultContext.createRunAsync(f);
return ExpectValue(w(), false);
},
check_called_directly: () => {
const result = Worklets.defaultContext.runAsync(() => {
"worklet";
return 42;
});
return ExpectValue(result, 42);
},
check_jsi_object_is_spreadable_after_worklet: async () => {
const func = () => {
"worklet";
return { a: 100 };
};
const result = Worklets.defaultContext.runAsync(func);
const spreadObject = { ...result };
return ExpectValue(spreadObject, { a: 100 });
},
check_jsi_object_is_assignable_after_worklet: async () => {
const func = () => {
"worklet";
return { a: 100 };
};
const result = Worklets.defaultContext.runAsync(func);
const assignedObject = Object.assign({}, result);
return ExpectValue(assignedObject, { a: 100 });
},
check_jsi_object_is_spreadable_inside_worklet: async () => {
const func = () => {
"worklet";
const testObject = { a: 100, b: "200" };
return { ...testObject };
};
const result = Worklets.defaultContext.runAsync(func);
return ExpectValue(result, { a: 100, b: "200" });
},
check_jsi_object_is_returned_from_worklet: async () => {
const func = () => {
"worklet";
return { a: 100, b: "200" };
};
const result = Worklets.defaultContext.runAsync(func);
return ExpectValue(result, { a: 100, b: "200" });
},
check_worklet_checker_works: () => {
const func = () => {
"worklet";
return 42;
};
const same = worklet(func);
return ExpectValue(same, func);
},
check_worklet_checker_throws_invalid_worklet: () => {
const func = () => {
return "not a worklet";
};
return ExpectException(() => {
worklet(func);
});
},
};