local D=script.Parent.Parent.Parent local w=require(D.LuauPolyfill)local W=w.console local c=require(D.LuauRegExp)local a=require(D.JestGlobals)local o=a.expect local C=a.test local t=a.jest local V=require(script.Parent.Parent.matches)local L=V.fuzzyMatches local M=V.matches local p=nil local function K(D)return D end C("\109\097\116\099\104\101\114\115 \097\099\099\101\112\116 \115\116\114\105\110\103\115",function()(o(M("\065\066\067",p,"\065\066\067",K))).toBe(true);(o(L("\065\066\067",p,"\065\066\067",K))).toBe(true)end)C("\109\097\116\099\104\101\114\115 \097\099\099\101\112\116 \114\101\103\101\120",function()(o(M("\065\066\067",p,c("\065\066\067"),K))).toBe(true);(o(L("\065\066\067",p,c("\065\066\067"),K))).toBe(true)end)C.skip("\097 \114\101\103\101\120 \119\105\116\104 \116\104\101 \103\108\111\098\097\108 \102\108\097\103 \099\111\110\115\105\115\116\101\110\116\108\121 \040\114\101-\041\102\105\110\100\115 \097 \109\097\116\099\104",function()local D=c("\065\066\067","\103")local w=W.warn W.warn=(t.fn()).mockImplementation()local a=W.warn;(o(M("\065\066\067",p,D,K))).toBe(true);(o(L("\065\066\067",p,D,K))).toBe(true);(o(a)).toBeCalledTimes(2);(o(a)).toHaveBeenCalledWith("\084\111 \109\097\116\099\104 \097\108\108 \101\108\101\109\101\110\116\115 \119\101 \104\097\100 \116\111 \114\101\115\101\116 \116\104\101 \108\097\115\116\073\110\100\101\120 \111\102 \116\104\101 \082\101\103\069\120\112 \098\101\099\097\117\115\101 \116\104\101 \103\108\111\098\097\108 \102\108\097\103 \105\115 \101\110\097\098\108\101\100\046 \087\101 \101\110\099\111\117\114\097\103\101 \116\111 \114\101\109\111\118\101 \116\104\101 \103\108\111\098\097\108 \102\108\097\103 \102\114\111\109 \116\104\101 \082\101\103\069\120\112\046")W.warn=w end)C("\109\097\116\099\104\101\114\115 \097\099\099\101\112\116 \102\117\110\099\116\105\111\110\115",function()(o(M("\065\066\067",p,function(D)return D=="\065\066\067"end,K))).toBe(true);(o(L("\065\066\067",p,function(D)return D=="\065\066\067"end,K))).toBe(true)end)C("\109\097\116\099\104\101\114\115 \114\101\116\117\114\110 \102\097\108\115\101 \105\102 \116\101\120\116 \116\111 \109\097\116\099\104 \105\115 \110\111\116 \097 \115\116\114\105\110\103",function()(o(M(nil,p,"\065\066\067",K))).toBe(false);(o(L(nil,p,"\065\066\067",K))).toBe(false)end)C("\109\097\116\099\104\101\114\115 \116\104\114\111\119 \111\110 \105\110\118\097\108\105\100 \109\097\116\099\104\101\114 \105\110\112\117\116\115",function()(o(function()return M("\065\066\067",p,nil,K)end)).toThrowError("\073\116 \108\111\111\107\115 \108\105\107\101 \110\105\108 \119\097\115 \112\097\115\115\101\100 \105\110\115\116\101\097\100 \111\102 \097 \109\097\116\099\104\101\114\046 \068\105\100 \121\111\117 \100\111 \115\111\109\101\116\104\105\110\103 \108\105\107\101 \103\101\116\066\121\084\101\120\116\040\110\105\108\041\063");(o(function()return L("\065\066\067",p,nil,K)end)).toThrowError("\073\116 \108\111\111\107\115 \108\105\107\101 \110\105\108 \119\097\115 \112\097\115\115\101\100 \105\110\115\116\101\097\100 \111\102 \097 \109\097\116\099\104\101\114\046 \068\105\100 \121\111\117 \100\111 \115\111\109\101\116\104\105\110\103 \108\105\107\101 \103\101\116\066\121\084\101\120\116\040\110\105\108\041\063")end)return{}
-- ROBLOX upstream: https://github.com/testing-library/dom-testing-library/blob/v8.14.0/src/__tests__/matches.js
local Packages = script.Parent.Parent.Parent

local LuauPolyfill = require(Packages.LuauPolyfill)
local console = LuauPolyfill.console

local RegExp = require(Packages.LuauRegExp)

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

local matchesModule = require(script.Parent.Parent.matches)
local fuzzyMatches = matchesModule.fuzzyMatches
local matches = matchesModule.matches

-- unit its for text match utils

local node = nil
local function normalizer(str)
	return str
end
test("matchers accept strings", function()
	expect(matches("ABC", node, "ABC", normalizer)).toBe(true)
	expect(fuzzyMatches("ABC", node, "ABC", normalizer)).toBe(true)
end)
test("matchers accept regex", function()
	expect(matches("ABC", node, RegExp("ABC"), normalizer)).toBe(true)
	expect(fuzzyMatches("ABC", node, RegExp("ABC"), normalizer)).toBe(true)
end)

-- https://stackoverflow.com/questions/1520800/why-does-a-regexp-with-global-flag-give-wrong-results
-- ROBLOX FIXME: global flag not available. Error: invalid regular expression flag g
test.skip("a regex with the global flag consistently (re-)finds a match", function()
	local regex = RegExp("ABC", "g")
	-- ROBLOX deviation START: workaround for jest.spyOn
	local originalFn = console.warn
	console.warn = jest.fn().mockImplementation()
	local spy = console.warn
	-- ROBLOX deviation END: workaround for jest.spyOn
	expect(matches("ABC", node, regex, normalizer)).toBe(true)
	expect(fuzzyMatches("ABC", node, regex, normalizer)).toBe(true)
	expect(spy).toBeCalledTimes(2)
	expect(spy).toHaveBeenCalledWith(
		"To match all elements we had to reset the lastIndex of the RegExp because the global flag is enabled. We encourage to remove the global flag from the RegExp."
	)
	-- ROBLOX deviation START: workaround for jest.spyOn
	console.warn = originalFn
	-- ROBLOX deviation END
end)
test("matchers accept functions", function()
	expect(matches("ABC", node, function(text)
		return text == "ABC"
	end, normalizer)).toBe(true)
	expect(fuzzyMatches("ABC", node, function(text)
		return text == "ABC"
	end, normalizer)).toBe(true)
end)
test("matchers return false if text to match is not a string", function()
	expect(matches(nil, node, "ABC", normalizer)).toBe(false)
	expect(fuzzyMatches(nil, node, "ABC", normalizer)).toBe(false)
end)
test("matchers throw on invalid matcher inputs", function()
	expect(function()
		return matches("ABC", node, nil, normalizer)
	end).toThrowError(
		-- ROBLOX deviation START: error message should match nil instead of null/undefined
		"It looks like nil was passed instead of a matcher. Did you do something like getByText(nil)?"
		-- ROBLOX deviation END
	)
	expect(function()
		return fuzzyMatches("ABC", node, nil, normalizer)
	end).toThrowError(
		-- ROBLOX deviation START: error message should match nil instead of null/undefined
		"It looks like nil was passed instead of a matcher. Did you do something like getByText(nil)?"
		-- ROBLOX deviation END
	)
end)
return {}
