@applicaster/quick-brick-core
Version:
Core package for Applicaster's Quick Brick App
144 lines (116 loc) • 3.8 kB
JavaScript
import { act, renderHook } from "@testing-library/react-hooks";
import { useErrorStore } from "../store";
describe("Error Store", () => {
beforeEach(() => {
act(() => {
useErrorStore.getState().dismissError();
});
});
describe("Initial state", () => {
it("should have correct default values", () => {
const { result } = renderHook(() => useErrorStore());
expect(result.current).toMatchObject({
hasError: false,
error: undefined,
info: undefined,
recoverable: true,
setError: expect.any(Function),
dismissError: expect.any(Function),
});
});
});
describe("setError action", () => {
it("should set error state with provided values", () => {
const testError = new Error("Test error");
const testInfo = "Test error info";
const isRecoverable = true;
const { result } = renderHook(() => useErrorStore());
act(() => {
result.current.setError(testError, testInfo, isRecoverable);
});
expect(result.current).toMatchObject({
hasError: true,
error: testError,
info: testInfo,
recoverable: isRecoverable,
setError: expect.any(Function),
dismissError: expect.any(Function),
});
});
it("should set non-recoverable error when specified", () => {
const testError = new Error("Test error");
const testInfo = "Test error info";
const isRecoverable = false;
const { result } = renderHook(() => useErrorStore());
act(() => {
result.current.setError(testError, testInfo, isRecoverable);
});
expect(result.current).toMatchObject({
recoverable: false,
});
});
it("should default to recoverable error when not specified", () => {
const testError = new Error("Test error");
const testInfo = "Test error info";
const { result } = renderHook(() => useErrorStore());
act(() => {
result.current.setError(testError, testInfo);
});
expect(result.current).toMatchObject({
recoverable: true,
});
});
});
describe("dismissError action", () => {
it("should reset error state to defaults", () => {
const testError = new Error("Test error");
const { result } = renderHook(() => useErrorStore());
act(() => {
result.current.setError(testError, "info", false);
});
expect(result.current).toMatchObject({
hasError: true,
recoverable: false,
});
act(() => {
result.current.dismissError();
});
expect(result.current).toMatchObject({
hasError: false,
error: undefined,
info: undefined,
recoverable: true,
setError: expect.any(Function),
dismissError: expect.any(Function),
});
});
});
describe("Direct store access via API", () => {
it("should allow direct access to store outside React components", () => {
act(() => {
useErrorStore
.getState()
.setError(new Error("API error"), "API info", false);
});
const { result } = renderHook(() => useErrorStore());
expect(result.current).toMatchObject({
hasError: true,
error: expect.objectContaining({ message: "API error" }),
info: "API info",
recoverable: false,
});
});
it("should allow clearing errors via API", () => {
act(() => {
useErrorStore.getState().setError(new Error("API error"), "API info");
});
act(() => {
useErrorStore.getState().dismissError();
});
const { result } = renderHook(() => useErrorStore());
expect(result.current).toMatchObject({
hasError: false,
});
});
});
});