return function()local O=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local z=require(script.Parent.Parent.createReconciler)local l=require(script.Parent.Parent.PropMarkers.Children)local o=require(script.Parent.Parent.createFragment)local r=require(script.Parent.Parent.RobloxRenderer)local J=require(script.Parent.Parent.createElement)local P=require(script.Parent.Parent.NoopRenderer)local F=require(script.Parent.Parent.Component)local V=require(script.Parent.Parent.createSpy)local L=require(script.Parent)local p=z(P)local Q=z(r)it("\115\104\111\117\108\100 \101\120\112\111\115\101 \097 \102\117\110\099\116\105\111\110",function()(expect(L)).to.be.a("\102\117\110\099\116\105\111\110")end)it("\115\104\111\117\108\100 \114\101\116\117\114\110 \097 \116\097\098\108\101",function()local O=L("\084\101\115\116");(expect(O)).to.be.ok();(expect(type(O))).to.equal("\116\097\098\108\101")end)it("\115\104\111\117\108\100 \116\104\114\111\119 \111\110 \097\116\116\101\109\112\116 \116\111 \109\111\100\105\102\121 \116\104\101 \114\101\116\117\114\110\101\100 \116\097\098\108\101",function()local O=L("\084\101\115\116");(expect(function()O.new={}end)).to.throw();(expect(function()setmetatable(O,{})end)).to.throw()end)it("\115\104\111\117\108\100 \099\111\110\116\097\105\110 \097 \080\114\111\118\105\100\101\114 \097\110\100 \097 \067\111\110\115\117\109\101\114",function()local O=L("\084\101\115\116");(expect(O.Provider)).to.be.ok();(expect(O.Consumer)).to.be.ok()end)describe("\080\114\111\118\105\100\101\114",function()it("\115\104\111\117\108\100 \114\101\110\100\101\114 \105\116\115 \099\104\105\108\100\114\101\110",function()local O=L("\084\101\115\116")local z=V(function()return nil end)local l=J(O.Provider,{value="\084\101\115\116"},{Listener=J(z.value)})local o=p.mountVirtualTree(l,nil,"\080\114\111\118\105\100\101 \084\114\101\101")p.unmountVirtualTree(o);(expect(z.callCount)).to.equal(1)end)end)describe("\067\111\110\115\117\109\101\114",function()it("\115\104\111\117\108\100 \101\120\112\101\099\116 \097 \114\101\110\100\101\114 \102\117\110\099\116\105\111\110",function()local O=L("\084\101\115\116")local z=J(O.Consumer);(expect(function()p.mountVirtualTree(z,nil,"\080\114\111\118\105\100\101 \084\114\101\101")end)).to.throw()end)it("\115\104\111\117\108\100 \114\101\116\117\114\110 \116\104\101 \100\101\102\097\117\108\116 \118\097\108\117\101 \105\102 \116\104\101\114\101 \105\115 \110\111 \080\114\111\118\105\100\101\114",function()local O=V()local z=L("\084\101\115\116")local l=J(z.Consumer,{render=O.value})local o=p.mountVirtualTree(l,nil,"\080\114\111\118\105\100\101 \084\114\101\101")p.unmountVirtualTree(o)O:assertCalledWith("\084\101\115\116")end)it("\115\104\111\117\108\100 \112\097\115\115 \116\104\101 \118\097\108\117\101 \116\111 \116\104\101 \114\101\110\100\101\114 \102\117\110\099\116\105\111\110",function()local O=V()local z=L("\084\101\115\116")local function l()return J(z.Consumer,{render=O.value})end local o=J(z.Provider,{value="\078\101\119\084\101\115\116"},{Listener=J(l)})local r=p.mountVirtualTree(o,nil,"\080\114\111\118\105\100\101 \084\114\101\101")p.unmountVirtualTree(r)O:assertCalledWith("\078\101\119\084\101\115\116")end)it("\115\104\111\117\108\100 \117\112\100\097\116\101 \119\104\101\110 \116\104\101 \118\097\108\117\101 \117\112\100\097\116\101\115",function()local O=V()local z=L("\084\101\115\116")local function l()return J(z.Consumer,{render=O.value})end local o=J(z.Provider,{value="\078\101\119\084\101\115\116"},{Listener=J(l)})local r=p.mountVirtualTree(o,nil,"\080\114\111\118\105\100\101 \084\114\101\101");(expect(O.callCount)).to.equal(1)O:assertCalledWith("\078\101\119\084\101\115\116")p.updateVirtualTree(r,J(z.Provider,{value="\084\104\105\114\100\084\101\115\116"},{Listener=J(l)}));(expect(O.callCount)).to.equal(2)O:assertCalledWith("\084\104\105\114\100\084\101\115\116")p.unmountVirtualTree(r)end)it("\115\104\111\117\108\100 \117\112\100\097\116\101 \119\104\101\110 \116\104\101 \118\097\108\117\101 \117\112\100\097\116\101\115 \116\104\114\111\117\103\104 \097\110 \117\112\100\097\116\101 \098\108\111\099\107\105\110\103 \099\111\109\112\111\110\101\110\116",function()local O=V()local z=L("\084\101\115\116")local r=F:extend("\085\112\100\097\116\101\066\108\111\099\107\101\114")function r.render(O)return o(O.props[l])end function r.shouldUpdate(O)return false end local function P()return J(z.Consumer,{render=O.value})end local Q=J(z.Provider,{value="\078\101\119\084\101\115\116"},{Blocker=J(r,nil,{Listener=J(P)})})local U=p.mountVirtualTree(Q,nil,"\080\114\111\118\105\100\101 \084\114\101\101");(expect(O.callCount)).to.equal(1)O:assertCalledWith("\078\101\119\084\101\115\116")p.updateVirtualTree(U,J(z.Provider,{value="\084\104\105\114\100\084\101\115\116"},{Blocker=J(r,nil,{Listener=J(P)})}));(expect(O.callCount)).to.equal(2)O:assertCalledWith("\084\104\105\114\100\084\101\115\116")p.unmountVirtualTree(U)end)it("\115\104\111\117\108\100 \098\101\104\097\118\101 \099\111\114\114\101\099\116\108\121 \119\104\101\110 \116\104\101 \100\101\102\097\117\108\116 \118\097\108\117\101 \105\115 \110\105\108",function()local O=L(nil)local z=V()local function l()return J(O.Consumer,{render=z.value})end local o=p.mountVirtualTree(J(l),nil,"\080\114\111\118\105\100\101 \084\114\101\101");(expect(z.callCount)).to.equal(1)z:assertCalledWith(nil)o=p.updateVirtualTree(o,J(l))p.unmountVirtualTree(o);(expect(z.callCount)).to.equal(2)z:assertCalledWith(nil)end)end)describe("\085\112\100\097\116\101 \111\114\100\101\114",function()it("\115\104\111\117\108\100 \117\112\100\097\116\101 \099\111\110\116\101\120\116 \097\116 \116\104\101 \115\097\109\101 \116\105\109\101 \097\115 \112\114\111\112\115",function()local O=false local z=false local l=0 local o=L("\100\101\102\097\117\108\116")local function r(r)return J(o.Consumer,{render=function(o)l=l+1 if o=="\099\111\110\116\101\120\116\095\049"then(expect(r.someProp)).to.equal("\112\114\111\112\095\049")O=true elseif o=="\099\111\110\116\101\120\116\095\050"then(expect(r.someProp)).to.equal("\112\114\111\112\095\050")z=true else error("\085\110\101\120\112\101\099\116\101\100 \099\111\110\116\101\120\116 \118\097\108\117\101")end end})end local P=J(o.Provider,{value="\099\111\110\116\101\120\116\095\049"},{Child=J(r,{someProp="\112\114\111\112\095\049"})})local F=J(o.Provider,{value="\099\111\110\116\101\120\116\095\050"},{Child=J(r,{someProp="\112\114\111\112\095\050"})})local V=p.mountVirtualTree(P,nil,"\085\112\100\097\116\101\079\098\115\101\114\118\097\116\105\111\110\073\115\070\117\110")p.updateVirtualTree(V,F);(expect(l)).to.equal(2);(expect(O)).to.equal(true);(expect(z)).to.equal(true)end)end)it("\100\111\101\115 \110\111\116 \116\104\114\111\119 \105\102 \119\105\108\108\085\110\109\111\117\110\116 \105\115 \099\097\108\108\101\100 \116\119\105\099\101 \111\110 \097 \099\111\110\116\101\120\116 \099\111\110\115\117\109\101\114",function()local z=L({})local l=F:extend("\076\111\119\101\115\116\067\111\109\112\111\110\101\110\116")function l.init(O) end function l.render(O)return J("\070\114\097\109\101")end function l.didMount(O)O.props.onDidMountCallback()end local o=F:extend("\070\105\114\115\116\067\111\109\112\111\110\101\110\116")function o.init(O) end function o.render(O)return J(z.Consumer,{render=function()return J("\084\101\120\116\076\097\098\101\108")end})end local r=F:extend("\067\104\105\108\100\067\111\109\112\111\110\101\110\116")function r.init(O)O:setState({firstTime=true})end local P function r.render(O)if O.state.firstTime then return J(o)end return J(l,{onDidMountCallback=O.props.onDidMountCallback})end function r.didMount(O)P=function()O:setState({firstTime=false})end end local V=F:extend("\080\097\114\101\110\116\067\111\109\112\111\110\101\110\116")local p=0 function V.init(O)O:setState({count=1})O.onDidMountCallback=function()p=p+1 if O.state.count<5 then O:setState({count=O.state.count+1})end end end function V.render(O)return J("\070\114\097\109\101",{},{Provider=J(z.Provider,{value={}},{ChildComponent=J(r,{count=O.state.count;onDidMountCallback=O.onDidMountCallback})})})end local U=Instance.new("\083\099\114\101\101\110\071\117\105")U.Parent=O local j="\083\111\109\101 \075\101\121"local h=Q.mountVirtualNode(J(V),U,j);(expect(function()P()end)).never.to.throw()Q.unmountVirtualNode(h)U:Destroy()end)end
return function()
	local ReplicatedStorage = game:GetService("ReplicatedStorage")

	local createReconciler = require(script.Parent.Parent.createReconciler)
	local Children = require(script.Parent.Parent.PropMarkers.Children)
	local createFragment = require(script.Parent.Parent.createFragment)
	local RobloxRenderer = require(script.Parent.Parent.RobloxRenderer)
	local createElement = require(script.Parent.Parent.createElement)
	local NoopRenderer = require(script.Parent.Parent.NoopRenderer)
	local Component = require(script.Parent.Parent.Component)
	local createSpy = require(script.Parent.Parent.createSpy)

	local createContext = require(script.Parent)

	local noopReconciler = createReconciler(NoopRenderer)

	local robloxReconciler = createReconciler(RobloxRenderer)

	it("should expose a function", function()
		expect(createContext).to.be.a("function")
	end)

	it("should return a table", function()
		local context = createContext("Test")
		expect(context).to.be.ok()
		expect(type(context)).to.equal("table")
	end)

	it("should throw on attempt to modify the returned table", function()
		local context = createContext("Test")

		expect(function()
			context.new = {}
		end).to.throw()

		expect(function()
			setmetatable(context, {})
		end).to.throw()
	end)

	it("should contain a Provider and a Consumer", function()
		local context = createContext("Test")
		expect(context.Provider).to.be.ok()
		expect(context.Consumer).to.be.ok()
	end)

	describe("Provider", function()
		it("should render its children", function()
			local context = createContext("Test")

			local Listener = createSpy(function()
				return nil
			end)

			local element = createElement(context.Provider, {
				value = "Test",
			}, {
				Listener = createElement(Listener.value),
			})

			local tree = noopReconciler.mountVirtualTree(element, nil, "Provide Tree")
			noopReconciler.unmountVirtualTree(tree)

			expect(Listener.callCount).to.equal(1)
		end)
	end)

	describe("Consumer", function()
		it("should expect a render function", function()
			local context = createContext("Test")
			local element = createElement(context.Consumer)

			expect(function()
				noopReconciler.mountVirtualTree(element, nil, "Provide Tree")
			end).to.throw()
		end)

		it("should return the default value if there is no Provider", function()
			local valueSpy = createSpy()
			local context = createContext("Test")

			local element = createElement(context.Consumer, {
				render = valueSpy.value,
			})

			local tree = noopReconciler.mountVirtualTree(element, nil, "Provide Tree")
			noopReconciler.unmountVirtualTree(tree)

			valueSpy:assertCalledWith("Test")
		end)

		it("should pass the value to the render function", function()
			local valueSpy = createSpy()
			local context = createContext("Test")

			local function Listener()
				return createElement(context.Consumer, {
					render = valueSpy.value,
				})
			end

			local element = createElement(context.Provider, {
				value = "NewTest",
			}, {
				Listener = createElement(Listener),
			})

			local tree = noopReconciler.mountVirtualTree(element, nil, "Provide Tree")
			noopReconciler.unmountVirtualTree(tree)

			valueSpy:assertCalledWith("NewTest")
		end)

		it("should update when the value updates", function()
			local valueSpy = createSpy()
			local context = createContext("Test")

			local function Listener()
				return createElement(context.Consumer, {
					render = valueSpy.value,
				})
			end

			local element = createElement(context.Provider, {
				value = "NewTest",
			}, {
				Listener = createElement(Listener),
			})

			local tree = noopReconciler.mountVirtualTree(element, nil, "Provide Tree")

			expect(valueSpy.callCount).to.equal(1)
			valueSpy:assertCalledWith("NewTest")

			noopReconciler.updateVirtualTree(
				tree,
				createElement(context.Provider, {
					value = "ThirdTest",
				}, {
					Listener = createElement(Listener),
				})
			)

			expect(valueSpy.callCount).to.equal(2)
			valueSpy:assertCalledWith("ThirdTest")

			noopReconciler.unmountVirtualTree(tree)
		end)

		--[[
			This test is the same as the one above, but with a component that
			always blocks updates in the middle. We expect behavior to be the
			same.
		]]
		it("should update when the value updates through an update blocking component", function()
			local valueSpy = createSpy()
			local context = createContext("Test")

			local UpdateBlocker = Component:extend("UpdateBlocker")

			function UpdateBlocker:render()
				return createFragment(self.props[Children])
			end

			function UpdateBlocker:shouldUpdate()
				return false
			end

			local function Listener()
				return createElement(context.Consumer, {
					render = valueSpy.value,
				})
			end

			local element = createElement(context.Provider, {
				value = "NewTest",
			}, {
				Blocker = createElement(UpdateBlocker, nil, {
					Listener = createElement(Listener),
				}),
			})

			local tree = noopReconciler.mountVirtualTree(element, nil, "Provide Tree")

			expect(valueSpy.callCount).to.equal(1)
			valueSpy:assertCalledWith("NewTest")

			noopReconciler.updateVirtualTree(
				tree,
				createElement(context.Provider, {
					value = "ThirdTest",
				}, {
					Blocker = createElement(UpdateBlocker, nil, {
						Listener = createElement(Listener),
					}),
				})
			)

			expect(valueSpy.callCount).to.equal(2)
			valueSpy:assertCalledWith("ThirdTest")

			noopReconciler.unmountVirtualTree(tree)
		end)

		it("should behave correctly when the default value is nil", function()
			local context = createContext(nil)

			local valueSpy = createSpy()
			local function Listener()
				return createElement(context.Consumer, {
					render = valueSpy.value,
				})
			end

			local tree = noopReconciler.mountVirtualTree(createElement(Listener), nil, "Provide Tree")
			expect(valueSpy.callCount).to.equal(1)
			valueSpy:assertCalledWith(nil)

			tree = noopReconciler.updateVirtualTree(tree, createElement(Listener))
			noopReconciler.unmountVirtualTree(tree)

			expect(valueSpy.callCount).to.equal(2)
			valueSpy:assertCalledWith(nil)
		end)
	end)

	describe("Update order", function()
		--[[
			This test ensures that there is no scenario where we can observe
			'update tearing' when props and context are updated at the same
			time.

			Update tearing is scenario where a single update is partially
			applied in multiple steps instead of atomically. This is observable
			by components and can lead to strange bugs or errors.

			This instance of update tearing happens when updating a prop and a
			context value in the same update. Image we represent our tree's
			state as the current prop and context versions. Our initial state
			is:

			(prop_1, context_1)

			The next state we would like to update to is:

			(prop_2, context_2)

			Under the bug reported in issue 259, Roact reaches three different
			states in sequence:

			1: (prop_1, context_1) - the initial state
			2: (prop_2, context_1) - woops!
			3: (prop_2, context_2) - correct end state

			In state 2, a user component was added that tried to access the
			current context value, which was not set at the time. This raised an
			error, because this state is not valid!

			The first proposed solution was to move the context update to happen
			before the props update. It is easy to show that this will still
			result in update tearing:

			1: (prop_1, context_1)
			2: (prop_1, context_2)
			3: (prop_2, context_2)

			Although the initial concern about newly added components observing
			old context values is fixed, there is still a state
			desynchronization between props and state.

			We would instead like the following update sequence:

			1: (prop_1, context_1)
			2: (prop_2, context_2)

			This test tries to ensure that is the case.

			The initial bug report is here:
			https://github.com/Roblox/roact/issues/259
		]]
		it("should update context at the same time as props", function()
			-- These values are used to make sure we reach both the first and
			-- second state combinations we want to visit.
			local observedA = false
			local observedB = false
			local updateCount = 0

			local context = createContext("default")

			local function Listener(props)
				return createElement(context.Consumer, {
					render = function(value)
						updateCount = updateCount + 1

						if value == "context_1" then
							expect(props.someProp).to.equal("prop_1")
							observedA = true
						elseif value == "context_2" then
							expect(props.someProp).to.equal("prop_2")
							observedB = true
						else
							error("Unexpected context value")
						end
					end,
				})
			end

			local element1 = createElement(context.Provider, {
				value = "context_1",
			}, {
				Child = createElement(Listener, {
					someProp = "prop_1",
				}),
			})

			local element2 = createElement(context.Provider, {
				value = "context_2",
			}, {
				Child = createElement(Listener, {
					someProp = "prop_2",
				}),
			})

			local tree = noopReconciler.mountVirtualTree(element1, nil, "UpdateObservationIsFun")
			noopReconciler.updateVirtualTree(tree, element2)

			expect(updateCount).to.equal(2)
			expect(observedA).to.equal(true)
			expect(observedB).to.equal(true)
		end)
	end)

	-- issue https://github.com/Roblox/roact/issues/319
	it("does not throw if willUnmount is called twice on a context consumer", function()
		local context = createContext({})

		local LowestComponent = Component:extend("LowestComponent")
		function LowestComponent:init() end

		function LowestComponent:render()
			return createElement("Frame")
		end

		function LowestComponent:didMount()
			self.props.onDidMountCallback()
		end

		local FirstComponent = Component:extend("FirstComponent")
		function FirstComponent:init() end

		function FirstComponent:render()
			return createElement(context.Consumer, {
				render = function()
					return createElement("TextLabel")
				end,
			})
		end

		local ChildComponent = Component:extend("ChildComponent")

		function ChildComponent:init()
			self:setState({ firstTime = true })
		end

		local childCallback

		function ChildComponent:render()
			if self.state.firstTime then
				return createElement(FirstComponent)
			end

			return createElement(LowestComponent, {
				onDidMountCallback = self.props.onDidMountCallback,
			})
		end

		function ChildComponent:didMount()
			childCallback = function()
				self:setState({ firstTime = false })
			end
		end

		local ParentComponent = Component:extend("ParentComponent")

		local didMountCallbackCalled = 0

		function ParentComponent:init()
			self:setState({ count = 1 })

			self.onDidMountCallback = function()
				didMountCallbackCalled = didMountCallbackCalled + 1
				if self.state.count < 5 then
					self:setState({ count = self.state.count + 1 })
				end
			end
		end

		function ParentComponent:render()
			return createElement("Frame", {}, {
				Provider = createElement(context.Provider, {
					value = {},
				}, {
					ChildComponent = createElement(ChildComponent, {
						count = self.state.count,
						onDidMountCallback = self.onDidMountCallback,
					}),
				}),
			})
		end

		local parent = Instance.new("ScreenGui")
		parent.Parent = ReplicatedStorage

		local hostKey = "Some Key"
		local node = robloxReconciler.mountVirtualNode(createElement(ParentComponent), parent, hostKey)

		expect(function()
			-- calling setState on ChildComponent will trigger `willUnmount` multiple times
			childCallback()
		end).never.to.throw()

		robloxReconciler.unmountVirtualNode(node)
		parent:Destroy()
	end)
end
