-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutils.test.js
210 lines (172 loc) · 5.22 KB
/
utils.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
import { waitFor } from '@testing-library/react';
import {
modifyObjectKeys,
camelCaseObject,
snakeCaseObject,
convertKeyNames,
parseURL,
getPath,
getQueryParameters,
} from '.';
describe('modifyObjectKeys', () => {
it('should use the provided modify function to change all keys in and object and its children', () => {
function meowKeys(key) {
return `${key}Meow`;
}
const result = modifyObjectKeys(
{
one: undefined,
two: null,
three: '',
four: 0,
five: NaN,
six: [1, 2, { seven: 'woof' }],
eight: { nine: { ten: 'bark' }, eleven: true },
},
meowKeys,
);
expect(result).toEqual({
oneMeow: undefined,
twoMeow: null,
threeMeow: '',
fourMeow: 0,
fiveMeow: NaN,
sixMeow: [1, 2, { sevenMeow: 'woof' }],
eightMeow: { nineMeow: { tenMeow: 'bark' }, elevenMeow: true },
});
});
});
describe('camelCaseObject', () => {
it('should make everything camelCase', () => {
const result = camelCaseObject({
what_now: 'brown cow',
but_who: { says_you_people: 'okay then', but_how: { will_we_even_know: 'the song is over' } },
'dot.dot.dot': 123,
});
expect(result).toEqual({
whatNow: 'brown cow',
butWho: { saysYouPeople: 'okay then', butHow: { willWeEvenKnow: 'the song is over' } },
dotDotDot: 123,
});
});
});
describe('snakeCaseObject', () => {
it('should make everything snake_case', () => {
const result = snakeCaseObject({
whatNow: 'brown cow',
butWho: { saysYouPeople: 'okay then', butHow: { willWeEvenKnow: 'the song is over' } },
'dot.dot.dot': 123,
});
expect(result).toEqual({
what_now: 'brown cow',
but_who: { says_you_people: 'okay then', but_how: { will_we_even_know: 'the song is over' } },
dot_dot_dot: 123,
});
});
});
describe('convertKeyNames', () => {
it('should replace the specified keynames', () => {
const result = convertKeyNames(
{
one: { two: { three: 'four' } },
five: 'six',
},
{
two: 'blue',
five: 'alive',
seven: 'heaven',
},
);
expect(result).toEqual({
one: { blue: { three: 'four' } },
alive: 'six',
});
});
});
describe('getQueryParameters', () => {
it('should use window location by default', () => {
expect(global.location.search).toEqual('');
expect(getQueryParameters()).toEqual({});
});
it('should make an empty object with no query string', () => {
expect(getQueryParameters('')).toEqual({});
});
it('should make an object with one key value pair', () => {
expect(getQueryParameters('?foo=bar')).toEqual({
foo: 'bar',
});
});
it('should make an object with one key value pair', () => {
expect(getQueryParameters('?foo=bar&baz=1')).toEqual({
foo: 'bar',
baz: '1',
});
});
});
describe('ParseURL', () => {
const testURL = 'http://example.com:3000/pathname/?search=test#hash';
const parsedURL = parseURL(testURL);
let originalDocument;
beforeEach(() => {
originalDocument = global.document;
});
afterEach(() => {
global.document = originalDocument;
});
it('String URL is correctly parsed', () => {
expect(parsedURL.toString()).toEqual(testURL);
expect(parsedURL.href).toEqual(testURL);
expect(typeof (parsedURL)).toEqual('object');
});
it('should return protocol from URL', () => {
expect(parsedURL.protocol).toEqual('http:');
});
it('should return hostname from URL', () => {
expect(parsedURL.hostname).toEqual('example.com');
});
it('should return port from URL', () => {
expect(parsedURL.port).toEqual('3000');
});
it('should return pathname from URL', () => {
expect(parsedURL.pathname).toEqual('/pathname/');
});
it('should return search rom URL', () => {
expect(parsedURL.search).toEqual('?search=test');
});
it('should return hash from URL', () => {
expect(parsedURL.hash).toEqual('#hash');
});
it('should return host from URL', () => {
expect(parsedURL.host).toEqual('example.com:3000');
});
it('should return empty object in case of document being undefined', () => {
global.document = undefined;
waitFor(() => { expect(parseURL(testURL)).toEqual({}); });
});
});
describe('getPath', () => {
it('Path is retrieved with full url', () => {
const testURL = 'http://example.com:3000/pathname/?search=test#hash';
expect(getPath(testURL)).toEqual('/pathname/');
});
it('Path is retrieved with only path', () => {
const testURL = '/learning/';
expect(getPath(testURL)).toEqual('/learning/');
});
it('Path is retrieved without protocol', () => {
const testURL = '//example.com:3000/accounts/';
expect(getPath(testURL)).toEqual('/accounts/');
});
it('Path is retrieved with base `/`', () => {
const testURL = '/';
expect(getPath(testURL)).toEqual('/');
});
it('Path is retrieved without port', () => {
const testURL = 'https://example.com/accounts/';
expect(getPath(testURL)).toEqual('/accounts/');
});
it('Path is retrieved without CDN shape', () => {
const testURL = 'https://d20blt6w1kfasr.cloudfront.net/learning/';
expect(getPath(testURL)).toEqual('/learning/');
});
});