local B=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 P=B.describe local Y=B.it local z=B.beforeEach local H=B.expect local q=B.jest P("\098\114\105\100\103\101",function()local B z(function()q.resetModules()q.useFakeTimers()B=require("\046\046\047\098\114\105\100\103\101")end)Y("\115\104\111\117\108\100 \115\104\117\116\100\111\119\110 \112\114\111\112\101\114\108\121",function()local P={listen=q.fn(function()return function() end end);send=q.fn()}local Y=B.new(P)Y:send("\114\101\108\111\097\100\065\112\112\070\111\114\080\114\111\102\105\108\105\110\103")q.runAllTimers();(H(P.send)).toHaveBeenCalledWith("\114\101\108\111\097\100\065\112\112\070\111\114\080\114\111\102\105\108\105\110\103")P.send.mockClear()Y:send("\117\112\100\097\116\101","\049")Y:send("\117\112\100\097\116\101","\050")Y:shutdown()q.runAllTimers();(H(P.send)).toHaveBeenCalledWith("\117\112\100\097\116\101","\049");(H(P.send)).toHaveBeenCalledWith("\117\112\100\097\116\101","\050");(H(P.send)).toHaveBeenCalledWith("\115\104\117\116\100\111\119\110")P.send.mockClear();(H(function()Y:send("\115\104\111\117\108\100 \110\111\116 \115\101\110\100")end)).toWarnDev("\067\097\110\110\111\116 \115\101\110\100 \109\101\115\115\097\103\101 \"\115\104\111\117\108\100 \110\111\116 \115\101\110\100\" \116\104\114\111\117\103\104 \097 \066\114\105\100\103\101 \116\104\097\116 \104\097\115 \098\101\101\110 \115\104\117\116\100\111\119\110\046",{withoutStack=true})q.runAllTimers();(H(P.send)).never.toHaveBeenCalled()end)end)
-- ROBLOX upstream: https://github.com/facebook/react/blob/v17.0.1/packages/react-devtools-shared/src/__tests__/bridge-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.
]]
local JestGlobals = require("@pkg/@jsdotlua/jest-globals")
local describe = JestGlobals.describe
local it = JestGlobals.it
local beforeEach = JestGlobals.beforeEach
local jestExpect = JestGlobals.expect
local jest = JestGlobals.jest

describe("bridge", function()
	local Bridge

	beforeEach(function()
		jest.resetModules()
		jest.useFakeTimers()
		Bridge = require("../bridge")
	end)

	it("should shutdown properly", function()
		local wall = {
			listen = jest.fn(function()
				return function() end
			end),
			send = jest.fn(),
		}
		local bridge = Bridge.new(wall)

		-- Check that we're wired up correctly.
		bridge:send("reloadAppForProfiling")
		jest.runAllTimers()
		jestExpect(wall.send).toHaveBeenCalledWith("reloadAppForProfiling")

		-- Should flush pending messages and then shut down.
		wall.send.mockClear()
		bridge:send("update", "1")
		bridge:send("update", "2")
		bridge:shutdown()
		jest.runAllTimers()
		jestExpect(wall.send).toHaveBeenCalledWith("update", "1")
		jestExpect(wall.send).toHaveBeenCalledWith("update", "2")
		jestExpect(wall.send).toHaveBeenCalledWith("shutdown")

		-- Verify that the Bridge doesn't send messages after shutdown.

		wall.send.mockClear()
		-- ROBLOX deviation: instead of spying on console, use toWarnDev matcher
		jestExpect(function()
			bridge:send("should not send")
		end).toWarnDev(
			'Cannot send message "should not send" through a Bridge that has been shutdown.',
			{ withoutStack = true }
		)
		jest.runAllTimers()
		jestExpect(wall.send).never.toHaveBeenCalled()
	end)
end)
