local j=require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\106\101\115\116-\103\108\111\098\097\108\115")local d=j.expect local A=j.jest local f=j.beforeEach local T=j.it local Z local O local I local g local G local J f(function()A.resetModules()A.useFakeTimers()local j=(require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\115\104\097\114\101\100")).ReactFeatureFlags j.debugRenderPhaseSideEffectsForStrictMode=false Z=require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\114\101\097\099\116")O=require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\114\101\097\099\116-\114\111\098\108\111\120")G=require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\115\099\104\101\100\117\108\101\114")g=require("\046\046\047\082\101\097\099\116\082\111\098\108\111\120\067\111\109\112\111\110\101\110\116\084\114\101\101")J=Instance.new("\070\111\108\100\101\114")I=O.createRoot(J)end)T("\103\101\116\067\108\111\115\101\115\116\073\110\115\116\097\110\099\101\070\114\111\109\078\111\100\101 \115\104\111\117\108\100 \114\101\116\117\114\110 \097 \099\097\099\104\101\100 \105\110\115\116\097\110\099\101",function()I:render(Z.createElement("\070\114\097\109\101",{},{Label=Z.createElement("\084\101\120\116\076\097\098\101\108",{Text="\072\101\108\108\111"})}))G.unstable_flushAllWithoutAsserting()local j=g.getClosestInstanceFromNode(J.Frame.Label);(d(j.memoizedProps.Text)).toEqual("\072\101\108\108\111")end)T("\103\101\116\067\108\111\115\101\115\116\073\110\115\116\097\110\099\101\070\114\111\109\078\111\100\101 \115\104\111\117\108\100 \114\101\116\117\114\110 \112\111\114\116\097\108\101\100 \105\110\115\116\097\110\099\101\115",function()local j=Instance.new("\070\114\097\109\101")local A=Instance.new("\070\114\097\109\101")local f=Instance.new("\070\114\097\109\101")I:render({Z.createElement("\084\101\120\116\076\097\098\101\108",{key="\097";Text="\110\111\114\109\097\108\091\048\093"});O.createPortal({Z.createElement("\084\101\120\116\076\097\098\101\108",{key="\098";Text="\112\111\114\116\097\108\049\091\048\093"}),O.createPortal(Z.createElement("\084\101\120\116\076\097\098\101\108",{key="\099",Text="\112\111\114\116\097\108\050\091\048\093"}),A);O.createPortal(Z.createElement("\084\101\120\116\076\097\098\101\108",{key="\100",Text="\112\111\114\116\097\108\051\091\048\093"}),f);Z.createElement("\084\101\120\116\076\097\098\101\108",{key="\101";Text="\112\111\114\116\097\108\049\091\049\093"})},j);Z.createElement("\084\101\120\116\076\097\098\101\108",{key="\102";Text="\110\111\114\109\097\108\091\049\093"})})G.unstable_flushAllWithoutAsserting()local T=g.getClosestInstanceFromNode((f:GetChildren())[1]);(d(T.memoizedProps.Text)).toEqual("\112\111\114\116\097\108\051\091\048\093")end)
-- ROBLOX upstream: https://github.com/facebook/react/blob/702fad4b1b48ac8f626ed3f35e8f86f5ea728084/packages/react-dom/src/__tests__/ReactDOMComponentTree-test.js
--[[*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow
]]

local JestGlobals = require("@pkg/@jsdotlua/jest-globals")
local jestExpect = JestGlobals.expect
local jest = JestGlobals.jest
local beforeEach = JestGlobals.beforeEach
local it = JestGlobals.it

local React
local ReactRoblox
local reactRobloxRoot
local ReactRobloxComponentTree
local Scheduler
local parent

beforeEach(function()
	jest.resetModules()
	jest.useFakeTimers()
	local ReactFeatureFlags = require("@pkg/@jsdotlua/shared").ReactFeatureFlags
	ReactFeatureFlags.debugRenderPhaseSideEffectsForStrictMode = false

	React = require("@pkg/@jsdotlua/react")
	ReactRoblox = require("@pkg/@jsdotlua/react-roblox")
	Scheduler = require("@pkg/@jsdotlua/scheduler")
	ReactRobloxComponentTree = require("../ReactRobloxComponentTree")
	parent = Instance.new("Folder")
	reactRobloxRoot = ReactRoblox.createRoot(parent)
end)

it("getClosestInstanceFromNode should return a cached instance", function()
	reactRobloxRoot:render(
		React.createElement(
			"Frame",
			{},
			{ Label = React.createElement("TextLabel", { Text = "Hello" }) }
		)
	)

	Scheduler.unstable_flushAllWithoutAsserting()

	local labelNode =
		ReactRobloxComponentTree.getClosestInstanceFromNode(parent.Frame.Label)
	jestExpect(labelNode.memoizedProps.Text).toEqual("Hello")
end)

it("getClosestInstanceFromNode should return portaled instances", function()
	local portalContainer1 = Instance.new("Frame")
	local portalContainer2 = Instance.new("Frame")
	local portalContainer3 = Instance.new("Frame")

	reactRobloxRoot:render({
		React.createElement("TextLabel", { key = "a", Text = "normal[0]" }),
		ReactRoblox.createPortal({
			React.createElement("TextLabel", { key = "b", Text = "portal1[0]" }),
			ReactRoblox.createPortal(
				React.createElement("TextLabel", { key = "c", Text = "portal2[0]" }),
				portalContainer2
			),
			ReactRoblox.createPortal(
				React.createElement("TextLabel", { key = "d", Text = "portal3[0]" }),
				portalContainer3
			),
			React.createElement("TextLabel", { key = "e", Text = "portal1[1]" }),
		}, portalContainer1),
		React.createElement("TextLabel", { key = "f", Text = "normal[1]" }),
	})

	Scheduler.unstable_flushAllWithoutAsserting()

	local portal3Label = ReactRobloxComponentTree.getClosestInstanceFromNode(
		portalContainer3:GetChildren()[1]
	)
	jestExpect(portal3Label.memoizedProps.Text).toEqual("portal3[0]")
end)
