mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Add context()
This commit is contained in:
parent
83db00a073
commit
8142acd1c1
8 changed files with 303 additions and 1 deletions
102
test/tests.luau
102
test/tests.luau
|
|
@ -2155,6 +2155,108 @@ TEST("read()", wrap_root(function()
|
|||
end
|
||||
end))
|
||||
|
||||
TEST("context()", function()
|
||||
local root = vide.root
|
||||
local context = vide.context
|
||||
local effect = vide.effect
|
||||
local untrack = vide.untrack
|
||||
local show = vide.show
|
||||
|
||||
do CASE "set context"
|
||||
local ctx = context()
|
||||
|
||||
root(function()
|
||||
ctx(1, function()
|
||||
CHECK(ctx() == 1)
|
||||
|
||||
effect(function()
|
||||
CHECK(ctx() == 1)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
|
||||
do CASE "set context outside of scope"
|
||||
local ctx = context()
|
||||
|
||||
local ok = pcall(function()
|
||||
ctx(1, function() end)
|
||||
end)
|
||||
|
||||
CHECK(not ok)
|
||||
end
|
||||
|
||||
do CASE "get default context"
|
||||
local ctx = context(1)
|
||||
|
||||
CHECK(ctx() == 1)
|
||||
|
||||
root(function()
|
||||
ctx(2, function()
|
||||
CHECK(ctx() == 2)
|
||||
end)
|
||||
|
||||
CHECK(ctx() == 1)
|
||||
end)
|
||||
end
|
||||
|
||||
do CASE "context cascade"
|
||||
local ctx = context(1)
|
||||
local ctx2 = context()
|
||||
|
||||
root(function()
|
||||
ctx(2, function()
|
||||
ctx2(true, function()
|
||||
show(function() return true end, function()
|
||||
ctx(3, function()
|
||||
effect(function()
|
||||
CHECK(ctx() == 3)
|
||||
untrack(function()
|
||||
effect(function()
|
||||
CHECK(ctx() == 3)
|
||||
end)
|
||||
CHECK(ctx2() == true)
|
||||
return {}
|
||||
end)
|
||||
end)
|
||||
CHECK(ctx() == 3)
|
||||
end)
|
||||
CHECK(ctx() == 2)
|
||||
return {}
|
||||
end)
|
||||
end)
|
||||
CHECK(ctx() == 2)
|
||||
end)
|
||||
|
||||
CHECK(ctx() == 1)
|
||||
end)
|
||||
end
|
||||
|
||||
do CASE "nil context"
|
||||
local ctx = context(nil)
|
||||
|
||||
root(function()
|
||||
CHECK(ctx() == nil)
|
||||
ctx(nil, function()
|
||||
CHECK(ctx() == nil)
|
||||
ctx(true :: any, function()
|
||||
CHECK(ctx() == true)
|
||||
ctx(nil, function()
|
||||
effect(function()
|
||||
untrack(function()
|
||||
effect(function()
|
||||
CHECK(ctx() == nil)
|
||||
end)
|
||||
return {}
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
end
|
||||
end)
|
||||
|
||||
TEST("nested effects cases", function()
|
||||
local vide = require "src/init"
|
||||
local source = vide.source
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue