Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
Fix missing nil check in context logic (#267)
Browse files Browse the repository at this point in the history
* Introduce breaking test

* Add fix
  • Loading branch information
ZoteTheMighty authored Apr 23, 2020
1 parent f55538b commit cd22f9c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/createContext.lua
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ local function createConsumer(context)
-- Store the value that we most recently updated with.
--
-- This value is compared in the contextEntry onUpdate hook below.
self.lastValue = self.contextEntry.value
if self.contextEntry ~= nil then
self.lastValue = self.contextEntry.value
end
end

function Consumer:didMount()
Expand Down
21 changes: 21 additions & 0 deletions src/createContext.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,27 @@ return function()

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()
Expand Down

0 comments on commit cd22f9c

Please sign in to comment.