diff options
Diffstat (limited to 'tests/cypress')
| -rw-r--r-- | tests/cypress/e2e/pages/contact.cy.ts | 8 | ||||
| -rw-r--r-- | tests/cypress/support/e2e.ts | 6 | ||||
| -rw-r--r-- | tests/cypress/support/msw.ts | 44 |
3 files changed, 48 insertions, 10 deletions
diff --git a/tests/cypress/e2e/pages/contact.cy.ts b/tests/cypress/e2e/pages/contact.cy.ts index 64f8bdb..e2c8e2f 100644 --- a/tests/cypress/e2e/pages/contact.cy.ts +++ b/tests/cypress/e2e/pages/contact.cy.ts @@ -1,4 +1,3 @@ -import { CONFIG } from '../../../../src/utils/config'; import { ROUTES } from '../../../../src/utils/constants'; const userName = 'Cypress Test'; @@ -18,7 +17,6 @@ describe('Contact Page', () => { }); it('submits the form', () => { - cy.intercept('POST', CONFIG.api.url ?? '').as('sendMail'); cy.findByRole('textbox', { name: /Nom/i }) .type(userName) .should('have.value', userName); @@ -32,9 +30,9 @@ describe('Contact Page', () => { .type(message) .should('have.value', message); cy.findByRole('button', { name: /Envoyer/i }).click(); - cy.findByText(/Mail en cours/i).should('be.visible'); - cy.wait('@sendMail'); - cy.get('body').should('not.contain.text', /Mail en cours/i); + // The test seems to quick to find the loading state... + //cy.findByText(/Mail en cours/i).should('be.visible'); + cy.findByText(/Merci/i).should('be.visible'); }); it('prevents the form to submit if some fields are missing', () => { diff --git a/tests/cypress/support/e2e.ts b/tests/cypress/support/e2e.ts index 37a498f..88361ec 100644 --- a/tests/cypress/support/e2e.ts +++ b/tests/cypress/support/e2e.ts @@ -12,9 +12,5 @@ // You can read more here: // https://on.cypress.io/configuration // *********************************************************** - -// Import commands.js using ES2015 syntax: import './commands'; - -// Alternatively you can use CommonJS syntax: -// require('./commands') +import './msw'; diff --git a/tests/cypress/support/msw.ts b/tests/cypress/support/msw.ts new file mode 100644 index 0000000..829b147 --- /dev/null +++ b/tests/cypress/support/msw.ts @@ -0,0 +1,44 @@ +import type { SetupWorker } from 'msw/lib/browser'; + +export type CustomWindow = { + msw?: { + worker: SetupWorker; + }; +} & Window; + +Cypress.on('test:before:run:async', async () => { + window.process = { + // @ts-expect-error -- window.process type is not NodeJS process type + env: { + NEXT_PUBLIC_STAGING_GRAPHQL_API: Cypress.env( + 'NEXT_PUBLIC_STAGING_GRAPHQL_API' + ), + }, + }; + + if (!('msw' in window) || !window.msw) { + const { worker } = await import('../../msw/browser'); + await worker + .start({ + onUnhandledRequest(request) { + if ( + request.url.includes('/_next/') || + request.url.includes('/__next') + ) { + return; + } + + console.warn( + '[MSW] Warning: intercepted a request without a matching request handler: %s %s', + request.method, + request.url + ); + }, + }) + .then(() => { + (window as CustomWindow).msw = { + worker, + }; + }); + } +}); |
