This repository has been archived by the owner on Jan 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Add tests to hooks #30
Open
chriscerie
wants to merge
5
commits into
Kampfkarren:main
Choose a base branch
from
chriscerie:add-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
std = "roblox" | ||
std = "roblox+testez" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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.handle = Roact.mount(e(Hooks.new(Roact)(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
local ReplicatedStorage = game:GetService("ReplicatedStorage") | ||
|
||
local Roact = require(ReplicatedStorage.Roact) | ||
local Hooks = require(ReplicatedStorage.Hooks) | ||
|
||
local e = Roact.createElement | ||
|
||
local function createTest(initialState, initialDependencies) | ||
local test = { | ||
useEffectRuns = 0, | ||
} | ||
|
||
local function Test(_, hooks) | ||
test.state, test.setState = hooks.useState(initialState) | ||
test.dependencies, test.setDependencies = hooks.useState(initialDependencies) | ||
|
||
hooks.useEffect(function() | ||
test.useEffectRuns += 1 | ||
end, test.dependencies) | ||
|
||
return nil | ||
end | ||
|
||
test.handle = Roact.mount(e(Hooks.new(Roact)(Test))) | ||
return test | ||
end | ||
|
||
return function() | ||
describe("useEffect", 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) | ||
|
||
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 dependencies 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 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) | ||
|
||
test.setState(2) | ||
task.wait(0.1) | ||
expect(test.useEffectRuns).to.equal(1) | ||
end) | ||
end) | ||
|
||
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.setDependencies({ 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.setDependencies({ 3 }) | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
local ReplicatedStorage = game:GetService("ReplicatedStorage") | ||
|
||
local Roact = require(ReplicatedStorage.Roact) | ||
local Hooks = require(ReplicatedStorage.Hooks) | ||
|
||
local e = Roact.createElement | ||
|
||
local function createTest(initialState, initialDependencies) | ||
local test = {} | ||
|
||
local function Test(_, hooks) | ||
test.state, test.setState = hooks.useState(initialState) | ||
test.dependencies, test.setDependencies = hooks.useState(initialDependencies) | ||
|
||
test.value = hooks.useMemo(function() | ||
return test.state | ||
end, test.dependencies) | ||
|
||
return nil | ||
end | ||
|
||
test.handle = Roact.mount(e(Hooks.new(Roact)(Test))) | ||
return test | ||
end | ||
|
||
return function() | ||
describe("useEffect", 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) | ||
|
||
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 dependencies 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 dependencies 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 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.setDependencies({ 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.setDependencies({ 3 }) | ||
task.wait(0.1) | ||
expect(test.value).to.equal(3) | ||
end) | ||
end) | ||
end) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
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.handle = Roact.mount(e(Hooks.new(Roact)(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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need wait? CI shouldn't need yields.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should instead just call Roact.update directly