local D=script.Parent.Parent.Parent local w=require(D.Promise)local W=require(D.JestGlobals)local c=W.expect local a=W.test local o=W.describe local C=W.jest local t=require(script.Parent.Parent.jsHelpers.document)local V={debug=function() end,logTestingPlaygroundURL=function() end}local L=require(script.Parent.Parent.helpers)local M=L.getDocument local p=L.getWindowFromNode local K=L.checkContainerType a("\114\101\116\117\114\110\115 \103\108\111\098\097\108 \100\111\099\117\109\101\110\116 \105\102 \101\120\105\115\116\115",function()(c(M())).toBe(t)end)o("\119\105\110\100\111\119 \114\101\116\114\105\101\118\097\108 \116\104\114\111\119\115 \119\104\101\110 \103\105\118\101\110 \115\111\109\101\116\104\105\110\103 \111\116\104\101\114 \116\104\097\110 \097 \110\111\100\101",function()a("\115\099\114\101\101\110 \097\115 \110\111\100\101",function()(c(function()return p(V)end)).toThrowError("\073\116 \108\111\111\107\115 \108\105\107\101 \121\111\117 \112\097\115\115\101\100 \097 \096\115\099\114\101\101\110\096 \111\098\106\101\099\116\046 \068\105\100 \121\111\117 \100\111 \115\111\109\101\116\104\105\110\103 \108\105\107\101 \096\102\105\114\101\069\118\101\110\116\046\099\108\105\099\107\040\115\099\114\101\101\110\044 \046\046\046\096 \119\104\101\110 \121\111\117 \109\101\097\110\116 \116\111 \117\115\101 \097 \113\117\101\114\121\044 \101\046\103\046 \096\102\105\114\101\069\118\101\110\116\046\099\108\105\099\107\040\115\099\114\101\101\110\046\103\101\116\066\121\046\046\046\044 \096\063")end)a("\080\114\111\109\105\115\101 \097\115 \110\111\100\101",function()(c(function()return p(w.new(C.fn()))end)).toThrowError("\073\116 \108\111\111\107\115 \108\105\107\101 \121\111\117 \112\097\115\115\101\100 \097 \080\114\111\109\105\115\101 \111\098\106\101\099\116 \105\110\115\116\101\097\100 \111\102 \097 \068\079\077 \110\111\100\101\046 \068\105\100 \121\111\117 \100\111 \115\111\109\101\116\104\105\110\103 \108\105\107\101 \096\102\105\114\101\069\118\101\110\116\046\099\108\105\099\107\040\115\099\114\101\101\110\046\102\105\110\100\066\121\046\046\046\096 \119\104\101\110 \121\111\117 \109\101\097\110\116 \116\111 \117\115\101 \097 \096\103\101\116\066\121\096 \113\117\101\114\121 \096\102\105\114\101\069\118\101\110\116\046\099\108\105\099\107\040\115\099\114\101\101\110\046\103\101\116\066\121\046\046\046\096\044 \111\114 \097\119\097\105\116 \116\104\101 \102\105\110\100\066\121 \113\117\101\114\121 \096\102\105\114\101\069\118\101\110\116\046\099\108\105\099\107\040\097\119\097\105\116 \115\099\114\101\101\110\046\102\105\110\100\066\121\046\046\046\096\063")end)a("\065\114\114\097\121 \097\115 \110\111\100\101",function()(c(function()return p({})end)).toThrowError("\073\116 \108\111\111\107\115 \108\105\107\101 \121\111\117 \112\097\115\115\101\100 \097\110 \065\114\114\097\121 \105\110\115\116\101\097\100 \111\102 \097 \068\079\077 \110\111\100\101\046 \068\105\100 \121\111\117 \100\111 \115\111\109\101\116\104\105\110\103 \108\105\107\101 \096\102\105\114\101\069\118\101\110\116\046\099\108\105\099\107\040\115\099\114\101\101\110\046\103\101\116\065\108\108\066\121\046\046\046\096 \119\104\101\110 \121\111\117 \109\101\097\110\116 \116\111 \117\115\101 \097 \096\103\101\116\066\121\096 \113\117\101\114\121 \096\102\105\114\101\069\118\101\110\116\046\099\108\105\099\107\040\115\099\114\101\101\110\046\103\101\116\066\121\046\046\046\096\063")end)a("\117\110\107\110\111\119\110 \097\115 \110\111\100\101",function()(c(function()return p({foo="\102\111\111"})end)).toThrowError("\084\104\101 \103\105\118\101\110 \110\111\100\101 \105\115 \110\111\116 \097\110 \069\108\101\109\101\110\116\044 \116\104\101 \110\111\100\101 \116\121\112\101 \105\115\058 \116\097\098\108\101\046")end)end)o("\113\117\101\114\121 \099\111\110\116\097\105\110\101\114 \118\097\108\105\100\097\116\105\111\110 \116\104\114\111\119\115 \119\104\101\110 \118\097\108\105\100\097\116\105\111\110 \102\097\105\108\115",function()a("\117\110\100\101\102\105\110\101\100 \097\115 \099\111\110\116\097\105\110\101\114",function()(c(function()return K(nil)end)).toThrowError("\069\120\112\101\099\116\101\100 \099\111\110\116\097\105\110\101\114 \116\111 \098\101 \097\110 \069\108\101\109\101\110\116\044 \097 \068\111\099\117\109\101\110\116 \111\114 \097 \068\111\099\117\109\101\110\116\070\114\097\103\109\101\110\116 \098\117\116 \103\111\116 \110\105\108\046")end)a("\110\117\108\108 \097\115 \099\111\110\116\097\105\110\101\114",function()(c(function()return K(nil)end)).toThrowError("\069\120\112\101\099\116\101\100 \099\111\110\116\097\105\110\101\114 \116\111 \098\101 \097\110 \069\108\101\109\101\110\116\044 \097 \068\111\099\117\109\101\110\116 \111\114 \097 \068\111\099\117\109\101\110\116\070\114\097\103\109\101\110\116 \098\117\116 \103\111\116 \110\105\108\046")end)a("\097\114\114\097\121 \097\115 \099\111\110\116\097\105\110\101\114",function()(c(function()return K({})end)).toThrowError("\069\120\112\101\099\116\101\100 \099\111\110\116\097\105\110\101\114 \116\111 \098\101 \097\110 \069\108\101\109\101\110\116\044 \097 \068\111\099\117\109\101\110\116 \111\114 \097 \068\111\099\117\109\101\110\116\070\114\097\103\109\101\110\116 \098\117\116 \103\111\116 \116\097\098\108\101\046")end)a("\111\098\106\101\099\116 \097\115 \099\111\110\116\097\105\110\101\114",function()(c(function()return K({foo="\102\111\111"})end)).toThrowError("\069\120\112\101\099\116\101\100 \099\111\110\116\097\105\110\101\114 \116\111 \098\101 \097\110 \069\108\101\109\101\110\116\044 \097 \068\111\099\117\109\101\110\116 \111\114 \097 \068\111\099\117\109\101\110\116\070\114\097\103\109\101\110\116 \098\117\116 \103\111\116 \116\097\098\108\101\046")end)end)return{}
-- ROBLOX upstream: https://github.com/testing-library/dom-testing-library/blob/v8.14.0/src/__tests__/helpers.js
local Packages = script.Parent.Parent.Parent

local Promise = require(Packages.Promise)

local JestGlobals = require(Packages.JestGlobals)
local expect = JestGlobals.expect
local test = JestGlobals.test
local describe = JestGlobals.describe
local jest = JestGlobals.jest

-- ROBLOX deviation START: assume constant document
local document = require(script.Parent.Parent.jsHelpers.document)
-- ROBLOX deviation END

-- ROBLOX TODO START: not ported yet. Mocked
-- local screen = require(script.Parent.Parent).screen
local screen = {
	debug = function() end,
	logTestingPlaygroundURL = function() end,
}
-- ROBLOX TODO END

local helpersModule = require(script.Parent.Parent.helpers)
local getDocument = helpersModule.getDocument
local getWindowFromNode = helpersModule.getWindowFromNode
local checkContainerType = helpersModule.checkContainerType
test("returns global document if exists", function()
	expect(getDocument()).toBe(document)
end)

describe("window retrieval throws when given something other than a node", function()
	-- we had an issue when user insert screen instead of query
	-- actually here should be another more clear error output
	test("screen as node", function()
		expect(function()
			return getWindowFromNode(screen)
		end).toThrowError(
			"It looks like you passed a `screen` object. Did you do something like `fireEvent.click(screen, ...` when you meant to use a query, e.g. `fireEvent.click(screen.getBy..., `?"
		)
	end)
	test("Promise as node", function()
		expect(function()
			return getWindowFromNode(Promise.new((jest.fn())))
		end).toThrowError(
			"It looks like you passed a Promise object instead of a DOM node. Did you do something like `fireEvent.click(screen.findBy...` when you meant to use a `getBy` query `fireEvent.click(screen.getBy...`, or await the findBy query `fireEvent.click(await screen.findBy...`?"
		)
	end)
	test("Array as node", function()
		expect(function()
			return getWindowFromNode({})
		end).toThrowError(
			"It looks like you passed an Array instead of a DOM node. Did you do something like `fireEvent.click(screen.getAllBy...` when you meant to use a `getBy` query `fireEvent.click(screen.getBy...`?"
		)
	end)
	-- ROBLOX deviation START: no document available
	-- it("window is not available for node", function()
	-- 	local elem = document:createElement("div")
	-- 	Object.defineProperty(elem.ownerDocument, "defaultView", {
	-- 		get = function(self)
	-- 			return nil
	-- 		end,
	-- 	})

	-- 	expect(function()
	-- 		return getWindowFromNode(elem)
	-- 	end).toThrowErrorMatchingInlineSnapshot(
	-- 		"It looks like the window object is not available for the provided node."
	-- 	)
	-- end)
	-- ROBLOX deviation END

	test("unknown as node", function()
		expect(function()
			-- ROBLOX deviation START: adding some properties to object top differentiate from array
			return getWindowFromNode({ foo = "foo" })
			-- ROBLOX deviation END
		end).toThrowError("The given node is not an Element, the node type is: table.")
	end)
end)

describe("query container validation throws when validation fails", function()
	test("undefined as container", function()
		expect(function()
			return checkContainerType(nil)
		end).toThrowError("Expected container to be an Element, a Document or a DocumentFragment but got nil.")
	end)
	test("null as container", function()
		expect(function()
			return checkContainerType(nil)
		end).toThrowError("Expected container to be an Element, a Document or a DocumentFragment but got nil.")
	end)
	test("array as container", function()
		expect(function()
			return checkContainerType({})
		end).toThrowError("Expected container to be an Element, a Document or a DocumentFragment but got table.")
	end)
	test("object as container", function()
		expect(function()
			-- ROBLOX deviation START: adding props to table to differentiate from array
			return checkContainerType({ foo = "foo" })
			-- ROBLOX deviation END
		end).toThrowError("Expected container to be an Element, a Document or a DocumentFragment but got table.")
	end)
end)
return {}
