aboutsummaryrefslogtreecommitdiffstats
path: root/tests/cypress/e2e/pages/blog.cy.ts
blob: 3a422d29658d246f67917b37ec9c0856029044a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { CONFIG } from '../../../../src/utils/config';
import { ROUTES } from '../../../../src/utils/constants';

type ArticlesGroup = {
  first: string;
  total: string;
};

describe('Blog Page', () => {
  beforeEach(() => {
    cy.visit(ROUTES.BLOG);
  });

  it('loads the correct number of pages', () => {
    cy.findByText(
      /(?<first>\d+) articles chargés sur un total de (?<total>\d+)/i
    )
      .then(($div) => {
        const firstLastNumbers = /(?<first>\d+).*[\D](?<total>\d+)/;
        const result = RegExp(firstLastNumbers).exec($div.text());

        // eslint-disable-next-line @typescript-eslint/no-unused-expressions
        expect(result).to.not.be.null;

        const { first, total } = result
          ? (result.groups as ArticlesGroup)
          : { first: '0', total: '0' };
        const totalArticles = parseInt(total, 10);

        expect(parseInt(first, 10)).to.be.within(1, CONFIG.postsPerPage);
        expect(totalArticles).to.be.at.least(1);

        const totalPages = Math.ceil(totalArticles / CONFIG.postsPerPage);
        const remainingPages = totalPages - 1;

        return Array.from({ length: remainingPages }, (_, i) => i + 1);
      })
      .then((remainingPages) => {
        if (remainingPages.length >= 1) {
          cy.wrap(remainingPages).each(() => {
            cy.findByRole('button', {
              name: /Charger plus d’articles/i,
            }).click();
          });
        }

        cy.findByRole('button', { name: /Charger plus d’articles/i }).should(
          'not.exist'
        );
      });
  });
});