| 21 | let indexPageContent |
| 22 | |
| 23 | const runTests = (isDev: boolean) => { |
| 24 | const getData = async () => { |
| 25 | if (isDev) { |
| 26 | appPort = await findPort() |
| 27 | app = await launchApp(appDir, appPort) |
| 28 | } else { |
| 29 | const { code } = await nextBuild(appDir) |
| 30 | if (code !== 0) throw new Error(`build faild, exit code: ${code}`) |
| 31 | appPort = await findPort() |
| 32 | app = await nextStart(appDir, appPort) |
| 33 | } |
| 34 | const html = await renderViaHTTP(appPort, '/') |
| 35 | await killApp(app) |
| 36 | const $ = cheerio.load(html) |
| 37 | return JSON.parse($('#__NEXT_DATA__').text()) |
| 38 | } |
| 39 | |
| 40 | it('should not have gip or appGip in NEXT_DATA for page without getInitialProps', async () => { |
| 41 | const data = await getData() |
| 42 | expect(data.gip).toBe(undefined) |
| 43 | expect(data.appGip).toBe(undefined) |
| 44 | }) |
| 45 | |
| 46 | it('should have gip in NEXT_DATA for page with getInitialProps', async () => { |
| 47 | indexPageContent = await fs.readFile(indexPage, 'utf8') |
| 48 | await fs.writeFile( |
| 49 | indexPage, |
| 50 | ` |
| 51 | const Page = () => 'hi' |
| 52 | Page.getInitialProps = () => ({ hello: 'world' }) |
| 53 | export default Page |
| 54 | ` |
| 55 | ) |
| 56 | const data = await getData() |
| 57 | expect(data.gip).toBe(true) |
| 58 | }) |
| 59 | |
| 60 | it('should have gip and appGip in NEXT_DATA for page with getInitialProps and _app with getInitialProps', async () => { |
| 61 | await fs.writeFile( |
| 62 | appPage, |
| 63 | ` |
| 64 | const App = ({ Component, pageProps }) => <Component {...pageProps} /> |
| 65 | App.getInitialProps = async (ctx) => { |
| 66 | let pageProps = {} |
| 67 | if (ctx.Component.getInitialProps) { |
| 68 | pageProps = await ctx.Component.getInitialProps(ctx.ctx) |
| 69 | } |
| 70 | return { pageProps } |
| 71 | } |
| 72 | export default App |
| 73 | ` |
| 74 | ) |
| 75 | const data = await getData() |
| 76 | expect(data.gip).toBe(true) |
| 77 | expect(data.appGip).toBe(true) |
| 78 | }) |
| 79 | |
| 80 | it('should only have appGip in NEXT_DATA for page without getInitialProps and _app with getInitialProps', async () => { |