import React from 'react';
import { render, cleanup } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';

import '~tests/helpers/match-media.mock';
import { getSingleContentData } from '~tests/factories/modules/content.factory';
import Main from '.';
import { schema, templatesInSchema } from '~tests/factories/modules/schema.factory.js';

const section = (title) => {
  const data = {
    modules: [
      {
        name: 'content',
        ...getSingleContentData('h2', 1, title),
      },
    ],
  };

  return data;
};

const pageObject = { page: { path: 's' } };

describe('Main Component', () => {
  test('render main', async () => {
    const sectionData = section('main');
    const { container } = render(<Main section={sectionData} pageContext={pageObject} />);
    expect(container).toBeTruthy();
    expect(container.querySelectorAll('main.modulePage')).toHaveLength(1);
  });
  test('render main with author', async () => {
    const sectionData = section('author');
    const pageObjectSecond = { page: { path: 'author', author: {} } };

    const { container } = render(<Main section={sectionData} pageContext={pageObjectSecond} />);

    expect(container).toBeTruthy();
    expect(container.querySelectorAll('main.modulePage')).toHaveLength(1);
  });

  test('render main with sitemap as path', async () => {
    const sectionData = section('sitemap');

    const pageObjectSecond = {
      page: { path: 'sitemap' },
      templates: templatesInSchema,
      sitemapData: schema,
    };

    const { container } = render(<Main section={sectionData} pageContext={pageObjectSecond} />);

    expect(container).toBeTruthy();
    expect(container.querySelectorAll('main.modulePage')).toHaveLength(1);
  });

  test('render main with 404 as path ', async () => {
    const sectionData = section('404Sites');

    const pageObjectSecond = { page: { path: '404', author: {} } };

    const { container } = render(<Main section={sectionData} pageContext={pageObjectSecond} />);
    expect(container).toBeTruthy();
    expect(container.querySelectorAll('main.modulePage')).toHaveLength(1);
  });
});
afterEach(() => {
  cleanup();
});
