Skip to content

chore: add e2e #789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 15, 2021
13 changes: 12 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,16 @@ module.exports = {
env: {
jest: true,
},
overrides: [...overrides],
overrides: [
...overrides,
{
files: ['cypress/**/*.spec.ts'],
rules: {
'max-nested-callbacks': 0,
'promise/prefer-await-to-then': 0,
'promise/always-return': 0,
'promise/catch-or-return': 0,
},
},
],
}
14 changes: 14 additions & 0 deletions cypress/integration/custom-errors.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @see {@link https://nextjs.org/docs/advanced-features/custom-error-page}
*/
describe('Custom error pages', () => {
it('should show custom 404 page on /404', () => {
cy.visit('/404', {failOnStatusCode: false})
cy.findByText('Custom 404 - Page Not Found')
})

it('should show custom 500 page on /500', () => {
cy.visit('/500', {failOnStatusCode: false})
cy.findByText('Custom 500 - Server-side error occurred')
})
})
24 changes: 24 additions & 0 deletions cypress/integration/default.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
describe('Default site', () => {
beforeEach(() => {
cy.visit('/')
})

it('loads home page', () => {
cy.findByText('Next Demo!')
cy.findByTestId("list-server-side").within(() => {
cy.findAllByRole("link").should('have.length', 5)
})

cy.findByTestId("list-dynamic-pages").within(() => {
cy.findAllByRole("link").should('have.length', 3)
})

cy.findByTestId("list-catch-all").within(() => {
cy.findAllByRole("link").should('have.length', 3)
})

cy.findByTestId("list-static").within(() => {
cy.findAllByRole("link").should('have.length', 2)
})
})
})
7 changes: 7 additions & 0 deletions cypress/integration/dynamic-routes.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
describe('Dynamic Routing', () => {
it('loads page', () => {
cy.visit('/shows/250')
cy.findByRole('heading').should('contain', '250')
cy.findByText('Kirby Buckets')
})
})
9 changes: 9 additions & 0 deletions cypress/integration/headers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('Headers', () => {
it('should set headers from the next.config.js', () => {
cy.request({
url: '/',
}).then((response) => {
expect(response.headers).to.have.property('x-custom-header', 'my custom header value')
})
})
})
38 changes: 38 additions & 0 deletions cypress/integration/i18n.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
describe('Localization', () => {
it('should use sub routing to determine current locale', () => {
cy.visit('/');

cy.findByText('The current locale is en')

cy.visit('/fr')
cy.findByText('The current locale is fr')
})

it('should use the NEXT_LOCALE cookie to determine the default locale', () => {
cy.setCookie('NEXT_LOCALE', 'fr')
cy.visit('/');

cy.findByText('The current locale is fr')
})

it('should use the NEXT_LOCALE cookie over Accept-Language header to determine the default locale', () => {
// cy.setCookie('NEXT_LOCALE', 'en')
cy.visit({
url: '/',
headers: {
'Accept-Language': 'fr;q=0.9'
}
});
cy.findByText('The current locale is fr')

cy.setCookie('NEXT_LOCALE', 'en')
cy.visit({
url: '/',
headers: {
'Accept-Language': 'fr;q=0.9'
}
});

cy.findByText('The current locale is en')
})
})
20 changes: 20 additions & 0 deletions cypress/integration/images.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @see {@link https://nextjs.org/docs/api-reference/next/image#required-props}
*/
describe('next/images', () => {
it('should show static image from /public', () => {
cy.visit('/')
cy.findByRole('img').should('be.visible').and(($img) => {
// "naturalWidth" and "naturalHeight" are set when the image loads
expect(
$img[0].naturalWidth,
'image has natural width'
).to.be.greaterThan(0)
})
})

it('should show image using next/image', () => {
cy.visit('/image')
cy.findByRole('img', { name: /shiba inu dog looks through a window/i })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this check if the image actually loads?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I'll add the same width/height as the other image test

})
})
21 changes: 21 additions & 0 deletions cypress/integration/preview.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
describe('Preview Mode', () => {
it('enters and exits preview mode', () => {
// preview mode is off by default
cy.visit('/previewTest')
cy.findByText('Number: 4')

// enter preview mode
cy.request('/api/enterPreview').then(
(response) => {
expect(response.body).to.have.property('name', 'preview mode')
}
)
cy.visit('/previewTest')
cy.findByText('Number: 3')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My preview mode PR changes the text here, so this will need an update


// exit preview mode
cy.request('/api/exitPreview')
cy.visit('/previewTest')
cy.findByText('Number: 4')
})
})
21 changes: 21 additions & 0 deletions cypress/integration/rewrites-redirects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
describe('Rewrites and Redirects', () => {
it('rewrites: points /old to /', () => {
// preview mode is off by default
cy.visit('/old')
cy.findByText('Next Demo!')
cy.url().should('eq', `${Cypress.config().baseUrl}/old/`)

// ensure headers are still set
cy.request('/api/enterPreview').then(
(response) => {
expect(response.body).to.have.property('name', 'preview mode')
}
)
})

it('redirects: redirects /redirectme to /', () => {
cy.visit('/redirectme')
cy.url().should('eq', `${Cypress.config().baseUrl}/`)
}
)
})
11 changes: 11 additions & 0 deletions cypress/integration/trailing-slash.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe('Trailing slash enabled', () => {
it('should keep the trailing slash, i.e. points /old/ to /old/', () => {
cy.visit('/old/')
cy.url().should('eq', `${Cypress.config().baseUrl}/old/`)
})

it('should put a trailing slash when there is none, i.e. points /old to /old/', () => {
cy.visit('/old')
cy.url().should('eq', `${Cypress.config().baseUrl}/old/`)
})
})
28 changes: 26 additions & 2 deletions demo/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,23 @@ module.exports = {
defaultLocale: 'en',
locales: ['en', 'es', 'fr']
},
// trailingSlash: true,
async headers() {
return [
{
source: '/',
headers: [
{
key: 'x-custom-header',
value: 'my custom header value',
}
],
},
]
},
trailingSlash: true,
// Configurable site features _to_ support:
// basePath: '/docs',
// Rewrites allow you to map an incoming request path to a different destination path.
async rewrites() {
return {
beforeFiles: [
Expand All @@ -18,5 +32,15 @@ module.exports = {
}
]
}
}
},
// Redirects allow you to redirect an incoming request path to a different destination path.
async redirects() {
return [
{
source: '/redirectme',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not one for this PR, but adding a regex-based rewrite would be good

destination: '/',
permanent: true,
},
]
},
}
Loading