Skip to content
This repository was archived by the owner on Oct 20, 2022. It is now read-only.

Commit c8062d6

Browse files
authored
Merge pull request #320 from netlify/chore/refactor-tests
2 parents cfd5cca + fd618da commit c8062d6

File tree

7 files changed

+444
-202
lines changed

7 files changed

+444
-202
lines changed

tests/all.js

Lines changed: 67 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,179 +1,163 @@
11
const test = require('ava')
22
const { each } = require('test-each')
33

4-
const { parseAllRedirects } = require('..')
5-
6-
const { FIXTURES_DIR } = require('./helpers/main')
7-
8-
const parseRedirects = async function ({ fileFixtureNames, configFixtureName, configRedirects, opts }) {
9-
const redirectsFiles =
10-
fileFixtureNames === undefined
11-
? undefined
12-
: fileFixtureNames.map((fileFixtureName) => `${FIXTURES_DIR}/redirects_file/${fileFixtureName}`)
13-
const netlifyConfigPath =
14-
configFixtureName === undefined ? undefined : `${FIXTURES_DIR}/netlify_config/${configFixtureName}.toml`
15-
return await parseAllRedirects({ redirectsFiles, netlifyConfigPath, configRedirects, ...opts })
16-
}
4+
const { validateSuccess, validateErrors } = require('./helpers/main')
175

186
each(
197
[
208
{
219
title: 'empty',
10+
input: {},
2211
output: [],
2312
},
2413
{
2514
title: 'only_config',
26-
configFixtureName: 'from_simple',
15+
input: {
16+
netlifyConfigPath: 'from_simple',
17+
},
2718
output: [
2819
{
2920
from: '/old-path',
3021
path: '/old-path',
31-
query: {},
3222
to: '/new-path',
33-
force: false,
34-
conditions: {},
35-
headers: {},
36-
proxy: false,
3723
},
3824
],
3925
},
4026
{
4127
title: 'only_files',
42-
fileFixtureNames: ['from_simple', 'from_absolute_uri'],
28+
input: {
29+
redirectsFiles: ['from_simple', 'from_absolute_uri'],
30+
},
4331
output: [
4432
{
4533
from: '/home',
4634
path: '/home',
47-
query: {},
4835
to: '/',
49-
force: false,
50-
conditions: {},
51-
headers: {},
52-
proxy: false,
5336
},
5437
{
5538
from: 'http://hello.bitballoon.com/*',
5639
scheme: 'http',
5740
host: 'hello.bitballoon.com',
5841
path: '/*',
59-
query: {},
6042
to: 'http://www.hello.com/:splat',
61-
force: false,
62-
conditions: {},
63-
headers: {},
64-
proxy: false,
6543
},
6644
],
6745
},
6846
{
6947
title: 'both_config_files',
70-
fileFixtureNames: ['from_simple', 'from_absolute_uri'],
71-
configFixtureName: 'from_simple',
48+
input: {
49+
redirectsFiles: ['from_simple', 'from_absolute_uri'],
50+
netlifyConfigPath: 'from_simple',
51+
},
7252
output: [
7353
{
7454
from: '/home',
7555
path: '/home',
76-
query: {},
7756
to: '/',
78-
force: false,
79-
conditions: {},
80-
headers: {},
81-
proxy: false,
8257
},
8358
{
8459
from: 'http://hello.bitballoon.com/*',
8560
scheme: 'http',
8661
host: 'hello.bitballoon.com',
8762
path: '/*',
88-
query: {},
8963
to: 'http://www.hello.com/:splat',
90-
force: false,
91-
conditions: {},
92-
headers: {},
93-
proxy: false,
9464
},
9565
{
9666
from: '/old-path',
9767
path: '/old-path',
98-
query: {},
9968
to: '/new-path',
100-
force: false,
101-
conditions: {},
102-
headers: {},
103-
proxy: false,
10469
},
10570
],
10671
},
10772
{
10873
title: 'config_redirects',
109-
configFixtureName: 'from_simple',
110-
configRedirects: [
111-
{
112-
from: '/home',
113-
to: '/',
114-
},
115-
],
74+
input: {
75+
netlifyConfigPath: 'from_simple',
76+
configRedirects: [
77+
{
78+
from: '/home',
79+
to: '/',
80+
},
81+
],
82+
},
11683
output: [
11784
{
11885
from: '/old-path',
11986
path: '/old-path',
120-
query: {},
12187
to: '/new-path',
122-
force: false,
123-
conditions: {},
124-
headers: {},
125-
proxy: false,
12688
},
12789
{
12890
from: '/home',
12991
path: '/home',
130-
query: {},
13192
to: '/',
132-
force: false,
133-
conditions: {},
134-
headers: {},
135-
proxy: false,
13693
},
13794
],
13895
},
13996
{
14097
title: 'minimal',
141-
fileFixtureNames: ['from_simple', 'from_absolute_uri'],
142-
configFixtureName: 'from_simple',
98+
input: {
99+
redirectsFiles: ['from_simple', 'from_absolute_uri'],
100+
netlifyConfigPath: 'from_simple',
101+
minimal: true,
102+
},
143103
output: [
144104
{
145105
from: '/home',
146-
query: {},
147106
to: '/',
148-
force: false,
149-
conditions: {},
150-
headers: {},
151107
},
152108
{
153109
from: 'http://hello.bitballoon.com/*',
154-
query: {},
155110
to: 'http://www.hello.com/:splat',
156-
force: false,
157-
conditions: {},
158-
headers: {},
159111
},
160112
{
161113
from: '/old-path',
162-
query: {},
163114
to: '/new-path',
164-
force: false,
165-
conditions: {},
166-
headers: {},
167115
},
168116
],
169-
opts: { minimal: true },
117+
},
118+
{
119+
title: 'valid_redirects_mixed',
120+
input: {
121+
redirectsFiles: ['from_simple'],
122+
configRedirects: {},
123+
},
124+
output: [
125+
{
126+
from: '/home',
127+
path: '/home',
128+
to: '/',
129+
},
130+
],
170131
},
171132
],
172-
({ title }, { fileFixtureNames, configFixtureName, configRedirects, output, opts }) => {
133+
({ title }, opts) => {
173134
test(`Parses netlify.toml and _redirects | ${title}`, async (t) => {
174-
const { redirects, errors } = await parseRedirects({ fileFixtureNames, configFixtureName, configRedirects, opts })
175-
t.is(errors.length, 0)
176-
t.deepEqual(redirects, output)
135+
await validateSuccess(t, opts)
136+
})
137+
},
138+
)
139+
140+
each(
141+
[
142+
{
143+
title: 'invalid_redirects_array',
144+
input: {
145+
configRedirects: {},
146+
},
147+
errorMessage: /must be an array/,
148+
},
149+
{
150+
title: 'invalid_redirects_mixed',
151+
input: {
152+
redirectsFiles: ['from_simple'],
153+
configRedirects: {},
154+
},
155+
errorMessage: /must be an array/,
156+
},
157+
],
158+
({ title }, opts) => {
159+
test(`Validate syntax errors | ${title}`, async (t) => {
160+
await validateErrors(t, opts)
177161
})
178162
},
179163
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/one /two
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/one /two
2+
/one /four

tests/helpers/main.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
1+
const { parseAllRedirects } = require('../..')
2+
13
const FIXTURES_DIR = `${__dirname}/../fixtures`
24

5+
// Pass an `input` to the main method and assert its output
6+
const validateSuccess = async function (t, { input, output }) {
7+
const { redirects } = await parseRedirects(input)
8+
t.deepEqual(
9+
redirects,
10+
output.map((redirect) => normalizeRedirect(redirect, input)),
11+
)
12+
}
13+
14+
// Pass an `input` to the main method and assert it fails with a specific error
15+
const validateErrors = async function (t, { input, errorMessage }) {
16+
const { errors } = await parseRedirects(input)
17+
t.not(errors.length, 0)
18+
t.true(errors.some((error) => errorMessage.test(error.message)))
19+
}
20+
21+
const parseRedirects = async function ({ redirectsFiles, netlifyConfigPath, configRedirects, minimal }) {
22+
return await parseAllRedirects({
23+
...(redirectsFiles && { redirectsFiles: redirectsFiles.map(addFileFixtureDir) }),
24+
...(netlifyConfigPath && { netlifyConfigPath: addConfigFixtureDir(netlifyConfigPath) }),
25+
configRedirects,
26+
minimal,
27+
})
28+
}
29+
30+
const addFileFixtureDir = function (name) {
31+
return `${FIXTURES_DIR}/redirects_file/${name}`
32+
}
33+
34+
const addConfigFixtureDir = function (name) {
35+
return `${FIXTURES_DIR}/netlify_config/${name}.toml`
36+
}
37+
338
// Assign default values to redirects
4-
const normalizeRedirect = function (redirect, { minimal } = {}) {
39+
const normalizeRedirect = function (redirect, { minimal }) {
540
return {
6-
...(!minimal && ADDED_DEFAULT_REDIRECTS),
41+
...(minimal || ADDED_DEFAULT_REDIRECTS),
742
...DEFAULT_REDIRECT,
843
...redirect,
944
}
@@ -20,4 +55,4 @@ const DEFAULT_REDIRECT = {
2055
headers: {},
2156
}
2257

23-
module.exports = { FIXTURES_DIR, normalizeRedirect }
58+
module.exports = { validateSuccess, validateErrors }

0 commit comments

Comments
 (0)