charm/tests/react/useAtom.spec.luau

46 lines
1.1 KiB
Text

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Charm = require(ReplicatedStorage.DevPackages.Charm)
local atom = Charm.atom
local ReactCharm = require(ReplicatedStorage.DevPackages.ReactCharm)
local useAtom = ReactCharm.useAtom
local renderHook = require(script.Parent.Parent.utils.renderHook)
return function()
it("tracks atom state", function()
local state = atom(0)
local hook = renderHook(function()
return useAtom(state)
end)
expect(hook.result).to.equal(0)
state(1)
expect(hook.result).to.equal(1)
end)
it("accepts a selector", function()
local state = atom(0)
local hook = renderHook(function()
return useAtom(function()
return 2 * state()
end)
end)
expect(hook.result).to.equal(0)
state(1)
expect(hook.result).to.equal(2)
end)
it("tracks dependencies", function()
local state = atom(0)
local hook = renderHook(function()
return useAtom(state, { state })
end)
expect(hook.result).to.equal(0)
state(1)
expect(hook.result).to.equal(1)
state = atom(0)
hook.rerender()
expect(hook.result).to.equal(0)
state(1)
expect(hook.result).to.equal(1)
end)
end