-
Notifications
You must be signed in to change notification settings - Fork 0
/
acceptance.spec.js
178 lines (141 loc) · 4.4 KB
/
acceptance.spec.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const puppeteer = require('puppeteer')
const request = require('supertest')
const { toMatchImageSnapshot } = require('jest-image-snapshot')
expect.extend({ toMatchImageSnapshot })
const HOST = process.env.ACCEPTENCE_SERVER || 'http://localhost:3000'
jest.setTimeout(60000)
let browser = null
async function createBrowser(size) {
return await puppeteer.launch({
slowMo: 0,
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
// This will write shared memory files into /tmp instead of /dev/shm,
// because Docker’s default for /dev/shm is 64MB
'--disable-dev-shm-usage'
]
})
}
async function createPage(images = false) {
const page = await browser.newPage()
if (!images) {
await page.setRequestInterception(true)
page.on('request', request => {
if (request.resourceType() === 'image') request.abort()
else request.continue()
})
}
return page
}
beforeAll(async () => {
// Wait for server to be up
while (true) {
try {
const response = await request(HOST).get('/')
if (response.status === 200) {
break
}
} catch (e) {
console.log(e)
}
await new Promise(resolve => {
return setTimeout(resolve, 200)
})
}
})
afterEach(async () => {
if (browser) {
await browser.close()
}
})
describe('acceptence', () => {
it('responds', async () => {
browser = await createBrowser()
const page = await createPage()
await page.goto(`${HOST}`)
expect(await page.title()).toBe('Home - Beefboard')
})
test('login page matches snapshot', async () => {
browser = await createBrowser()
const page = await createPage(true)
await page.goto(`${HOST}/login`)
const screen = await page.screenshot()
expect(screen).toMatchImageSnapshot({
failureThreshold: '0.03',
failureThresholdType: 'percent'
})
})
test('registration page matches snapshot', async () => {
browser = await createBrowser()
const page = await createPage(true)
await page.goto(`${HOST}/register`)
const screen = await page.screenshot()
expect(screen).toMatchImageSnapshot({
failureThreshold: '0.03',
failureThresholdType: 'percent'
})
})
it('not found page matches snapshot', async () => {
browser = await createBrowser()
const page = await createPage(true)
await page.goto(`${HOST}/lksadfjhsfgdhfs`)
const screen = await page.screenshot()
expect(screen).toMatchImageSnapshot({
failureThreshold: '0.03',
failureThresholdType: 'percent'
})
})
it('mobile view matches snapshot', async () => {
browser = await createBrowser()
const page = await createPage(true)
// iPhone 7 size
await page.setViewport({ height: 667, width: 357 })
await page.goto(`${HOST}/login`)
await page.click('.menu-button')
// Wait for drawer to open
await page.waitFor(1000)
const screen = await page.screenshot()
expect(screen).toMatchImageSnapshot({
failureThreshold: '0.03',
failureThresholdType: 'percent'
})
})
it('allows login, shows profile after login, logout navigates back to home', async () => {
browser = await createBrowser()
const page = await createPage()
await page.goto(`${HOST}/login`)
await page.click('input[type=text]')
await page.keyboard.type('admin')
await page.click('input[type=password]')
await page.keyboard.type('admin')
await Promise.all([
page.click('button[type=submit]'),
page.waitForNavigation({
waitUntil: 'networkidle0'
})
])
expect(await page.title()).toBe('Home - Beefboard')
await page.evaluate(() => {
document.querySelector('a[href="/profiles/admin"]').click()
})
await page.waitFor(1000)
expect(await page.title()).toBe("admin's profile - Beefboard")
await page.evaluate(() => {
document.querySelector('a.logout').click()
})
await page.waitFor(1000)
expect(await page.title()).toBe('Home - Beefboard')
const profileLink = await page.$('a[href="/profiles/admin"]')
expect(profileLink).toBeNull()
const logoutButton = await page.$('a.logout')
expect(logoutButton).toBeNull()
})
test('new post redirects to login when not logged in', async () => {
browser = await createBrowser()
const page = await createPage()
await page.goto(`${HOST}/posts/new`)
expect(await page.url()).toBe(`${HOST}/login`)
})
})