forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add
queryReady
to router. It will be true when the query
va…
…lue is populated. fix vercel#8259
- Loading branch information
Julian Coy
committed
Nov 11, 2019
1 parent
788654e
commit 0ede8d2
Showing
5 changed files
with
128 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { useRouter } from 'next/router' | ||
import React from 'react' | ||
|
||
export default () => { | ||
const { query, queryReady } = useRouter() | ||
return ( | ||
<> | ||
<pre id="queryReady">{`ready: ${queryReady}`}</pre> | ||
{queryReady ? <pre id="test">query: {query.test}</pre> : null} | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* eslint-env jest */ | ||
/* global jasmine */ | ||
import fs from 'fs-extra' | ||
import { | ||
findPort, | ||
killApp, | ||
launchApp, | ||
nextServer, | ||
runNextCommand, | ||
startApp, | ||
stopApp, | ||
} from 'next-test-utils' | ||
import webdriver from 'next-webdriver' | ||
import path, { join } from 'path' | ||
import { | ||
nextBuild, | ||
nextStart, | ||
renderViaHTTP, | ||
} from '../../../lib/next-test-utils' | ||
|
||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 30 | ||
|
||
let app | ||
let appPort | ||
let server | ||
const appDir = join(__dirname, '../') | ||
const nextConfig = path.join(appDir, 'next.config.js') | ||
|
||
function runTests(params) { | ||
describe(params ? `using query: ${params}` : 'with no query params', () => { | ||
const pathname = params ? `/?${params}` : '/' | ||
it('query params should not be populated on immediate sever load', async () => { | ||
const html = await renderViaHTTP(appPort, pathname) | ||
// this regex checks to make sure we don't have an element with id="test" | ||
expect(html).toMatch(/^((?!id="test").)*$/i) | ||
expect(html).toMatch(/id="queryReady">ready: false/i) | ||
}) | ||
|
||
it('should have params after hydration on client is complete', async () => { | ||
const browser = await webdriver(appPort, pathname) | ||
// we don't care about parameter hydration if we didn't pass any parameters | ||
if (params) { | ||
// if we did pass `test=true` we want to make sure it worked | ||
const text = await browser.elementByCss('#test').text() | ||
expect(text).toEqual('query: true') | ||
} | ||
// regardless, we want to make sure router.ready was set | ||
expect(await browser.elementByCss('#queryReady').text()).toEqual( | ||
'ready: true' | ||
) | ||
}) | ||
}) | ||
} | ||
|
||
describe('next/dynamic', () => { | ||
describe('dev mode', () => { | ||
beforeAll(async () => { | ||
appPort = await findPort() | ||
app = await launchApp(appDir, appPort) | ||
}) | ||
afterAll(() => killApp(app)) | ||
|
||
runTests() | ||
runTests('test=true') | ||
}) | ||
|
||
describe('production mode', () => { | ||
beforeAll(async () => { | ||
await runNextCommand(['build', appDir]) | ||
|
||
app = nextServer({ | ||
dir: appDir, | ||
dev: false, | ||
quiet: true, | ||
}) | ||
|
||
server = await startApp(app) | ||
appPort = server.address().port | ||
}) | ||
afterAll(() => stopApp(server)) | ||
|
||
runTests() | ||
runTests('test=true') | ||
}) | ||
|
||
describe('serverless mode', () => { | ||
beforeAll(async () => { | ||
await fs.writeFile( | ||
nextConfig, | ||
`module.exports = { target: 'serverless' }` | ||
) | ||
await nextBuild(appDir) | ||
appPort = await findPort() | ||
app = await nextStart(appDir, appPort) | ||
}) | ||
afterAll(async () => { | ||
await killApp(app) | ||
await fs.remove(nextConfig) | ||
}) | ||
runTests() | ||
runTests('test=true') | ||
}) | ||
}) |