-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
utils.spec.js
213 lines (195 loc) · 4.01 KB
/
utils.spec.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
import path from 'path';
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { validate_config } from '../../core/config/index.js';
import { posixify } from '../../utils/filesystem.js';
import { deep_merge, get_app_aliases, get_config_aliases } from './utils.js';
test('basic test no conflicts', async () => {
const merged = deep_merge(
{
version: 1,
animalSounds: {
cow: 'moo'
}
},
{
animalSounds: {
duck: 'quack'
},
locale: 'en_US'
}
);
assert.equal(merged, {
version: 1,
locale: 'en_US',
animalSounds: {
cow: 'moo',
duck: 'quack'
}
});
});
test('three way merge no conflicts', async () => {
const merged = deep_merge(
{
animalSounds: {
cow: 'moo'
}
},
{
animalSounds: {
duck: 'quack'
}
},
{
animalSounds: {
dog: {
singular: 'bark',
plural: 'barks'
}
}
}
);
assert.equal(merged, {
animalSounds: {
cow: 'moo',
duck: 'quack',
dog: {
singular: 'bark',
plural: 'barks'
}
}
});
});
test('merge with conflicts', async () => {
const merged = deep_merge(
{
person: {
firstName: 'John',
lastName: 'Doe',
address: {
line1: '123 Main St',
city: 'Seattle',
state: 'WA'
}
}
},
{
person: {
middleInitial: 'Q',
address: '123 Main St, Seattle, WA'
}
}
);
assert.equal(merged, {
person: {
firstName: 'John',
middleInitial: 'Q',
lastName: 'Doe',
address: '123 Main St, Seattle, WA'
}
});
});
test('merge with arrays', async () => {
const merged = deep_merge(
{
paths: ['/foo', '/bar']
},
{
paths: ['/alpha', '/beta']
}
);
assert.equal(merged, {
paths: ['/foo', '/bar', '/alpha', '/beta']
});
});
test('empty', async () => {
const merged = deep_merge();
assert.equal(merged, {});
});
test('mutability safety', () => {
const input1 = {
person: {
firstName: 'John',
lastName: 'Doe',
address: {
line1: '123 Main St',
city: 'Seattle'
}
}
};
const input2 = {
person: {
middleInitial: 'L',
lastName: 'Smith',
address: {
state: 'WA'
}
}
};
const snapshot1 = JSON.stringify(input1);
const snapshot2 = JSON.stringify(input2);
const merged = deep_merge(input1, input2);
// Mess with the result
merged.person.middleInitial = 'Z';
merged.person.address.zipCode = '98103';
merged.person = {};
// Make sure nothing in the inputs changed
assert.snapshot(snapshot1, JSON.stringify(input1));
assert.snapshot(snapshot2, JSON.stringify(input2));
});
test('merge buffer', () => {
const merged = deep_merge(
{
x: Buffer.from('foo', 'utf-8')
},
{
y: 12345
}
);
assert.equal(Object.keys(merged), ['x', 'y']);
});
test('merge including toString', () => {
const merged = deep_merge(
{
toString: () => '',
constructor: () => ''
},
{
y: 12345
}
);
assert.equal(Object.keys(merged), ['toString', 'constructor', 'y']);
});
test('transform kit.alias to resolve.alias', () => {
const config = validate_config({
kit: {
alias: {
simpleKey: 'simple/value',
key: 'value',
'key/*': 'value/*',
$regexChar: 'windows\\path',
'$regexChar/*': 'windows\\path\\*'
}
}
});
// combine aliases from config with generated and runtime aliases
const aliases = [...get_app_aliases(config.kit), ...get_config_aliases(config.kit)];
const transformed = aliases.map((entry) => {
const replacement = posixify(path.relative('.', entry.replacement));
return {
find: entry.find.toString(), // else assertion fails
replacement
};
});
assert.equal(transformed, [
{ find: '__GENERATED__', replacement: '.svelte-kit/generated' },
{ find: '$app', replacement: 'src/runtime/app' },
{ find: '$lib', replacement: 'src/lib' },
{ find: 'simpleKey', replacement: 'simple/value' },
{ find: /^key$/.toString(), replacement: 'value' },
{ find: /^key\/(.+)$/.toString(), replacement: 'value/$1' },
{ find: /^\$regexChar$/.toString(), replacement: 'windows/path' },
{ find: /^\$regexChar\/(.+)$/.toString(), replacement: 'windows/path/$1' }
]);
});
test.run();