import { render, screen, fireEvent } from '@testing-library/react';
import { ThemeProvider } from '../../../theme';

const replace = jest.fn();

jest.mock('next-intl', () => ({
  useLocale: () => 'fa',
  useTranslations: () => (key: string, params?: Record<string, unknown>) =>
    params ? `${key}:${params.locale}` : key,
}));

jest.mock('@/i18n/navigation', () => ({
  usePathname: () => '/nurse/earnings',
  useRouter: () => ({ replace }),
}));

import LocaleSwitcher from './LocaleSwitcher';

describe('<LocaleSwitcher/> component', () => {
  beforeEach(() => replace.mockClear());

  it('renders a single icon button', () => {
    render(
      <ThemeProvider>
        <LocaleSwitcher />
      </ThemeProvider>,
    );
    expect(screen.getAllByRole('button')).toHaveLength(1);
  });

  it('replaces the current path with the other locale on click, preserving the route', () => {
    render(
      <ThemeProvider>
        <LocaleSwitcher />
      </ThemeProvider>,
    );
    fireEvent.click(screen.getByRole('button'));
    expect(replace).toHaveBeenCalledWith('/nurse/earnings', { locale: 'en' });
  });
});
