|
| 1 | +-- ROBLOX upstream: https://github.com/testing-library/react-testing-library/blob/v12.1.5/src/act-compat.js |
| 2 | +local Packages = script.Parent.Parent |
| 3 | + |
| 4 | +local LuauPolyfill = require(Packages.LuauPolyfill) |
| 5 | +local console = LuauPolyfill.console |
| 6 | +type Promise<T> = LuauPolyfill.Promise<T> |
| 7 | + |
| 8 | +local Promise = require(Packages.Promise) |
| 9 | + |
| 10 | +local exports = {} |
| 11 | + |
| 12 | +-- local React = require(Packages.React) |
| 13 | +-- local ReactDOM = require(Packages["react-dom"]).default |
| 14 | +local testUtils = require(script.Parent.jsHelpers["react-dom"]["test-utils"]) |
| 15 | +local reactAct = testUtils.act |
| 16 | +local actSupported = reactAct ~= nil |
| 17 | + |
| 18 | +-- act is supported react-dom@16.8.0 |
| 19 | +-- so for versions that don't have act from test utils |
| 20 | +-- we do this little polyfill. No warnings, but it's |
| 21 | +-- better than nothing. |
| 22 | +-- ROBLOX deviation START: don't polyfill |
| 23 | +-- local function actPolyfill(cb) |
| 24 | +-- ReactDOM:unstable_batchedUpdates(cb) |
| 25 | +-- ReactDOM:render(React.createElement("Frame", nil), Instance.new("Frame")) |
| 26 | +-- end |
| 27 | + |
| 28 | +local act = reactAct |
| 29 | +-- ROBLOX deviation END |
| 30 | + |
| 31 | +local youHaveBeenWarned = false |
| 32 | +local isAsyncActSupported = nil |
| 33 | + |
| 34 | +local function asyncAct(cb) |
| 35 | + if actSupported == true then |
| 36 | + if isAsyncActSupported == nil then |
| 37 | + return Promise.new(function(resolve, reject) |
| 38 | + -- patch console.error here |
| 39 | + local originalConsoleError = console.error |
| 40 | + console.error = function(...: any) |
| 41 | + local args = table.pack(...) |
| 42 | + --[[ if console.error fired *with that specific message* ]] |
| 43 | + --[[ istanbul ignore next ]] |
| 44 | + local firstArgIsString = typeof(args[1]) == "string" |
| 45 | + if |
| 46 | + firstArgIsString |
| 47 | + and string.find( |
| 48 | + args[ |
| 49 | + 1 --[[ ROBLOX adaptation: added 1 to array index ]] |
| 50 | + ], |
| 51 | + "Warning: Do not await the result of calling ReactTestUtils.act" |
| 52 | + ) |
| 53 | + == 1 |
| 54 | + then |
| 55 | + -- v16.8.6 |
| 56 | + isAsyncActSupported = false |
| 57 | + elseif |
| 58 | + firstArgIsString |
| 59 | + and string.find( |
| 60 | + args[1], |
| 61 | + "Warning: The callback passed to ReactTestUtils.act(...) function must not return anything" |
| 62 | + ) |
| 63 | + == 1 |
| 64 | + then |
| 65 | + -- no-op |
| 66 | + else |
| 67 | + originalConsoleError(...) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + local cbReturn, result |
| 72 | + local ok, err = pcall(function() |
| 73 | + result = reactAct(function() |
| 74 | + cbReturn = cb() |
| 75 | + return cbReturn |
| 76 | + end) |
| 77 | + end) |
| 78 | + |
| 79 | + if not ok then |
| 80 | + console.error = originalConsoleError |
| 81 | + reject(err) |
| 82 | + return |
| 83 | + end |
| 84 | + |
| 85 | + result:andThen(function() |
| 86 | + console.error = originalConsoleError |
| 87 | + -- if it got here, it means async act is supported |
| 88 | + isAsyncActSupported = true |
| 89 | + resolve() |
| 90 | + end, function(err) |
| 91 | + console.error = originalConsoleError |
| 92 | + isAsyncActSupported = true |
| 93 | + reject(err) |
| 94 | + end) |
| 95 | + |
| 96 | + -- 16.8.6's act().then() doesn't call a resolve handler, so we need to manually flush here, sigh |
| 97 | + |
| 98 | + if isAsyncActSupported == false then |
| 99 | + console.error = originalConsoleError |
| 100 | + --[[ istanbul ignore next ]] |
| 101 | + if not youHaveBeenWarned then |
| 102 | + -- if act is supported and async act isn't and they're trying to use async |
| 103 | + -- act, then they need to upgrade from 16.8 to 16.9. |
| 104 | + -- This is a seamless upgrade, so we'll add a warning |
| 105 | + console.error( |
| 106 | + 'It looks like you\'re using a version of react-dom that supports the "act" function, but not an awaitable version of "act" which you will need. Please upgrade to at least react-dom@16.9.0 to remove this warning.' |
| 107 | + ) |
| 108 | + youHaveBeenWarned = true |
| 109 | + end |
| 110 | + |
| 111 | + cbReturn:andThen(function() |
| 112 | + -- a faux-version. |
| 113 | + -- todo - copy https://github.com/facebook/react/blob/master/packages/shared/enqueueTask.js |
| 114 | + Promise.resolve():andThen(function() |
| 115 | + -- use sync act to flush effects |
| 116 | + act(function() end) |
| 117 | + resolve() |
| 118 | + end) |
| 119 | + end, reject) |
| 120 | + end |
| 121 | + end) |
| 122 | + elseif isAsyncActSupported == false then |
| 123 | + -- use the polyfill directly |
| 124 | + local result: Promise<any> |
| 125 | + act(function() |
| 126 | + result = cb() :: any |
| 127 | + end) |
| 128 | + return result:andThen(function() |
| 129 | + return (Promise.resolve() :: Promise<any>):andThen(function() |
| 130 | + -- use sync act to flush effects |
| 131 | + act(function() end) |
| 132 | + end) |
| 133 | + end) :: any |
| 134 | + end |
| 135 | + -- all good! regular act |
| 136 | + return act(cb) |
| 137 | + end |
| 138 | + |
| 139 | + -- use the polyfill |
| 140 | + local result: Promise<any> |
| 141 | + act(function() |
| 142 | + result = cb() :: any |
| 143 | + end) |
| 144 | + return result:andThen(function() |
| 145 | + return Promise.resolve():andThen(function() |
| 146 | + -- use sync act to flush effects |
| 147 | + act(function() end) |
| 148 | + end) |
| 149 | + end) |
| 150 | +end |
| 151 | + |
| 152 | +exports.default = act |
| 153 | +exports.asyncAct = asyncAct |
| 154 | + |
| 155 | +--[[ eslint no-console:0 ]] |
| 156 | +return exports |
0 commit comments