-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsitemap.test.js
226 lines (207 loc) · 6.75 KB
/
sitemap.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
226
const { readFile, unlink } = require('fs')
const path = require('path')
const { promisify } = require('util')
const test = require('ava')
const tempy = require('tempy')
const { parseString } = require('xml2js')
const pParseString = promisify(parseString)
const pUnlink = promisify(unlink)
const pReadFile = promisify(readFile)
const makeSitemap = require('../make_sitemap.js')
test.beforeEach((t) => {
t.context.fileName = tempy.file({ name: 'sitemap.xml' })
})
test.afterEach(async (t) => {
try {
await pUnlink(t.context.fileName)
} catch (_) {}
})
// Test relative and absolute paths
const CONFIGS = [
{
distPath: './fixtures',
testNamePostfix: 'relative path',
cwd: __dirname,
excludePath: './fixtures/children/grandchildren/grandchild-two.html',
},
{
distPath: path.resolve(__dirname, './fixtures'),
testNamePostfix: 'absolute path',
excludePath: path.resolve(__dirname, './fixtures/children/grandchildren/grandchild-two.html'),
},
{
distPath: '.',
testNamePostfix: 'root relative path',
cwd: path.join(__dirname, 'fixtures'),
excludePath: './children/grandchildren/grandchild-two.html',
},
]
// eslint-disable-next-line max-lines-per-function
CONFIGS.forEach(({ distPath, testNamePostfix, cwd, excludePath }) => {
test(`Creates Sitemap with all html files - ${testNamePostfix}`, async (t) => {
const { fileName } = t.context
const sitemapData = await makeSitemap({
homepage: 'https://site.com/',
distPath,
prettyURLs: true,
failBuild() {},
fileName,
cwd,
})
const xmlData = await parseXml(fileName)
const pages = getPages(xmlData)
t.truthy(sitemapData.sitemapPath)
t.deepEqual(pages, [
'https://site.com/',
'https://site.com/page-one',
'https://site.com/page-three',
'https://site.com/page-two',
'https://site.com/children/child-one',
'https://site.com/children/child-two',
'https://site.com/children/grandchildren/grandchild-one',
'https://site.com/children/grandchildren/grandchild-two',
])
})
test(`Creates Sitemap with all html files with trailing slash - ${testNamePostfix}`, async (t) => {
const { fileName } = t.context
const sitemapData = await makeSitemap({
homepage: 'https://site.com/',
distPath,
prettyURLs: true,
trailingSlash: true,
failBuild() {},
fileName,
cwd,
})
const xmlData = await parseXml(fileName)
const pages = getPages(xmlData)
t.truthy(sitemapData.sitemapPath)
t.deepEqual(pages, [
'https://site.com/',
'https://site.com/page-one/',
'https://site.com/page-three/',
'https://site.com/page-two/',
'https://site.com/children/child-one/',
'https://site.com/children/child-two/',
'https://site.com/children/grandchildren/grandchild-one/',
'https://site.com/children/grandchildren/grandchild-two/',
])
})
test(`Sitemap pretty urls off works correctly - ${testNamePostfix}`, async (t) => {
const { fileName } = t.context
const sitemapData = await makeSitemap({
homepage: 'https://site.com/',
distPath,
prettyURLs: false,
failBuild() {},
fileName,
cwd,
})
const xmlData = await parseXml(fileName)
const pages = getPages(xmlData)
t.truthy(sitemapData.sitemapPath)
t.deepEqual(pages, [
'https://site.com/index.html',
'https://site.com/page-one.html',
'https://site.com/page-three.html',
'https://site.com/page-two.html',
'https://site.com/children/child-one.html',
'https://site.com/children/child-two.html',
'https://site.com/children/grandchildren/grandchild-one.html',
'https://site.com/children/grandchildren/grandchild-two.html',
])
})
test(`Sitemap applies changeFreq and priority when configured - ${testNamePostfix}`, async (t) => {
const { fileName } = t.context
const defaultChangeFreq = 'daily'
const defaultPriority = 0.9
await makeSitemap({
homepage: 'https://site.com/',
distPath,
prettyURLs: false,
failBuild() {},
changeFreq: defaultChangeFreq,
priority: defaultPriority,
fileName,
cwd,
})
const xmlData = await parseXml(fileName)
t.is(xmlData.urlset.url[0].changefreq[0], defaultChangeFreq)
t.is(xmlData.urlset.url[0].priority[0], defaultPriority.toString())
})
test(`Sitemap changefreq and priority defaults to weekly and 0.8 - ${testNamePostfix}`, async (t) => {
const { fileName } = t.context
await makeSitemap({
homepage: 'https://site.com/',
distPath,
prettyURLs: false,
failBuild() {},
fileName,
cwd,
})
const xmlData = await parseXml(fileName)
t.is(xmlData.urlset.url[0].changefreq[0], 'weekly')
t.is(xmlData.urlset.url[0].priority[0], '0.8')
})
test(`Sitemap exclude works correctly - ${testNamePostfix}`, async (t) => {
const { fileName } = t.context
const sitemapData = await makeSitemap({
homepage: 'https://site.com/',
distPath,
prettyURLs: true,
exclude: [
// Path
excludePath,
// Glob pattern
'**/**/child-one.html',
],
failBuild() {},
fileName,
cwd,
})
const xmlData = await parseXml(fileName)
const pages = getPages(xmlData)
t.truthy(sitemapData.sitemapPath)
t.deepEqual(pages, [
'https://site.com/',
'https://site.com/page-one',
'https://site.com/page-three',
'https://site.com/page-two',
// excluded 'https://site.com/children/child-one.html',
'https://site.com/children/child-two',
'https://site.com/children/grandchildren/grandchild-one',
// excluded 'https://site.com/children/grandchildren/grandchild-two.html'
])
})
test(`Sitemap urlPrefix works correctly - ${testNamePostfix}`, async (t) => {
const { fileName } = t.context
const sitemapData = await makeSitemap({
homepage: 'https://site.com/',
distPath,
prettyURLs: true,
urlPrefix: 'en/',
failBuild() {},
fileName,
cwd,
})
const xmlData = await parseXml(fileName)
const pages = getPages(xmlData)
t.truthy(sitemapData.sitemapPath)
t.deepEqual(pages, [
'https://site.com/en/',
'https://site.com/en/page-one',
'https://site.com/en/page-three',
'https://site.com/en/page-two',
'https://site.com/en/children/child-one',
'https://site.com/en/children/child-two',
'https://site.com/en/children/grandchildren/grandchild-one',
'https://site.com/en/children/grandchildren/grandchild-two',
])
})
})
const getPages = (data) => data.urlset.url.map((record) => record.loc[0])
const parseXml = async (filePath) => {
const xml = await pReadFile(filePath, 'utf-8')
const parsed = await pParseString(xml)
return parsed
}