|
| 1 | +import { createNext, FileRef } from 'e2e-utils' |
| 2 | +import { NextInstance } from 'test/lib/next-modes/base' |
| 3 | +import { fetchViaHTTP, renderViaHTTP } from 'next-test-utils' |
| 4 | +import path from 'path' |
| 5 | +import cheerio from 'cheerio' |
| 6 | +import webdriver from 'next-webdriver' |
| 7 | + |
| 8 | +describe('app-dir trailingSlash handling', () => { |
| 9 | + if ((global as any).isNextDeploy) { |
| 10 | + it('should skip next deploy for now', () => {}) |
| 11 | + return |
| 12 | + } |
| 13 | + |
| 14 | + if (process.env.NEXT_TEST_REACT_VERSION === '^17') { |
| 15 | + it('should skip for react v17', () => {}) |
| 16 | + return |
| 17 | + } |
| 18 | + let next: NextInstance |
| 19 | + |
| 20 | + beforeAll(async () => { |
| 21 | + next = await createNext({ |
| 22 | + files: new FileRef(path.join(__dirname, 'trailingslash')), |
| 23 | + dependencies: { |
| 24 | + react: 'experimental', |
| 25 | + 'react-dom': 'experimental', |
| 26 | + }, |
| 27 | + skipStart: true, |
| 28 | + }) |
| 29 | + |
| 30 | + await next.start() |
| 31 | + }) |
| 32 | + afterAll(() => next.destroy()) |
| 33 | + |
| 34 | + it('should redirect route when requesting it directly', async () => { |
| 35 | + const res = await fetchViaHTTP( |
| 36 | + next.url, |
| 37 | + '/a', |
| 38 | + {}, |
| 39 | + { |
| 40 | + redirect: 'manual', |
| 41 | + } |
| 42 | + ) |
| 43 | + expect(res.status).toBe(308) |
| 44 | + expect(res.headers.get('location')).toBe(next.url + '/a/') |
| 45 | + }) |
| 46 | + |
| 47 | + it('should render link with trailing slash', async () => { |
| 48 | + const html = await renderViaHTTP(next.url, '/') |
| 49 | + const $ = cheerio.load(html) |
| 50 | + expect($('#to-a-trailing-slash').attr('href')).toBe('/a/') |
| 51 | + }) |
| 52 | + |
| 53 | + it('should redirect route when requesting it directly by browser', async () => { |
| 54 | + const browser = await webdriver(next.url, '/a') |
| 55 | + expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page') |
| 56 | + }) |
| 57 | + |
| 58 | + it('should redirect route when clicking link', async () => { |
| 59 | + const browser = await webdriver(next.url, '/') |
| 60 | + await browser |
| 61 | + .elementByCss('#to-a-trailing-slash') |
| 62 | + .click() |
| 63 | + .waitForElementByCss('#a-page') |
| 64 | + expect(await browser.waitForElementByCss('#a-page').text()).toBe('A page') |
| 65 | + }) |
| 66 | +}) |
0 commit comments