Add tests for useBoolean and useStateWithReset

This commit is contained in:
2019-12-31 18:01:18 +05:30
parent 97d9ae5e96
commit 0705d18e23
2 changed files with 221 additions and 0 deletions
+125
View File
@@ -0,0 +1,125 @@
import { renderHook, act } from "@testing-library/react-hooks";
import useBoolean from "./useBoolean";
test("useBoolean should return correct initial values", () => {
const { result } = renderHook(() => useBoolean(true));
expect(result.current.value).toBe(true);
expect(typeof result.current.setValue).toBe("function");
expect(typeof result.current.toggle).toBe("function");
expect(typeof result.current.setTrue).toBe("function");
expect(typeof result.current.setFalse).toBe("function");
});
test("useBoolean should have proper setValue", () => {
const { result } = renderHook(() => useBoolean(true));
// Ensure it can set to false
{
act(() => {
result.current.setValue(false);
});
expect(result.current.value).toBe(false);
}
// Ensure it can set to true
{
act(() => {
result.current.setValue(true);
});
expect(result.current.value).toBe(true);
}
// Ensure it is not just toggling values
{
act(() => {
result.current.setValue(true);
});
expect(result.current.value).toBe(true);
}
});
test("useBoolean should have proper toggle", () => {
const { result } = renderHook(() => useBoolean(false));
// Ensure it does toggle
{
act(() => {
result.current.toggle();
});
expect(result.current.value).toBe(true);
act(() => {
result.current.toggle();
});
expect(result.current.value).toBe(false);
}
});
test("useBoolean should have proper setTrue", () => {
const { result } = renderHook(() => useBoolean(false));
// Ensure setTrue basic works
{
act(() => {
result.current.setTrue();
});
expect(result.current.value).toBe(true);
}
// Ensure setTrue is not toggling
{
act(() => {
result.current.setTrue();
});
expect(result.current.value).toBe(true);
}
// Ensure setTrue ignores input values
{
act(() => {
(result.current.setTrue as any)(false);
});
expect(result.current.value).toBe(true);
}
});
test("useBoolean should have proper setFalse", () => {
const { result } = renderHook(() => useBoolean(false));
// Ensure setFalse basic works
{
act(() => {
result.current.setFalse();
});
expect(result.current.value).toBe(false);
}
// Ensure setFalse is not toggling
{
act(() => {
result.current.setFalse();
});
expect(result.current.value).toBe(false);
}
// Ensure setFalse ignores input values
{
act(() => {
(result.current.setFalse as any)(false);
});
expect(result.current.value).toBe(false);
}
});
+96
View File
@@ -0,0 +1,96 @@
import { renderHook, act } from "@testing-library/react-hooks";
import useStateWithReset from "./useStateWithReset";
test("useStateWithReset should return correct initial values", () => {
const randomNumber = Math.random() * 1000;
const { result } = renderHook(() => useStateWithReset(randomNumber));
expect(result.current.value).toBe(randomNumber);
expect(typeof result.current.setValue).toBe("function");
expect(typeof result.current.reset).toBe("function");
});
test("useStateWithReset should have proper setValue", () => {
const randomNumber1 = Math.random() * 1000;
const randomNumber2 = Math.random() * 1000;
const randomNumber3 = Math.random() * 1000;
const { result } = renderHook(() => useStateWithReset(randomNumber1));
// Ensure it can set correctly
{
act(() => {
result.current.setValue(randomNumber2);
});
expect(result.current.value).toBe(randomNumber2);
}
// Ensure it can set again
{
act(() => {
result.current.setValue(randomNumber3);
});
expect(result.current.value).toBe(randomNumber3);
}
});
test("useStateWithReset should have proper reset", () => {
const randomNumber1 = Math.random() * 1000;
const randomNumber2 = Math.random() * 1000;
const randomNumber3 = Math.random() * 1000;
const { result } = renderHook(() => useStateWithReset(randomNumber1));
// After set, ensure it can reset correctly
{
act(() => {
result.current.setValue(randomNumber2);
});
expect(result.current.value).toBe(randomNumber2);
act(() => {
result.current.reset();
});
expect(result.current.value).toBe(randomNumber1);
}
// After reset, ensure it can set again
{
act(() => {
result.current.setValue(randomNumber3);
});
expect(result.current.value).toBe(randomNumber3);
}
});
test("useStateWithReset - reset ignores future initialValues", () => {
const randomNumber1 = 1;
const randomNumber2 = 2;
const randomNumber3 = 3;
const { result, rerender } = renderHook(props => useStateWithReset(props), {
initialProps: randomNumber1
});
// After set and changing initialValue, ensure it can reset correctly to first initialValue
{
const oldReset = result.current.reset;
act(() => {
rerender(randomNumber3);
});
const newReset = result.current.reset;
act(() => {
result.current.reset();
});
expect(oldReset).toBe(newReset);
expect(result.current.value).toBe(randomNumber1);
}
});