Commit 704edcc1 authored by tangye's avatar tangye Committed by JJ Kasper

Make router UrlIsNew comparing method work as expected (#6383)

* make router UrlIsNew comparing method work as expected

* Remove shallow-equals from router and update urlIsNew check

* Remove shallow-equals test since it is no longer used

* Add integration test for asPath query
parent 42cff0a0
......@@ -2,7 +2,6 @@
import { parse } from 'url'
import mitt from '../mitt'
import shallowEquals from './shallow-equals'
import { loadGetInitialProps, getURL, formatWithValidation } from '../utils'
export default class Router {
......@@ -363,9 +362,7 @@ export default class Router {
}
urlIsNew (asPath) {
const { pathname, query } = parse(asPath, true)
const { pathname: curPathname } = parse(this.asPath, true)
return curPathname !== pathname || !shallowEquals(query, this.query)
return this.asPath !== asPath
}
isShallowRoutingPossible (route) {
......
export default function shallowEquals (a, b) {
for (const i in a) {
if (b[i] !== a[i]) return false
}
for (const i in b) {
if (b[i] !== a[i]) return false
}
return true
}
import Link from 'next/link'
import { withRouter } from 'next/router'
export default withRouter(({ router: { asPath, query } }) => {
return <div
id={asPath
.replace('/', '').replace('/', '-').replace('?', '-').replace('=', '-')
}>
<div id='router-query'>{JSON.stringify(query)}</div>
<div>
<Link href='/nav/as-path-query?something=hello' as='/something/hello?something=hello'>
<a id='hello'>hello</a>
</Link>
</div>
<div>
<Link href='/nav/as-path-query?something=else' as='/something/hello?something=else'>
<a id='hello2'>hello</a>
</Link>
</div>
</div>
})
......@@ -659,6 +659,23 @@ export default (context) => {
}
}
})
it('should detect asPath query changes correctly', async () => {
let browser
try {
browser = await webdriver(context.appPort, '/nav/as-path-query')
await browser.elementByCss('#hello').click().waitForElementByCss('#something-hello-something-hello')
const queryOne = JSON.parse(await browser.elementByCss('#router-query').text())
expect(queryOne.something).toBe('hello')
await browser.elementByCss('#hello2').click().waitForElementByCss('#something-hello-something-else')
const queryTwo = JSON.parse(await browser.elementByCss('#router-query').text())
expect(queryTwo.something).toBe('else')
} finally {
if (browser) {
browser.close()
}
}
})
})
})
......
/* eslint-env jest */
import shallowEquals from 'next-server/dist/lib/router/shallow-equals'
describe('Shallow Equals', () => {
it('should be true if both objects are the same', () => {
const a = { aa: 10 }
expect(shallowEquals(a, a)).toBe(true)
})
it('should be true if both objects are similar', () => {
const a = { aa: 10, bb: 30 }
const b = { aa: 10, bb: 30 }
expect(shallowEquals(a, b)).toBe(true)
})
it('should be false if objects have different keys', () => {
const a = { aa: 10, bb: 30 }
const b = { aa: 10, cc: 50 }
expect(shallowEquals(a, b)).toBe(false)
})
it('should be false if objects have same keys but different values', () => {
const a = { aa: 10, bb: 30 }
const b = { aa: 10, bb: 50 }
expect(shallowEquals(a, b)).toBe(false)
})
it('should be false if objects matched deeply', () => {
const a = { aa: 10, bb: { a: 10 } }
const b = { aa: 10, bb: { a: 10 } }
expect(shallowEquals(a, b)).toBe(false)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment