import { passOrYield } from "../unit";

class TestError {
  constructor(readonly message: string) {}
}

describe("passOrYield", () => {
  const f = () => new TestError("Assert!");
  it("passes constructed error correctly", () => {
    const e = new TestError("Failed!");
    expect(passOrYield(TestError, f)(e)).toEqual(e);
  });

  it("handles thrown object", () => {
    try {
      throw new TestError("Thrown!");
    } catch (e) {
      expect(passOrYield(TestError, f)(e)).toEqual(e);
    }
  });

  it("handles a synthetic error", () => {
    // 3rd party code can mangle our errors but usually produce a descendant
    // of the builtin, Error, and preserve the name from our classes
    // constructor function
    const synthetic = new Error("Synthetic!");
    synthetic.name = TestError.name;
    expect(passOrYield(TestError, f)(synthetic)).toEqual(synthetic);
  });
});
