/
index.test.js
63 lines (60 loc) · 2.15 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* eslint-env jest */
/* global jasmine */
import { join } from 'path'
import webdriver from 'next-webdriver'
import { killApp, findPort, nextBuild, nextStart } from 'next-test-utils'
const appDir = join(__dirname, '../')
let appPort
let server
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 60 * 2
describe('Analytics relayer', () => {
beforeAll(async () => {
appPort = await findPort()
await nextBuild(appDir)
server = await nextStart(appDir, appPort)
})
afterAll(() => killApp(server))
it('Relays the data to user code', async () => {
const browser = await webdriver(appPort, '/')
await browser.waitForElementByCss('h1')
const h1Text = await browser.elementByCss('h1').text()
const data = parseFloat(
await browser.eval('localStorage.getItem("Next.js-hydration")')
)
const firstPaint = parseFloat(
await browser.eval('localStorage.getItem("first-paint")')
)
const firstContentfulPaint = parseFloat(
await browser.eval('localStorage.getItem("first-contentful-paint")')
)
let largestContentfulPaint = await browser.eval(
'localStorage.getItem("largest-contentful-paint")'
)
let cls = await browser.eval(
'localStorage.getItem("cumulative-layout-shift")'
)
expect(h1Text).toMatch(/Foo!/)
expect(data).not.toBeNaN()
expect(data).toBeGreaterThan(0)
expect(firstPaint).not.toBeNaN()
expect(firstPaint).toBeGreaterThan(0)
expect(firstContentfulPaint).not.toBeNaN()
expect(firstContentfulPaint).toBeGreaterThan(0)
expect(largestContentfulPaint).toBeNull()
expect(cls).toBeNull()
// Create an artificial layout shift
await browser.eval('document.querySelector("h1").style.display = "none"')
await browser.refresh()
await browser.waitForElementByCss('h1')
largestContentfulPaint = parseFloat(
await browser.eval('localStorage.getItem("largest-contentful-paint")')
)
cls = parseFloat(
await browser.eval('localStorage.getItem("cumulative-layout-shift")')
)
expect(cls).not.toBeNull()
expect(largestContentfulPaint).not.toBeNaN()
expect(largestContentfulPaint).toBeGreaterThan(0)
await browser.close()
})
})