import { setIdleTimeout } from './popup';

// this help us to simulate setTimeout and cleraTimeOut
jest.useFakeTimers();

describe('setIdleTimeout', () => {
  it('calls onIdle after the specified number of seconds of inactivity', () => {
    const onIdle = jest.fn();
    setIdleTimeout(1, onIdle);

    jest.advanceTimersByTime(999);
    expect(onIdle).not.toHaveBeenCalled();

    jest.advanceTimersByTime(1);
    expect(onIdle).toHaveBeenCalled();
  });

  it('restarts the timer when activity is detected', () => {
    const onIdle = jest.fn();
    setIdleTimeout(1, onIdle);

    jest.advanceTimersByTime(999);
    document.dispatchEvent(new Event('mousemove'));
    jest.advanceTimersByTime(1000);

    jest.advanceTimersByTime(999);
    expect(onIdle).not.toHaveBeenCalled();

    jest.advanceTimersByTime(1);
    expect(onIdle).toHaveBeenCalled();
  });
});
