|
| 1 | +const project = "optionalCatchAll" |
| 2 | + |
| 3 | +before(() => { |
| 4 | + // When changing the base URL within a spec file, Cypress runs the spec twice |
| 5 | + // To avoid rebuilding and redeployment on the second run, we check if the |
| 6 | + // project has already been deployed. |
| 7 | + cy.task('isDeployed').then(isDeployed => { |
| 8 | + // Cancel setup, if already deployed |
| 9 | + if(isDeployed) |
| 10 | + return |
| 11 | + |
| 12 | + // Clear project folder |
| 13 | + cy.task('clearProject', { project }) |
| 14 | + |
| 15 | + // Copy NextJS files |
| 16 | + cy.task('copyFixture', { |
| 17 | + project, from: 'pages-with-optionalCatchAll', to: 'pages' |
| 18 | + }) |
| 19 | + cy.task('copyFixture', { |
| 20 | + project, from: 'next.config.js-with-optionalCatchAll', to: 'next.config.js' |
| 21 | + }) |
| 22 | + |
| 23 | + // Copy package.json file |
| 24 | + cy.task('copyFixture', { |
| 25 | + project, from: 'package.json', to: 'package.json' |
| 26 | + }) |
| 27 | + |
| 28 | + // Copy Netlify settings |
| 29 | + cy.task('copyFixture', { |
| 30 | + project, from: 'netlify.toml', to: 'netlify.toml' |
| 31 | + }) |
| 32 | + cy.task('copyFixture', { |
| 33 | + project, from: '.netlify', to: '.netlify' |
| 34 | + }) |
| 35 | + |
| 36 | + // Build |
| 37 | + cy.task('buildProject', { project }) |
| 38 | + |
| 39 | + // Deploy |
| 40 | + cy.task('deployProject', { project }, { timeout: 180 * 1000 }) |
| 41 | + }) |
| 42 | + |
| 43 | + // Set base URL |
| 44 | + cy.task('getBaseUrl', { project }).then((url) => { |
| 45 | + Cypress.config('baseUrl', url) |
| 46 | + }) |
| 47 | +}) |
| 48 | + |
| 49 | +after(() => { |
| 50 | + // While the before hook runs twice (it's re-run when the base URL changes), |
| 51 | + // the after hook only runs once. |
| 52 | + cy.task('clearDeployment') |
| 53 | +}) |
| 54 | + |
| 55 | +describe('Page with optional catch all routing', () => { |
| 56 | + it('responds to base path', () => { |
| 57 | + cy.visit('/catch') |
| 58 | + |
| 59 | + cy.get('h1').should('contain', 'Show #1') |
| 60 | + cy.get('p').should('contain', 'Under the Dome') |
| 61 | + }) |
| 62 | + |
| 63 | + it('responds to catch-all path', () => { |
| 64 | + cy.visit('/catch/25/catch/all') |
| 65 | + |
| 66 | + cy.get('h1').should('contain', 'Show #25') |
| 67 | + cy.get('p').should('contain', 'Hellsing') |
| 68 | + }) |
| 69 | + |
| 70 | + it('loads page props from data .json file when navigating to it', () => { |
| 71 | + cy.visit('/') |
| 72 | + cy.window().then(w => w.noReload = true) |
| 73 | + |
| 74 | + // Navigate to page and test that no reload is performed |
| 75 | + // See: https://glebbahmutov.com/blog/detect-page-reload/ |
| 76 | + cy.contains('/catch').click() |
| 77 | + cy.get('h1').should('contain', 'Show #1') |
| 78 | + cy.get('p').should('contain', 'Under the Dome') |
| 79 | + |
| 80 | + cy.contains('Go back home').click() |
| 81 | + cy.contains('/catch/25/catch/all').click() |
| 82 | + |
| 83 | + cy.get('h1').should('contain', 'Show #25') |
| 84 | + cy.get('p').should('contain', 'Hellsing') |
| 85 | + |
| 86 | + cy.contains('Go back home').click() |
| 87 | + cy.contains('/catch/75/undefined/path/test').click() |
| 88 | + |
| 89 | + cy.get('h1').should('contain', 'Show #75') |
| 90 | + cy.get('p').should('contain', 'The Mindy Project') |
| 91 | + cy.window().should('have.property', 'noReload', true) |
| 92 | + }) |
| 93 | +}) |
0 commit comments