return function()local I=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 G=I.new(function(I,G)return"\104\101\108\108\111\044 \119\111\114\108\100"end);(expect(G)).to.be.ok();(expect(G:getState())).to.equal("\104\101\108\108\111\044 \119\111\114\108\100")G: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 G=I.new(function(I,G)return I end,"\105\110\105\116\105\097\108 \115\116\097\116\101");(expect(G)).to.be.ok();(expect(G:getState())).to.equal("\105\110\105\116\105\097\108 \115\116\097\116\101")G: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 G=I.new(function(I,G)return I end,"\105\110\105\116\105\097\108 \115\116\097\116\101",{});(expect(G)).to.be.ok();(expect(G:getState())).to.equal("\105\110\105\116\105\097\108 \115\116\097\116\101")G: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 G=0 local j=0 local x local h local s local function H(I,G)if G.type=="\116\101\115\116"then return"\116\101\115\116 \115\116\097\116\101"end return I end local function B(I,H)G=G+1 x=I h=H return function(G)j=j+1 s=G I(G)end end local W=I.new(H,"\105\110\105\116\105\097\108 \115\116\097\116\101",{B});(expect(G)).to.equal(1);(expect(j)).to.equal(0);(expect(x)).to.be.a("\102\117\110\099\116\105\111\110");(expect(h)).to.equal(W)W:dispatch({type="\116\101\115\116"});(expect(G)).to.equal(1);(expect(j)).to.equal(1);(expect(s.type)).to.equal("\116\101\115\116")W:flush();(expect(W:getState())).to.equal("\116\101\115\116 \115\116\097\116\101")W: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 G={}local function j(I)return I end local function x(I,j)table.insert(G,"\105\110\115\116\097\110\116\105\097\116\101 \097")return function(j)table.insert(G,"\101\120\101\099\117\116\101 \097")return I(j)end end local function h(I,j)table.insert(G,"\105\110\115\116\097\110\116\105\097\116\101 \098")return function(j)table.insert(G,"\101\120\101\099\117\116\101 \098")return I(j)end end local s=I.new(j,5,{x,h});(expect(#G)).to.equal(2);(expect(G[1])).to.equal("\105\110\115\116\097\110\116\105\097\116\101 \098");(expect(G[2])).to.equal("\105\110\115\116\097\110\116\105\097\116\101 \097")s:dispatch({type="\116\101\115\116"});(expect(#G)).to.equal(4);(expect(G[3])).to.equal("\101\120\101\099\117\116\101 \097");(expect(G[4])).to.equal("\101\120\101\099\117\116\101 \098")s:destruct()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 G local j=0 local x=I.new(function(I,x)G=x j=j+1 return I end);(expect(j)).to.equal(1);(expect(G)).to.be.a("\116\097\098\108\101");(expect(G.type)).to.be.ok()x:destruct()end)it("\115\104\111\117\108\100 \114\101\112\111\114\116 \097 \114\101\100\117\099\101\114 \101\114\114\111\114 \116\104\114\111\119\110 \119\104\101\110 \104\097\110\100\108\105\110\103 \116\104\101 \073\078\073\084 \097\099\116\105\111\110",function()local G,j,x local h={reportReducerError=function(I,h,s)G=I j=h x=s end,reportUpdateError=function() end}local s="\090\052\080\072\048\068"local H=function(I,G)error(s)end local B B=I.new(H,{Value=1},nil,h);(expect(G.Value)).to.equal(1);(expect(j.type)).to.equal("\064\064\073\078\073\084");(expect(x.message)).to.equal("\067\097\117\103\104\116 \101\114\114\111\114 \105\110 \114\101\100\117\099\101\114 \119\105\116\104 \105\110\105\116")local W=string.find(x.thrownValue,s);(expect(W)).to.be.ok()W=string.find(x.thrownValue,script.Name);(expect(W)).to.be.ok()B:destruct()end)it("\115\104\111\117\108\100 \114\101\112\111\114\116 \097 \114\101\100\117\099\101\114 \101\114\114\111\114 \116\104\114\111\119\110 \119\104\101\110 \104\097\110\100\108\105\110\103 \097 \115\117\098\115\101\113\117\101\110\116 \097\099\116\105\111\110",function()local G,j,x local h={reportReducerError=function(I,h,s)G=I j=h x=s end;reportUpdateError=function() end}local s="\090\052\080\072\048\068"local H=function(I,G)if G.type=="\084\104\114\111\119\069\114\114\111\114"then error(s)elseif G.type=="\073\110\099\114\101\109\101\110\116"then return{Value=I.Value+1}end return I end local B B=I.new(H,{Value=1},nil,h);(expect(G)).to.equal(nil);(expect(j)).to.equal(nil);(expect(x)).to.equal(nil)B:dispatch({type="\073\110\099\114\101\109\101\110\116"})B:dispatch({type="\084\104\114\111\119\069\114\114\111\114"});(expect(G.Value)).to.equal(2);(expect(j.type)).to.equal("\084\104\114\111\119\069\114\114\111\114");(expect(x.message)).to.equal("\067\097\117\103\104\116 \101\114\114\111\114 \105\110 \114\101\100\117\099\101\114")local W=string.find(x.thrownValue,s);(expect(W)).to.be.ok()W=string.find(x.thrownValue,script.Name);(expect(W)).to.be.ok()B: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 G=I.new(function(I,G)return"\102\111\111"end)local j=G:getState();(expect(j)).to.equal("\102\111\111")G:destruct()end)it("\115\104\111\117\108\100 \116\104\114\111\119 \101\114\114\111\114\115 \105\102 \116\114\105\103\103\101\114\101\100 \100\117\114\105\110\103 \100\105\115\112\097\116\099\104\101\115",function()local G G=I.new(function(I,j)if j.type~="\064\064\073\078\073\084"then G:getState()end end);(expect(function()G:dispatch({type="\083\111\109\101\065\099\116\105\111\110"})end)).to.throw()G: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 G=I.new(function(I,G)I=I or"\102\111\111"if G.type=="\097\099\116"then return"\098\097\114"end return I end);(expect(G)).to.be.ok();(expect(G:getState())).to.equal("\102\111\111")G:dispatch({type="\097\099\116"})G:flush();(expect(G:getState())).to.equal("\098\097\114")G: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 G=I.new(function(I,G)I=I or 0 if G.type=="\105\110\099\114\101\109\101\110\116"then return I+1 end return I end)local j=0 G.changed:connect(function(I,G)(expect(G)).to.equal(0);(expect(I)).to.equal(1)j=j+1 end)G:dispatch({type="\105\110\099\114\101\109\101\110\116"})G:flush();(expect(j)).to.equal(1)G: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 G=I.new(function(I,G)I=I or{value=0}if G.type=="\105\110\099\114\101\109\101\110\116"then return{value=I.value+1}elseif G.type=="\100\101\099\114\101\109\101\110\116"then return{value=I.value-1}end return I end)local j=0 G.changed:connect(function(I,x)(expect(I)).never.to.equal(x)if I.value>0 then G:dispatch({type="\100\101\099\114\101\109\101\110\116"})end j=j+1 end)G:dispatch({type="\105\110\099\114\101\109\101\110\116"})G:flush()G:flush();(expect(j)).to.equal(2)G: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 G,j local x={reportUpdateError=function(I,x,h,s)G=s.message j=s.thrownValue end,reportReducerError=function() end}local h=0 local s=0 local H=I.new(function(I,G)I=I or 0 return I+1 end,nil,nil,x)H.changed:connect(function(I,G)h=h+1 wait()s=s+1 end)H:dispatch({type="\105\110\099\114\101\109\101\110\116"})H:flush();(expect(h)).to.equal(1);(expect(s)).to.equal(0);(expect(G)).to.equal("\067\097\117\103\104\116 \101\114\114\111\114 \102\108\117\115\104\105\110\103 \115\116\111\114\101 \117\112\100\097\116\101\115")local B=string.find(j,script.Name);(expect(B)).to.be.ok()local W="\116\111 \121\105\101\108\100"B=string.find(j,W);(expect(B)).to.be.ok()H: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 G=I.new(function(I,G)return I end);(expect(function()G:dispatch({})end)).to.throw()G: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 G=I.new(function(I,G)return I end);(expect(function()G:dispatch(1)end)).to.throw()G:destruct()end)it("\115\104\111\117\108\100 \114\101\112\111\114\116 \097\110 \101\114\114\111\114 \105\102 \116\104\101 \108\105\115\116\101\110\101\114\115 \101\114\114\111\114 \119\104\101\110 \102\108\117\115\104\105\110\103",function()local G,j,x,h local s={reportReducerError=function() end;reportUpdateError=function(I,s,H,B)G=I j=s x=H h=B end}local H=function(I,G)if G.type=="\073\110\099\114\101\109\101\110\116"then return{Value=I.Value+G.amount}end return I end local B=I.new(H,{Value=1},nil,s)local W="\090\052\080\072\048\068"B.changed:connect(function(I,G)if I.Value==15 then error(W)end end)local M={{type="\073\110\099\114\101\109\101\110\116",amount=1},{type="\073\110\099\114\101\109\101\110\116",amount=3};{type="\073\110\099\114\101\109\101\110\116";amount=10}}for I,G in ipairs(M)do B:dispatch(G)end B:flush();(expect(h.message)).to.equal("\067\097\117\103\104\116 \101\114\114\111\114 \102\108\117\115\104\105\110\103 \115\116\111\114\101 \117\112\100\097\116\101\115")local k=string.find(h.thrownValue,script.Name);(expect(k)).to.be.ok();(expect(x[1])).to.equal(M[1]);(expect(x[2])).to.equal(M[2]);(expect(x[3])).to.equal(M[3]);(expect(G.Value)).to.equal(1);(expect(j.Value)).to.equal(15)B:destruct()end)it("\115\104\111\117\108\100 \116\104\114\111\119 \101\114\114\111\114\115 \105\102 \100\105\115\112\097\116\099\104\105\110\103 \119\104\105\108\101 \097 \100\105\115\112\097\116\099\104 \105\115 \097\108\114\101\097\100\121 \104\097\112\112\101\110\105\110\103",function()local G G=I.new(function(I,j)if j.type=="\083\111\109\101\065\099\116\105\111\110"then G:dispatch({type="\077\105\100\068\105\115\112\097\116\099\104\065\099\116\105\111\110"})end end);(expect(function()G:dispatch({type="\083\111\109\101\065\099\116\105\111\110"})end)).to.throw()G: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 G=I.new(function() end)local j=0 G.changed:connect(function()j=j+1 end)G:flush();(expect(j)).to.equal(0)G:dispatch({type="\105\110\099\114\101\109\101\110\116"})G:flush();(expect(j)).to.equal(1)G:flush();(expect(j)).to.equal(1)G: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")

			store:destruct()
		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)

		it("should report a reducer error thrown when handling the INIT action", function()
			local caughtState, caughtAction, caughtErrorResult
			local mockErrorReporter = {
				reportReducerError = function(state, action, errorResult)
					caughtState = state
					caughtAction = action
					caughtErrorResult = errorResult
				end,
				reportUpdateError = function()
					-- no op
				end,
			}

			local innerErrorMessage = "Z4PH0D"
			local reducerThatErrors = function(state, action)
				error(innerErrorMessage)
			end

			local store
			store = Store.new(reducerThatErrors, {
				Value = 1,
			}, nil, mockErrorReporter)

			expect(caughtState.Value).to.equal(1)
			expect(caughtAction.type).to.equal("@@INIT")
			expect(caughtErrorResult.message).to.equal("Caught error in reducer with init")
			local found = string.find(caughtErrorResult.thrownValue, innerErrorMessage)
			expect(found).to.be.ok()
			-- We want to verify that this is a stacktrace without caring too
			-- much about the format, so we look for the stack frame associated
			-- with this test file
			found = string.find(caughtErrorResult.thrownValue, script.Name)
			expect(found).to.be.ok()

			store:destruct()
		end)

		it("should report a reducer error thrown when handling a subsequent action", function()
			local caughtState, caughtAction, caughtErrorResult
			local mockErrorReporter = {
				reportReducerError = function(state, action, errorResult)
					caughtState = state
					caughtAction = action
					caughtErrorResult = errorResult
				end,
				reportUpdateError = function()
					-- no op
				end,
			}

			local innerErrorMessage = "Z4PH0D"
			local reducerThatErrorsAfterInit = function(state, action)
				if action.type == "ThrowError" then
					error(innerErrorMessage)
				elseif action.type == "Increment" then
					return {
						Value = state.Value + 1,
					}
				end
				return state
			end

			local store
			store = Store.new(reducerThatErrorsAfterInit, {
				Value = 1,
			}, nil, mockErrorReporter)

			expect(caughtState).to.equal(nil)
			expect(caughtAction).to.equal(nil)
			expect(caughtErrorResult).to.equal(nil)

			store:dispatch({ type = "Increment" })
			store:dispatch({ type = "ThrowError" })

			expect(caughtState.Value).to.equal(2)
			expect(caughtAction.type).to.equal("ThrowError")
			expect(caughtErrorResult.message).to.equal("Caught error in reducer")
			local found = string.find(caughtErrorResult.thrownValue, innerErrorMessage)
			expect(found).to.be.ok()
			-- We want to verify that this is a stacktrace without caring too
			-- much about the format, so we look for the stack frame associated
			-- with this test file
			found = string.find(caughtErrorResult.thrownValue, script.Name)
			expect(found).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)

		it("should throw errors if triggered during dispatches", function()
			local store
			store = Store.new(function(state, action)
				if action.type ~= "@@INIT" then
					store:getState()
				end
			end)

			expect(function()
				store:dispatch({ type = "SomeAction" })
			end).to.throw()

			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 reportedErrorMessage, reportedErrorError
			local mockErrorReporter = {
				reportUpdateError = function(_, _, _, errorResult)
					reportedErrorMessage = errorResult.message
					reportedErrorError = errorResult.thrownValue
				end,
				reportReducerError = function()
					-- noop
				end,
			}
			local preCount = 0
			local postCount = 0

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

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

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

			store:flush()

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

			expect(reportedErrorMessage).to.equal("Caught error flushing store updates")
			-- We want to verify that this is a stacktrace without caring too
			-- much about the format, so we look for the stack frame associated
			-- with this test file
			local found = string.find(reportedErrorError, script.Name)
			expect(found).to.be.ok()
			-- In vanilla lua, we get this message:
			--   "attempt to yield across metamethod/C-call boundary"
			-- In luau, we should end up wrapping our own NoYield message:
			--   "Attempted to yield inside changed event!"
			-- For convenience's sake, we just look for the common substring
			local caughtErrorSubstring = "to yield"
			found = string.find(reportedErrorError, caughtErrorSubstring)
			expect(found).to.be.ok()

			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)

		it("should report an error if the listeners error when flushing", function()
			local caughtPrevState, caughtState, caughtActionLog, caughtErrorResult
			local mockErrorReporter = {
				reportReducerError = function()
					-- no op
				end,
				reportUpdateError = function(prevState, state, actionLog, errorResult)
					caughtPrevState = prevState
					caughtState = state
					caughtActionLog = actionLog
					caughtErrorResult = errorResult
				end,
			}

			local reducer = function(state, action)
				if action.type == "Increment" then
					return {
						Value = state.Value + action.amount,
					}
				end
				return state
			end
			local store = Store.new(reducer, {
				Value = 1,
			}, nil, mockErrorReporter)

			local innerErrorMessage = "Z4PH0D"
			store.changed:connect(function(state, prevState)
				if state.Value == 15 then
					error(innerErrorMessage)
				end
			end)

			local actions = {
				{ type = "Increment", amount = 1 },
				{ type = "Increment", amount = 3 },
				{ type = "Increment", amount = 10 },
			}
			for _, action in ipairs(actions) do
				store:dispatch(action)
			end
			store:flush()

			expect(caughtErrorResult.message).to.equal("Caught error flushing store updates")
			-- We want to verify that this is a stacktrace without caring too
			-- much about the format, so we look for the stack frame associated
			-- with this test file
			local found = string.find(caughtErrorResult.thrownValue, script.Name)
			expect(found).to.be.ok()

			expect(caughtActionLog[1]).to.equal(actions[1])
			expect(caughtActionLog[2]).to.equal(actions[2])
			expect(caughtActionLog[3]).to.equal(actions[3])

			-- This is before any of the actions were processed; the flush will
			-- apply the new state from all three actions
			expect(caughtPrevState.Value).to.equal(1)
			expect(caughtState.Value).to.equal(15)

			store:destruct()
		end)

		it("should throw errors if dispatching while a dispatch is already happening", function()
			local store
			store = Store.new(function(state, action)
				if action.type == "SomeAction" then
					store:dispatch({ type = "MidDispatchAction" })
				end
			end)

			expect(function()
				store:dispatch({ type = "SomeAction" })
			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
