/
writeRedirectFiles.test.ts
208 lines (181 loc) · 5.72 KB
/
writeRedirectFiles.test.ts
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import fs from 'fs-extra';
import path from 'path';
import writeRedirectFiles, {
toRedirectFiles,
createToUrl,
} from '../writeRedirectFiles';
// Test to fix toUrl bugs we had:
// - https://github.com/facebook/docusaurus/issues/3886
// - https://github.com/facebook/docusaurus/issues/3925
describe('createToUrl', () => {
it('creates appropriate redirect urls', () => {
expect(createToUrl('/', '/docs/something/else')).toBe(
'/docs/something/else',
);
expect(createToUrl('/', '/docs/something/else/')).toBe(
'/docs/something/else/',
);
expect(createToUrl('/', 'docs/something/else')).toBe(
'/docs/something/else',
);
});
it('creates appropriate redirect urls with baseUrl', () => {
expect(createToUrl('/baseUrl/', '/docs/something/else')).toBe(
'/baseUrl/docs/something/else',
);
expect(createToUrl('/baseUrl/', '/docs/something/else/')).toBe(
'/baseUrl/docs/something/else/',
);
expect(createToUrl('/baseUrl/', 'docs/something/else')).toBe(
'/baseUrl/docs/something/else',
);
});
});
describe('toRedirectFiles', () => {
it('creates appropriate metadata trailingSlash=undefined', () => {
const pluginContext = {
outDir: '/tmp/someFixedOutDir',
baseUrl: 'https://docusaurus.io',
};
const redirectFiles = toRedirectFiles(
[
{from: '/abc.html', to: '/abc'},
{from: '/def', to: '/def.html'},
{from: '/xyz', to: '/'},
],
pluginContext,
undefined,
);
expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
path.join(pluginContext.outDir, '/abc.html/index.html'),
path.join(pluginContext.outDir, '/def/index.html'),
path.join(pluginContext.outDir, '/xyz/index.html'),
]);
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
'fileContent',
);
});
it('creates appropriate metadata trailingSlash=true', () => {
const pluginContext = {
outDir: '/tmp/someFixedOutDir',
baseUrl: 'https://docusaurus.io',
};
const redirectFiles = toRedirectFiles(
[
{from: '/abc.html', to: '/abc'},
{from: '/def', to: '/def.html'},
{from: '/xyz', to: '/'},
],
pluginContext,
true,
);
expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
path.join(pluginContext.outDir, '/abc.html/index.html'),
path.join(pluginContext.outDir, '/def/index.html'),
path.join(pluginContext.outDir, '/xyz/index.html'),
]);
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
'fileContent',
);
});
it('creates appropriate metadata trailingSlash=false', () => {
const pluginContext = {
outDir: '/tmp/someFixedOutDir',
baseUrl: 'https://docusaurus.io',
};
const redirectFiles = toRedirectFiles(
[
{from: '/abc.html', to: '/abc'},
{from: '/def', to: '/def.html'},
{from: '/xyz', to: '/'},
],
pluginContext,
false,
);
expect(redirectFiles.map((f) => f.fileAbsolutePath)).toEqual([
// Can't be used because /abc.html already exists, and file/folder can't
// share same name on Unix!
// path.join(pluginContext.outDir, '/abc.html/index.html'),
path.join(pluginContext.outDir, '/abc.html.html'), // Weird but on purpose!
path.join(pluginContext.outDir, '/def/index.html'),
path.join(pluginContext.outDir, '/xyz/index.html'),
]);
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
'fileContent',
);
});
it('creates appropriate metadata for root baseUrl', () => {
const pluginContext = {
outDir: '/tmp/someFixedOutDir',
baseUrl: '/',
};
const redirectFiles = toRedirectFiles(
[{from: '/abc.html', to: '/abc'}],
pluginContext,
undefined,
);
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
'fileContent baseUrl=/',
);
});
it('creates appropriate metadata for empty baseUrl', () => {
const pluginContext = {
outDir: '/tmp/someFixedOutDir',
baseUrl: '',
};
const redirectFiles = toRedirectFiles(
[{from: '/abc.html', to: '/abc'}],
pluginContext,
undefined,
);
expect(redirectFiles.map((f) => f.fileContent)).toMatchSnapshot(
'fileContent baseUrl=empty',
);
});
});
describe('writeRedirectFiles', () => {
it('write the files', async () => {
const outDir = `/tmp/docusaurus_tests_${Math.random()}`;
const filesMetadata = [
{
fileAbsolutePath: path.join(outDir, 'someFileName'),
fileContent: 'content 1',
},
{
fileAbsolutePath: path.join(outDir, '/some/nested/filename'),
fileContent: 'content 2',
},
];
await writeRedirectFiles(filesMetadata);
await expect(
fs.readFile(filesMetadata[0]!.fileAbsolutePath, 'utf8'),
).resolves.toBe('content 1');
await expect(
fs.readFile(filesMetadata[1]!.fileAbsolutePath, 'utf8'),
).resolves.toBe('content 2');
});
it('avoid overwriting existing files', async () => {
const outDir = `/tmp/docusaurus_tests_${Math.random()}`;
const filesMetadata = [
{
fileAbsolutePath: path.join(outDir, 'someFileName/index.html'),
fileContent: 'content 1',
},
];
await fs.outputFile(
filesMetadata[0]!.fileAbsolutePath,
'file already exists!',
);
await expect(
writeRedirectFiles(filesMetadata),
).rejects.toThrowErrorMatchingInlineSnapshot(
`"The redirect plugin is not supposed to override existing files."`,
);
});
});