-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathReactHooksInspection.spec.luau
More file actions
495 lines (494 loc) · 14.3 KB
/
ReactHooksInspection.spec.luau
File metadata and controls
495 lines (494 loc) · 14.3 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
-- ROBLOX upstream: https://github.com/facebook/react/blob/v17.0.2/packages/react-debug-tools/src/__tests__/ReactHooksInspection-test.js
--[[*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails react-core
* @jest-environment node
]]
-- ROBLOX deviation START: not needed
-- local LuauPolyfill = require("@pkg/@jsdotlua/luau-polyfill")
-- local Boolean = LuauPolyfill.Boolean
-- ROBLOX deviation END
local JestGlobals = require("@pkg/@jsdotlua/jest-globals")
local beforeEach = JestGlobals.beforeEach
local describe = JestGlobals.describe
local expect = JestGlobals.expect
local it = JestGlobals.it
local jest = JestGlobals.jest
local React
local ReactDebugTools
describe("ReactHooksInspection", function()
beforeEach(function()
jest.resetModules()
-- ROBLOX deviation START: fix requires
-- React = require_("react")
-- ReactDebugTools = require_("react-debug-tools")
React = require("@pkg/@jsdotlua/react")
ReactDebugTools = require("@pkg/@jsdotlua/react-debug-tools")
-- ROBLOX deviation END
end)
it("should inspect a simple useState hook", function()
local function Foo(props)
-- ROBLOX deviation START: useState returns 2 values
-- local state = React.useState("hello world")[1]
local state = React.useState("hello world")
-- ROBLOX deviation END
return React.createElement("div", nil, state)
end
-- ROBLOX deviation START: use dot notation
-- local tree = ReactDebugTools:inspectHooks(Foo, {})
local tree = ReactDebugTools.inspectHooks(Foo, {})
-- ROBLOX deviation END
expect(tree).toEqual({
{
isStateEditable = true,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 0,
id = 1,
-- ROBLOX deviation END
name = "State",
value = "hello world",
subHooks = {},
},
})
end)
it("should inspect a simple custom hook", function()
local function useCustom(value)
local state = React.useState(value)[1]
React.useDebugValue("custom hook label")
return state
end
local function Foo(props)
local value = useCustom("hello world")
return React.createElement("div", nil, value)
end
-- ROBLOX deviation START: use dot notation
-- local tree = ReactDebugTools:inspectHooks(Foo, {})
local tree = ReactDebugTools.inspectHooks(Foo, {})
-- ROBLOX deviation END
expect(tree).toEqual({
{
isStateEditable = false,
id = nil,
name = "Custom",
-- ROBLOX deviation START: use _G.__DEV__
-- value = if Boolean.toJSBoolean(__DEV__) then "custom hook label" else nil,
value = if _G.__DEV__ then "custom hook label" else nil,
-- ROBLOX deviation END
subHooks = {
{
isStateEditable = true,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 0,
id = 1,
-- ROBLOX deviation END
name = "State",
value = "hello world",
subHooks = {},
},
},
},
})
end)
it("should inspect a tree of multiple hooks", function()
local function effect() end
local function useCustom(value)
-- ROBLOX deviation START: useState returns 2 values
-- local state = React.useState(value)[1]
local state = React.useState(value)
-- ROBLOX deviation END
React.useEffect(effect)
return state
end
local function Foo(props)
local value1 = useCustom("hello")
local value2 = useCustom("world")
return React.createElement("div", nil, value1, " ", value2)
end
-- ROBLOX deviation START: use dot notation
-- local tree = ReactDebugTools:inspectHooks(Foo, {})
local tree = ReactDebugTools.inspectHooks(Foo, {})
-- ROBLOX deviation END
expect(tree).toEqual({
{
isStateEditable = false,
id = nil,
name = "Custom",
value = nil,
subHooks = {
{
isStateEditable = true,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 0,
id = 1,
-- ROBLOX deviation END
name = "State",
subHooks = {},
-- ROBLOX deviation START: tell Luau to type this field loosely
value = "hello" :: any,
-- ROBLOX deviation END
},
{
isStateEditable = false,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 1,
id = 2,
-- ROBLOX deviation END
name = "Effect",
subHooks = {},
value = effect,
},
},
},
{
isStateEditable = false,
id = nil,
name = "Custom",
value = nil,
subHooks = {
{
isStateEditable = true,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 2,
id = 3,
-- ROBLOX deviation END
name = "State",
-- ROBLOX deviation START: Luau doesn't support mixed arrays
-- value = "world",
value = "world" :: any,
-- ROBLOX deviation END
subHooks = {},
},
{
isStateEditable = false,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 3,
id = 4,
-- ROBLOX deviation END
name = "Effect",
value = effect,
subHooks = {},
},
},
},
})
end)
it("should inspect a tree of multiple levels of hooks", function()
local function effect() end
local function useCustom(value)
local state = React.useReducer(function(s, a)
return s
-- ROBLOX deviation START: useReducer returns 2 values
-- end, value)[1]
end, value)
-- ROBLOX deviation END
React.useEffect(effect)
return state
end
local function useBar(value)
local result = useCustom(value)
React.useLayoutEffect(effect)
return result
end
local function useBaz(value)
React.useLayoutEffect(effect)
local result = useCustom(value)
return result
end
local function Foo(props)
local value1 = useBar("hello")
local value2 = useBaz("world")
return React.createElement("div", nil, value1, " ", value2)
end
-- ROBLOX deviation START: use dot notation
-- local tree = ReactDebugTools:inspectHooks(Foo, {})
local tree = ReactDebugTools.inspectHooks(Foo, {})
-- ROBLOX deviation END
expect(tree).toEqual({
{
isStateEditable = false,
-- ROBLOX deviation START: tell Luau to type this field loosely
id = nil :: number?,
-- ROBLOX deviation END
name = "Bar",
value = nil,
subHooks = {
{
isStateEditable = false,
-- ROBLOX deviation START: Luau doesn't support mixed arrays
-- id = nil,
id = nil :: number | nil,
-- ROBLOX deviation END
name = "Custom",
-- ROBLOX deviation START: Luau doesn't support mixed arrays
-- value = nil,
value = nil :: any,
-- ROBLOX deviation END
subHooks = {
{
isStateEditable = true,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 0,
id = 1,
-- ROBLOX deviation END
name = "Reducer",
-- ROBLOX deviation START: Luau doesn't support mixed arrays
-- value = "hello",
value = "hello" :: any,
-- ROBLOX deviation END
subHooks = {},
},
{
isStateEditable = false,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 1,
id = 2,
-- ROBLOX deviation END
name = "Effect",
value = effect,
subHooks = {},
},
},
},
{
isStateEditable = false,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 2,
id = 3,
-- ROBLOX deviation END
name = "LayoutEffect",
value = effect,
subHooks = {},
},
},
},
{
isStateEditable = false,
id = nil,
name = "Baz",
value = nil,
subHooks = {
{
isStateEditable = false,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 3,
id = 4 :: number?,
-- ROBLOX deviation END
name = "LayoutEffect",
-- ROBLOX deviation START: Luau doesn't support mixed arrays
-- value = effect,
value = effect :: any,
-- ROBLOX deviation END
subHooks = {},
},
{
isStateEditable = false,
id = nil,
name = "Custom",
subHooks = {
{
isStateEditable = true,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 4,
id = 5,
-- ROBLOX deviation END
name = "Reducer",
subHooks = {},
-- ROBLOX deviation START: Luau doesn't support mixed arrays
-- value = "world",
value = "world" :: any,
-- ROBLOX deviation END
},
{
isStateEditable = false,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 5,
id = 6,
-- ROBLOX deviation END
name = "Effect",
subHooks = {},
value = effect,
},
},
value = nil,
},
},
},
})
end)
it("should inspect the default value using the useContext hook", function()
local MyContext = React.createContext("default")
local function Foo(props)
local value = React.useContext(MyContext)
return React.createElement("div", nil, value)
end
-- ROBLOX deviation START: use dot notation
-- local tree = ReactDebugTools:inspectHooks(Foo, {})
local tree = ReactDebugTools.inspectHooks(Foo, {})
-- ROBLOX deviation END
expect(tree).toEqual({
{
isStateEditable = false,
id = nil,
name = "Context",
value = "default",
subHooks = {},
},
})
end)
it("should support an injected dispatcher", function()
local function Foo(props)
-- ROBLOX deviation START: useState returns 2 values
-- local state = React.useState("hello world")[1]
local state = React.useState("hello world")
-- ROBLOX deviation END
return React.createElement("div", nil, state)
end
local initial = {}
local current = initial
local getterCalls = 0
local setterCalls = {}
-- ROBLOX deviation START: implement getter and setter
-- local FakeDispatcherRef = {
-- current = function(self)
-- getterCalls += 1
-- return current
-- end,
-- current = function(self, value)
-- table.insert(setterCalls, value) --[[ ROBLOX CHECK: check if 'setterCalls' is an Array ]]
-- current = value
-- end,
-- }
local FakeDispatcherRef = setmetatable({
__getters = {
current = function(self)
print("getting current")
getterCalls += 1
return current
end,
},
__setters = {
current = function(self, value)
print("setting current", value)
table.insert(setterCalls, value)
current = value
end,
},
}, {
__index = function(self, key)
if typeof(self.__getters[key]) == "function" then
return self.__getters[key](self)
else
return nil
end
end,
__newindex = function(self, key, value)
if typeof(self.__setters[key]) == "function" then
return self.__setters[key](self, value)
else
return nil
end
end,
}) :: any
-- ROBLOX deviation END
-- ROBLOX deviation START: aligned to React 18 so we get a hot path optimization in upstream
-- expect(function()
-- ReactDebugTools:inspectHooks(Foo, {}, FakeDispatcherRef)
-- end).toThrow(
-- "Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for"
-- .. " one of the following reasons:\n"
-- .. "1. You might have mismatching versions of React and the renderer (such as React DOM)\n"
-- .. "2. You might be breaking the Rules of Hooks\n"
-- .. "3. You might have more than one copy of React in the same app\n"
-- .. "See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem."
-- )
local didCatch = false
expect(function()
-- mock the Error constructor to check the internal of the error instance
expect(function()
ReactDebugTools.inspectHooks(Foo, {}, FakeDispatcherRef)
end).toThrow(
-- ROBLOX NOTE: Lua-specific error on nil deref
"attempt to index nil with 'useState'"
)
didCatch = true
end).toErrorDev(
"Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for"
.. " one of the following reasons:\n"
.. "1. You might have mismatching versions of React and the renderer (such as React DOM)\n"
.. "2. You might be breaking the Rules of Hooks\n"
.. "3. You might have more than one copy of React in the same app\n"
.. "See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.",
{ withoutStack = true }
)
-- avoid false positive if no error was thrown at all
expect(didCatch).toBe(true)
-- ROBLOX deviation END
expect(getterCalls).toBe(1)
expect(setterCalls).toHaveLength(2)
expect(setterCalls[
1 --[[ ROBLOX adaptation: added 1 to array index ]]
-- ROBLOX deviation START: use never instead of ["not"]
-- ])["not"].toBe(initial)
]).never.toBe(initial)
-- ROBLOX deviation END
expect(setterCalls[
2 --[[ ROBLOX adaptation: added 1 to array index ]]
]).toBe(initial)
end)
describe("useDebugValue", function()
it("should be ignored when called outside of a custom hook", function()
local function Foo(props)
React.useDebugValue("this is invalid")
return nil
end
-- ROBLOX deviation START: use dot notation
-- local tree = ReactDebugTools:inspectHooks(Foo, {})
local tree = ReactDebugTools.inspectHooks(Foo, {})
-- ROBLOX deviation END
expect(tree).toHaveLength(0)
end)
it("should support an optional formatter function param", function()
local function useCustom()
React.useDebugValue({ bar = 123 }, function(object)
return ("bar:%s"):format(tostring(object.bar))
end)
React.useState(0)
end
local function Foo(props)
useCustom()
return nil
end
-- ROBLOX deviation START: use dot notation
-- local tree = ReactDebugTools:inspectHooks(Foo, {})
local tree = ReactDebugTools.inspectHooks(Foo, {})
-- ROBLOX deviation END
expect(tree).toEqual({
{
isStateEditable = false,
id = nil,
name = "Custom",
-- ROBLOX deviation START: use _G.__DEV__
-- value = if Boolean.toJSBoolean(__DEV__) then "bar:123" else nil,
value = if _G.__DEV__ then "bar:123" else nil,
-- ROBLOX deviation END
subHooks = {
{
isStateEditable = true,
-- ROBLOX deviation START: adjust for 1-based indexing
-- id = 0,
id = 1,
-- ROBLOX deviation END
name = "State",
subHooks = {},
value = 0,
},
},
},
})
end)
end)
end)