import React from 'react';
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import { MemoryRouter, Route, Routes } from 'react-router-dom';

import { api } from './api';
import PublicCvPage from './views/PublicCvPage';

jest.mock('./api', () => ({
  api: {
    defaults: { baseURL: '/api' },
    get: jest.fn(),
  },
}));

const mockedApi = api as jest.Mocked<typeof api>;

test('public CV exposes the rendered CV and PDF download', async () => {
  mockedApi.get.mockResolvedValueOnce({ data: { html: '<p>Public CV</p>', name: 'Ada Lovelace' } } as any);

  render(
    <MemoryRouter initialEntries={['/cv/public-slug']} future={{ v7_startTransition: true, v7_relativeSplatPath: true }}>
      <Routes><Route path="/cv/:slug" element={<PublicCvPage />} /></Routes>
    </MemoryRouter>,
  );

  expect(await screen.findByTitle('Public CV')).toHaveAttribute('srcdoc', '<p>Public CV</p>');
  expect(screen.getByRole('link', { name: 'Download PDF' })).toHaveAttribute('href', '/api/public-cv/public-slug/pdf');
  expect(mockedApi.get).toHaveBeenCalledWith('/public-cv/public-slug');
});
