mirror of
https://github.com/littensy/charm.git
synced 2026-10-21 11:44:08 +00:00
feat: initial commit
This commit is contained in:
commit
18723c1377
50 changed files with 3238 additions and 0 deletions
42
src/__tests__/react/helpers/renderHook.luau
Normal file
42
src/__tests__/react/helpers/renderHook.luau
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
_G.__ROACT_17_MOCK_SCHEDULER__ = true
|
||||
|
||||
local Package = script:FindFirstAncestor("__tests__").Parent
|
||||
|
||||
local React = require(Package.modules.React)
|
||||
local ReactRoblox = require(Package.modules.ReactRoblox)
|
||||
|
||||
type RenderedHook<Props, Result> = {
|
||||
result: Result,
|
||||
rerender: (Props?) -> (),
|
||||
unmount: () -> (),
|
||||
}
|
||||
|
||||
local function renderHook<Props, Result>(callback: (Props) -> Result, props: Props?): RenderedHook<Props, Result>
|
||||
local root = ReactRoblox.createLegacyRoot(Instance.new("Folder"))
|
||||
local hook = {}
|
||||
|
||||
local function Component(props)
|
||||
hook.result = callback(props)
|
||||
end
|
||||
|
||||
function hook.rerender(newProps)
|
||||
props = newProps or props
|
||||
ReactRoblox.act(function()
|
||||
root:render(React.createElement(Component, props))
|
||||
end)
|
||||
end
|
||||
|
||||
function hook.unmount()
|
||||
ReactRoblox.act(function()
|
||||
root:unmount()
|
||||
end)
|
||||
end
|
||||
|
||||
ReactRoblox.act(function()
|
||||
hook.rerender(props)
|
||||
end)
|
||||
|
||||
return hook
|
||||
end
|
||||
|
||||
return renderHook
|
||||
27
src/__tests__/react/useAtomState.spec.luau
Normal file
27
src/__tests__/react/useAtomState.spec.luau
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
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
|
||||
34
src/__tests__/react/useSetAtom.spec.luau
Normal file
34
src/__tests__/react/useSetAtom.spec.luau
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
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(source)
|
||||
return useSetAtom(source)
|
||||
end, atom(0))
|
||||
local fn = hook.result
|
||||
hook.rerender(atom(1))
|
||||
expect(hook.result).to.never.equal(fn)
|
||||
end)
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue