From 6ab3e6606d6c692fb90cd62e683eb6f4b9bf064f Mon Sep 17 00:00:00 2001 From: Arunoda Susiripala Date: Fri, 31 Mar 2017 20:05:45 +0530 Subject: [PATCH] Add test cases for getInitialProps based redirect logic. (#1578) With that, we are making sure we don't break this functionality in the future. --- test/integration/basic/pages/nav/index.js | 1 + test/integration/basic/pages/nav/redirect.js | 18 +++++++++++++++ .../basic/test/client-navigation.js | 23 +++++++++++++++++++ test/integration/basic/test/index.test.js | 3 ++- 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 test/integration/basic/pages/nav/redirect.js diff --git a/test/integration/basic/pages/nav/index.js b/test/integration/basic/pages/nav/index.js index fad7a159..f92a7cba 100644 --- a/test/integration/basic/pages/nav/index.js +++ b/test/integration/basic/pages/nav/index.js @@ -27,6 +27,7 @@ export default class extends Component { Empty Props Self Reload Shallow Routing + Redirect ( +

This is the page

+) + +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 diff --git a/test/integration/basic/test/client-navigation.js b/test/integration/basic/test/client-navigation.js index 8807bdb0..2f4a187f 100644 --- a/test/integration/basic/test/client-navigation.js +++ b/test/integration/basic/test/client-navigation.js @@ -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() + }) + }) }) } diff --git a/test/integration/basic/test/index.test.js b/test/integration/basic/test/index.test.js index 94bacaa7..d7a7d5a1 100644 --- a/test/integration/basic/test/index.test.js +++ b/test/integration/basic/test/index.test.js @@ -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))