Skip to content

Commit 6ab3e66

Browse files
arunodaimpronunciable
authored andcommitted
Add test cases for getInitialProps based redirect logic. (vercel#1578)
With that, we are making sure we don't break this functionality in the future.
1 parent c2036e1 commit 6ab3e66

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

test/integration/basic/pages/nav/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default class extends Component {
2727
<Link href='/empty-get-initial-props'><a id='empty-props' style={linkStyle}>Empty Props</a></Link>
2828
<Link href='/nav/self-reload'><a id='self-reload-link' style={linkStyle}>Self Reload</a></Link>
2929
<Link href='/nav/shallow-routing'><a id='shallow-routing-link' style={linkStyle}>Shallow Routing</a></Link>
30+
<Link href='/nav/redirect'><a id='redirect-link' style={linkStyle}>Redirect</a></Link>
3031
<Link
3132
href={{ pathname: '/nav/querystring', query: { id: 10 } }}
3233
as={{ pathname: '/nav/querystring/10', hash: '10' }}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import Router from 'next/router'
2+
3+
const Page = () => (
4+
<p>This is the page</p>
5+
)
6+
7+
Page.getInitialProps = (ctx) => {
8+
if (ctx.res) {
9+
ctx.res.writeHead(302, { Location: '/nav/about' })
10+
ctx.res.end()
11+
} else {
12+
Router.push('/nav/about')
13+
}
14+
15+
return {}
16+
}
17+
18+
export default Page

test/integration/basic/test/client-navigation.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,5 +298,28 @@ export default (context, render) => {
298298
browser.close()
299299
})
300300
})
301+
302+
describe('with getInitialProp redirect', () => {
303+
it('should redirect the page via client side', async () => {
304+
const browser = await webdriver(context.appPort, '/nav')
305+
const text = await browser
306+
.elementByCss('#redirect-link').click()
307+
.waitForElementByCss('.nav-about')
308+
.elementByCss('p').text()
309+
310+
expect(text).toBe('This is the about page.')
311+
browser.close()
312+
})
313+
314+
it('should redirect the page when loading', async () => {
315+
const browser = await webdriver(context.appPort, '/nav/redirect')
316+
const text = await browser
317+
.waitForElementByCss('.nav-about')
318+
.elementByCss('p').text()
319+
320+
expect(text).toBe('This is the about page.')
321+
browser.close()
322+
})
323+
})
301324
})
302325
}

test/integration/basic/test/index.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ describe('Basic Features', () => {
4848
renderViaHTTP(context.appPort, '/nav/querystring'),
4949
renderViaHTTP(context.appPort, '/nav/self-reload'),
5050
renderViaHTTP(context.appPort, '/nav/hash-changes'),
51-
renderViaHTTP(context.appPort, '/nav/shallow-routing')
51+
renderViaHTTP(context.appPort, '/nav/shallow-routing'),
52+
renderViaHTTP(context.appPort, '/nav/redirect')
5253
])
5354
})
5455
afterAll(() => stopApp(context.server))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy