return function()local O=require(script.Parent.Store)describe("\110\101\119",function()it("\115\104\111\117\108\100 \105\110\115\116\097\110\116\105\097\116\101 \119\105\116\104 \097 \114\101\100\117\099\101\114",function()local Z=O.new(function(O,Z)return"\104\101\108\108\111\044 \119\111\114\108\100"end);(expect(Z)).to.be.ok();(expect(Z:getState())).to.equal("\104\101\108\108\111\044 \119\111\114\108\100")Z:destruct()end)it("\115\104\111\117\108\100 \105\110\115\116\097\110\116\105\097\116\101 \119\105\116\104 \097 \114\101\100\117\099\101\114 \097\110\100 \097\110 \105\110\105\116\105\097\108 \115\116\097\116\101",function()local Z=O.new(function(O,Z)return O end,"\105\110\105\116\105\097\108 \115\116\097\116\101");(expect(Z)).to.be.ok();(expect(Z:getState())).to.equal("\105\110\105\116\105\097\108 \115\116\097\116\101")Z:destruct()end)it("\115\104\111\117\108\100 \105\110\115\116\097\110\116\105\097\116\101 \119\105\116\104 \097 \114\101\100\117\099\101\114\044 \105\110\105\116\105\097\108 \115\116\097\116\101\044 \097\110\100 \109\105\100\100\108\101\119\097\114\101\115",function()local Z=O.new(function(O,Z)return O end,"\105\110\105\116\105\097\108 \115\116\097\116\101",{});(expect(Z)).to.be.ok();(expect(Z:getState())).to.equal("\105\110\105\116\105\097\108 \115\116\097\116\101")Z:destruct()end)it("\115\104\111\117\108\100 \109\111\100\105\102\121 \116\104\101 \100\105\115\112\097\116\099\104 \109\101\116\104\111\100 \119\104\101\110 \109\105\100\100\108\101\119\097\114\101\115 \097\114\101 \112\097\115\115\101\100",function()local Z=0 local p=0 local j local N local T local function c(O,Z)if Z.type=="\116\101\115\116"then return"\116\101\115\116 \115\116\097\116\101"end return O end local function C(O,c)Z=Z+1 j=O N=c return function(Z)p=p+1 T=Z O(Z)end end local Q=O.new(c,"\105\110\105\116\105\097\108 \115\116\097\116\101",{C});(expect(Z)).to.equal(1);(expect(p)).to.equal(0);(expect(j)).to.be.a("\102\117\110\099\116\105\111\110");(expect(N)).to.equal(Q)Q:dispatch({type="\116\101\115\116"});(expect(Z)).to.equal(1);(expect(p)).to.equal(1);(expect(T.type)).to.equal("\116\101\115\116")Q:flush();(expect(Q:getState())).to.equal("\116\101\115\116 \115\116\097\116\101")Q:destruct()end)it("\115\104\111\117\108\100 \101\120\101\099\117\116\101 \109\105\100\100\108\101\119\097\114\101 \108\101\102\116-\116\111-\114\105\103\104\116",function()local Z={}local function p(O)return O end local function j(O,p)table.insert(Z,"\105\110\115\116\097\110\116\105\097\116\101 \097")return function(p)table.insert(Z,"\101\120\101\099\117\116\101 \097")return O(p)end end local function N(O,p)table.insert(Z,"\105\110\115\116\097\110\116\105\097\116\101 \098")return function(p)table.insert(Z,"\101\120\101\099\117\116\101 \098")return O(p)end end local T=O.new(p,5,{j,N});(expect(#Z)).to.equal(2);(expect(Z[1])).to.equal("\105\110\115\116\097\110\116\105\097\116\101 \098");(expect(Z[2])).to.equal("\105\110\115\116\097\110\116\105\097\116\101 \097")T:dispatch({type="\116\101\115\116"});(expect(#Z)).to.equal(4);(expect(Z[3])).to.equal("\101\120\101\099\117\116\101 \097");(expect(Z[4])).to.equal("\101\120\101\099\117\116\101 \098")end)it("\115\104\111\117\108\100 \115\101\110\100 \097\110 \105\110\105\116\105\097\108 \097\099\116\105\111\110 \119\105\116\104 \097 \'\116\121\112\101\' \102\105\101\108\100",function()local Z local p=0 local j=O.new(function(O,j)Z=j p=p+1 return O end);(expect(p)).to.equal(1);(expect(Z)).to.be.a("\116\097\098\108\101");(expect(Z.type)).to.be.ok()j:destruct()end)end)describe("\103\101\116\083\116\097\116\101",function()it("\115\104\111\117\108\100 \103\101\116 \116\104\101 \099\117\114\114\101\110\116 \115\116\097\116\101",function()local Z=O.new(function(O,Z)return"\102\111\111"end)local p=Z:getState();(expect(p)).to.equal("\102\111\111")Z:destruct()end)end)describe("\100\105\115\112\097\116\099\104",function()it("\115\104\111\117\108\100 \098\101 \115\101\110\116 \116\104\114\111\117\103\104 \116\104\101 \114\101\100\117\099\101\114",function()local Z=O.new(function(O,Z)O=O or"\102\111\111"if Z.type=="\097\099\116"then return"\098\097\114"end return O end);(expect(Z)).to.be.ok();(expect(Z:getState())).to.equal("\102\111\111")Z:dispatch({type="\097\099\116"})Z:flush();(expect(Z:getState())).to.equal("\098\097\114")Z:destruct()end)it("\115\104\111\117\108\100 \116\114\105\103\103\101\114 \116\104\101 \099\104\097\110\103\101\100 \101\118\101\110\116 \097\102\116\101\114 \097 \102\108\117\115\104",function()local Z=O.new(function(O,Z)O=O or 0 if Z.type=="\105\110\099\114\101\109\101\110\116"then return O+1 end return O end)local p=0 Z.changed:connect(function(O,Z)(expect(Z)).to.equal(0);(expect(O)).to.equal(1)p=p+1 end)Z:dispatch({type="\105\110\099\114\101\109\101\110\116"})Z:flush();(expect(p)).to.equal(1)Z:destruct()end)it("\115\104\111\117\108\100 \104\097\110\100\108\101 \097\099\116\105\111\110\115 \100\105\115\112\097\116\099\104\101\100 \119\105\116\104\105\110 \116\104\101 \099\104\097\110\103\101\100 \101\118\101\110\116",function()local Z=O.new(function(O,Z)O=O or{value=0}if Z.type=="\105\110\099\114\101\109\101\110\116"then return{value=O.value+1}elseif Z.type=="\100\101\099\114\101\109\101\110\116"then return{value=O.value-1}end return O end)local p=0 Z.changed:connect(function(O,j)(expect(O)).never.to.equal(j)if O.value>0 then Z:dispatch({type="\100\101\099\114\101\109\101\110\116"})end p=p+1 end)Z:dispatch({type="\105\110\099\114\101\109\101\110\116"})Z:flush()Z:flush();(expect(p)).to.equal(2)Z:destruct()end)it("\115\104\111\117\108\100 \112\114\101\118\101\110\116 \121\105\101\108\100\105\110\103 \102\114\111\109 \099\104\097\110\103\101\100 \104\097\110\100\108\101\114",function()local Z=0 local p=0 local j=O.new(function(O,Z)O=O or 0 return O+1 end)j.changed:connect(function(O,j)Z=Z+1 wait()p=p+1 end)j:dispatch({type="\105\110\099\114\101\109\101\110\116"});(expect(function()j:flush()end)).to.throw();(expect(Z)).to.equal(1);(expect(p)).to.equal(0)j:destruct()end)it("\115\104\111\117\108\100 \116\104\114\111\119 \105\102 \097\110 \097\099\116\105\111\110 \105\115 \100\105\115\112\097\116\099\104\101\100 \119\105\116\104\111\117\116 \097 \116\121\112\101 \102\105\101\108\100",function()local Z=O.new(function(O,Z)return O end);(expect(function()Z:dispatch({})end)).to.throw()Z:destruct()end)it("\115\104\111\117\108\100 \116\104\114\111\119 \105\102 \116\104\101 \097\099\116\105\111\110 \105\115 \110\111\116 \097 \102\117\110\099\116\105\111\110 \111\114 \116\097\098\108\101",function()local Z=O.new(function(O,Z)return O end);(expect(function()Z:dispatch(1)end)).to.throw()Z:destruct()end)end)describe("\102\108\117\115\104",function()it("\115\104\111\117\108\100 \110\111\116 \102\105\114\101 \097 \099\104\097\110\103\101\100 \101\118\101\110\116 \105\102 \116\104\101\114\101 \119\101\114\101 \110\111 \100\105\115\112\097\116\099\104\101\115",function()local Z=O.new(function() end)local p=0 Z.changed:connect(function()p=p+1 end)Z:flush();(expect(p)).to.equal(0)Z:dispatch({type="\105\110\099\114\101\109\101\110\116"})Z:flush();(expect(p)).to.equal(1)Z:flush();(expect(p)).to.equal(1)Z:destruct()end)end)end
return function()
	local Store = require(script.Parent.Store)

	describe("new", function()
		it("should instantiate with a reducer", function()
			local store = Store.new(function(state, action)
				return "hello, world"
			end)

			expect(store).to.be.ok()
			expect(store:getState()).to.equal("hello, world")

			store:destruct()
		end)

		it("should instantiate with a reducer and an initial state", function()
			local store = Store.new(function(state, action)
				return state
			end, "initial state")

			expect(store).to.be.ok()
			expect(store:getState()).to.equal("initial state")

			store:destruct()
		end)

		it("should instantiate with a reducer, initial state, and middlewares", function()
			local store = Store.new(function(state, action)
				return state
			end, "initial state", {})

			expect(store).to.be.ok()
			expect(store:getState()).to.equal("initial state")

			store:destruct()
		end)

		it("should modify the dispatch method when middlewares are passed", function()
			local middlewareInstantiateCount = 0
			local middlewareInvokeCount = 0
			local passedDispatch
			local passedStore
			local passedAction

			local function reducer(state, action)
				if action.type == "test" then
					return "test state"
				end

				return state
			end

			local function testMiddleware(nextDispatch, store)
				middlewareInstantiateCount = middlewareInstantiateCount + 1
				passedDispatch = nextDispatch
				passedStore = store

				return function(action)
					middlewareInvokeCount = middlewareInvokeCount + 1
					passedAction = action

					nextDispatch(action)
				end
			end

			local store = Store.new(reducer, "initial state", { testMiddleware })

			expect(middlewareInstantiateCount).to.equal(1)
			expect(middlewareInvokeCount).to.equal(0)
			expect(passedDispatch).to.be.a("function")
			expect(passedStore).to.equal(store)

			store:dispatch({
				type = "test",
			})

			expect(middlewareInstantiateCount).to.equal(1)
			expect(middlewareInvokeCount).to.equal(1)
			expect(passedAction.type).to.equal("test")

			store:flush()

			expect(store:getState()).to.equal("test state")

			store:destruct()
		end)

		it("should execute middleware left-to-right", function()
			local events = {}

			local function reducer(state)
				return state
			end

			local function middlewareA(nextDispatch, store)
				table.insert(events, "instantiate a")
				return function(action)
					table.insert(events, "execute a")
					return nextDispatch(action)
				end
			end

			local function middlewareB(nextDispatch, store)
				table.insert(events, "instantiate b")
				return function(action)
					table.insert(events, "execute b")
					return nextDispatch(action)
				end
			end

			local store = Store.new(reducer, 5, { middlewareA, middlewareB })

			expect(#events).to.equal(2)
			expect(events[1]).to.equal("instantiate b")
			expect(events[2]).to.equal("instantiate a")

			store:dispatch({
				type = "test",
			})

			expect(#events).to.equal(4)
			expect(events[3]).to.equal("execute a")
			expect(events[4]).to.equal("execute b")
		end)

		it("should send an initial action with a 'type' field", function()
			local lastAction
			local callCount = 0

			local store = Store.new(function(state, action)
				lastAction = action
				callCount = callCount + 1

				return state
			end)

			expect(callCount).to.equal(1)
			expect(lastAction).to.be.a("table")
			expect(lastAction.type).to.be.ok()

			store:destruct()
		end)
	end)

	describe("getState", function()
		it("should get the current state", function()
			local store = Store.new(function(state, action)
				return "foo"
			end)

			local state = store:getState()

			expect(state).to.equal("foo")

			store:destruct()
		end)
	end)

	describe("dispatch", function()
		it("should be sent through the reducer", function()
			local store = Store.new(function(state, action)
				state = state or "foo"

				if action.type == "act" then
					return "bar"
				end

				return state
			end)

			expect(store).to.be.ok()
			expect(store:getState()).to.equal("foo")

			store:dispatch({
				type = "act",
			})

			store:flush()

			expect(store:getState()).to.equal("bar")

			store:destruct()
		end)

		it("should trigger the changed event after a flush", function()
			local store = Store.new(function(state, action)
				state = state or 0

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

				return state
			end)

			local callCount = 0

			store.changed:connect(function(state, oldState)
				expect(oldState).to.equal(0)
				expect(state).to.equal(1)

				callCount = callCount + 1
			end)

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

			store:flush()

			expect(callCount).to.equal(1)

			store:destruct()
		end)

		it("should handle actions dispatched within the changed event", function()
			local store = Store.new(function(state, action)
				state = state or {
					value = 0,
				}

				if action.type == "increment" then
					return {
						value = state.value + 1,
					}
				elseif action.type == "decrement" then
					return {
						value = state.value - 1,
					}
				end

				return state
			end)

			local changeCount = 0

			store.changed:connect(function(state, oldState)
				expect(state).never.to.equal(oldState)

				if state.value > 0 then
					store:dispatch({
						type = "decrement",
					})
				end

				changeCount = changeCount + 1
			end)

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

			expect(changeCount).to.equal(2)

			store:destruct()
		end)

		it("should prevent yielding from changed handler", function()
			local preCount = 0
			local postCount = 0

			local store = Store.new(function(state, action)
				state = state or 0
				return state + 1
			end)

			store.changed:connect(function(state, oldState)
				preCount = preCount + 1
				wait()
				postCount = postCount + 1
			end)

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

			expect(function()
				store:flush()
			end).to.throw()

			expect(preCount).to.equal(1)
			expect(postCount).to.equal(0)

			store:destruct()
		end)

		it("should throw if an action is dispatched without a type field", function()
			local store = Store.new(function(state, action)
				return state
			end)

			expect(function()
				store:dispatch({})
			end).to.throw()

			store:destruct()
		end)

		it("should throw if the action is not a function or table", function()
			local store = Store.new(function(state, action)
				return state
			end)

			expect(function()
				store:dispatch(1)
			end).to.throw()

			store:destruct()
		end)
	end)

	describe("flush", function()
		it("should not fire a changed event if there were no dispatches", function()
			local store = Store.new(function()
			end)

			local count = 0
			store.changed:connect(function()
				count = count + 1
			end)

			store:flush()

			expect(count).to.equal(0)

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

			expect(count).to.equal(1)

			store:flush()

			expect(count).to.equal(1)

			store:destruct()
		end)
	end)
end
