mirror of
https://github.com/littensy/charm.git
synced 2026-10-20 17:51:37 +00:00
44 lines
1.3 KiB
Text
44 lines
1.3 KiB
Text
local Charm = require("../Charm")
|
|
local ReactCharm = require("../src")
|
|
local renderHook = require("./helpers/renderHook")
|
|
local test = require("../../../tests/test")
|
|
|
|
test("tracks signal state", function()
|
|
local getState, setState = Charm.signal(0)
|
|
local hook = renderHook(function()
|
|
return ReactCharm.useSignalBinding(getState)
|
|
end)
|
|
assert(hook.result:getValue() == 0, "should return 0")
|
|
setState(1)
|
|
assert(hook.result:getValue() == 1, "should return 1")
|
|
end)
|
|
|
|
test("accepts a selector", function()
|
|
local getState, setState = Charm.signal(0)
|
|
local hook = renderHook(function()
|
|
return ReactCharm.useSignalBinding(function()
|
|
return 2 * getState()
|
|
end)
|
|
end)
|
|
assert(hook.result:getValue() == 0, "should return 0")
|
|
setState(1)
|
|
assert(hook.result:getValue() == 2, "should return 2")
|
|
end)
|
|
|
|
test("tracks dependencies", function()
|
|
local getState, setState = Charm.signal(0)
|
|
local hook = renderHook(function()
|
|
return ReactCharm.useSignalBinding(getState, { getState })
|
|
end)
|
|
assert(hook.result:getValue() == 0, "should return 0")
|
|
setState(1)
|
|
assert(hook.result:getValue() == 1, "should return 1")
|
|
|
|
getState, setState = Charm.signal(0)
|
|
hook.rerender()
|
|
assert(hook.result:getValue() == 0, "should return 0")
|
|
setState(1)
|
|
assert(hook.result:getValue() == 1, "should return 1")
|
|
end)
|
|
|
|
return {}
|