|
1 | 1 | -- ROBLOX upstream: https://github.com/testing-library/react-testing-library/blob/v12.1.5/src/__tests__/act.js |
2 | | -return function() |
3 | | - local Packages = script.Parent.Parent.Parent |
| 2 | +local Packages = script.Parent.Parent.Parent |
4 | 3 |
|
5 | | - local JestGlobals = require(Packages.Dev.JestGlobals) |
6 | | - local jestExpect = JestGlobals.expect |
7 | | - local jest = JestGlobals.jest |
| 4 | +local JestGlobals = require(Packages.JestGlobals) |
| 5 | +local expect = JestGlobals.expect |
| 6 | +local test = JestGlobals.test |
| 7 | +local jest = JestGlobals.jest |
8 | 8 |
|
9 | | - local Promise = require(Packages.Promise) |
| 9 | +local Promise = require(Packages.Promise) |
10 | 10 |
|
11 | | - local React = require(Packages.React) |
12 | | - local ParentModule = require(script.Parent.Parent)(afterEach) |
13 | | - local render = ParentModule.render |
14 | | - local fireEvent = ParentModule.fireEvent |
15 | | - local screen = ParentModule.screen |
| 11 | +local React = require(Packages.React) |
| 12 | +local ParentModule = require(script.Parent.Parent) |
| 13 | +local render = ParentModule.render |
| 14 | +local fireEvent = ParentModule.fireEvent |
| 15 | +local screen = ParentModule.screen |
16 | 16 |
|
17 | | - it("render calls useEffect immediately", function() |
18 | | - local effectCb = jest.fn() |
19 | | - local function MyUselessComponent() |
20 | | - React.useEffect(effectCb) |
21 | | - return nil |
22 | | - end |
23 | | - render(React.createElement(MyUselessComponent, nil)) |
24 | | - jestExpect(effectCb).toHaveBeenCalledTimes(1) |
25 | | - end) |
| 17 | +test("render calls useEffect immediately", function() |
| 18 | + local effectCb = jest.fn() |
| 19 | + local function MyUselessComponent() |
| 20 | + React.useEffect(effectCb) |
| 21 | + return nil |
| 22 | + end |
| 23 | + render(React.createElement(MyUselessComponent, nil)) |
| 24 | + expect(effectCb).toHaveBeenCalledTimes(1) |
| 25 | +end) |
26 | 26 |
|
27 | | - it("findByTestId returns the element", function() |
28 | | - return Promise.resolve() |
29 | | - :andThen(function() |
30 | | - local ref = React.createRef() |
31 | | - render(React.createElement("Frame", { ref = ref, [React.Tag] = "data-testid=foo" })) |
32 | | - jestExpect(screen.findByTestId("foo"):expect()).toBe(ref.current) |
33 | | - end) |
34 | | - :expect() |
35 | | - end) |
| 27 | +test("findByTestId returns the element", function() |
| 28 | + return Promise.resolve() |
| 29 | + :andThen(function() |
| 30 | + local ref = React.createRef() |
| 31 | + render(React.createElement("Frame", { ref = ref, [React.Tag] = "data-testid=foo" })) |
| 32 | + expect(screen.findByTestId("foo"):expect()).toBe(ref.current) |
| 33 | + end) |
| 34 | + :expect() |
| 35 | +end) |
36 | 36 |
|
37 | | - it("fireEvent triggers useEffect calls", function() |
38 | | - local effectCb = jest.fn() |
39 | | - local function Counter() |
40 | | - React.useEffect(effectCb) |
41 | | - local count, setCount = React.useState(0) |
42 | | - return React.createElement("TextButton", { |
43 | | - Size = UDim2.new(0, 100, 0, 100), |
44 | | - [React.Event.Activated] = function() |
45 | | - return setCount(count + 1) |
46 | | - end, |
47 | | - Text = count, |
48 | | - }) |
49 | | - end |
50 | | - local buttonNode = render(React.createElement(Counter, nil)).container:GetChildren()[1] |
51 | | - task.wait() |
52 | | - effectCb:mockClear() |
53 | | - fireEvent.click(buttonNode) |
54 | | - jestExpect(buttonNode).toHaveTextContent("1") |
55 | | - jestExpect(effectCb).toHaveBeenCalledTimes(1) |
56 | | - end) |
| 37 | +test("fireEvent triggers useEffect calls", function() |
| 38 | + local effectCb = jest.fn() |
| 39 | + local function Counter() |
| 40 | + React.useEffect(effectCb) |
| 41 | + local count, setCount = React.useState(0) |
| 42 | + return React.createElement("TextButton", { |
| 43 | + Size = UDim2.new(0, 100, 0, 100), |
| 44 | + [React.Event.Activated] = function() |
| 45 | + return setCount(count + 1) |
| 46 | + end, |
| 47 | + Text = count, |
| 48 | + }) |
| 49 | + end |
| 50 | + local buttonNode = render(React.createElement(Counter, nil)).container:GetChildren()[1] |
| 51 | + task.wait() |
| 52 | + effectCb:mockClear() |
| 53 | + fireEvent.click(buttonNode) |
| 54 | + expect(buttonNode).toHaveTextContent("1") |
| 55 | + expect(effectCb).toHaveBeenCalledTimes(1) |
| 56 | +end) |
57 | 57 |
|
58 | | - -- ROBLOX deviation START: hydrate is not supported |
59 | | - -- it("calls to hydrate will run useEffects", function() |
60 | | - -- local effectCb = jest.fn() |
61 | | - -- local function MyUselessComponent() |
62 | | - -- React.useEffect(effectCb) |
63 | | - -- return nil |
64 | | - -- end |
65 | | - -- render(React.createElement(MyUselessComponent, nil), { hydrate = true }) |
66 | | - -- jestExpect(effectCb).toHaveBeenCalledTimes(1) |
67 | | - -- end) |
68 | | - -- ROBLOX deviation END |
69 | | -end |
| 58 | +-- ROBLOX deviation START: hydrate is not supported |
| 59 | +-- it("calls to hydrate will run useEffects", function() |
| 60 | +-- local effectCb = jest.fn() |
| 61 | +-- local function MyUselessComponent() |
| 62 | +-- React.useEffect(effectCb) |
| 63 | +-- return nil |
| 64 | +-- end |
| 65 | +-- render(React.createElement(MyUselessComponent, nil), { hydrate = true }) |
| 66 | +-- expect(effectCb).toHaveBeenCalledTimes(1) |
| 67 | +-- end) |
| 68 | +-- ROBLOX deviation END |
| 69 | +return {} |
0 commit comments