Skip to content

Commit 570c818

Browse files
author
RoFlection Bot
committed
Port rerender tests (#24)
1 parent dac5096 commit 570c818

File tree

5 files changed

+94
-13
lines changed

5 files changed

+94
-13
lines changed

src/__tests__/act.spec.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ return function()
5151
task.wait()
5252
effectCb:mockClear()
5353
fireEvent.click(buttonNode)
54-
-- ROBLOX deviation START: replace toHaveTextContext
55-
jestExpect(buttonNode.Text).toBe("1")
56-
-- ROBLOX deviation END
54+
jestExpect(buttonNode).toHaveTextContent("1")
5755
jestExpect(effectCb).toHaveBeenCalledTimes(1)
5856
end)
5957

src/__tests__/auto-cleanup.spec.lua

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ return function()
1616
end)
1717

1818
it("second", function()
19-
-- ROBLOX deviation START: not using toBeEmptyDOMElement
20-
jestExpect(#document:GetChildren()).toBe(0)
21-
-- ROBLOX deviation END
19+
jestExpect(document).toBeEmptyDOMElement()
2220
end)
2321
end

src/__tests__/end-to-end.spec.lua

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ return function()
9393
return screen.getByText("Loading...")
9494
end
9595
waitForElementToBeRemoved(loading):expect()
96-
jestExpect(screen.getByTestId("message").Text).toMatch(RegExp("Hello World"))
96+
jestExpect(screen.getByTestId("message")).toHaveTextContent(RegExp("Hello World"))
9797
end)
9898
:expect()
9999
end)
@@ -106,9 +106,7 @@ return function()
106106
return screen.getByText(RegExp("Loaded this message:"))
107107
end
108108
waitFor(message):expect()
109-
-- ROBLOX deviation START: replace .toHaveTextContent
110-
jestExpect(screen.getByTestId("message").Text).toMatch(RegExp("Hello World"))
111-
-- ROBLOX deviation END
109+
jestExpect(screen.getByTestId("message")).toHaveTextContent(RegExp("Hello World"))
112110
end)
113111
:expect()
114112
end)
@@ -117,9 +115,7 @@ return function()
117115
return Promise.resolve()
118116
:andThen(function()
119117
render(React.createElement(ComponentWithLoader, nil))
120-
-- ROBLOX deviation START: replace .resolves.toHaveTextContent
121-
jestExpect((screen.findByTestId("message"):expect()).Text).toMatch(RegExp("Hello World"))
122-
-- ROBLOX deviation END
118+
jestExpect((screen.findByTestId("message"):expect())).toHaveTextContent(RegExp("Hello World"))
123119
end)
124120
:expect()
125121
end)

src/__tests__/rerender.spec.lua

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
-- ROBLOX upstream: https://github.com/testing-library/react-testing-library/blob/v12.1.5/src/__tests__/rerender.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+
8+
local React = require(Packages.React)
9+
local render = require(script.Parent.Parent)(afterEach).render
10+
11+
it("rerender will re-render the element", function()
12+
local function Greeting(props)
13+
return React.createElement("TextLabel", { Text = props.message })
14+
end
15+
local ref = render(React.createElement(Greeting, { message = "hi" }))
16+
local container, rerender = ref.container, ref.rerender
17+
-- ROBLOX deviation START: replace firstChild with Instance equivalent
18+
jestExpect(container:GetChildren()[1]).toHaveTextContent("hi")
19+
rerender(React.createElement(Greeting, { message = "hey" }))
20+
jestExpect(container:GetChildren()[1]).toHaveTextContent("hey")
21+
-- ROBLOX deviation END
22+
end)
23+
24+
-- ROBLOX deviation START: hydrate not supported
25+
-- it("hydrate will not update props until next render", function()
26+
-- local initialInputElement = document:createElement("input")
27+
-- local container = document:createElement("div")
28+
-- container:appendChild(initialInputElement)
29+
-- document.body:appendChild(container)
30+
-- local firstValue = "hello"
31+
-- initialInputElement.value = firstValue
32+
-- local rerender = render(
33+
-- React.createElement("input", {
34+
-- value = "",
35+
-- onChange = function()
36+
-- return nil
37+
-- end,
38+
-- }),
39+
-- { container = container, hydrate = true }
40+
-- ).rerender
41+
-- jestExpect(initialInputElement).toHaveValue(firstValue)
42+
-- local secondValue = "goodbye"
43+
-- rerender(React.createElement("input", {
44+
-- value = secondValue,
45+
-- onChange = function()
46+
-- return nil
47+
-- end,
48+
-- }))
49+
-- jestExpect(initialInputElement).toHaveValue(secondValue)
50+
-- end)
51+
-- ROBLOX deviation END
52+
end

src/jsHelpers/jest-dom.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,41 @@ local function toBeEmptyDOMElement(self, received: Instance, expected)
4242
end
4343
exports.toBeEmptyDOMElement = toBeEmptyDOMElement
4444

45+
local function toHaveTextContent(self, received: Instance, expected)
46+
local matcherName = "toHaveTextContent"
47+
if received:IsA("TextBox") then
48+
error("toHaveTextContent is not meant to be used with TextBox")
49+
end
50+
51+
local text = (received :: any).Text
52+
local checkingWithEmptyString = not expected or expected == "" or text == ""
53+
54+
local check
55+
if typeof(expected) == "string" then
56+
check = string.find(text, expected, 1, true)
57+
elseif typeof(expected) == "table" and typeof(expected.test) == "function" then
58+
check = expected:test(text)
59+
else
60+
error("Unhandled expected value type: string or regex is required")
61+
end
62+
63+
local options = {
64+
isNot = self.isNot,
65+
promise = self.promise,
66+
}
67+
68+
local pass = not checkingWithEmptyString and not not check
69+
local message = function()
70+
return self.utils.matcherHint(matcherName, nil, nil, options) :: string
71+
.. "\n\n"
72+
.. (
73+
if checkingWithEmptyString
74+
then "Checking with empty string will always match, use .toBeEmptyDOMElement() instead"
75+
else "Expected element " .. (if self.isNot then "not to" else "to") .. " have text content"
76+
)
77+
end
78+
return { message = message, pass = pass }
79+
end
80+
exports.toHaveTextContent = toHaveTextContent
81+
4582
return exports

0 commit comments

Comments
 (0)