import wait from './wait';

describe('wait', () => {
  beforeEach(() => jest.useFakeTimers());
  afterEach(() => jest.useRealTimers());

  it('resolves after the specified timeout', async () => {
    const p = wait(1000);
    jest.advanceTimersByTime(1000);
    await expect(p).resolves.toBeUndefined();
  });

  it('resolves immediately when signal is already aborted', async () => {
    const controller = new AbortController();
    controller.abort();
    const p = wait(30_000, { signal: controller.signal });
    await expect(p).resolves.toBeUndefined();
  });

  it('resolves immediately when signal is aborted mid-wait', async () => {
    const controller = new AbortController();
    const p = wait(30_000, { signal: controller.signal });
    controller.abort();
    await expect(p).resolves.toBeUndefined();
  });

  it('still resolves via timer when no signal is provided', async () => {
    const p = wait(500);
    jest.advanceTimersByTime(500);
    await expect(p).resolves.toBeUndefined();
  });
});
