/
project-basic.build.e2e.js
225 lines (179 loc) · 8.58 KB
/
project-basic.build.e2e.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
const path = require('path')
const fs = require('fs-extra')
const build = require('../build')
const cheerio = require('cheerio')
const express = require('express')
const puppeteer = require('puppeteer')
const { uniq } = require('lodash')
const context = path.join(__dirname, '__fixtures__', 'project-basic')
const content = file => fs.readFileSync(path.join(context, file), 'utf8')
const exists = file => fs.existsSync(path.join(context, file))
const load = file => cheerio.load(content(file))
const app = express()
let browser, page, server
beforeAll(async () => {
await build(context)
app.use(express.static(path.join(context, 'dist')))
browser = await puppeteer.launch({ headless: false })
page = await browser.newPage()
server = app.listen(8080)
}, 20000)
afterAll(async () => {
server && await server.close()
browser && await browser.close()
await fs.remove(path.join(context, 'dist'))
await fs.remove(path.join(context, 'src', '.temp'))
await fs.remove(path.join(context, '.cache'))
})
test('build basic project', () => {
const indexHTML = content('dist/index.html')
const $home = cheerio.load(indexHTML)
// no objects should be rendered
expect(indexHTML).not.toMatch('[object Object]')
expect($home('head > title').text()).toEqual('Gridsome | Test')
expect($home('#app').data('server-rendered')).toEqual(true)
expect($home('meta[name="keywords"]').attr('content')).toEqual('test')
expect($home('meta[name="og:title"]').attr('content')).toEqual('bar')
expect($home('meta[name="test"]').attr('content')).toEqual('test-meta')
expect($home('meta[name="og:description"]').attr('content')).toEqual('Test Value')
expect($home('meta[name="description"]').attr('content')).toEqual('Index description')
expect($home('meta[name="description"]').get()).toHaveLength(1)
expect($home('h2.meta-data').text()).toEqual('Test Value')
expect($home('span.from-custom-root-field').text()).toEqual('string from custom schema')
expect($home('span.from-env-production').text()).toEqual('PROD_2')
expect($home('span.from-plugin').text()).toEqual('test 2')
expect($home('span.from-chain-webpack').text()).toEqual('test 3')
expect($home('.footer span.meta-data-1').text()).toEqual('Test Value')
expect($home('.footer span.meta-data-2').text()).toEqual('bar')
})
test('render custom html template', () => {
const $home = load('dist/index.html')
expect($home('div.custom-html-template').text().trim()).toEqual('custom html template')
})
test('render favicons', () => {
expect(exists('dist/assets/static/favicon.1539b60.test.png')).toBeTruthy()
})
// #163 - no duplicate style links
test('do not render duplicate style links', () => {
expect(content('dist/index.html').match(/styles\.css/g)).toHaveLength(2)
})
test('render g-link components', () => {
const $home = load('dist/index.html')
expect($home('a.g-link-2.test-active.active--exact').attr('href')).toEqual('/')
expect($home('a[href="http://outsidelink1.com"]').attr('target')).toEqual('_blank')
expect($home('a[href="http://outsidelink1.com"]').attr('rel')).toEqual('noopener')
expect($home('a[href="https://outsidelink2.com"]').attr('target')).toEqual('_blank')
expect($home('a[href="https://outsidelink2.com"]').attr('rel')).toEqual('noopener')
expect($home('a[href="//outsidelink3.com"]').attr('target')).toEqual('_blank')
expect($home('a[href="//outsidelink3.com"]').attr('rel')).toEqual('noopener')
expect($home('a[href="https://www.gridsome.org/docs"]').attr('target')).toBeUndefined()
expect($home('a[href="https://www.gridsome.org/docs"]').attr('rel')).toBeUndefined()
expect($home('a.g-link-file').attr('href')).toEqual('/assets/files/dummy.pdf')
expect(exists('dist/assets/files/dummy.pdf')).toBeTruthy()
})
test('render g-image components', () => {
const $home = load('dist/index.html')
expect(exists('dist/assets/static/test.82a2fbd.test.png')).toBeTruthy()
expect(exists('dist/assets/static/test.97c148e.test.png')).toBeTruthy()
expect($home('img.g-image-1').attr('src')).toMatch('data:image/svg+xml')
expect($home('img.g-image-1').attr('alt')).toMatch('SVG logo')
expect($home('img.g-image-2').attr('alt')).toMatch('Test image')
expect($home('img.g-image-2').data('srcset')).toMatch('/assets/static/test.82a2fbd.test.png 480w')
expect($home('img.g-image-2').data('src')).toEqual('/assets/static/test.97c148e.test.png')
expect($home('img.g-image-2').attr('class')).not.toEqual('g-image-false')
expect($home('img.g-image-static').attr('src')).toEqual('/uploads/test.png')
expect($home('img.g-image-static').attr('alt')).toEqual('Static image')
expect($home('img.g-image-immediate').attr('src')).toEqual('/assets/static/test.cbab2cf.test.png')
expect($home('img.g-image-immediate').attr('alt')).toEqual('Immediate image')
expect($home('img.g-image-external').attr('src')).toEqual('https://www.example.com/assets/image.png')
expect($home('img.g-image-external').attr('alt')).toEqual('External image')
expect($home('img[width=""]').get().length).toEqual(0)
// no duplicate classes
const classes = $home('img.g-image-1').attr('class').split(' ')
expect(uniq(classes).length - classes.length).toEqual(0)
})
test('render template with static routes', () => {
const $page1 = load('dist/pages/1/index.html')
const $page2 = load('dist/pages/2/index.html')
expect($page1('h1').text()).toEqual('Page 1')
expect($page1('h2').text()).toEqual('Doc 4')
expect($page2('h1').text()).toEqual('Page 2')
expect($page2('h2').text()).toEqual('Doc 3')
})
test('render template with static routes and pagination', () => {
expect(load('dist/docs/1/index.html')('h1').text()).toEqual('Doc 1')
expect(load('dist/docs/2/index.html')('h1').text()).toEqual('Doc 2')
expect(load('dist/docs/2/2/index.html')('h1').text()).toEqual('Doc 2')
expect(load('dist/docs/2/3/index.html')('h1').text()).toEqual('Doc 2')
expect(exists('dist/docs/1/2/index.html')).toBeFalsy()
expect(exists('dist/docs/2/4/index.html')).toBeFalsy()
expect(exists('dist/docs/3/2/index.html')).toBeFalsy()
})
test('generate /404.html', () => {
const $404 = load('dist/404.html')
expect($404('h1').text()).toEqual('Custom 404 - not found')
expect($404('span.page-query-value').text().trim()).toEqual('string from custom schema')
})
test('compile scripts correctly', () => {
const appJS = content('dist/assets/js/app.js')
const homeJS = content('dist/assets/js/component--home.js')
// never include the context path
expect(appJS).not.toMatch(context)
expect(homeJS).not.toMatch(context)
// api.transpileDependencies
expect(appJS).not.toMatch('testToArray1 (...args)') // transpiled
expect(appJS).toMatch('testToArray2 (...args)') // not transpiled
expect(appJS).not.toMatch('classTestMethod ()') // transpiled
// transpile custom sfc blocks
expect(appJS).not.toMatch('Component =>') // static-query
expect(homeJS).not.toMatch('Component =>') // page-query
// env variables
expect(homeJS).toMatch('GRIDSOME_PROD_VARIABLE: "PROD_1"')
expect(homeJS).toMatch('PROD_VARIABLE: process.env.PROD_VARIABLE')
// polyfills
expect(appJS).toMatch('// ECMAScript 6 symbols shim')
})
test('compile a single css file', () => {
const files = fs.readdirSync(path.join(context, 'dist/assets/css'))
expect(files.length).toEqual(1)
})
test('remove the styles.js chunk', () => {
const files = fs.readdirSync(path.join(context, 'dist/assets/js'))
const chunk = files.find(file => file === 'styles.js')
expect(chunk).toBeUndefined()
})
test('open homepage in browser', async () => {
await page.goto('http://localhost:8080/', { waitUntil: 'networkidle2' })
await page.waitForSelector('#app.is-mounted')
})
test('load .g-image', async () => {
await page.waitForSelector('.g-image--loaded')
})
test('navigate to /docs/1', async () => {
await page.click('.doc-link-1')
await page.waitForSelector('#app.doc-template')
})
test('navigate to /pages/2', async () => {
await page.click('.page-link-2')
await page.waitForSelector('#app.page-template')
})
test('navigate to /', async () => {
await page.click('.home-link')
await page.waitForSelector('#app.home')
})
test('navigate to /asdf', async () => {
await page.click('.not-found-link')
await page.waitForSelector('#app.not-found')
})
test('navigate to /', async () => {
await page.click('.home-link')
await page.waitForSelector('#app.home')
})
test('open /docs/1/ directly', async () => {
await page.goto('http://localhost:8080/docs/1/', { waitUntil: 'networkidle2' })
await page.waitForSelector('#app.is-mounted')
})
test('open /pages/2/ directly', async () => {
await page.goto('http://localhost:8080/pages/2/', { waitUntil: 'networkidle2' })
await page.waitForSelector('#app.is-mounted')
})