|
| 1 | +-- ROBLOX upstream: https://github.com/testing-library/react-testing-library/blob/v12.1.5/src/__tests__/render.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 document = require(Packages.DomTestingLibrary).document |
| 10 | + |
| 11 | + local CollectionService = game:GetService("CollectionService") |
| 12 | + |
| 13 | + local React = require(Packages.React) |
| 14 | + local ReactRoblox = require(Packages.ReactRoblox) |
| 15 | + local ParentModule = require(script.Parent.Parent)(afterEach) |
| 16 | + local render = ParentModule.render |
| 17 | + local screen = ParentModule.screen |
| 18 | + |
| 19 | + it("renders div into document", function() |
| 20 | + local ref = React.createRef() |
| 21 | + local container = render(React.createElement("Frame", { ref = ref })).container |
| 22 | + -- ROBLOX deviation START: replace firstChild with Instance equivalent |
| 23 | + jestExpect(container:GetChildren()[1]).toBe(ref.current) |
| 24 | + -- ROBLOX deviation END |
| 25 | + end) |
| 26 | + |
| 27 | + it("works great with react portals", function() |
| 28 | + -- ROBLOX deviation START: predeclare |
| 29 | + local Greet |
| 30 | + -- ROBLOX deviation END |
| 31 | + |
| 32 | + local MyPortal = React.Component:extend("MyPortal") |
| 33 | + |
| 34 | + function MyPortal:init(...: any) |
| 35 | + self.portalNode = Instance.new("Frame") |
| 36 | + CollectionService:AddTag(self.portalNode, "data-testid=my-portal") |
| 37 | + end |
| 38 | + |
| 39 | + function MyPortal:componentDidMount() |
| 40 | + self.portalNode.Parent = document |
| 41 | + end |
| 42 | + |
| 43 | + function MyPortal:componentWillUnmount() |
| 44 | + self.portalNode.Parent = nil |
| 45 | + end |
| 46 | + |
| 47 | + function MyPortal:render() |
| 48 | + return ReactRoblox.createPortal( |
| 49 | + React.createElement(Greet, { greeting = "Hello", subject = "World" }), |
| 50 | + self.portalNode |
| 51 | + ) |
| 52 | + end |
| 53 | + |
| 54 | + function Greet(ref: { greeting: string, subject: string }) |
| 55 | + local greeting, subject = ref.greeting, ref.subject |
| 56 | + return React.createElement( |
| 57 | + "Frame", |
| 58 | + nil, |
| 59 | + React.createElement("TextLabel", { Text = greeting .. " " .. subject }) |
| 60 | + ) |
| 61 | + end |
| 62 | + |
| 63 | + local unmount = render(React.createElement(MyPortal, nil)).unmount |
| 64 | + jestExpect(screen.getByText("Hello World")).toBeInTheDocument() |
| 65 | + local portalNode = screen.getByTestId("my-portal") |
| 66 | + jestExpect(portalNode).toBeInTheDocument() |
| 67 | + unmount() |
| 68 | + jestExpect(portalNode).never.toBeInTheDocument() |
| 69 | + end) |
| 70 | + |
| 71 | + it("returns baseElement which defaults to document.body", function() |
| 72 | + local baseElement = render(React.createElement("Frame", nil)).baseElement |
| 73 | + jestExpect(baseElement).toBe(document) |
| 74 | + end) |
| 75 | + |
| 76 | + -- ROBLOX deviation START: asFragment not supported |
| 77 | + -- it("supports fragments", function() |
| 78 | + -- local Test = React.Component:extend("Test") |
| 79 | + -- function Test:render() |
| 80 | + -- return React.createElement( |
| 81 | + -- "Frame", |
| 82 | + -- nil, |
| 83 | + -- React.createElement("code", nil, "DocumentFragment"), |
| 84 | + -- " is pretty cool!" |
| 85 | + -- ) |
| 86 | + -- end |
| 87 | + -- local asFragment = render(React.createElement(Test, nil)).asFragment |
| 88 | + -- jestExpect(asFragment()).toMatchSnapshot() |
| 89 | + -- end) |
| 90 | + -- ROBLOX deviation END |
| 91 | + |
| 92 | + it("renders options.wrapper around node", function() |
| 93 | + local function WrapperComponent(ref) |
| 94 | + local children = ref.children |
| 95 | + return React.createElement("Frame", { [React.Tag] = "data-testid=wrapper" }, children) |
| 96 | + end |
| 97 | + |
| 98 | + local container = render( |
| 99 | + React.createElement("Frame", { [React.Tag] = "data-testid=inner" }), |
| 100 | + { wrapper = WrapperComponent } |
| 101 | + ).container |
| 102 | + |
| 103 | + jestExpect(screen.getByTestId("wrapper")).toBeInTheDocument() |
| 104 | + -- ROBLOX deviation START: replace to MatchInlineSnapshot |
| 105 | + jestExpect(CollectionService:GetTags(container:GetChildren()[1])).toContain("data-testid=wrapper") |
| 106 | + jestExpect(CollectionService:GetTags(container:GetChildren()[1]:GetChildren()[1])).toContain( |
| 107 | + "data-testid=inner" |
| 108 | + ) |
| 109 | + -- ROBLOX deviation END |
| 110 | + end) |
| 111 | + |
| 112 | + -- ROBLOX FIXME: useEffect is triggered before unmount |
| 113 | + itFIXME("flushes useEffect cleanup functions sync on unmount()", function() |
| 114 | + local spy = jest.fn() |
| 115 | + local function Component() |
| 116 | + React.useEffect(function() |
| 117 | + spy() |
| 118 | + end, {}) |
| 119 | + return nil |
| 120 | + end |
| 121 | + local unmount = render(React.createElement(Component, nil)).unmount |
| 122 | + jestExpect(spy).toHaveBeenCalledTimes(0) |
| 123 | + unmount() |
| 124 | + jestExpect(spy).toHaveBeenCalledTimes(1) |
| 125 | + end) |
| 126 | +end |
0 commit comments