2021-11-30 13:07:44 +00:00
|
|
|
/**
|
2022-07-07 05:23:54 +00:00
|
|
|
* @vitest-environment jsdom
|
2021-11-30 13:07:44 +00:00
|
|
|
*/
|
|
|
|
import { act, renderHook } from "@testing-library/react-hooks";
|
2022-07-07 05:23:54 +00:00
|
|
|
import { describe, expect, it } from "vitest";
|
2021-11-30 13:07:44 +00:00
|
|
|
import useToggle from "./useToggle";
|
|
|
|
|
|
|
|
describe("useToggle", () => {
|
|
|
|
it("has a default value of false", () => {
|
|
|
|
const { result } = renderHook(() => useToggle());
|
|
|
|
const [value] = result.current;
|
|
|
|
|
|
|
|
expect(value).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("uses the initial value", () => {
|
|
|
|
const { result } = renderHook(() => useToggle(true));
|
|
|
|
const [value] = result.current;
|
|
|
|
|
|
|
|
expect(value).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("toggles the value", () => {
|
|
|
|
const { result } = renderHook(() => useToggle());
|
|
|
|
const [, toggleValue] = result.current;
|
|
|
|
|
|
|
|
act(() => toggleValue());
|
|
|
|
|
|
|
|
const [value] = result.current;
|
|
|
|
expect(value).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("sets the value", () => {
|
|
|
|
const { result } = renderHook(() => useToggle());
|
|
|
|
const [, , setValue] = result.current;
|
|
|
|
|
|
|
|
act(() => setValue(true));
|
|
|
|
|
|
|
|
const [value] = result.current;
|
|
|
|
expect(value).toBe(true);
|
|
|
|
});
|
|
|
|
});
|