-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core,react): errors &
useAsyncAction
hook (#462)
- Loading branch information
Showing
5 changed files
with
539 additions
and
7 deletions.
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { ReactiveDotError, QueryError, MutationError } from "./errors.js"; | ||
import { describe, it, expect } from "vitest"; | ||
|
||
describe("ReactiveDotError", () => { | ||
it("should create error with message", () => { | ||
const error = new ReactiveDotError("test error"); | ||
|
||
expect(error.message).toBe("test error"); | ||
expect(error).toBeInstanceOf(Error); | ||
}); | ||
|
||
it("should create error from another error", () => { | ||
const originalError = new Error("original error"); | ||
const error = ReactiveDotError.from(originalError, "wrapped error"); | ||
|
||
expect(error.message).toBe("wrapped error"); | ||
expect(error.cause).toBe(originalError); | ||
}); | ||
}); | ||
|
||
describe("QueryError", () => { | ||
it("should extend ReactiveDotError", () => { | ||
const error = new QueryError("query error"); | ||
|
||
expect(error).toBeInstanceOf(ReactiveDotError); | ||
expect(error.message).toBe("query error"); | ||
}); | ||
}); | ||
|
||
describe("MutationError", () => { | ||
it("should extend ReactiveDotError", () => { | ||
const error = new MutationError("mutation error"); | ||
|
||
expect(error).toBeInstanceOf(ReactiveDotError); | ||
expect(error.message).toBe("mutation error"); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { useAsyncAction } from "./use-async-action.js"; | ||
import { idle, MutationError, pending } from "@reactive-dot/core"; | ||
import { act, renderHook } from "@testing-library/react"; | ||
import { Subject } from "rxjs"; | ||
import { expect, it, vi } from "vitest"; | ||
|
||
it("should handle Promise-based actions", async () => { | ||
const mockAction = vi.fn().mockResolvedValue("success"); | ||
const { result } = renderHook(() => useAsyncAction(mockAction)); | ||
|
||
expect(result.current[0]).toBe(idle); | ||
|
||
await act(async () => { | ||
result.current[1](); | ||
}); | ||
|
||
expect(mockAction).toHaveBeenCalled(); | ||
expect(result.current[0]).toBe("success"); | ||
}); | ||
|
||
it("should handle Observable-based actions", async () => { | ||
const subject = new Subject<string>(); | ||
const mockAction = vi.fn().mockReturnValue(subject); | ||
const { result } = renderHook(() => useAsyncAction(mockAction)); | ||
|
||
expect(result.current[0]).toBe(idle); | ||
|
||
act(() => { | ||
result.current[1](); | ||
}); | ||
|
||
expect(result.current[0]).toBe(pending); | ||
|
||
act(() => { | ||
subject.next("success"); | ||
}); | ||
|
||
expect(result.current[0]).toBe("success"); | ||
}); | ||
|
||
it("should handle synchronous rejection", async () => { | ||
const error = new Error("test error"); | ||
const mockAction = vi.fn(() => { | ||
throw error; | ||
}); | ||
const { result } = renderHook(() => useAsyncAction(mockAction)); | ||
|
||
await act(async () => { | ||
try { | ||
await result.current[1](); | ||
} catch { | ||
/* empty */ | ||
} | ||
}); | ||
|
||
expect(result.current[0]).toBeInstanceOf(MutationError); | ||
expect((result.current[0] as MutationError).cause).toBe(error); | ||
}); | ||
|
||
it("should handle Promise rejection", async () => { | ||
const error = new Error("test error"); | ||
const mockAction = vi.fn().mockRejectedValue(error); | ||
const { result } = renderHook(() => useAsyncAction(mockAction)); | ||
|
||
await act(async () => { | ||
try { | ||
await result.current[1](); | ||
} catch { | ||
/* empty */ | ||
} | ||
}); | ||
|
||
expect(result.current[0]).toBeInstanceOf(MutationError); | ||
expect((result.current[0] as MutationError).cause).toBe(error); | ||
}); | ||
|
||
it("should handle Observable errors", async () => { | ||
const subject = new Subject<string>(); | ||
const error = new Error("test error"); | ||
const mockAction = vi.fn().mockReturnValue(subject); | ||
const { result } = renderHook(() => useAsyncAction(mockAction)); | ||
|
||
act(() => { | ||
result.current[1](); | ||
}); | ||
|
||
act(() => { | ||
subject.error(error); | ||
}); | ||
|
||
expect(result.current[0]).toBeInstanceOf(MutationError); | ||
expect((result.current[0] as MutationError).cause).toBe(error); | ||
}); |
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,5 @@ | ||
import { defineProject } from "vitest/config"; | ||
|
||
export default defineProject({ | ||
test: { environment: "jsdom" }, | ||
}); |
Oops, something went wrong.