Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Timer committed Feb 17, 2020
1 parent ac46941 commit 779bf29
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
2 changes: 1 addition & 1 deletion test/integration/preload-viewport/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default () => {
/>
<p id="scroll-to-me">Hi 👋</p>
<Link href="/another">
<a>to /another</a>
<a id="link-another">to /another</a>
</Link>
</div>
)
Expand Down
58 changes: 50 additions & 8 deletions test/integration/preload-viewport/test/index.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/* eslint-env jest */
/* global jasmine */
import webdriver from 'next-webdriver'
import { join } from 'path'
import {
nextServer,
runNextCommand,
startApp,
stopApp,
waitFor,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import { join } from 'path'

jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 5

Expand Down Expand Up @@ -98,24 +98,66 @@ describe('Prefetching Links in viewport', () => {
}
})

it('should fallback to prefetching onMouseEnter with invalid ref', async () => {
it('should prefetch with link in viewport and inject script on hover', async () => {
let browser
try {
browser = await webdriver(appPort, '/invalid-ref')
await browser.elementByCss('#btn-link').moveTo()
browser = await webdriver(appPort, '/')
await browser.elementByCss('#scroll-to-another').click()
await waitFor(2 * 1000)

const links = await browser.elementsByCss('link[rel=prefetch]')
let found = false
let foundLink = false

for (const link of links) {
const href = await link.getAttribute('href')
if (href.includes('another')) {
found = true
foundLink = true
break
}
}
expect(found).toBe(true)
expect(foundLink).toBe(true)

await browser.elementByCss('#link-another').moveTo()
await waitFor(2 * 1000)

const scripts = await browser.elementsByCss(
// Mouse hover is a high-priority fetch
'script:not([async])'
)
let scriptFound = false
for (const aScript of scripts) {
const href = await aScript.getAttribute('src')
if (href.includes('another')) {
scriptFound = true
break
}
}
expect(scriptFound).toBe(true)
} finally {
if (browser) await browser.close()
}
})

it('should inject a <script> tag when onMouseEnter (even with invalid ref)', async () => {
let browser
try {
browser = await webdriver(appPort, '/invalid-ref')
await browser.elementByCss('#btn-link').moveTo()
await waitFor(2 * 1000)

const scripts = await browser.elementsByCss(
// Mouse hover is a high-priority fetch
'script:not([async])'
)
let scriptFound = false
for (const aScript of scripts) {
const href = await aScript.getAttribute('src')
if (href.includes('another')) {
scriptFound = true
break
}
}
expect(scriptFound).toBe(true)
} finally {
if (browser) await browser.close()
}
Expand Down

0 comments on commit 779bf29

Please sign in to comment.