Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Jan 24, 2023
1 parent 33a5d78 commit 212ff6d
Showing 1 changed file with 56 additions and 33 deletions.
89 changes: 56 additions & 33 deletions test/integration/preload-viewport/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-env jest */

import {
check,
findPort,
killApp,
nextBuild,
Expand Down Expand Up @@ -94,17 +95,21 @@ describe('Prefetching Links in viewport', () => {
let browser
try {
browser = await webdriver(appPort, '/')
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false

for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('first')) {
found = true
break
await check(async () => {
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false

for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('first')) {
found = true
break
}
}
}
expect(found).toBe(true)
expect(found).toBe(true)
return 'success'
}, 'success')
} finally {
if (browser) await browser.close()
}
Expand Down Expand Up @@ -146,17 +151,21 @@ describe('Prefetching Links in viewport', () => {
let browser
try {
browser = await webdriver(appPort, '/rewrite-prefetch')
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false

for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('%5Bslug%5D')) {
found = true
break
await check(async () => {
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false

for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('%5Bslug%5D')) {
found = true
break
}
}
}
expect(found).toBe(true)
expect(found).toBe(true)
return 'success'
}, 'success')

const hrefs = await browser.eval(`Object.keys(window.next.router.sdc)`)
expect(hrefs.map((href) => new URL(href).pathname)).toEqual([
Expand Down Expand Up @@ -433,26 +442,40 @@ describe('Prefetching Links in viewport', () => {
expect(hrefs.some((e) => e.includes(`%5Bhello%5D-`))).toBe(true)
})

it('should not add an another observer for a prefetched page', async () => {
it('should not re-prefetch for an already prefetched page', async () => {
// info: both `/` and `/de-duped` ref the `/first` page, which we don't
// want to be re-fetched/re-observed.
const browser = await webdriver(appPort, '/', {
waitHydration: false,
})

let requests = []
const browser = await webdriver(appPort, '/')

browser.on('request', (req) => {
requests.push(new URL(req.url()).pathname)
})
await check(async () => {
const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false

await waitFor(3 * 1000)
const initialRequests = requests.length
await browser.eval(`window.next.router.push('/de-duped')`)
await waitFor(3 * 1000)
const finalRequests = requests.length
for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('first')) {
found = true
break
}
}
expect(found).toBe(true)
return 'success'
}, 'success')

expect(finalRequests).toBe(initialRequests + 1)
await browser.eval(`(function() {
window.calledPrefetch = false
window.next.router.prefetch = function() {
window.calledPrefetch = true
return Promise.resolve()
}
window.next.router.push('/de-duped')
})()`)
await check(
() => browser.eval('document.documentElement.innerHTML'),
/to \/first/
)
const calledPrefetch = await browser.eval(`window.calledPrefetch`)
expect(calledPrefetch).toBe(false)
})

it('should prefetch with a different asPath for a prefetched page', async () => {
Expand Down

0 comments on commit 212ff6d

Please sign in to comment.