import { render } from '@testing-library/react';
import React from 'react';
import { describe, expect, it, vi } from 'vitest';
import { on } from '../../initialize';
import { useBusy } from './useBusy';

let callback: (busyState: boolean) => void;

vi.mock('../../initialize', () => ({
  on: vi.fn().mockImplementationOnce((event, cb) => {
    callback = cb;
  }),
}));

const TestWrapper: React.FC = () => {
  const { isBusy } = useBusy();

  return <p id="value">{String(isBusy)}</p>;
};

describe('useBusy', () => {
  it(`both 'val' and 'debouncedValue' are set to the initial value`, () => {
    const { container } = render(<TestWrapper />);
    const currentValue = container.querySelector('#value');

    expect(on).toHaveBeenCalled();
    expect(currentValue?.textContent).toBe('false');

    callback(true);

    expect(container.querySelector('#value')?.textContent).toBe('true');
  });
});
