Skip to content

Commit

Permalink
Added Tests for Result Matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
LorisSigrist committed Oct 11, 2023
1 parent 5797634 commit 282995e
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/core/utils/resultMatcher.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { describe, expect, it, vitest } from "vitest";
import { ResultMatcher } from "./resultMatcher";

describe("ResultMatcher", () => {
it("runs the success handler if the function succeeds and returns it's return type", () => {
const mockFunc = () => "success";
const mockSuccessHandler = vitest.fn(() => "return value");

const result = new ResultMatcher(mockFunc)
.success(mockSuccessHandler)
.rescue(Error, () => "error")
.rescueAll(() => "rescue all")
.call();

expect(mockSuccessHandler).toHaveBeenCalledWith("success");
expect(result).toBe("return value");
});

it("runs the rescue handler if the function throws the specified error", () => {
const mockFunc = () => {
throw new SyntaxError("error");
};

const mockRescueHandler = vitest.fn(() => "return value");

const result = new ResultMatcher(mockFunc)
.success(() => "success")
.rescue(RangeError, () => "range error")
.rescue(SyntaxError, mockRescueHandler) // <-- this one
.rescue(TypeError, () => "type error")
.rescueAll(() => "rescue all")
.call();

expect(mockRescueHandler).toHaveBeenCalledWith(new SyntaxError("error"));
expect(result).toBe("return value");
});

it("runs the rescueAll handler if the function throws an error that is not specified", () => {
const mockFunc = () => {
throw new SyntaxError("error");
};

const mockRescueAllHandler = vitest.fn(() => "return value");

const result = new ResultMatcher(mockFunc)
.success(() => "success")
.rescue(RangeError, () => "range error")
.rescue(TypeError, () => "type error")
.rescueAll(mockRescueAllHandler) // <-- this one
.call();

expect(mockRescueAllHandler).toHaveBeenCalledWith(new SyntaxError("error"));
expect(result).toBe("return value");
});

it("rethrows unhandled errors", () => {
const mockFunc = () => {
throw new SyntaxError("error");
};

const runUnhandled = () =>
new ResultMatcher(mockFunc)
.success(() => "success")
.rescue(RangeError, () => "range error")
.rescue(TypeError, () => "type error")
.call();

expect(runUnhandled).toThrow(new SyntaxError("error"));
});
});

0 comments on commit 282995e

Please sign in to comment.