-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathutils.spec.js
More file actions
161 lines (132 loc) · 4.78 KB
/
Copy pathutils.spec.js
File metadata and controls
161 lines (132 loc) · 4.78 KB
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
import process from 'node:process';
import { describe, test, expect, vi } from 'vitest';
import { matches, get_publish_directory } from './utils.js';
/**
* Helper to create a static route segment
* @param {string} content
* @returns {{ rest: boolean, dynamic: boolean, content: string }}
*/
function static_segment(content) {
return { rest: false, dynamic: false, content };
}
/**
* Helper to create a dynamic route segment
* @param {string} content
* @returns {{ rest: boolean, dynamic: boolean, content: string }}
*/
function dynamic_segment(content) {
return { rest: false, dynamic: true, content };
}
/**
* Helper to create a rest route segment
* @param {string} content
* @returns {{ rest: boolean, dynamic: boolean, content: string }}
*/
function rest_segment(content) {
return { rest: true, dynamic: true, content };
}
describe('matches', () => {
test('two identical static routes match', () => {
expect(
matches(
[static_segment('blog'), static_segment('post')],
[static_segment('blog'), static_segment('post')]
)
).toBe(true);
});
test('static segment mismatch returns false', () => {
expect(matches([static_segment('blog')], [static_segment('about')])).toBe(false);
});
test('dynamic segment matches any static segment', () => {
expect(matches([static_segment('blog')], [dynamic_segment('[slug]')])).toBe(true);
});
test('rest segment at end matches everything', () => {
expect(
matches([static_segment('blog'), static_segment('post')], [rest_segment('[...rest]')])
).toBe(true);
});
test('rest-only route matches any route', () => {
expect(
matches(
[static_segment('a'), static_segment('b'), static_segment('c')],
[rest_segment('[...rest]')]
)
).toBe(true);
});
test('rest segment matches remaining segments', () => {
expect(
matches(
[static_segment('blog'), static_segment('2024'), static_segment('post')],
[static_segment('blog'), rest_segment('[...rest]')]
)
).toBe(true);
});
test('empty segments (index routes)', () => {
expect(matches([], [])).toBe(false);
});
test('mismatched lengths without rest return false', () => {
expect(
matches([static_segment('blog'), static_segment('post')], [static_segment('blog')])
).toBe(false);
});
test('rest with following segments', () => {
expect(
matches(
[static_segment('a'), static_segment('b'), static_segment('page')],
[rest_segment('[...rest]'), static_segment('page')]
)
).toBe(true);
});
test('rest with following segments that do not match', () => {
expect(
matches(
[static_segment('a'), static_segment('b'), static_segment('other')],
[rest_segment('[...rest]'), static_segment('page')]
)
).toBe(false);
});
test('a has trailing rest and b is shorter', () => {
expect(matches([rest_segment('[...rest]')], [static_segment('blog')])).toBe(true);
});
test('a is longer without rest in b', () => {
expect(matches([static_segment('a'), static_segment('b')], [static_segment('a')])).toBe(false);
});
test('b is longer without rest in a', () => {
expect(matches([static_segment('a')], [static_segment('a'), static_segment('b')])).toBe(false);
});
test('dynamic in a matches static in b', () => {
expect(matches([dynamic_segment('[id]')], [static_segment('blog')])).toBe(true);
});
test('both dynamic segments match', () => {
expect(matches([dynamic_segment('[id]')], [dynamic_segment('[slug]')])).toBe(true);
});
});
describe('get_publish_directory', () => {
test('returns undefined when no netlify.toml, with warning logged', () => {
const warn = vi.fn();
const builder = /** @type {any} */ ({ log: { warn, minor: vi.fn() } });
const result = get_publish_directory(null, builder);
expect(result).toBeUndefined();
expect(warn).toHaveBeenCalledOnce();
expect(warn).toHaveBeenCalledWith(expect.stringContaining('No netlify.toml found'));
});
test('returns undefined when config has no build.publish, with minor log', () => {
const minor = vi.fn();
const builder = /** @type {any} */ ({ log: { warn: vi.fn(), minor } });
const result = get_publish_directory({ build: {} }, builder);
expect(result).toBeUndefined();
expect(minor).toHaveBeenCalledOnce();
expect(minor).toHaveBeenCalledWith(expect.stringContaining('No publish directory specified'));
});
test('returns the publish value when specified', () => {
const builder = /** @type {any} */ ({ log: { warn: vi.fn(), minor: vi.fn() } });
const result = get_publish_directory({ build: { publish: 'dist' } }, builder);
expect(result).toBe('dist');
});
test('throws when publish is site root', () => {
const builder = /** @type {any} */ ({ log: { warn: vi.fn(), minor: vi.fn() } });
expect(() => get_publish_directory({ build: { publish: process.cwd() } }, builder)).toThrow(
'The publish directory cannot be set to the site root'
);
});
});