1
0
Fork 0
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:
Arunoda Susiripala 2017-03-31 20:05:45 +05:30 committed by Dan Zajdband
parent c2036e1326
commit 6ab3e6606d
4 changed files with 44 additions and 1 deletions

View file

@ -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='/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/redirect'><a id='redirect-link' style={linkStyle}>Redirect</a></Link>
<Link
href={{ pathname: '/nav/querystring', query: { id: 10 } }}
as={{ pathname: '/nav/querystring/10', hash: '10' }}

View 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

View file

@ -298,5 +298,28 @@ export default (context, render) => {
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()
})
})
})
}

View file

@ -48,7 +48,8 @@ describe('Basic Features', () => {
renderViaHTTP(context.appPort, '/nav/querystring'),
renderViaHTTP(context.appPort, '/nav/self-reload'),
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))