mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 03:36:11 +00:00
refactor!: minimal react api
This commit is contained in:
parent
047be71eaa
commit
f2f7a16599
10 changed files with 59 additions and 144 deletions
42
src/__tests__/react/useAtom.spec.luau
Normal file
42
src/__tests__/react/useAtom.spec.luau
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
local atom = require(script.Parent.Parent.Parent.atom)
|
||||
local useAtom = require(script.Parent.Parent.Parent.react.useAtom)
|
||||
local renderHook = require(script.Parent.helpers.renderHook)
|
||||
|
||||
return function()
|
||||
it("tracks atom state", function()
|
||||
local source = atom(0)
|
||||
local hook = renderHook(function()
|
||||
return useAtom(source)
|
||||
end)
|
||||
expect(hook.result).to.equal(0)
|
||||
source(1)
|
||||
expect(hook.result).to.equal(1)
|
||||
end)
|
||||
|
||||
it("accepts a molecule", function()
|
||||
local source = atom(0)
|
||||
local hook = renderHook(function()
|
||||
return useAtom(function()
|
||||
return 2 * source()
|
||||
end)
|
||||
end)
|
||||
expect(hook.result).to.equal(0)
|
||||
source(1)
|
||||
expect(hook.result).to.equal(2)
|
||||
end)
|
||||
|
||||
it("tracks dependencies", function()
|
||||
local source = atom(0)
|
||||
local hook = renderHook(function()
|
||||
return useAtom(source, { source })
|
||||
end)
|
||||
expect(hook.result).to.equal(0)
|
||||
source(1)
|
||||
expect(hook.result).to.equal(1)
|
||||
source = atom(0)
|
||||
hook.rerender()
|
||||
expect(hook.result).to.equal(0)
|
||||
source(1)
|
||||
expect(hook.result).to.equal(1)
|
||||
end)
|
||||
end
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
local atom = require(script.Parent.Parent.Parent.atom)
|
||||
local useAtomState = require(script.Parent.Parent.Parent.react.useAtomState)
|
||||
local renderHook = require(script.Parent.helpers.renderHook)
|
||||
|
||||
return function()
|
||||
it("tracks atom state", function()
|
||||
local source = atom(0)
|
||||
local hook = renderHook(function()
|
||||
return useAtomState(source)
|
||||
end)
|
||||
expect(hook.result).to.equal(0)
|
||||
source(1)
|
||||
expect(hook.result).to.equal(1)
|
||||
end)
|
||||
|
||||
it("receives a selector", function()
|
||||
local source = atom(0)
|
||||
local hook = renderHook(function()
|
||||
return useAtomState(source, function(value)
|
||||
return 2 * value
|
||||
end)
|
||||
end)
|
||||
expect(hook.result).to.equal(0)
|
||||
source(1)
|
||||
expect(hook.result).to.equal(2)
|
||||
end)
|
||||
end
|
||||
|
|
@ -1,34 +0,0 @@
|
|||
local atom = require(script.Parent.Parent.Parent.atom)
|
||||
local useSetAtom = require(script.Parent.Parent.Parent.react.useSetAtom)
|
||||
local renderHook = require(script.Parent.helpers.renderHook)
|
||||
|
||||
return function()
|
||||
it("sets atom state", function()
|
||||
local source = atom(0)
|
||||
local hook = renderHook(function()
|
||||
return useSetAtom(source)
|
||||
end)
|
||||
expect(hook.result).to.be.a("function")
|
||||
hook.result(1)
|
||||
expect(source()).to.equal(1)
|
||||
end)
|
||||
|
||||
it("returns a stable function", function()
|
||||
local source = atom(0)
|
||||
local hook = renderHook(function()
|
||||
return useSetAtom(source)
|
||||
end)
|
||||
local fn = hook.result
|
||||
hook.rerender()
|
||||
expect(hook.result).to.equal(fn)
|
||||
end)
|
||||
|
||||
it("memoizes the function", function()
|
||||
local hook = renderHook(function(props)
|
||||
return useSetAtom(props.source)
|
||||
end, { source = atom(0) })
|
||||
local fn = hook.result
|
||||
hook.rerender({ source = atom(1) })
|
||||
expect(hook.result).to.never.equal(fn)
|
||||
end)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue