diff options
| author | Armand Philippot <git@armandphilippot.com> | 2023-09-26 15:54:28 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2023-10-24 12:23:48 +0200 |
| commit | 70efcfeaa0603415dd992cb662d8efb960e6e49a (patch) | |
| tree | 5d37e98fae9aa7e5c3d8ef30a10db9fed9b63e36 /tests/cypress | |
| parent | 31695306bfed44409f03006ea717fd2cceff8f87 (diff) | |
refactor(routes): replace hardcoded routes with constants
It makes it easier to change a route if needed and it avoid typo
mistakes.
I also refactored a bit the concerned files to be complient with the
new ESlint config. However, I should rewrite the pages to reduce
the number of statements.
Diffstat (limited to 'tests/cypress')
| -rw-r--r-- | tests/cypress/e2e/nav.cy.ts | 24 | ||||
| -rw-r--r-- | tests/cypress/e2e/pages/blog.cy.ts | 30 | ||||
| -rw-r--r-- | tests/cypress/e2e/pages/contact.cy.ts | 8 | ||||
| -rw-r--r-- | tests/cypress/e2e/pages/cv.cy.ts | 4 | ||||
| -rw-r--r-- | tests/cypress/e2e/pages/legal-notice.cy.ts | 4 | ||||
| -rw-r--r-- | tests/cypress/e2e/search.cy.ts | 14 |
6 files changed, 52 insertions, 32 deletions
diff --git a/tests/cypress/e2e/nav.cy.ts b/tests/cypress/e2e/nav.cy.ts index 5851058..14d4e8c 100644 --- a/tests/cypress/e2e/nav.cy.ts +++ b/tests/cypress/e2e/nav.cy.ts @@ -1,3 +1,5 @@ +import { ROUTES } from '../../../src/utils/constants'; + describe( 'Main navigation', { viewportWidth: 1280, viewportHeight: 720 }, @@ -35,27 +37,27 @@ describe( cy.findByLabelText(/Ouvrir le menu/i).should('be.hidden'); }); - it('should navigate to the blog page', async () => { + it('should navigate to the blog page', () => { cy.findByRole('link', { name: /Blog/i }).click(); - cy.url().should('include', '/blog'); + cy.url().should('include', ROUTES.BLOG); cy.findByRole('heading', { level: 1 }).contains('Blog'); }); - it('should navigate to the CV page', async () => { + it('should navigate to the CV page', () => { cy.findByRole('link', { name: /CV/i }).click(); - cy.url().should('include', '/cv'); + cy.url().should('include', ROUTES.CV); cy.findByRole('heading', { level: 1 }).contains('CV'); }); - it('should navigate to the projects page', async () => { - cy.findByRole('link', { name: /Projects/i }).click(); - cy.url().should('include', '/projets'); + it('should navigate to the projects page', () => { + cy.findByRole('link', { name: /Projets/i }).click(); + cy.url().should('include', ROUTES.PROJECTS); cy.findByRole('heading', { level: 1 }).contains('Projets'); }); - it('should navigate to the contact page', async () => { + it('should navigate to the contact page', () => { cy.findByRole('link', { name: /Contact/i }).click(); - cy.url().should('include', '/contact'); + cy.url().should('include', ROUTES.CONTACT); cy.findByRole('heading', { level: 1 }).contains('Contact'); }); } @@ -66,9 +68,9 @@ describe('Footer navigation', () => { cy.visit('/'); }); - it('should navigate to the legal notice page', async () => { + it('should navigate to the legal notice page', () => { cy.findByRole('link', { name: /Mentions légales/i }).click(); - cy.url().should('include', '/mentions-legales'); + cy.url().should('include', ROUTES.LEGAL_NOTICE); cy.findByRole('heading', { level: 1 }).contains('Mentions légales'); }); }); diff --git a/tests/cypress/e2e/pages/blog.cy.ts b/tests/cypress/e2e/pages/blog.cy.ts index 4327332..2ec14c2 100644 --- a/tests/cypress/e2e/pages/blog.cy.ts +++ b/tests/cypress/e2e/pages/blog.cy.ts @@ -1,30 +1,38 @@ import { settings } from '../../../../src/utils/config'; +import { ROUTES } from '../../../../src/utils/constants'; + +type ArticlesGroup = { + first: string; + total: string; +}; describe('Blog Page', () => { beforeEach(() => { - cy.visit('/blog'); + cy.visit(ROUTES.BLOG); }); it('loads the correct number of pages', () => { - cy.findByText(/(\d+) articles chargés sur un total de (\d+)/i) + cy.findByText( + /(?<first>\d+) articles chargés sur un total de (?<total>\d+)/i + ) .then(($div) => { - type ArticlesGroup = { - first: string; - total: string; - }; - const firstLastNumbers = /(?<first>\d+).*[\D](?<total>\d+)/; - const result = $div.text().match(firstLastNumbers); + 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!.groups as ArticlesGroup; - const firstArticles = parseInt(first, 10); + const { first, total } = result + ? (result.groups as ArticlesGroup) + : { first: '0', total: '0' }; const totalArticles = parseInt(total, 10); - expect(firstArticles).to.be.within(1, settings.postsPerPage); + + expect(parseInt(first, 10)).to.be.within(1, settings.postsPerPage); expect(totalArticles).to.be.at.least(1); const totalPages = Math.ceil(totalArticles / settings.postsPerPage); const remainingPages = totalPages - 1; + return Array.from({ length: remainingPages }, (_, i) => i + 1); }) .then((remainingPages) => { diff --git a/tests/cypress/e2e/pages/contact.cy.ts b/tests/cypress/e2e/pages/contact.cy.ts index 6e1cdb6..fa5a928 100644 --- a/tests/cypress/e2e/pages/contact.cy.ts +++ b/tests/cypress/e2e/pages/contact.cy.ts @@ -1,3 +1,5 @@ +import { ROUTES } from '../../../../src/utils/constants'; + const userName = 'Cypress Test'; const userEmail = 'cypress@testing.com'; const object = '[Cypress] quos aperiam culpa'; @@ -6,7 +8,7 @@ const message = describe('Contact Page', () => { beforeEach(() => { - cy.visit('/contact'); + cy.visit(ROUTES.CONTACT); }); it('shows a heading and a contact form', () => { @@ -14,7 +16,7 @@ describe('Contact Page', () => { cy.findByRole('form', { name: /Formulaire de contact/i }); }); - it('submits the form', async () => { + it('submits the form', () => { cy.findByRole('textbox', { name: /Nom/i }) .type(userName) .should('have.value', userName); @@ -31,7 +33,7 @@ describe('Contact Page', () => { cy.findByText(/E-mail en cours d'envoi/i).should('be.visible'); }); - it('prevents the form to submit if some fields are missing', async () => { + it('prevents the form to submit if some fields are missing', () => { cy.findByRole('textbox', { name: /E-mail/i }) .type(userEmail) .should('have.value', userEmail); diff --git a/tests/cypress/e2e/pages/cv.cy.ts b/tests/cypress/e2e/pages/cv.cy.ts index 419a098..13b937f 100644 --- a/tests/cypress/e2e/pages/cv.cy.ts +++ b/tests/cypress/e2e/pages/cv.cy.ts @@ -1,6 +1,8 @@ +import { ROUTES } from '../../../../src/utils/constants'; + describe('CV Page', () => { it('successfully loads', () => { - cy.visit('/cv'); + cy.visit(ROUTES.CV); cy.findByRole('heading', { level: 1 }).contains('CV'); }); }); diff --git a/tests/cypress/e2e/pages/legal-notice.cy.ts b/tests/cypress/e2e/pages/legal-notice.cy.ts index f338a7a..0f2fb8c 100644 --- a/tests/cypress/e2e/pages/legal-notice.cy.ts +++ b/tests/cypress/e2e/pages/legal-notice.cy.ts @@ -1,6 +1,8 @@ +import { ROUTES } from '../../../../src/utils/constants'; + describe('Legal Notice Page', () => { it('successfully loads', () => { - cy.visit('/mentions-legales'); + cy.visit(ROUTES.LEGAL_NOTICE); cy.findByRole('heading', { level: 1 }).contains('Mentions légales'); }); }); diff --git a/tests/cypress/e2e/search.cy.ts b/tests/cypress/e2e/search.cy.ts index f105a5c..5f2081d 100644 --- a/tests/cypress/e2e/search.cy.ts +++ b/tests/cypress/e2e/search.cy.ts @@ -1,7 +1,10 @@ +import { ROUTES } from '../../../src/utils/constants'; + const queryWithArticles = 'Coldark'; const queryWithoutArticles = 'etEtRerum'; describe('Search', () => { + /* eslint-disable max-statements */ it('should open and close search form by clicking on search button', () => { cy.visit('/'); cy.findByLabelText(/Fermer la recherche/i).should('not.exist'); @@ -15,27 +18,28 @@ describe('Search', () => { cy.findByRole('searchbox', { name: /Rechercher/i }).should('not.exist'); cy.findByLabelText(/Ouvrir la recherche/i).should('exist'); }); + /* eslint-enable max-statements */ - it('should navigate the search page', async () => { + it('should navigate the search page', () => { cy.visit('/'); cy.findByLabelText(/Ouvrir la recherche/i).click(); cy.findByRole('searchbox', { name: /Rechercher/i }).type( `${queryWithArticles}{enter}` ); - cy.url().should('include', '/blog'); + cy.url().should('include', ROUTES.SEARCH); cy.findByRole('heading', { level: 1 }).contains( /Résultats de la recherche pour/i ); }); - it('should display the total of articles if successful', async () => { - cy.visit(`/recherche?s=${encodeURIComponent(queryWithArticles)}`); + it('should display the total of articles if successful', () => { + cy.visit(`${ROUTES.SEARCH}?s=${encodeURIComponent(queryWithArticles)}`); const dtSiblings = cy.findByRole('term', { name: /Total/i }).siblings(); dtSiblings.findByRole('definition').contains(/article/i); }); it('should display a search form if unsuccessful', () => { - cy.visit(`/recherche?s=${encodeURIComponent(queryWithoutArticles)}`); + cy.visit(`${ROUTES.SEARCH}?s=${encodeURIComponent(queryWithoutArticles)}`); cy.findByRole('searchbox', { name: /Rechercher/i }).should('exist'); }); }); |
