Unverified Commit db216e00 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Even more reliable error-recovery tests (#5284)

parent 139bc40f
......@@ -128,7 +128,7 @@
"babel-jest": "23.4.2",
"benchmark": "2.1.4",
"cheerio": "0.22.0",
"chromedriver": "2.32.3",
"chromedriver": "2.42.0",
"clone": "2.1.1",
"coveralls": "2.13.1",
"cross-env": "5.2.0",
......
/* global describe, it, expect */
import webdriver from 'next-webdriver'
import { waitFor } from 'next-test-utils'
import { waitFor, check, File } from 'next-test-utils'
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'
......@@ -10,7 +10,7 @@ export default (context, render) => {
it('should have config available on the client', async () => {
const browser = await webdriver(context.appPort, '/next-config')
// Wait for client side to load
await waitFor(5000)
await waitFor(10000)
const serverText = await browser.elementByCss('#server-only').text()
const serverClientText = await browser.elementByCss('#server-and-client').text()
......@@ -37,8 +37,7 @@ export default (context, render) => {
// Change the page
writeFileSync(pagePath, editedContent, 'utf8')
// wait for 5 seconds
await waitFor(5000)
await waitFor(10000)
try {
// Check whether the this page has reloaded or not.
......@@ -59,36 +58,47 @@ export default (context, render) => {
})
it('should update sass styles using hmr', async () => {
const file = new File(join(__dirname, '../', 'components', 'hello-webpack-sass.scss'))
let browser
try {
browser = await webdriver(context.appPort, '/webpack-css')
const pTag = await browser.elementByCss('.hello-world')
const initialFontSize = await pTag.getComputedCss('color')
expect(initialFontSize).toBe('rgba(255, 255, 0, 1)')
expect(
await browser.elementByCss('.hello-world').getComputedCss('color')
).toBe('rgba(255, 255, 0, 1)')
const pagePath = join(__dirname, '../', 'components', 'hello-webpack-sass.scss')
file.replace('yellow', 'red')
const originalContent = readFileSync(pagePath, 'utf8')
const editedContent = originalContent.replace('yellow', 'red')
await waitFor(10000)
// Change the page
writeFileSync(pagePath, editedContent, 'utf8')
await check(
async () => {
const tag = await browser.elementByCss('.hello-world')
const prop = await tag.getComputedCss('color')
// wait for 5 seconds
await waitFor(5000)
expect(prop).toBe('rgba(255, 0, 0, 1)')
return 'works'
},
/works/
)
try {
// Check whether the this page has reloaded or not.
const editedPTag = await browser.elementByCss('.hello-world')
const editedFontSize = await editedPTag.getComputedCss('color')
file.restore()
expect(editedFontSize).toBe('rgba(255, 0, 0, 1)')
} finally {
// Finally is used so that we revert the content back to the original regardless of the test outcome
// restore the about page content.
writeFileSync(pagePath, originalContent, 'utf8')
}
await waitFor(10000)
await check(
async () => {
const tag = await browser.elementByCss('.hello-world')
const prop = await tag.getComputedCss('color')
expect(prop).toBe('rgba(255, 255, 0, 1)')
return 'works'
},
/works/
)
} catch (err) {
file.restore()
throw err
} finally {
if (browser) {
browser.close()
......
/* global describe, it, expect */
import webdriver from 'next-webdriver'
import { waitFor } from 'next-test-utils'
import { check } from 'next-test-utils'
export default function (context) {
describe('Render via browser', () => {
......@@ -100,39 +100,36 @@ export default function (context) {
.elementByCss('#dynamic-imports-page').click()
.waitForElementByCss('#dynamic-imports-page')
// Wait until browser loads the dynamic import chunk
// TODO: We may need to find a better way to do this
await waitFor(5000)
await check(
() => browser.elementByCss('#dynamic-imports-page p').text(),
/Welcome to dynamic imports/
)
const text = await browser
.elementByCss('#dynamic-imports-page p').text()
expect(text).toBe('Welcome to dynamic imports.')
browser.close()
})
it('should render pages with url hash correctly', async () => {
const browser = await webdriver(context.port, '/')
// Check for the query string content
const text = await browser
.elementByCss('#with-hash').click()
.waitForElementByCss('#dynamic-page')
.elementByCss('#dynamic-page p').text()
expect(text).toBe('zeit is awesome')
// Check for the hash
while (true) {
const hashText = await browser
.elementByCss('#hash').text()
let browser
try {
browser = await webdriver(context.port, '/')
if (/cool/.test(hashText)) {
break
// Check for the query string content
const text = await browser
.elementByCss('#with-hash').click()
.waitForElementByCss('#dynamic-page')
.elementByCss('#dynamic-page p').text()
expect(text).toBe('zeit is awesome')
await check(
() => browser.elementByCss('#hash').text(),
/cool/
)
} finally {
if (browser) {
browser.close()
}
}
browser.close()
})
it('should navigate even if used a button inside <Link />', async () => {
......
import Link from 'next/link'
export default () => <div>
<Link href='/nav/dynamic'><a id='to-dynamic'>To dynamic import</a></Link>
<Link href='/nav/dynamic'>
<a id='to-dynamic'>To dynamic import</a>
</Link>
</div>
......@@ -7,7 +7,9 @@ import {
findPort,
launchApp,
killApp,
waitFor
waitFor,
check,
getBrowserBodyText
} from 'next-test-utils'
const context = {}
......@@ -63,12 +65,13 @@ describe('On Demand Entries', () => {
let browser
try {
browser = await webdriver(context.appPort, '/nav')
const text = await browser
.elementByCss('#to-dynamic').click()
.waitForElementByCss('.dynamic-page')
.elementByCss('p').text()
expect(text).toBe('Hello')
await browser.eval('document.getElementById("to-dynamic").click()')
await check(async () => {
const text = await getBrowserBodyText(browser)
return text
}, /Hello/)
} finally {
if (browser) {
browser.close()
......
......@@ -152,7 +152,7 @@ export async function startStaticServer (dir) {
export async function check (contentFn, regex) {
let found = false
setTimeout(async () => {
const timeout = setTimeout(async () => {
if (found) {
return
}
......@@ -170,6 +170,7 @@ export async function check (contentFn, regex) {
const newContent = await contentFn()
if (regex.test(newContent)) {
found = true
clearTimeout(timeout)
break
}
await waitFor(1000)
......@@ -231,3 +232,7 @@ export async function getReactErrorOverlayContent (browser) {
}
return browser.eval(`document.querySelector('iframe').contentWindow.document.body.innerHTML`)
}
export function getBrowserBodyText (browser) {
return browser.eval('document.getElementsByTagName("body")[0].innerText')
}
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