test: add benchmarks, dev mode

This commit is contained in:
Richard 2024-03-02 02:41:30 -08:00
parent 3548ca5c58
commit 4586882d3c
6 changed files with 95 additions and 11 deletions

View file

@ -0,0 +1,53 @@
-- This file is for use by Benchmarker
-- https://boatbomber.itch.io/benchmarker
local Package = game:GetService("ReplicatedStorage").Packages.charm
local atom = require(Package.atom)
local derive = require(Package.derive)
return {
ParameterGenerator = function()
return atom({ count = 0 })
end,
Functions = {
atom = function(_, source: any)
for _ = 1, 500 do
source({ count = math.random(1, 2) })
end
end,
derived = function(_, source: any)
derive(source, function(state)
return { count = 2 * state.count }
end)
for _ = 1, 500 do
source({ count = math.random(1, 2) })
end
end,
derived_memo = function(_, source: any)
source:memo(function(previous, current)
return previous.count == current.count
end)
derive(source, function(state)
return { count = 2 * state.count }
end)
for _ = 1, 500 do
source({ count = math.random(1, 2) })
end
end,
derived_writable = function(_, source: any)
local derived = derive(source, function(state)
return { count = 2 * state.count }
end):writable(function(update)
source({ count = 0.5 * update.count })
end)
for _ = 1, 500 do
derived({ count = 2 * math.random(1, 2) })
end
end,
},
}

View file

@ -89,4 +89,14 @@ return function()
error("None of the derived atoms were collected")
end)
it("unmounts from parent atom", function()
local source = atom(1)
local derived = derive(source, function(value)
return value
end)
expect((next(source.listeners))).to.be.ok()
derived:unmount()
expect((next(source.listeners))).to.never.be.ok()
end)
end

View file

@ -1,6 +1,16 @@
local reference = require(script.Parent.Parent.utils.reference)
return function()
local previous = _G.__DEV__
beforeAll(function()
_G.__DEV__ = true
end)
afterAll(function()
_G.__DEV__ = previous
end)
afterEach(function()
reference.clear()
end)

View file

@ -2,7 +2,7 @@ return function()
local previous = _G.__DEV__
beforeAll(function()
_G.__DEV__ = true
_G.__DEV__ = false -- prevent React errors
end)
afterAll(function()