import React from 'react';
import {render, screen} from '@testing-library/react';

jest.mock('@/services/mediaPageService', () => ({
  getMediaPage: jest.fn(),
}));

jest.mock('next/link', () => {
  return ({children, href, ...props}: {children: React.ReactNode; href: string; [key: string]: unknown}) => (
    <a href={href} {...props}>{children}</a>
  );
});

import Page from '../page';
import {getMediaPage} from '@/services/mediaPageService';

const mockedGetMediaPage = getMediaPage as jest.Mock;

const props = {params: Promise.resolve({locale: 'en'})};

describe('Page association', () => {
  beforeEach(() => {
    jest.clearAllMocks();
    mockedGetMediaPage.mockResolvedValue({
      slug: 'association',
      langue: 'en',
      titre: 'The association',
      contenu: '<p>Since 2001</p>',
    });
  });

  it('shouldRenderTheEditorialContentOfTheAssociationHome', async () => {
    render(await Page(props));

    expect(mockedGetMediaPage).toHaveBeenCalledWith('association', 'en');
    expect(screen.getByRole('heading', {level: 1})).toHaveTextContent('The association');
    expect(screen.getByText('Since 2001')).toBeInTheDocument();
  });

  it.each([
    ['menu_adherer', '/en/adhesion'],
    ['menu_defiscalisation', '/en/association/defiscalisation'],
    ['menu_questions', '/en/association/questions-reponses'],
  ])('shouldLinkToTheSubPage %s at %s', async (label, href) => {
    render(await Page(props));

    expect(screen.getByText(label).closest('a')).toHaveAttribute('href', href);
  });
});
