import { expect, test, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import routes from '../src/constants/routes';
import Header from '../src/components/Shared/Header';
import { Provider } from 'react-redux';
import { store } from '../src/redux/store';

const mockedUseNavigate = vi.fn(() => {
  return (path: string) => {
    return routes[path];
  };
});
vi.mock('react-router-dom', async () => {
  const mod = await vi.importActual<typeof import('react-router-dom')>(
    'react-router-dom'
  );
  return {
    ...mod,
    useNavigate: () => mockedUseNavigate
  };
});

test('Header - test links', () => {
  render(
    <Provider store={store}>
      <Header />
    </Provider>
  );
  const homeLink = screen.getAllByText('Silk Road');
  const firstLink = homeLink[0];
  expect(firstLink).toBeTruthy();
  fireEvent.click(firstLink);
  expect(mockedUseNavigate).toHaveBeenCalledWith('/');
  const secondLink = homeLink[1];
  expect(secondLink).toBeTruthy();
  fireEvent.click(secondLink);
  expect(mockedUseNavigate).toHaveBeenCalledWith('/');
  const addLink = screen.getAllByText('Add Drug');
  const add = addLink[0];
  expect(add).toBeTruthy();
  fireEvent.click(add);
  expect(mockedUseNavigate).toHaveBeenCalledWith('/add');
});
