local e local k local p local f=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 S=require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\112\114\111\109\105\115\101")local A=f.expect local M=f.beforeEach local O=f.it local X=f.jest M(function()X.resetModules()e=require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\114\101\097\099\116")k=require("\064\112\107\103\047\064\106\115\100\111\116\108\117\097\047\114\101\097\099\116-\110\111\111\112-\114\101\110\100\101\114\101\114")p=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")end)O("\099\097\110 \117\115\101 \097\099\116 \116\111 \102\108\117\115\104 \101\102\102\101\099\116\115",function()local function f(k)e.useEffect(k.callback)return nil end local S={}k.act(function()k.render(e.createElement(f,{callback=function()table.insert(S,#S)end}))end);(A(p)).toFlushWithoutYielding();(A(S)).toEqual({0})end)O("\115\104\111\117\108\100 \119\111\114\107 \119\105\116\104 \097\115\121\110\099\047\097\119\097\105\116",function()local function f()local k,f=e.useState(0)local function S()p.unstable_yieldValue("\115\116\097\103\101 \049")p.unstable_yieldValue("\115\116\097\103\101 \050")f(1)end e.useEffect(function()S()end,{})return k end;(S.try(function()k.act(function()k.render(e.createElement(f))end)end)):await();(A(p)).toHaveYielded({"\115\116\097\103\101 \049";"\115\116\097\103\101 \050"});(A(p)).toFlushWithoutYielding();(A(k.getChildren())).toEqual({{text="\049";hidden=false}})end)
-- ROBLOX upstream: https://github.com/facebook/react/blob/d17086c7c813402a550d15a2f56dc43f1dbd1735/packages/react-reconciler/src/__tests__/ReactNoopRendererAct-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.
 *
 * @jest-environment node
 ]]

-- sanity tests for ReactNoop.act()

local React
local ReactNoop
local Scheduler

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

beforeEach(function()
	jest.resetModules()

	React = require("@pkg/@jsdotlua/react")
	ReactNoop = require("@pkg/@jsdotlua/react-noop-renderer")
	Scheduler = require("@pkg/@jsdotlua/scheduler")
end)

it("can use act to flush effects", function()
	local function App(props)
		React.useEffect(props.callback)
		return nil
	end

	local calledLog = {}
	ReactNoop.act(function()
		ReactNoop.render(React.createElement(App, {
			callback = function()
				table.insert(calledLog, #calledLog)
			end,
		}))
	end)
	jestExpect(Scheduler).toFlushWithoutYielding()
	jestExpect(calledLog).toEqual({ 0 })
end)
it("should work with async/await", function()
	local function App()
		local ctr, setCtr = React.useState(0)
		local function someAsyncFunction()
			Scheduler.unstable_yieldValue("stage 1")
			Scheduler.unstable_yieldValue("stage 2")
			setCtr(1)
		end
		React.useEffect(function()
			someAsyncFunction()
		end, {})
		return ctr
	end
	Promise.try(function()
		ReactNoop.act(function()
			ReactNoop.render(React.createElement(App))
		end)
	end):await()
	jestExpect(Scheduler).toHaveYielded({ "stage 1", "stage 2" })
	jestExpect(Scheduler).toFlushWithoutYielding()
	jestExpect(ReactNoop.getChildren()).toEqual({ { text = "1", hidden = false } })
end)
