From 3c138cb29e7c3512721d9f0ada998fa4e4d09a04 Mon Sep 17 00:00:00 2001 From: Christopher <51393127+chriscerie@users.noreply.github.com> Date: Fri, 18 Feb 2022 22:02:23 -0800 Subject: [PATCH 1/5] Add tests for hooks --- .gitmodules | 3 ++ selene.toml | 2 +- src/createUseBinding.spec.lua | 61 ++++++++++++++++++++++++ src/createUseEffect.spec.lua | 88 +++++++++++++++++++++++++++++++++++ src/createUseMemo.spec.lua | 87 ++++++++++++++++++++++++++++++++++ src/createUseReducer.spec.lua | 55 ++++++++++++++++++++++ src/createUseState.spec.lua | 71 ++++++++++++++++++++++++++++ src/createUseValue.spec.lua | 49 +++++++++++++++++++ stories.project.json | 12 +++++ test/testRunner.client.lua | 5 ++ testez.toml | 78 +++++++++++++++++++++++++++++++ vendor/testez | 1 + 12 files changed, 511 insertions(+), 1 deletion(-) create mode 100644 src/createUseBinding.spec.lua create mode 100644 src/createUseEffect.spec.lua create mode 100644 src/createUseMemo.spec.lua create mode 100644 src/createUseReducer.spec.lua create mode 100644 src/createUseState.spec.lua create mode 100644 src/createUseValue.spec.lua create mode 100644 test/testRunner.client.lua create mode 100644 testez.toml create mode 160000 vendor/testez diff --git a/.gitmodules b/.gitmodules index 31a60e5..7a801fa 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "vendor/roact"] path = vendor/roact url = git://github.com/Roblox/roact.git +[submodule "vendor/testez"] + path = vendor/testez + url = https://github.com/Roblox/testez.git diff --git a/selene.toml b/selene.toml index 1f1e170..c1911be 100644 --- a/selene.toml +++ b/selene.toml @@ -1 +1 @@ -std = "roblox" \ No newline at end of file +std = "roblox+testez" \ No newline at end of file diff --git a/src/createUseBinding.spec.lua b/src/createUseBinding.spec.lua new file mode 100644 index 0000000..22f78b1 --- /dev/null +++ b/src/createUseBinding.spec.lua @@ -0,0 +1,61 @@ +local ReplicatedStorage = game:GetService("ReplicatedStorage") + +local Roact = require(ReplicatedStorage.Roact) +local Hooks = require(ReplicatedStorage.Hooks) + +local e = Roact.createElement + +local function createTest(initialState, initialValue) + local test = { + renders = 0, + } + + local function Test(_, hooks) + test.renders += 1 + test.state, test.setState = hooks.useState(initialState) + test.binding, test.setBinding = hooks.useBinding(initialValue) + return nil + end + + Test = Hooks.new(Roact)(Test) + test.handle = Roact.mount(e(Test)) + + return test +end + +return function() + describe("useBinding", function() + it("should set the initial binding", function() + local test = createTest(1, 2) + expect(test.binding:getValue()).to.equal(2) + expect(test.renders).to.equal(1) + end) + + it("should set to nil when passed nil", function() + local test = createTest(1) + + expect(test.binding:getValue()).to.never.be.ok() + + test.setBinding(2) + expect(test.binding:getValue()).to.equal(2) + + test.setBinding(nil) + expect(test.binding:getValue()).to.never.be.ok() + end) + + it("should not rerender when binding changes", function() + local test = createTest(1, 2) + + test.setBinding(3) + expect(test.binding:getValue()).to.equal(3) + expect(test.renders).to.equal(1) + end) + + it("should maintain its value after rerenders", function() + local test = createTest(1, 2) + + test.setState(3) + expect(test.binding:getValue()).to.equal(2) + end) + end) +end diff --git a/src/createUseEffect.spec.lua b/src/createUseEffect.spec.lua new file mode 100644 index 0000000..d21fb70 --- /dev/null +++ b/src/createUseEffect.spec.lua @@ -0,0 +1,88 @@ +local ReplicatedStorage = game:GetService("ReplicatedStorage") + +local Roact = require(ReplicatedStorage.Roact) +local Hooks = require(ReplicatedStorage.Hooks) + +local e = Roact.createElement + +local function createTest(initialState, initialDeps) + local test = { + useEffectRuns = 0, + } + + local function Test(_, hooks) + test.state, test.setState = hooks.useState(initialState) + test.deps, test.setDeps = hooks.useState(initialDeps) + + hooks.useEffect(function() + test.useEffectRuns += 1 + end, test.deps) + + return nil + end + + Test = Hooks.new(Roact)(Test) + test.handle = Roact.mount(e(Test)) + + return test +end + +return function() + describe("useEffect", function() + describe("when deps are nil", function() + it("should run the effect after each rerender", function() + local test = createTest(1) + expect(test.useEffectRuns).to.equal(1) + + test.setState(2) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(2) + + test.setState(3) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(3) + end) + end) + + describe("when deps are empty", function() + it("should not run the effect after each rerender", function() + local test = createTest(1, {}) + expect(test.useEffectRuns).to.equal(1) + + test.setState(2) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(1) + end) + end) + + describe("when deps are constant", function() + it("should not run the effect after each rerender", function() + local test = createTest(1, { 1 }) + expect(test.useEffectRuns).to.equal(1) + + test.setState(2) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(1) + end) + end) + + describe("when deps are changing", function() + it("should run the effect when deps change", function() + local test = createTest(1, { 1 }) + expect(test.useEffectRuns).to.equal(1) + + test.setDeps({ 2 }) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(2) + + test.setState(2) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(2) + + test.setDeps({ 3 }) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(3) + end) + end) + end) +end diff --git a/src/createUseMemo.spec.lua b/src/createUseMemo.spec.lua new file mode 100644 index 0000000..cb7ffd1 --- /dev/null +++ b/src/createUseMemo.spec.lua @@ -0,0 +1,87 @@ +local ReplicatedStorage = game:GetService("ReplicatedStorage") + +local Roact = require(ReplicatedStorage.Roact) +local Hooks = require(ReplicatedStorage.Hooks) + +local e = Roact.createElement + +local function createTest(initialState, initialDeps) + local test = {} + + local function Test(_, hooks) + test.state, test.setState = hooks.useState(initialState) + test.deps, test.setDeps = hooks.useState(initialDeps) + + test.value = hooks.useMemo(function() + return test.state + end, test.deps) + + return nil + end + + Test = Hooks.new(Roact)(Test) + test.handle = Roact.mount(e(Test)) + + return test +end + +return function() + describe("useEffect", function() + describe("when deps are nil", function() + it("should recalculate value after each rerender", function() + local test = createTest(1) + expect(test.value).to.equal(1) + + test.setState(2) + task.wait(0.1) + expect(test.value).to.equal(2) + + test.setState(3) + task.wait(0.1) + expect(test.value).to.equal(3) + end) + end) + + describe("when deps are empty", function() + it("should not recalculate value after each rerender", function() + local test = createTest(1, {}) + expect(test.value).to.equal(1) + + test.setState(2) + task.wait(0.1) + expect(test.value).to.equal(1) + end) + end) + + describe("when deps are constant", function() + it("should not recalculate value after each rerender", function() + local test = createTest(1, { 1 }) + expect(test.value).to.equal(1) + + test.setState(2) + task.wait(0.1) + expect(test.value).to.equal(1) + end) + end) + + describe("when deps are changing", function() + it("should recalculate value when deps change", function() + local test = createTest(1, { 1 }) + expect(test.value).to.equal(1) + + test.setState(2) + test.setDeps({ 2 }) + task.wait(0.1) + expect(test.value).to.equal(2) + + test.setState(3) + task.wait(0.1) + expect(test.value).to.equal(2) + + test.setDeps({ 3 }) + task.wait(0.1) + expect(test.value).to.equal(3) + end) + end) + end) +end diff --git a/src/createUseReducer.spec.lua b/src/createUseReducer.spec.lua new file mode 100644 index 0000000..7a6e179 --- /dev/null +++ b/src/createUseReducer.spec.lua @@ -0,0 +1,55 @@ +local ReplicatedStorage = game:GetService("ReplicatedStorage") + +local Roact = require(ReplicatedStorage.Roact) +local Hooks = require(ReplicatedStorage.Hooks) + +local e = Roact.createElement + +local function reducer(state, action) + if action.type == "increment" then + return { count = state.count + 1 } + elseif action.type == "decrement" then + return { count = state.count - 1 } + else + error("Unknown type: " .. tostring(action.type)) + end +end + +local function createTest(initialState) + local test = { + renders = 0, + } + + local function Test(_, hooks) + test.renders += 1 + test.state, test.dispatch = hooks.useReducer(reducer, initialState) + return nil + end + + Test = Hooks.new(Roact)(Test) + test.handle = Roact.mount(e(Test)) + + return test +end + +return function() + describe("useReducer", function() + it("should set the initial state", function() + local test = createTest({ count = 1}) + expect(test.state.count).to.equal(1) + expect(test.renders).to.equal(1) + end) + + it("should rerender when the state changes", function() + local test = createTest({ count = 1}) + + test.dispatch({ type = "increment" }) + expect(test.state.count).to.equal(2) + expect(test.renders).to.equal(2) + + test.dispatch({ type = "decrement" }) + expect(test.state.count).to.equal(1) + expect(test.renders).to.equal(3) + end) + end) +end diff --git a/src/createUseState.spec.lua b/src/createUseState.spec.lua new file mode 100644 index 0000000..67e7845 --- /dev/null +++ b/src/createUseState.spec.lua @@ -0,0 +1,71 @@ +local ReplicatedStorage = game:GetService("ReplicatedStorage") + +local Roact = require(ReplicatedStorage.Roact) +local Hooks = require(ReplicatedStorage.Hooks) + +local e = Roact.createElement + +local function createTest(initialState) + local test = { + renders = 0, + } + + local function Test(_, hooks) + test.renders += 1 + test.state, test.setState = hooks.useState(initialState) + return nil + end + + Test = Hooks.new(Roact)(Test) + test.handle = Roact.mount(e(Test)) + + return test +end + +return function() + describe("useState", function() + it("should set the initial state", function() + local test = createTest(1) + expect(test.state).to.equal(1) + expect(test.renders).to.equal(1) + end) + + it("should rerender when the state changes", function() + local test = createTest(1) + + test.setState(2) + expect(test.state).to.equal(2) + expect(test.renders).to.equal(2) + + test.setState(2) + expect(test.state).to.equal(2) + expect(test.renders).to.equal(3) + + test.setState() + expect(test.state).to.never.be.ok() + expect(test.renders).to.equal(4) + end) + + it("should provide previous state when setState is called with a function", function() + local test = createTest(1) + + test.setState(function(prevState) + return prevState + 1 + end) + expect(test.state).to.equal(2) + expect(test.renders).to.equal(2) + + test.setState(function(prevState) + return prevState + 1 + end) + expect(test.state).to.equal(3) + expect(test.renders).to.equal(3) + + test.setState(function() + return nil + end) + expect(test.state).to.never.be.ok() + expect(test.renders).to.equal(4) + end) + end) +end diff --git a/src/createUseValue.spec.lua b/src/createUseValue.spec.lua new file mode 100644 index 0000000..c89a044 --- /dev/null +++ b/src/createUseValue.spec.lua @@ -0,0 +1,49 @@ +local ReplicatedStorage = game:GetService("ReplicatedStorage") + +local Roact = require(ReplicatedStorage.Roact) +local Hooks = require(ReplicatedStorage.Hooks) + +local e = Roact.createElement + +local function createTest(initialState, initialValue) + local test = { + renders = 0, + } + + local function Test(_, hooks) + test.renders += 1 + test.state, test.setState = hooks.useState(initialState) + test.value = hooks.useValue(initialValue) + return nil + end + + Test = Hooks.new(Roact)(Test) + test.handle = Roact.mount(e(Test)) + + return test +end + +return function() + describe("useValue", function() + it("should set the initial value", function() + local test = createTest(1, 2) + expect(test.value.value).to.equal(2) + expect(test.renders).to.equal(1) + end) + + it("should not rerender when value changes", function() + local test = createTest(1, 2) + + test.value.value = 3 + expect(test.value.value).to.equal(3) + expect(test.renders).to.equal(1) + end) + + it("should maintain its value after rerenders", function() + local test = createTest(1, 2) + + test.setState(3) + expect(test.value.value).to.equal(2) + end) + end) +end diff --git a/stories.project.json b/stories.project.json index feb8fe6..ce57eb2 100644 --- a/stories.project.json +++ b/stories.project.json @@ -12,9 +12,21 @@ "$path": "vendor/roact" }, + "TestEZ": { + "$path": "vendor/testez" + }, + "Stories": { "$path": "stories" } + }, + + "StarterPlayer": { + "StarterPlayerScripts": { + "testRunner": { + "$path": "test/testRunner.client.lua" + } + } } } } \ No newline at end of file diff --git a/test/testRunner.client.lua b/test/testRunner.client.lua new file mode 100644 index 0000000..f1f3689 --- /dev/null +++ b/test/testRunner.client.lua @@ -0,0 +1,5 @@ +local ReplicatedStorage = game:GetService("ReplicatedStorage") + +local TestEZ = require(ReplicatedStorage.TestEZ) + +TestEZ.TestBootstrap:run({ ReplicatedStorage.Hooks }) diff --git a/testez.toml b/testez.toml new file mode 100644 index 0000000..018a35a --- /dev/null +++ b/testez.toml @@ -0,0 +1,78 @@ +[[afterAll.args]] +type = "function" + +[[afterEach.args]] +type = "function" + +[[beforeAll.args]] +type = "function" + +[[beforeEach.args]] +type = "function" + +[[describe.args]] +type = "string" + +[[describe.args]] +type = "function" + +[[describeFOCUS.args]] +type = "string" + +[[describeFOCUS.args]] +type = "function" + +[[describeSKIP.args]] +type = "string" + +[[describeSKIP.args]] +type = "function" + +[[expect.args]] +type = "any" + +[[FIXME.args]] +type = "string" +required = false + +[FOCUS] +args = [] + +[[it.args]] +type = "string" + +[[it.args]] +type = "function" + +[[itFIXME.args]] +type = "string" + +[[itFIXME.args]] +type = "function" + +[[itFOCUS.args]] +type = "string" + +[[itFOCUS.args]] +type = "function" + +[[fit.args]] +type = "string" + +[[fit.args]] +type = "function" + +[[itSKIP.args]] +type = "string" + +[[itSKIP.args]] +type = "function" + +[[xit.args]] +type = "string" + +[[xit.args]] +type = "function" + +[SKIP] +args = [] \ No newline at end of file diff --git a/vendor/testez b/vendor/testez new file mode 160000 index 0000000..5fc9541 --- /dev/null +++ b/vendor/testez @@ -0,0 +1 @@ +Subproject commit 5fc95414d6e48ab51b02898be940584684de82ad From b4f46294c021b2371f1499e5356328a2b518c07f Mon Sep 17 00:00:00 2001 From: Christopher <51393127+chriscerie@users.noreply.github.com> Date: Fri, 18 Feb 2022 22:05:43 -0800 Subject: [PATCH 2/5] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1261abb..204d808 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased ### Added - Provide Roact instance in hooks argument (#28) +- Added testing (#30) ### Fixed - Fix useContext for uninitialized and/or changing contexts (#26) From ea41ee810554ad504037aee24c6f39468bb0483a Mon Sep 17 00:00:00 2001 From: Christopher <51393127+chriscerie@users.noreply.github.com> Date: Fri, 18 Feb 2022 22:23:04 -0800 Subject: [PATCH 3/5] Add test for init.lua --- src/init.spec.lua | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/init.spec.lua diff --git a/src/init.spec.lua b/src/init.spec.lua new file mode 100644 index 0000000..8d1463c --- /dev/null +++ b/src/init.spec.lua @@ -0,0 +1,23 @@ +local ReplicatedStorage = game:GetService("ReplicatedStorage") + +local Roact = require(ReplicatedStorage.Roact) +local Hooks = require(ReplicatedStorage.Hooks) + +return function() + describe("Hooks", function() + it("should throw when component is not a function", function() + expect(function() + Hooks.new(Roact)() + end).to.throw("Hooked components must be functions.") + expect(function() + Hooks.new(Roact)(1) + end).to.throw("Hooked components must be functions.") + expect(function() + Hooks.new(Roact)("1") + end).to.throw("Hooked components must be functions.") + expect(function() + Hooks.new(Roact)({}) + end).to.throw("Hooked components must be functions.") + end) + end) +end From c4bbc636f6fb7e3808d00dd46668d0745f81fab6 Mon Sep 17 00:00:00 2001 From: Christopher <51393127+chriscerie@users.noreply.github.com> Date: Sat, 19 Feb 2022 08:05:19 -0800 Subject: [PATCH 4/5] Change deps to dependencies --- src/createUseBinding.spec.lua | 4 +--- src/createUseEffect.spec.lua | 24 +++++++++++------------- src/createUseMemo.spec.lua | 24 +++++++++++------------- src/createUseReducer.spec.lua | 4 +--- src/createUseState.spec.lua | 4 +--- src/createUseValue.spec.lua | 4 +--- 6 files changed, 26 insertions(+), 38 deletions(-) diff --git a/src/createUseBinding.spec.lua b/src/createUseBinding.spec.lua index 22f78b1..ec4cc2e 100644 --- a/src/createUseBinding.spec.lua +++ b/src/createUseBinding.spec.lua @@ -17,9 +17,7 @@ local function createTest(initialState, initialValue) return nil end - Test = Hooks.new(Roact)(Test) - test.handle = Roact.mount(e(Test)) - + test.handle = Roact.mount(e(Hooks.new(Roact)(Test))) return test end diff --git a/src/createUseEffect.spec.lua b/src/createUseEffect.spec.lua index d21fb70..a448530 100644 --- a/src/createUseEffect.spec.lua +++ b/src/createUseEffect.spec.lua @@ -5,31 +5,29 @@ local Hooks = require(ReplicatedStorage.Hooks) local e = Roact.createElement -local function createTest(initialState, initialDeps) +local function createTest(initialState, initialDependencies) local test = { useEffectRuns = 0, } local function Test(_, hooks) test.state, test.setState = hooks.useState(initialState) - test.deps, test.setDeps = hooks.useState(initialDeps) + test.dependencies, test.setDependencies = hooks.useState(initialDependencies) hooks.useEffect(function() test.useEffectRuns += 1 - end, test.deps) + end, test.dependencies) return nil end - Test = Hooks.new(Roact)(Test) - test.handle = Roact.mount(e(Test)) - + test.handle = Roact.mount(e(Hooks.new(Roact)(Test))) return test end return function() describe("useEffect", function() - describe("when deps are nil", function() + describe("when dependencies are nil", function() it("should run the effect after each rerender", function() local test = createTest(1) expect(test.useEffectRuns).to.equal(1) @@ -44,7 +42,7 @@ return function() end) end) - describe("when deps are empty", function() + describe("when dependencies are empty", function() it("should not run the effect after each rerender", function() local test = createTest(1, {}) expect(test.useEffectRuns).to.equal(1) @@ -55,7 +53,7 @@ return function() end) end) - describe("when deps are constant", function() + describe("when dependencies are constant", function() it("should not run the effect after each rerender", function() local test = createTest(1, { 1 }) expect(test.useEffectRuns).to.equal(1) @@ -66,12 +64,12 @@ return function() end) end) - describe("when deps are changing", function() - it("should run the effect when deps change", function() + describe("when dependencies are changing", function() + it("should run the effect when dependencies change", function() local test = createTest(1, { 1 }) expect(test.useEffectRuns).to.equal(1) - test.setDeps({ 2 }) + test.setDependencies({ 2 }) task.wait(0.1) expect(test.useEffectRuns).to.equal(2) @@ -79,7 +77,7 @@ return function() task.wait(0.1) expect(test.useEffectRuns).to.equal(2) - test.setDeps({ 3 }) + test.setDependencies({ 3 }) task.wait(0.1) expect(test.useEffectRuns).to.equal(3) end) diff --git a/src/createUseMemo.spec.lua b/src/createUseMemo.spec.lua index cb7ffd1..9a9c591 100644 --- a/src/createUseMemo.spec.lua +++ b/src/createUseMemo.spec.lua @@ -5,29 +5,27 @@ local Hooks = require(ReplicatedStorage.Hooks) local e = Roact.createElement -local function createTest(initialState, initialDeps) +local function createTest(initialState, initialDependencies) local test = {} local function Test(_, hooks) test.state, test.setState = hooks.useState(initialState) - test.deps, test.setDeps = hooks.useState(initialDeps) + test.dependencies, test.setDependencies = hooks.useState(initialDependencies) test.value = hooks.useMemo(function() return test.state - end, test.deps) + end, test.dependencies) return nil end - Test = Hooks.new(Roact)(Test) - test.handle = Roact.mount(e(Test)) - + test.handle = Roact.mount(e(Hooks.new(Roact)(Test))) return test end return function() describe("useEffect", function() - describe("when deps are nil", function() + describe("when dependencies are nil", function() it("should recalculate value after each rerender", function() local test = createTest(1) expect(test.value).to.equal(1) @@ -42,7 +40,7 @@ return function() end) end) - describe("when deps are empty", function() + describe("when dependencies are empty", function() it("should not recalculate value after each rerender", function() local test = createTest(1, {}) expect(test.value).to.equal(1) @@ -53,7 +51,7 @@ return function() end) end) - describe("when deps are constant", function() + describe("when dependencies are constant", function() it("should not recalculate value after each rerender", function() local test = createTest(1, { 1 }) expect(test.value).to.equal(1) @@ -64,13 +62,13 @@ return function() end) end) - describe("when deps are changing", function() - it("should recalculate value when deps change", function() + describe("when dependencies are changing", function() + it("should recalculate value when dependencies change", function() local test = createTest(1, { 1 }) expect(test.value).to.equal(1) test.setState(2) - test.setDeps({ 2 }) + test.setDependencies({ 2 }) task.wait(0.1) expect(test.value).to.equal(2) @@ -78,7 +76,7 @@ return function() task.wait(0.1) expect(test.value).to.equal(2) - test.setDeps({ 3 }) + test.setDependencies({ 3 }) task.wait(0.1) expect(test.value).to.equal(3) end) diff --git a/src/createUseReducer.spec.lua b/src/createUseReducer.spec.lua index 7a6e179..78c3cd3 100644 --- a/src/createUseReducer.spec.lua +++ b/src/createUseReducer.spec.lua @@ -26,9 +26,7 @@ local function createTest(initialState) return nil end - Test = Hooks.new(Roact)(Test) - test.handle = Roact.mount(e(Test)) - + test.handle = Roact.mount(e(Hooks.new(Roact)(Test))) return test end diff --git a/src/createUseState.spec.lua b/src/createUseState.spec.lua index 67e7845..a35ba75 100644 --- a/src/createUseState.spec.lua +++ b/src/createUseState.spec.lua @@ -16,9 +16,7 @@ local function createTest(initialState) return nil end - Test = Hooks.new(Roact)(Test) - test.handle = Roact.mount(e(Test)) - + test.handle = Roact.mount(e(Hooks.new(Roact)(Test))) return test end diff --git a/src/createUseValue.spec.lua b/src/createUseValue.spec.lua index c89a044..70186d3 100644 --- a/src/createUseValue.spec.lua +++ b/src/createUseValue.spec.lua @@ -17,9 +17,7 @@ local function createTest(initialState, initialValue) return nil end - Test = Hooks.new(Roact)(Test) - test.handle = Roact.mount(e(Test)) - + test.handle = Roact.mount(e(Hooks.new(Roact)(Test))) return test end From 918b2da8f538e03a7ebc3b445baee857185898d4 Mon Sep 17 00:00:00 2001 From: Christopher <51393127+chriscerie@users.noreply.github.com> Date: Sat, 19 Mar 2022 11:31:16 -0700 Subject: [PATCH 5/5] Test for useEffect dependencies changing to/from nil --- src/createUseEffect.spec.lua | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/createUseEffect.spec.lua b/src/createUseEffect.spec.lua index a448530..df46166 100644 --- a/src/createUseEffect.spec.lua +++ b/src/createUseEffect.spec.lua @@ -81,6 +81,35 @@ return function() task.wait(0.1) expect(test.useEffectRuns).to.equal(3) end) + + it("should run the effect when dependencies change to/from nil", function() + local test = createTest(1, { 1, 2 }) + expect(test.useEffectRuns).to.equal(1) + + test.setDependencies({ 1, nil }) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(2) + + test.setState(2) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(2) + + test.setDependencies({ 1, 2 }) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(3) + + test.setDependencies({ nil, 2 }) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(4) + + test.setDependencies({ 1, 2 }) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(5) + + test.setDependencies({ nil, nil }) + task.wait(0.1) + expect(test.useEffectRuns).to.equal(6) + end) end) end) end