mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Add test cases for getInitialProps based redirect logic. (#1578)
With that, we are making sure we don't break this functionality in the future.
This commit is contained in:
parent
c2036e1326
commit
6ab3e6606d
|
@ -27,6 +27,7 @@ export default class extends Component {
|
||||||
<Link href='/empty-get-initial-props'><a id='empty-props' style={linkStyle}>Empty Props</a></Link>
|
<Link href='/empty-get-initial-props'><a id='empty-props' style={linkStyle}>Empty Props</a></Link>
|
||||||
<Link href='/nav/self-reload'><a id='self-reload-link' style={linkStyle}>Self Reload</a></Link>
|
<Link href='/nav/self-reload'><a id='self-reload-link' style={linkStyle}>Self Reload</a></Link>
|
||||||
<Link href='/nav/shallow-routing'><a id='shallow-routing-link' style={linkStyle}>Shallow Routing</a></Link>
|
<Link href='/nav/shallow-routing'><a id='shallow-routing-link' style={linkStyle}>Shallow Routing</a></Link>
|
||||||
|
<Link href='/nav/redirect'><a id='redirect-link' style={linkStyle}>Redirect</a></Link>
|
||||||
<Link
|
<Link
|
||||||
href={{ pathname: '/nav/querystring', query: { id: 10 } }}
|
href={{ pathname: '/nav/querystring', query: { id: 10 } }}
|
||||||
as={{ pathname: '/nav/querystring/10', hash: '10' }}
|
as={{ pathname: '/nav/querystring/10', hash: '10' }}
|
||||||
|
|
18
test/integration/basic/pages/nav/redirect.js
Normal file
18
test/integration/basic/pages/nav/redirect.js
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import Router from 'next/router'
|
||||||
|
|
||||||
|
const Page = () => (
|
||||||
|
<p>This is the page</p>
|
||||||
|
)
|
||||||
|
|
||||||
|
Page.getInitialProps = (ctx) => {
|
||||||
|
if (ctx.res) {
|
||||||
|
ctx.res.writeHead(302, { Location: '/nav/about' })
|
||||||
|
ctx.res.end()
|
||||||
|
} else {
|
||||||
|
Router.push('/nav/about')
|
||||||
|
}
|
||||||
|
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Page
|
|
@ -298,5 +298,28 @@ export default (context, render) => {
|
||||||
browser.close()
|
browser.close()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('with getInitialProp redirect', () => {
|
||||||
|
it('should redirect the page via client side', async () => {
|
||||||
|
const browser = await webdriver(context.appPort, '/nav')
|
||||||
|
const text = await browser
|
||||||
|
.elementByCss('#redirect-link').click()
|
||||||
|
.waitForElementByCss('.nav-about')
|
||||||
|
.elementByCss('p').text()
|
||||||
|
|
||||||
|
expect(text).toBe('This is the about page.')
|
||||||
|
browser.close()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should redirect the page when loading', async () => {
|
||||||
|
const browser = await webdriver(context.appPort, '/nav/redirect')
|
||||||
|
const text = await browser
|
||||||
|
.waitForElementByCss('.nav-about')
|
||||||
|
.elementByCss('p').text()
|
||||||
|
|
||||||
|
expect(text).toBe('This is the about page.')
|
||||||
|
browser.close()
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,8 @@ describe('Basic Features', () => {
|
||||||
renderViaHTTP(context.appPort, '/nav/querystring'),
|
renderViaHTTP(context.appPort, '/nav/querystring'),
|
||||||
renderViaHTTP(context.appPort, '/nav/self-reload'),
|
renderViaHTTP(context.appPort, '/nav/self-reload'),
|
||||||
renderViaHTTP(context.appPort, '/nav/hash-changes'),
|
renderViaHTTP(context.appPort, '/nav/hash-changes'),
|
||||||
renderViaHTTP(context.appPort, '/nav/shallow-routing')
|
renderViaHTTP(context.appPort, '/nav/shallow-routing'),
|
||||||
|
renderViaHTTP(context.appPort, '/nav/redirect')
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
afterAll(() => stopApp(context.server))
|
afterAll(() => stopApp(context.server))
|
||||||
|
|
Loading…
Reference in a new issue