return function()local Y=require(script.Parent.Parent.assertDeepEqual)local i=require(script.Parent.Parent.createElement)local h=require(script.Parent.Parent.createReconciler)local x=require(script.Parent.Parent.NoopRenderer)local C=require(script.Parent.Parent.Component)local P=h(x)it("\115\104\111\117\108\100 \098\101 \112\114\111\118\105\100\101\100 \097\115 \097 \109\117\116\097\098\108\101 \115\101\108\102\046\095\099\111\110\116\101\120\116 \105\110 \067\111\109\112\111\110\101\110\116\058\105\110\105\116",function()local h=C:extend("\080\114\111\118\105\100\101\114")function h.init(Y)Y._context.foo="\098\097\114"end function h.render(Y) end local x=i(h)local R=nil local K="\080\114\111\118\105\100\101\114"local A=P.mountVirtualNode(x,R,K)local q={foo="\098\097\114"}Y(A.legacyContext,q)end)it("\115\104\111\117\108\100 \098\101 \105\110\104\101\114\105\116\101\100 \102\114\111\109 \112\097\114\101\110\116 \115\116\097\116\101\102\117\108 \110\111\100\101\115",function()local h=C:extend("\067\111\110\115\117\109\101\114")local x function h.init(Y)x=Y._context end function h.render(Y) end local R=C:extend("\080\097\114\101\110\116")function R.render(Y)return i(h)end local K=i(R)local A=nil local q="\080\097\114\101\110\116"local k={hello="\119\111\114\108\100";value=6}local H=P.mountVirtualNode(K,A,q,nil,k);(expect(x)).never.to.equal(k);(expect(x)).never.to.equal(H.legacyContext)Y(H.legacyContext,k)Y(x,k)end)it("\115\104\111\117\108\100 \098\101 \105\110\104\101\114\105\116\101\100 \102\114\111\109 \112\097\114\101\110\116 \102\117\110\099\116\105\111\110 \110\111\100\101\115",function()local h=C:extend("\067\111\110\115\117\109\101\114")local x function h.init(Y)x=Y._context end function h.render(Y) end local function R()return i(h)end local K=i(R)local A=nil local q="\080\097\114\101\110\116"local k={hello="\119\111\114\108\100";value=6}local H=P.mountVirtualNode(K,A,q,nil,k);(expect(x)).never.to.equal(k);(expect(x)).never.to.equal(H.legacyContext)Y(H.legacyContext,k)Y(x,k)end)it("\115\104\111\117\108\100 \099\111\110\116\097\105\110 \118\097\108\117\101\115 \112\117\116 \105\110\116\111 \116\104\101 \116\114\101\101 \098\121 \112\097\114\101\110\116 \110\111\100\101\115",function()local h=C:extend("\067\111\110\115\117\109\101\114")local x function h.init(Y)x=Y._context end function h.render(Y) end local R=C:extend("\080\114\111\118\105\100\101\114")function R.init(Y)Y._context.frob="\117\108\097\116\111\114"end function R.render(Y)return i(h)end local K=i(R)local A=nil local q="\067\111\110\115\117\109\101\114"local k={dont="\116\114\121 \105\116"}local H=P.mountVirtualNode(K,A,q,nil,k)local D={dont="\116\114\121 \105\116"}local N={dont="\116\114\121 \105\116",frob="\117\108\097\116\111\114"};(expect(H.legacyContext)).never.to.equal(k);(expect(x)).never.to.equal(k);(expect(x)).never.to.equal(H.legacyContext)Y(k,D)Y(H.legacyContext,N)Y(x,N)end)it("\115\104\111\117\108\100 \116\114\097\110\115\102\101\114 \099\111\110\116\101\120\116 \116\111 \099\104\105\108\100\114\101\110 \116\104\097\116 \097\114\101 \114\101\112\108\097\099\101\100",function()local h=C:extend("\067\111\110\115\117\109\101\114\065")local x function h.init(Y)Y._context.A="\104\101\108\108\111"x=Y._context end function h.render(Y) end local R=C:extend("\067\111\110\115\117\109\101\114\066")local K function R.init(Y)Y._context.B="\104\101\108\108\111"K=Y._context end function R.render(Y) end local A=C:extend("\080\114\111\118\105\100\101\114")function A.init(Y)Y._context.frob="\117\108\097\116\111\114"end function A.render(Y)local x=Y.props.useConsumerB if x then return i(R)else return i(h)end end local q=nil local k="\067\111\110\115\117\109\101\114"local H=i(A)local D=P.mountVirtualNode(H,q,k)local N={frob="\117\108\097\116\111\114",A="\104\101\108\108\111"}Y(x,N)local Z={frob="\117\108\097\116\111\114";B="\104\101\108\108\111"}local J=i(A,{useConsumerB=true})P.updateVirtualNode(D,J)Y(K,Z)end)end
return function()
	local assertDeepEqual = require(script.Parent.Parent.assertDeepEqual)
	local createElement = require(script.Parent.Parent.createElement)
	local createReconciler = require(script.Parent.Parent.createReconciler)
	local NoopRenderer = require(script.Parent.Parent.NoopRenderer)

	local Component = require(script.Parent.Parent.Component)

	local noopReconciler = createReconciler(NoopRenderer)

	it("should be provided as a mutable self._context in Component:init", function()
		local Provider = Component:extend("Provider")

		function Provider:init()
			self._context.foo = "bar"
		end

		function Provider:render() end

		local element = createElement(Provider)
		local hostParent = nil
		local hostKey = "Provider"
		local node = noopReconciler.mountVirtualNode(element, hostParent, hostKey)

		local expectedContext = {
			foo = "bar",
		}

		assertDeepEqual(node.legacyContext, expectedContext)
	end)

	it("should be inherited from parent stateful nodes", function()
		local Consumer = Component:extend("Consumer")

		local capturedContext
		function Consumer:init()
			capturedContext = self._context
		end

		function Consumer:render() end

		local Parent = Component:extend("Parent")

		function Parent:render()
			return createElement(Consumer)
		end

		local element = createElement(Parent)
		local hostParent = nil
		local hostKey = "Parent"
		local context = {
			hello = "world",
			value = 6,
		}
		local node = noopReconciler.mountVirtualNode(element, hostParent, hostKey, nil, context)

		expect(capturedContext).never.to.equal(context)
		expect(capturedContext).never.to.equal(node.legacyContext)
		assertDeepEqual(node.legacyContext, context)
		assertDeepEqual(capturedContext, context)
	end)

	it("should be inherited from parent function nodes", function()
		local Consumer = Component:extend("Consumer")

		local capturedContext
		function Consumer:init()
			capturedContext = self._context
		end

		function Consumer:render() end

		local function Parent()
			return createElement(Consumer)
		end

		local element = createElement(Parent)
		local hostParent = nil
		local hostKey = "Parent"
		local context = {
			hello = "world",
			value = 6,
		}
		local node = noopReconciler.mountVirtualNode(element, hostParent, hostKey, nil, context)

		expect(capturedContext).never.to.equal(context)
		expect(capturedContext).never.to.equal(node.legacyContext)
		assertDeepEqual(node.legacyContext, context)
		assertDeepEqual(capturedContext, context)
	end)

	it("should contain values put into the tree by parent nodes", function()
		local Consumer = Component:extend("Consumer")

		local capturedContext
		function Consumer:init()
			capturedContext = self._context
		end

		function Consumer:render() end

		local Provider = Component:extend("Provider")

		function Provider:init()
			self._context.frob = "ulator"
		end

		function Provider:render()
			return createElement(Consumer)
		end

		local element = createElement(Provider)
		local hostParent = nil
		local hostKey = "Consumer"
		local context = {
			dont = "try it",
		}
		local node = noopReconciler.mountVirtualNode(element, hostParent, hostKey, nil, context)

		local initialContext = {
			dont = "try it",
		}

		local expectedContext = {
			dont = "try it",
			frob = "ulator",
		}

		-- Because components mutate context, we're careful with equality
		expect(node.legacyContext).never.to.equal(context)
		expect(capturedContext).never.to.equal(context)
		expect(capturedContext).never.to.equal(node.legacyContext)

		assertDeepEqual(context, initialContext)
		assertDeepEqual(node.legacyContext, expectedContext)
		assertDeepEqual(capturedContext, expectedContext)
	end)

	it("should transfer context to children that are replaced", function()
		local ConsumerA = Component:extend("ConsumerA")

		local capturedContextA
		function ConsumerA:init()
			self._context.A = "hello"

			capturedContextA = self._context
		end

		function ConsumerA:render() end

		local ConsumerB = Component:extend("ConsumerB")

		local capturedContextB
		function ConsumerB:init()
			self._context.B = "hello"

			capturedContextB = self._context
		end

		function ConsumerB:render() end

		local Provider = Component:extend("Provider")

		function Provider:init()
			self._context.frob = "ulator"
		end

		function Provider:render()
			local useConsumerB = self.props.useConsumerB

			if useConsumerB then
				return createElement(ConsumerB)
			else
				return createElement(ConsumerA)
			end
		end

		local hostParent = nil
		local hostKey = "Consumer"

		local element = createElement(Provider)
		local node = noopReconciler.mountVirtualNode(element, hostParent, hostKey)

		local expectedContextA = {
			frob = "ulator",
			A = "hello",
		}

		assertDeepEqual(capturedContextA, expectedContextA)

		local expectedContextB = {
			frob = "ulator",
			B = "hello",
		}

		local replacedElement = createElement(Provider, {
			useConsumerB = true,
		})
		noopReconciler.updateVirtualNode(node, replacedElement)

		assertDeepEqual(capturedContextB, expectedContextB)
	end)
end
