|
| 1 | +/* eslint-env jest */ |
| 2 | + |
| 3 | +import { check } from 'next-test-utils' |
| 4 | +import path from 'path' |
| 5 | +import { nextTestSetup } from 'e2e-utils' |
| 6 | + |
| 7 | +describe('Client navigation with asPath', () => { |
| 8 | + const { next } = nextTestSetup({ |
| 9 | + files: path.join(__dirname, 'fixture'), |
| 10 | + env: { |
| 11 | + TEST_STRICT_NEXT_HEAD: String(true), |
| 12 | + }, |
| 13 | + }) |
| 14 | + |
| 15 | + describe('inside getInitialProps', () => { |
| 16 | + it('should show the correct asPath with a Link with as prop', async () => { |
| 17 | + const browser = await next.browser('/nav') |
| 18 | + const asPath = await browser |
| 19 | + .elementByCss('#as-path-link') |
| 20 | + .click() |
| 21 | + .waitForElementByCss('.as-path-content') |
| 22 | + .elementByCss('.as-path-content') |
| 23 | + .text() |
| 24 | + |
| 25 | + expect(asPath).toBe('/as/path') |
| 26 | + await browser.close() |
| 27 | + }) |
| 28 | + |
| 29 | + it('should show the correct asPath with a Link without the as prop', async () => { |
| 30 | + const browser = await next.browser('/nav') |
| 31 | + const asPath = await browser |
| 32 | + .elementByCss('#as-path-link-no-as') |
| 33 | + .click() |
| 34 | + .waitForElementByCss('.as-path-content') |
| 35 | + .elementByCss('.as-path-content') |
| 36 | + .text() |
| 37 | + |
| 38 | + expect(asPath).toBe('/nav/as-path') |
| 39 | + await browser.close() |
| 40 | + }) |
| 41 | + }) |
| 42 | + |
| 43 | + describe('with next/router', () => { |
| 44 | + it('should show the correct asPath', async () => { |
| 45 | + const browser = await next.browser('/nav') |
| 46 | + const asPath = await browser |
| 47 | + .elementByCss('#as-path-using-router-link') |
| 48 | + .click() |
| 49 | + .waitForElementByCss('.as-path-content') |
| 50 | + .elementByCss('.as-path-content') |
| 51 | + .text() |
| 52 | + |
| 53 | + expect(asPath).toBe('/nav/as-path-using-router') |
| 54 | + await browser.close() |
| 55 | + }) |
| 56 | + |
| 57 | + it('should navigate an absolute url on push', async () => { |
| 58 | + const browser = await next.browser(`/absolute-url?port=${next.appPort}`) |
| 59 | + await browser.waitForElementByCss('#router-push').click() |
| 60 | + await check( |
| 61 | + () => browser.eval(() => window.location.origin), |
| 62 | + 'https://vercel.com' |
| 63 | + ) |
| 64 | + }) |
| 65 | + |
| 66 | + it('should navigate an absolute url on replace', async () => { |
| 67 | + const browser = await next.browser(`/absolute-url?port=${next.appPort}`) |
| 68 | + await browser.waitForElementByCss('#router-replace').click() |
| 69 | + await check( |
| 70 | + () => browser.eval(() => window.location.origin), |
| 71 | + 'https://vercel.com' |
| 72 | + ) |
| 73 | + }) |
| 74 | + |
| 75 | + it('should navigate an absolute local url on push', async () => { |
| 76 | + const browser = await next.browser(`/absolute-url?port=${next.appPort}`) |
| 77 | + // @ts-expect-error _didNotNavigate is set intentionally |
| 78 | + await browser.eval(() => (window._didNotNavigate = true)) |
| 79 | + await browser.waitForElementByCss('#router-local-push').click() |
| 80 | + const text = await browser |
| 81 | + .waitForElementByCss('.nav-about') |
| 82 | + .elementByCss('p') |
| 83 | + .text() |
| 84 | + expect(text).toBe('This is the about page.') |
| 85 | + // @ts-expect-error _didNotNavigate is set intentionally |
| 86 | + expect(await browser.eval(() => window._didNotNavigate)).toBe(true) |
| 87 | + }) |
| 88 | + |
| 89 | + it('should navigate an absolute local url on replace', async () => { |
| 90 | + const browser = await next.browser(`/absolute-url?port=${next.appPort}`) |
| 91 | + // @ts-expect-error _didNotNavigate is set intentionally |
| 92 | + await browser.eval(() => (window._didNotNavigate = true)) |
| 93 | + await browser.waitForElementByCss('#router-local-replace').click() |
| 94 | + const text = await browser |
| 95 | + .waitForElementByCss('.nav-about') |
| 96 | + .elementByCss('p') |
| 97 | + .text() |
| 98 | + expect(text).toBe('This is the about page.') |
| 99 | + // @ts-expect-error _didNotNavigate is set intentionally |
| 100 | + expect(await browser.eval(() => window._didNotNavigate)).toBe(true) |
| 101 | + }) |
| 102 | + }) |
| 103 | + |
| 104 | + describe('with next/link', () => { |
| 105 | + it('should use pushState with same href and different asPath', async () => { |
| 106 | + const browser = await next.browser('/nav/as-path-pushstate') |
| 107 | + await browser |
| 108 | + .elementByCss('#hello') |
| 109 | + .click() |
| 110 | + .waitForElementByCss('#something-hello') |
| 111 | + const queryOne = JSON.parse( |
| 112 | + await browser.elementByCss('#router-query').text() |
| 113 | + ) |
| 114 | + expect(queryOne.something).toBe('hello') |
| 115 | + await browser |
| 116 | + .elementByCss('#same-query') |
| 117 | + .click() |
| 118 | + .waitForElementByCss('#something-same-query') |
| 119 | + const queryTwo = JSON.parse( |
| 120 | + await browser.elementByCss('#router-query').text() |
| 121 | + ) |
| 122 | + expect(queryTwo.something).toBe('hello') |
| 123 | + await browser.back().waitForElementByCss('#something-hello') |
| 124 | + const queryThree = JSON.parse( |
| 125 | + await browser.elementByCss('#router-query').text() |
| 126 | + ) |
| 127 | + expect(queryThree.something).toBe('hello') |
| 128 | + await browser |
| 129 | + .elementByCss('#else') |
| 130 | + .click() |
| 131 | + .waitForElementByCss('#something-else') |
| 132 | + await browser |
| 133 | + .elementByCss('#hello2') |
| 134 | + .click() |
| 135 | + .waitForElementByCss('#nav-as-path-pushstate') |
| 136 | + await browser.back().waitForElementByCss('#something-else') |
| 137 | + const queryFour = JSON.parse( |
| 138 | + await browser.elementByCss('#router-query').text() |
| 139 | + ) |
| 140 | + expect(queryFour.something).toBe(undefined) |
| 141 | + }) |
| 142 | + |
| 143 | + it('should detect asPath query changes correctly', async () => { |
| 144 | + const browser = await next.browser('/nav/as-path-query') |
| 145 | + await browser |
| 146 | + .elementByCss('#hello') |
| 147 | + .click() |
| 148 | + .waitForElementByCss('#something-hello-something-hello') |
| 149 | + const queryOne = JSON.parse( |
| 150 | + await browser.elementByCss('#router-query').text() |
| 151 | + ) |
| 152 | + expect(queryOne.something).toBe('hello') |
| 153 | + await browser |
| 154 | + .elementByCss('#hello2') |
| 155 | + .click() |
| 156 | + .waitForElementByCss('#something-hello-something-else') |
| 157 | + const queryTwo = JSON.parse( |
| 158 | + await browser.elementByCss('#router-query').text() |
| 159 | + ) |
| 160 | + expect(queryTwo.something).toBe('else') |
| 161 | + }) |
| 162 | + }) |
| 163 | +}) |
0 commit comments