return function()local p=require(script.Parent.Parent.Parent.FunctionUtils.noop)local G=require(script.Parent.Parent.Parent.Roact)local g=require(script.Parent.Parent.Parent.Rodux)local t=require(script.Parent.Parent.StoreProvider)local a=require(script.Parent)local function m()return nil end local function Z(p,G)p=p or 0 if G.type=="\105\110\099\114\101\109\101\110\116"then return p+1 end return p end local J=g.combineReducers({count=Z})describe("\065\114\103\117\109\101\110\116 \118\097\108\105\100\097\116\105\111\110",function()it("\115\104\111\117\108\100 \097\099\099\101\112\116 \110\111 \097\114\103\117\109\101\110\116\115",function()a()end)it("\115\104\111\117\108\100 \097\099\099\101\112\116 \111\110\101 \102\117\110\099\116\105\111\110",function()a(p)end)it("\115\104\111\117\108\100 \097\099\099\101\112\116 \116\119\111 \102\117\110\099\116\105\111\110\115",function()a(p,p)end)it("\115\104\111\117\108\100 \097\099\099\101\112\116 \111\110\108\121 \116\104\101 \115\101\099\111\110\100 \102\117\110\099\116\105\111\110",function()a(nil,function() end)end)it("\115\104\111\117\108\100 \097\099\099\101\112\116 \111\110\101 \116\097\098\108\101 \111\102 \097\099\116\105\111\110 \099\114\101\097\116\111\114\115",function()a(nil,{foo=function() end})end)it("\115\104\111\117\108\100 \116\104\114\111\119 \105\102 \110\111\116 \112\097\115\115\101\100 \097 \099\111\109\112\111\110\101\110\116",function()local p=function(p)return{}end;(expect(function()(a(p))(nil)end)).to.throw()end)end)it("\115\104\111\117\108\100 \116\104\114\111\119 \105\102 \110\111\116 \109\111\117\110\116\101\100 \117\110\100\101\114 \097 \083\116\111\114\101\080\114\111\118\105\100\101\114",function()local p=(a())(m);(expect(function()G.mount(G.createElement(p))end)).to.throw()end)it("\115\104\111\117\108\100 \097\099\099\101\112\116 \097 \104\105\103\104\101\114-\111\114\100\101\114 \102\117\110\099\116\105\111\110 \109\097\112\083\116\097\116\101\084\111\080\114\111\112\115",function()local function p()return function(p)return{count=p.count}end end local Z=(a(p))(m)local d=g.createStore(J)local K=G.createElement(t,{store=d},{someComponent=G.createElement(Z)})local S=G.mount(K)G.unmount(S)end)it("\115\104\111\117\108\100 \110\111\116 \097\099\099\101\112\116 \097 \104\105\103\104\101\114-\111\114\100\101\114 \109\097\112\083\116\097\116\101\084\111\080\114\111\112\115 \116\104\097\116 \114\101\116\117\114\110\115 \097 \110\111\110-\116\097\098\108\101 \118\097\108\117\101",function()local function p()return function(p)return"\110\111\112\101"end end local Z=(a(p))(m)local d=g.createStore(J)local K=G.createElement(t,{store=d},{someComponent=G.createElement(Z)});(expect(function()G.mount(K)end)).to.throw()end)it("\115\104\111\117\108\100 \110\111\116 \097\099\099\101\112\116 \097 \109\097\112\083\116\097\116\101\084\111\080\114\111\112\115 \116\104\097\116 \114\101\116\117\114\110\115 \097 \110\111\110-\116\097\098\108\101 \118\097\108\117\101",function()local function p()return"\110\097\104"end local Z=(a(p))(m)local d=g.createStore(J)local K=G.createElement(t,{store=d},{someComponent=G.createElement(Z)});(expect(function()G.mount(K)end)).to.throw()end)it("\115\104\111\117\108\100 \097\098\111\114\116 \114\101\110\100\101\114\115 \119\104\101\110 \109\097\112\083\116\097\116\101\084\111\080\114\111\112\115 \114\101\116\117\114\110\115 \116\104\101 \115\097\109\101 \100\097\116\097",function()local function p(p)return{count=p.count}end local m=0 local function Z(p)m=m+1 end local d=(a(p))(Z)local K=g.createStore(J)local S=G.createElement(t,{store=K},{someComponent=G.createElement(d)})local T=G.mount(S);(expect(m)).to.equal(1)K:dispatch({type="\097\110 \117\110\107\110\111\119\110 \097\099\116\105\111\110"})K:flush();(expect(m)).to.equal(1)K:dispatch({type="\105\110\099\114\101\109\101\110\116"})K:flush();(expect(m)).to.equal(2)G.unmount(T)end)it("\115\104\111\117\108\100 \111\110\108\121 \099\097\108\108 \109\097\112\068\105\115\112\097\116\099\104\084\111\080\114\111\112\115 \111\110\099\101 \097\110\100 \110\101\118\101\114 \114\101-\114\101\110\100\101\114 \105\102 \110\111 \109\097\112\083\116\097\116\101\084\111\080\114\111\112\115 \119\097\115 \112\097\115\115\101\100",function()local p=0 local m=function(G)p=p+1 return{increment=function()return G({type="\105\110\099\114\101\109\101\110\116"})end}end local Z=0 local function d(p)Z=Z+1 end local K=(a(nil,m))(d)local S=g.createStore(J)local T=G.createElement(t,{store=S},{someComponent=G.createElement(K)})local C=G.mount(T);(expect(p)).to.equal(1);(expect(Z)).to.equal(1)S:dispatch({type="\097\110 \117\110\107\110\111\119\110 \097\099\116\105\111\110"})S:flush();(expect(p)).to.equal(1);(expect(Z)).to.equal(1)S:dispatch({type="\105\110\099\114\101\109\101\110\116"})S:flush();(expect(p)).to.equal(1);(expect(Z)).to.equal(1)G.unmount(C)end)it("\115\104\111\117\108\100 \100\105\115\112\097\116\099\104 \116\104\101 \097\099\116\105\111\110 \117\115\105\110\103 \097 \116\097\098\108\101 \111\102 \097\099\116\105\111\110 \099\114\101\097\116\111\114\115",function()local p={increment=function()return{type="\105\110\099\114\101\109\101\110\116"}end}local function m(p)p.increment()end local Z=(a(nil,p))(m)local d=g.createStore(J)local K=G.createElement(t,{store=d},{someComponent=G.createElement(Z)})local S=G.mount(K)d.changed:connect(function(p)(expect(p.count)).to.equal(1)end)G.unmount(S)end)it("\115\104\111\117\108\100 \114\101\116\117\114\110 \114\101\115\117\108\116 \118\097\108\117\101\115 \102\114\111\109 \116\104\101 \100\105\115\112\097\116\099\104 \112\097\115\115\101\100 \116\111 \109\097\112\068\105\115\112\097\116\099\104\084\111\080\114\111\112\115",function()local function p()return 0 end local function m()return 5 end local Z local function J(p)Z=p.dispatch end local function d(p)return{dispatch=p}end local K=(a(nil,d))(J)local S=g.createStore(p,nil,{g.thunkMiddleware})local T=G.createElement(t,{store=S},{someComponent=G.createElement(K)})local C=G.mount(T);(expect(Z)).to.be.a("\102\117\110\099\116\105\111\110");(expect(Z(m))).to.equal(5)G.unmount(C)end)it("\115\104\111\117\108\100 \097\108\108\111\119 \102\105\101\108\100\115 \116\111 \098\101 \097\115\115\105\103\110\101\100 \111\110 \099\111\110\110\101\099\116\101\100 \099\111\109\112\111\110\101\110\116\115",function()local function p(p)return{count=p.count}end local G=(a(p))(m);(expect(function()G.SomeEnum={Value=1}end)).never.to.throw();(expect(G.SomeEnum)).to.be.ok();(expect(G.SomeEnum.Value)).to.equal(1)end)it("\115\104\111\117\108\100 \110\101\118\101\114 \112\097\115\115 \116\104\101 \115\116\111\114\101 \097\110\100 \105\110\110\101\114\080\114\111\112\115 \116\111 \096\109\097\112\083\116\097\116\101\084\111\080\114\111\112\115\096",function()local p={}local Z=nil local function d(G,g)Z=g;(expect(g.store)).to.equal(nil);(expect(g.innerProps)).to.equal(nil);(expect(g.somePropName)).to.equal(p)return{count=G.count}end local K=(a(d))(m)local S=g.createStore(J)local T=G.createElement(t,{store=S},{parent=G.createElement(K,{somePropName=p})})local C=G.mount(T)S:dispatch({type="\105\110\099\114\101\109\101\110\116"})S:flush();(expect(Z.store)).to.equal(nil);(expect(Z.innerProps)).to.equal(nil);(expect(Z.somePropName)).to.equal(p)G.unmount(C)end)end
return function()
	local noop = require(script.Parent.Parent.Parent.FunctionUtils.noop)
	local Roact = require(script.Parent.Parent.Parent.Roact)
	local Rodux = require(script.Parent.Parent.Parent.Rodux)

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

	local connect = require(script.Parent)

	local function NoopComponent()
		return nil
	end

	local function countReducer(state, action)
		state = state or 0

		if action.type == "increment" then
			return state + 1
		end

		return state
	end

	local reducer = Rodux.combineReducers({
		count = countReducer,
	})

	describe("Argument validation", function()
		it("should accept no arguments", function()
			connect()
		end)

		it("should accept one function", function()
			connect(noop)
		end)

		it("should accept two functions", function()
			connect(noop, noop)
		end)

		it("should accept only the second function", function()
			connect(nil, function() end)
		end)

		it("should accept one table of action creators", function()
			connect(nil, {
				foo = function() end,
			})
		end)

		it("should throw if not passed a component", function()
			local selector = function(store)
				return {}
			end

			expect(function()
				connect(selector)(nil)
			end).to.throw()
		end)
	end)

	it("should throw if not mounted under a StoreProvider", function()
		local ConnectedSomeComponent = connect()(NoopComponent)

		expect(function()
			Roact.mount(Roact.createElement(ConnectedSomeComponent))
		end).to.throw()
	end)

	it("should accept a higher-order function mapStateToProps", function()
		local function mapStateToProps()
			return function(state)
				return {
					count = state.count,
				}
			end
		end

		local ConnectedSomeComponent = connect(mapStateToProps)(NoopComponent)

		local store = Rodux.createStore(reducer)

		local tree = Roact.createElement(StoreProvider, {
			store = store,
		}, {
			someComponent = Roact.createElement(ConnectedSomeComponent),
		})

		local handle = Roact.mount(tree)

		Roact.unmount(handle)
	end)

	it("should not accept a higher-order mapStateToProps that returns a non-table value", function()
		local function mapStateToProps()
			return function(state)
				return "nope"
			end
		end

		local ConnectedSomeComponent = connect(mapStateToProps)(NoopComponent)

		local store = Rodux.createStore(reducer)
		local tree = Roact.createElement(StoreProvider, {
			store = store,
		}, {
			someComponent = Roact.createElement(ConnectedSomeComponent),
		})

		expect(function()
			Roact.mount(tree)
		end).to.throw()
	end)

	it("should not accept a mapStateToProps that returns a non-table value", function()
		local function mapStateToProps()
			return "nah"
		end

		local ConnectedSomeComponent = connect(mapStateToProps)(NoopComponent)

		local store = Rodux.createStore(reducer)
		local tree = Roact.createElement(StoreProvider, {
			store = store,
		}, {
			someComponent = Roact.createElement(ConnectedSomeComponent),
		})

		expect(function()
			Roact.mount(tree)
		end).to.throw()
	end)

	it("should abort renders when mapStateToProps returns the same data", function()
		local function mapStateToProps(state)
			return {
				count = state.count,
			}
		end

		local renderCount = 0
		local function SomeComponent(props)
			renderCount = renderCount + 1
		end

		local ConnectedSomeComponent = connect(mapStateToProps)(SomeComponent)

		local store = Rodux.createStore(reducer)
		local tree = Roact.createElement(StoreProvider, {
			store = store,
		}, {
			someComponent = Roact.createElement(ConnectedSomeComponent),
		})

		local handle = Roact.mount(tree)

		expect(renderCount).to.equal(1)

		store:dispatch({ type = "an unknown action" })
		store:flush()

		expect(renderCount).to.equal(1)

		store:dispatch({ type = "increment" })
		store:flush()

		expect(renderCount).to.equal(2)

		Roact.unmount(handle)
	end)

	it("should only call mapDispatchToProps once and never re-render if no mapStateToProps was passed", function()
		local dispatchCount = 0
		local mapDispatchToProps = function(dispatch)
			dispatchCount = dispatchCount + 1

			return {
				increment = function()
					return dispatch({ type = "increment" })
				end,
			}
		end

		local renderCount = 0
		local function SomeComponent(props)
			renderCount = renderCount + 1
		end

		local ConnectedSomeComponent = connect(nil, mapDispatchToProps)(SomeComponent)

		local store = Rodux.createStore(reducer)
		local tree = Roact.createElement(StoreProvider, {
			store = store,
		}, {
			someComponent = Roact.createElement(ConnectedSomeComponent),
		})

		local handle = Roact.mount(tree)

		expect(dispatchCount).to.equal(1)
		expect(renderCount).to.equal(1)

		store:dispatch({ type = "an unknown action" })
		store:flush()

		expect(dispatchCount).to.equal(1)
		expect(renderCount).to.equal(1)

		store:dispatch({ type = "increment" })
		store:flush()

		expect(dispatchCount).to.equal(1)
		expect(renderCount).to.equal(1)

		Roact.unmount(handle)
	end)

	it("should dispatch the action using a table of action creators", function()
		local mapDispatchToProps = {
			increment = function()
				return {
					type = "increment",
				}
			end,
		}

		local function SomeComponent(props)
			props.increment()
		end

		local ConnectedSomeComponent = connect(nil, mapDispatchToProps)(SomeComponent)

		local store = Rodux.createStore(reducer)
		local tree = Roact.createElement(StoreProvider, {
			store = store,
		}, {
			someComponent = Roact.createElement(ConnectedSomeComponent),
		})

		local handle = Roact.mount(tree)

		store.changed:connect(function(state)
			expect(state.count).to.equal(1)
		end)

		Roact.unmount(handle)
	end)

	it("should return result values from the dispatch passed to mapDispatchToProps", function()
		local function reducer()
			return 0
		end

		local function fiveThunk()
			return 5
		end

		local dispatch
		local function SomeComponent(props)
			dispatch = props.dispatch
		end

		local function mapDispatchToProps(dispatch)
			return {
				dispatch = dispatch,
			}
		end

		local ConnectedSomeComponent = connect(nil, mapDispatchToProps)(SomeComponent)

		-- We'll use the thunk middleware, as it should always return its result
		local store = Rodux.createStore(reducer, nil, { Rodux.thunkMiddleware })
		local tree = Roact.createElement(StoreProvider, {
			store = store,
		}, {
			someComponent = Roact.createElement(ConnectedSomeComponent),
		})

		local handle = Roact.mount(tree)

		expect(dispatch).to.be.a("function")
		expect(dispatch(fiveThunk)).to.equal(5)

		Roact.unmount(handle)
	end)

	it("should allow fields to be assigned on connected components", function()
		local function mapStateToProps(state)
			return {
				count = state.count,
			}
		end

		local ConnectedSomeComponent = connect(mapStateToProps)(NoopComponent)

		expect(function()
			ConnectedSomeComponent.SomeEnum = {
				Value = 1,
			}
		end).never.to.throw()

		expect(ConnectedSomeComponent.SomeEnum).to.be.ok()
		expect(ConnectedSomeComponent.SomeEnum.Value).to.equal(1)
	end)

	-- Issue https://github.com/Roblox/roact-rodux/issues/48
	it("should never pass the store and innerProps to `mapStateToProps`", function()
		local somePropValue = {}
		local lastMappedProps = nil
		local function mapStateToProps(state, props)
			lastMappedProps = props
			expect(props.store).to.equal(nil)
			expect(props.innerProps).to.equal(nil)
			expect(props.somePropName).to.equal(somePropValue)
			return {
				count = state.count,
			}
		end
		local ConnectedComponent = connect(mapStateToProps)(NoopComponent)

		local store = Rodux.createStore(reducer)
		local tree = Roact.createElement(StoreProvider, {
			store = store,
		}, {
			parent = Roact.createElement(ConnectedComponent, {
				somePropName = somePropValue,
			}),
		})

		local handle = Roact.mount(tree)

		store:dispatch({ type = "increment" })
		store:flush()

		expect(lastMappedProps.store).to.equal(nil)
		expect(lastMappedProps.innerProps).to.equal(nil)
		expect(lastMappedProps.somePropName).to.equal(somePropValue)

		Roact.unmount(handle)
	end)
end
