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

jest.mock('@/i18n', () => jest.fn().mockResolvedValue({messages: {ArticlesPage: {articles_title: 'Le Mag', categories_title: 'Rubriques', no_articles: 'Aucun article'}}}));
jest.mock('@/services/articleService', () => ({
  getAllArticlesPublished: jest.fn().mockResolvedValue([]),
  getArticleCategories: jest.fn().mockResolvedValue([]),
}));
jest.mock('@/components/Articles/ArticleCard', () => ({__esModule: true, default: ({article}: {article: {id: number}}) => <div data-testid="card" data-id={article.id} />}));
jest.mock('next/link', () => ({
  __esModule: true,
  default: ({children, href, ...rest}: {children: React.ReactNode; href: string}) => <a href={href} {...rest}>{children}</a>,
}));

import Page from '../page';
import {getArticleCategories} from '@/services/articleService';

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

describe('Accueil du Mag (spec L67)', () => {
  beforeEach(() => jest.clearAllMocks());

  it('shouldListTheCategoriesAsLinksToTheirPage', async () => {
    (getArticleCategories as jest.Mock).mockResolvedValue([
      {id: 1, nom: 'Actualités', slug: 'actualites', description: null, ordre: 1},
      {id: 2, nom: 'Portraits', slug: 'portraits', description: null, ordre: 2},
    ]);

    render(await Page(props));

    expect(screen.getByText('Rubriques')).toBeInTheDocument();
    expect(screen.getByRole('link', {name: 'Actualités'})).toHaveAttribute('href', '/fr/articles/categorie/actualites');
    expect(screen.getByRole('link', {name: 'Portraits'})).toHaveAttribute('href', '/fr/articles/categorie/portraits');
  });

  it('shouldHideTheCategoriesBlockWithoutCategory', async () => {
    (getArticleCategories as jest.Mock).mockResolvedValue([]);

    render(await Page(props));

    expect(screen.queryByText('Rubriques')).not.toBeInTheDocument();
  });
});
