|
| 1 | +-- ROBLOX upstream: https://github.com/testing-library/react-testing-library/blob/v12.1.5/src/__tests__/cleanup.js |
| 2 | +return function() |
| 3 | + local Packages = script.Parent.Parent.Parent |
| 4 | + |
| 5 | + local JestGlobals = require(Packages.Dev.JestGlobals) |
| 6 | + local jestExpect = JestGlobals.expect |
| 7 | + local jest = JestGlobals.jest |
| 8 | + |
| 9 | + local LuauPolyfill = require(Packages.LuauPolyfill) |
| 10 | + local console = LuauPolyfill.console |
| 11 | + local setTimeout = LuauPolyfill.setTimeout |
| 12 | + |
| 13 | + local document = require(Packages.DomTestingLibrary).document |
| 14 | + local Promise = require(Packages.Promise) |
| 15 | + local getElementByName = require(script.Parent.Parent.jsHelpers.Element).getElementByName |
| 16 | + |
| 17 | + local React = require(Packages.React) |
| 18 | + |
| 19 | + local ParentModule = require(script.Parent.Parent)(afterEach) |
| 20 | + local render = ParentModule.render |
| 21 | + local cleanup = ParentModule.cleanup |
| 22 | + |
| 23 | + it("cleans up the document", function() |
| 24 | + local spy = jest.fn() |
| 25 | + local divId = "my-div" |
| 26 | + |
| 27 | + local Test = React.Component:extend("Test") |
| 28 | + |
| 29 | + function Test:componentWillUnmount() |
| 30 | + jestExpect(getElementByName(document, divId)).toBeInTheDocument() |
| 31 | + spy() |
| 32 | + end |
| 33 | + |
| 34 | + function Test:render() |
| 35 | + return React.createElement("Frame", { Name = divId }) |
| 36 | + end |
| 37 | + |
| 38 | + render(React.createElement(Test, nil)) |
| 39 | + |
| 40 | + cleanup() |
| 41 | + |
| 42 | + jestExpect(document).toBeEmptyDOMElement() |
| 43 | + jestExpect(spy).toHaveBeenCalledTimes(1) |
| 44 | + end) |
| 45 | + |
| 46 | + it("cleanup does not error when an element is not a child", function() |
| 47 | + render(React.createElement("Frame", nil), { container = Instance.new("Frame") }) |
| 48 | + cleanup() |
| 49 | + end) |
| 50 | + |
| 51 | + it("cleanup runs effect cleanup functions", function() |
| 52 | + local spy = jest.fn() |
| 53 | + |
| 54 | + local function Test() |
| 55 | + React.useEffect(function() |
| 56 | + spy() |
| 57 | + end) |
| 58 | + return nil |
| 59 | + end |
| 60 | + |
| 61 | + render(React.createElement(Test, nil)) |
| 62 | + cleanup() |
| 63 | + jestExpect(spy).toHaveBeenCalledTimes(1) |
| 64 | + end) |
| 65 | + |
| 66 | + describe("fake timers and missing act warnings", function() |
| 67 | + local originalConsoleError = console.error |
| 68 | + beforeEach(function() |
| 69 | + jest.resetAllMocks() |
| 70 | + -- ROBLOX deviation START: replace spyOn |
| 71 | + console.error = jest.fn(function() |
| 72 | + -- assert messages explicitly |
| 73 | + end) |
| 74 | + -- ROBLOX deviation END |
| 75 | + jest.useFakeTimers() |
| 76 | + end) |
| 77 | + |
| 78 | + afterEach(function() |
| 79 | + -- ROBLOX deviation START: replace spyOn |
| 80 | + console.error = originalConsoleError |
| 81 | + -- ROBLOX deviation END |
| 82 | + jest.useRealTimers() |
| 83 | + end) |
| 84 | + |
| 85 | + it("cleanup does not flush microtasks", function() |
| 86 | + local microTaskSpy = jest.fn() |
| 87 | + local function Test() |
| 88 | + local counter = 1 |
| 89 | + local _, setDeferredCounter = React.useState(nil :: number?) |
| 90 | + React.useEffect(function() |
| 91 | + local cancelled = false |
| 92 | + -- ROBLOX deviation START: Lua Promise.resolve is not scheduled. Must use delay(0) for the same behavior |
| 93 | + Promise.delay(0):andThen(function() |
| 94 | + microTaskSpy() |
| 95 | + -- eslint-disable-next-line jest/no-if -- false positive |
| 96 | + if not cancelled then |
| 97 | + setDeferredCounter(counter) |
| 98 | + end |
| 99 | + end) |
| 100 | + |
| 101 | + return function() |
| 102 | + cancelled = true |
| 103 | + end |
| 104 | + end, { counter }) |
| 105 | + |
| 106 | + return nil |
| 107 | + end |
| 108 | + render(React.createElement(Test, nil)) |
| 109 | + |
| 110 | + cleanup() |
| 111 | + |
| 112 | + jestExpect(microTaskSpy).toHaveBeenCalledTimes(0) |
| 113 | + -- console.error is mocked |
| 114 | + -- eslint-disable-next-line no-console |
| 115 | + |
| 116 | + -- ROBLOX deviation START: React.version not available, but will stick to React 17 for now |
| 117 | + jestExpect(console.error).toHaveBeenCalledTimes( |
| 118 | + -- ReactDOM.render is deprecated in React 18 |
| 119 | + 0 |
| 120 | + ) |
| 121 | + -- ROBLOX deviation END |
| 122 | + end) |
| 123 | + |
| 124 | + it("cleanup does not swallow missing act warnings", function() |
| 125 | + local deferredStateUpdateSpy = jest.fn() |
| 126 | + local function Test() |
| 127 | + local counter = 1 |
| 128 | + local _, setDeferredCounter = React.useState(nil :: number?) |
| 129 | + React.useEffect(function() |
| 130 | + local cancelled = false |
| 131 | + setTimeout(function() |
| 132 | + deferredStateUpdateSpy() |
| 133 | + if not cancelled then |
| 134 | + setDeferredCounter(counter) |
| 135 | + end |
| 136 | + end, 0) |
| 137 | + |
| 138 | + return function() |
| 139 | + cancelled = true |
| 140 | + end |
| 141 | + end, { counter }) |
| 142 | + |
| 143 | + return nil |
| 144 | + end |
| 145 | + render(React.createElement(Test, nil)) |
| 146 | + task.wait() |
| 147 | + |
| 148 | + jest.runAllTimers() |
| 149 | + cleanup() |
| 150 | + |
| 151 | + jestExpect(deferredStateUpdateSpy).toHaveBeenCalledTimes(1) |
| 152 | + -- console.error is mocked |
| 153 | + -- eslint-disable-next-line no-console |
| 154 | + -- ROBLOX deviation START: React.version not available, but will stick to React 17 for now |
| 155 | + jestExpect(console.error).toHaveBeenCalledTimes( |
| 156 | + -- ReactDOM.render is deprecated in React 18 |
| 157 | + 1 |
| 158 | + ) |
| 159 | + -- eslint-disable-next-line no-console |
| 160 | + jestExpect((console.error :: any).mock.calls[ |
| 161 | + 1 -- ReactDOM.render is deprecated in React 18 |
| 162 | + ][1]).toMatch("a test was not wrapped in act(...)") |
| 163 | + -- ROBLOX deviation END |
| 164 | + end) |
| 165 | + end) |
| 166 | +end |
0 commit comments