import { withTimeout } from "../../src/utils/timeout";

describe("withTimeout()", () => {
  jest.useFakeTimers();

  it("resolves if promise completes within time", async () => {
    const promise = withTimeout(Promise.resolve("ok"), 500);
    await expect(promise).resolves.toBe("ok");
  });

  it("rejects with provided error on timeout", async () => {
    const promise = withTimeout(
      new Promise(() => {}),
      500,
      new Error("timeout")
    );
    jest.advanceTimersByTime(500);
    await expect(promise).rejects.toThrow("timeout");
  });
});
