-
Notifications
You must be signed in to change notification settings - Fork 319
/
app.test.js
225 lines (194 loc) · 6.95 KB
/
app.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
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 request = require('request')
const cheerio = require('cheerio')
const lib = require('../lib/file-helper')
const configPaths = require('../config/paths.json')
const PORT = configPaths.ports.test
const expectedPages = [
'/',
'/components/all',
'/examples/form-alignment',
'/examples/form-elements',
'/examples/grid',
'/examples/links',
'/examples/typography',
'/examples/template-default',
'/examples/template-custom'
]
// Returns a wrapper for `request` which applies these options by default
const requestPath = request.defaults({
baseUrl: `http://localhost:${PORT}`,
headers: {
'Content-Type': 'text/plain'
}
})
describe(`http://localhost:${PORT}`, () => {
describe.each(expectedPages)('%s', path => {
it('should resolve with a http status code of 200', done => {
requestPath.get(path, (err, res) => {
expect(res.statusCode).toEqual(200)
done(err)
})
})
it('should resolve with a ‘Content-Type’ header of "text/html"', done => {
requestPath.get(path, (err, res) => {
expect(res.headers['content-type']).toContain('text/html')
done(err)
})
})
it('should prevent search indexing', done => {
requestPath.get(path, (err, res) => {
expect(res.headers['x-robots-tag']).toEqual('none')
done(err)
})
})
})
describe('/', () => {
it('should display the list of components', done => {
requestPath('/', (err, res) => {
const $ = cheerio.load(res.body)
const componentsList = $('li a[href^="/components/"]').get()
// Since we have an 'all' component link that renders the default example of all
// components, there will always be one more expected link.
const expectedComponentLinks = lib.allComponents.length + 1
expect(componentsList.length).toEqual(expectedComponentLinks)
done(err)
})
})
})
describe('/robots.txt', () => {
it('should allow crawling by robots', done => {
requestPath('/robots.txt', (err, res) => {
expect(res.body).toMatch(/^Allow: \/$/m)
done(err)
})
})
})
describe('/examples/template-custom', () => {
const templatePath = '/examples/template-custom'
it.each([
'headIcons',
'bodyStart',
'main',
'content',
'bodyEnd'
])('should have a %s block set', (block, done) => {
requestPath(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
expect($.html()).toContain(`<!-- block:${block} -->`)
done(err)
})
})
it('should have additional `htmlClasses`', done => {
requestPath(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $html = $('html')
expect($html.attr('class')).toBe('govuk-template app-html-class')
done(err)
})
})
it('should have assets overriden', done => {
requestPath(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $linkAsset = $('link[href^="/images/"]')
expect($linkAsset.length).toBe(6)
done(err)
})
})
it('should have theme-color overriden', done => {
requestPath(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $linkMaskIcon = $('link[rel="mask-icon"]')
const $metaThemeColor = $('meta[name="theme-color"]')
expect($linkMaskIcon.attr('color')).toBe('blue')
expect($metaThemeColor.attr('content')).toBe('blue')
done(err)
})
})
it('should have additional `bodyClasses`', done => {
requestPath(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $body = $('body')
expect($body.attr('class')).toBe('govuk-template__body app-body-class')
done(err)
})
})
it('should have `pageTitle` overriden', done => {
requestPath(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $title = $('title')
expect($title.html()).toBe('GOV.UK - Le meilleur endroit pour trouver des services gouvernementaux et de l'information')
done(err)
})
})
it('should have an application stylesheet', done => {
requestPath(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $appStylesheet = $('link[href="/public/app.css"]')
expect($appStylesheet.length).toBe(1)
done(err)
})
})
it('should have a custom Skip link component', done => {
requestPath(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $skipLink = $('.govuk-skip-link')
expect($skipLink.html()).toBe('Passer au contenu principal')
done(err)
})
})
it('should have a custom Header component', done => {
requestPath(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $header = $('.govuk-header')
const $serviceName = $header.find('.govuk-header__service-name')
expect($serviceName.html()).toContain('Nom du service')
done(err)
})
})
it('should have a Phase banner component', done => {
requestPath.get(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $phaseBanner = $('.govuk-phase-banner')
const $text = $phaseBanner.find('.govuk-phase-banner__text')
expect($text.html()).toContain('C'est un nouveau service - vos <a class="govuk-link" href="#">commentaires</a> nous aideront à l'améliorer.')
done(err)
})
})
it('should have a custom Footer component', done => {
requestPath.get(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $footer = $('.govuk-footer')
const $footerLink = $footer.find('.govuk-footer__link')
expect($footerLink.html()).toContain('Aidez-moi')
done(err)
})
})
it('should have `content` within the main section of the page', done => {
requestPath.get(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $main = $('main')
expect($main.html()).toContain('<!-- block:content -->')
done(err)
})
})
it('should have `beforeContent` outside the main section of the page', done => {
requestPath.get(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $container = $('.govuk-width-container')
const $phaseBanner = $container.find('> .govuk-phase-banner')
const $backLink = $container.find('> .govuk-back-link')
expect($phaseBanner.length).toBe(1)
expect($backLink.length).toBe(1)
done(err)
})
})
it('should have set `mainClasses`', done => {
requestPath.get(templatePath, (err, res) => {
const $ = cheerio.load(res.body)
const $main = $('main')
expect($main.attr('class')).toBe('govuk-main-wrapper govuk-main-wrapper--auto-spacing app-main-class')
done(err)
})
})
})
})