-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.test.js
700 lines (608 loc) · 22.9 KB
/
index.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/**
* External dependencies
*/
import { every } from 'lodash';
/**
* Internal dependencies
*/
import {
isURL,
isEmail,
getProtocol,
isValidProtocol,
getAuthority,
isValidAuthority,
getPath,
isValidPath,
getQueryString,
isValidQueryString,
getFragment,
isValidFragment,
addQueryArgs,
getQueryArg,
hasQueryArg,
removeQueryArgs,
prependHTTP,
safeDecodeURI,
filterURLForDisplay,
cleanForSlug,
} from '../';
describe( 'isURL', () => {
it.each( [
[ 'http://wordpress.org' ],
[ 'https://wordpress.org' ],
[ 'HTTPS://WORDPRESS.ORG' ],
[ 'https://wordpress.org/./foo' ],
[ 'https://wordpress.org/path?query#fragment' ],
[ 'https://localhost/foo#bar' ],
[ 'https:///localhost/foo#bar' ],
[ 'mailto:[email protected]' ],
[ 'ssh://user:[email protected]:8080' ],
[ 'file:///localfolder/file.mov' ],
[ 'file:/localfolder/file.mov' ],
] )( 'valid (true): %s', ( url ) => {
expect( isURL( url ) ).toBe( true );
} );
it.each( [
[ 'http://word press.org' ],
[ 'http://wordpress.org:port' ],
[ 'http://[wordpress.org]/' ],
[ 'HTTP: HyperText Transfer Protocol' ],
[ 'URLs begin with a http:// prefix' ],
[ 'Go here: http://wordpress.org' ],
[ 'http://' ],
[ '' ],
] )( 'invalid (false): %s', ( url ) => {
expect( isURL( url ) ).toBe( false );
} );
} );
describe( 'isEmail', () => {
it( 'returns true when given things that look like an email', () => {
const emails = [
];
expect( every( emails, isEmail ) ).toBe( true );
} );
it( "returns false when given things that don't look like an email", () => {
const emails = [
'Abc.wordpress.org',
'A@b@[email protected]',
'a"b(c)d,e:f;g<h>i[jk][email protected]',
'just"not"[email protected]',
'this is"[email protected]',
'this still"not\\[email protected]',
'1234567890123456789012345678901234567890123456789012345678901234+x@wordpress.org',
];
expect( every( emails, isEmail ) ).toBe( false );
} );
} );
describe( 'getProtocol', () => {
it( 'returns the protocol part of a URL', () => {
expect(
getProtocol(
'http://user:[email protected]:1020/test-path/file.extension#anchor?query=params&more'
)
).toBe( 'http:' );
expect(
getProtocol(
'https://user:[email protected]:1020/test-path/file.extension#anchor?query=params&more'
)
).toBe( 'https:' );
expect( getProtocol( 'https://wordpress.org#test' ) ).toBe( 'https:' );
expect( getProtocol( 'https://wordpress.org/' ) ).toBe( 'https:' );
expect( getProtocol( 'https://wordpress.org?test' ) ).toBe( 'https:' );
expect( getProtocol( 'https://localhost:8080' ) ).toBe( 'https:' );
expect( getProtocol( 'tel:1234' ) ).toBe( 'tel:' );
expect( getProtocol( 'blob:data' ) ).toBe( 'blob:' );
} );
it( 'returns undefined when the provided value does not contain a URL protocol', () => {
expect( getProtocol( '' ) ).toBeUndefined();
expect( getProtocol( 'https' ) ).toBeUndefined();
expect( getProtocol( 'test.com' ) ).toBeUndefined();
expect( getProtocol( ' https:// ' ) ).toBeUndefined();
} );
} );
describe( 'isValidProtocol', () => {
it( 'returns true if the protocol is valid', () => {
expect( isValidProtocol( 'tel:' ) ).toBe( true );
expect( isValidProtocol( 'http:' ) ).toBe( true );
expect( isValidProtocol( 'https:' ) ).toBe( true );
expect( isValidProtocol( 'file:' ) ).toBe( true );
expect( isValidProtocol( 'test.protocol:' ) ).toBe( true );
expect( isValidProtocol( 'test-protocol:' ) ).toBe( true );
expect( isValidProtocol( 'test+protocol:' ) ).toBe( true );
expect( isValidProtocol( 'test+protocol123:' ) ).toBe( true );
} );
it( 'returns false if the protocol is invalid', () => {
expect( isValidProtocol() ).toBe( false );
expect( isValidProtocol( '' ) ).toBe( false );
expect( isValidProtocol( ' http: ' ) ).toBe( false );
expect( isValidProtocol( 'http :' ) ).toBe( false );
expect( isValidProtocol( 'http: //' ) ).toBe( false );
expect( isValidProtocol( 'test protocol:' ) ).toBe( false );
expect( isValidProtocol( 'test#protocol:' ) ).toBe( false );
expect( isValidProtocol( 'test?protocol:' ) ).toBe( false );
expect( isValidProtocol( '123test+protocol:' ) ).toBe( false );
} );
} );
describe( 'getAuthority', () => {
it( 'returns the authority part of a URL', () => {
expect(
getAuthority(
'https://user:[email protected]:1020/test-path/file.extension#anchor?query=params&more'
)
).toBe( 'user:[email protected]:1020' );
expect(
getAuthority(
'http://user:[email protected]:1020/test-path/file.extension#anchor?query=params&more'
)
).toBe( 'user:[email protected]:1020' );
expect( getAuthority( 'https://wordpress.org#test' ) ).toBe(
'wordpress.org'
);
expect( getAuthority( 'https://wordpress.org/' ) ).toBe(
'wordpress.org'
);
expect( getAuthority( 'https://wordpress.org?test' ) ).toBe(
'wordpress.org'
);
expect( getAuthority( 'https://localhost:8080' ) ).toBe(
'localhost:8080'
);
} );
it( 'returns undefined when the provided value does not contain a URL authority', () => {
expect( getAuthority( '' ) ).toBeUndefined();
expect( getAuthority( 'https://' ) ).toBeUndefined();
expect( getAuthority( 'https:///' ) ).toBeUndefined();
expect( getAuthority( 'https://#' ) ).toBeUndefined();
expect( getAuthority( 'https://?' ) ).toBeUndefined();
expect( getAuthority( 'test.com' ) ).toBeUndefined();
expect( getAuthority( 'https://#?hello' ) ).toBeUndefined();
} );
} );
describe( 'isValidAuthority', () => {
it( 'returns true if the authority is valid', () => {
expect(
isValidAuthority( 'user:[email protected]:1020' )
).toBe( true );
expect( isValidAuthority( 'wordpress.org' ) ).toBe( true );
expect( isValidAuthority( 'localhost' ) ).toBe( true );
expect( isValidAuthority( 'localhost:8080' ) ).toBe( true );
expect( isValidAuthority( 'www.the-best-website.co.uk' ) ).toBe( true );
expect( isValidAuthority( 'WWW.VERYLOUD.COM' ) ).toBe( true );
} );
it( 'returns false if the authority is invalid', () => {
expect( isValidAuthority() ).toBe( false );
expect( isValidAuthority( '' ) ).toBe( false );
expect( isValidAuthority( 'inv alid.website.com' ) ).toBe( false );
expect( isValidAuthority( 'test#.com' ) ).toBe( false );
expect( isValidAuthority( 'test?.com' ) ).toBe( false );
} );
} );
describe( 'getPath', () => {
it( 'returns the path part of a URL', () => {
expect(
getPath(
'https://user:[email protected]:1020/test-path/file.extension#anchor?query=params&more'
)
).toBe( 'test-path/file.extension' );
expect(
getPath(
'http://user:[email protected]:1020/test-path/file.extension#anchor?query=params&more'
)
).toBe( 'test-path/file.extension' );
expect( getPath( 'https://wordpress.org/test-path#anchor' ) ).toBe(
'test-path'
);
expect( getPath( 'https://wordpress.org/test-path?query' ) ).toBe(
'test-path'
);
expect(
getPath(
'https://www.google.com/search?source=hp&ei=tP7kW8-_FoK89QORoa2QBQ&q=test+url&oq=test+url&gs_l=psy-ab.3..0l10'
)
).toBe( 'search' );
expect( getPath( 'https://wordpress.org/this%20is%20a%20test' ) ).toBe(
'this%20is%20a%20test'
);
expect(
getPath( 'https://wordpress.org/this%20is%20a%20test?query' )
).toBe( 'this%20is%20a%20test' );
} );
it( 'returns undefined when the provided value does not contain a URL path', () => {
expect( getPath() ).toBeUndefined();
expect( getPath( '' ) ).toBeUndefined();
expect( getPath( 'https://wordpress.org#test' ) ).toBeUndefined();
expect( getPath( 'https://wordpress.org/' ) ).toBeUndefined();
expect( getPath( 'https://wordpress.org?test' ) ).toBeUndefined();
expect( getPath( 'https://localhost:8080' ) ).toBeUndefined();
expect( getPath( 'https://' ) ).toBeUndefined();
expect( getPath( 'https:///test' ) ).toBeUndefined();
expect( getPath( 'https://#' ) ).toBeUndefined();
expect( getPath( 'https://?' ) ).toBeUndefined();
expect( getPath( 'test.com' ) ).toBeUndefined();
expect( getPath( 'https://#?hello' ) ).toBeUndefined();
expect( getPath( 'https' ) ).toBeUndefined();
} );
} );
describe( 'isValidPath', () => {
it( 'returns true if the path is valid', () => {
expect( isValidPath( 'test-path/file.extension' ) ).toBe( true );
expect( isValidPath( '/absolute/path' ) ).toBe( true );
expect( isValidPath( 'relative/path' ) ).toBe( true );
expect( isValidPath( 'slightly/longer/path/' ) ).toBe( true );
expect( isValidPath( 'path/with/percent%20encoding' ) ).toBe( true );
} );
it( 'returns false if the path is invalid', () => {
expect( isValidPath() ).toBe( false );
expect( isValidPath( '' ) ).toBe( false );
expect( isValidPath( 'path /with/spaces' ) ).toBe( false );
expect( isValidPath( 'path/with/number/symbol#' ) ).toBe( false );
expect( isValidPath( 'path/with/question/mark?' ) ).toBe( false );
expect( isValidPath( ' path/with/padding ' ) ).toBe( false );
} );
} );
describe( 'getQueryString', () => {
it( 'returns the query string of a URL', () => {
expect(
getQueryString(
'https://user:[email protected]:1020/test-path/file.extension#anchor?query=params&more'
)
).toBe( 'query=params&more' );
expect(
getQueryString(
'http://user:[email protected]:1020/test-path/file.extension?query=params&more#anchor'
)
).toBe( 'query=params&more' );
expect(
getQueryString( 'https://wordpress.org/test-path?query' )
).toBe( 'query' );
expect(
getQueryString(
'https://www.google.com/search?source=hp&ei=tP7kW8-_FoK89QORoa2QBQ&q=test+url&oq=test+url&gs_l=psy-ab.3..0l10'
)
).toBe(
'source=hp&ei=tP7kW8-_FoK89QORoa2QBQ&q=test+url&oq=test+url&gs_l=psy-ab.3..0l10'
);
expect(
getQueryString( 'https://wordpress.org/this%20is%20a%20test?query' )
).toBe( 'query' );
expect(
getQueryString(
'https://wordpress.org/test?query=something%20with%20spaces'
)
).toBe( 'query=something%20with%20spaces' );
expect(
getQueryString(
'https://andalouses.example/beach?foo[]=bar&foo[]=baz'
)
).toBe( 'foo[]=bar&foo[]=baz' );
expect( getQueryString( 'test.com?foo[]=bar&foo[]=baz' ) ).toBe(
'foo[]=bar&foo[]=baz'
);
expect( getQueryString( 'test.com?foo=bar&foo=baz?test' ) ).toBe(
'foo=bar&foo=baz?test'
);
} );
it( 'returns undefined when the provided does not contain a url query string', () => {
expect( getQueryString( '' ) ).toBeUndefined();
expect(
getQueryString( 'https://wordpress.org/test-path#anchor' )
).toBeUndefined();
expect(
getQueryString( 'https://wordpress.org/this%20is%20a%20test' )
).toBeUndefined();
expect(
getQueryString( 'https://wordpress.org#test' )
).toBeUndefined();
expect( getQueryString( 'https://wordpress.org/' ) ).toBeUndefined();
expect( getQueryString( 'https://localhost:8080' ) ).toBeUndefined();
expect( getQueryString( 'https://' ) ).toBeUndefined();
expect( getQueryString( 'https:///test' ) ).toBeUndefined();
expect( getQueryString( 'https://#' ) ).toBeUndefined();
expect( getQueryString( 'https://?' ) ).toBeUndefined();
expect( getQueryString( 'test.com' ) ).toBeUndefined();
} );
} );
describe( 'isValidQueryString', () => {
it( 'returns true if the query string is valid', () => {
expect( isValidQueryString( 'test' ) ).toBe( true );
expect( isValidQueryString( 'test=true' ) ).toBe( true );
expect( isValidQueryString( 'test=true&another' ) ).toBe( true );
expect( isValidQueryString( 'test=true&another=false' ) ).toBe( true );
expect( isValidQueryString( 'test[]=true&another[]=false' ) ).toBe(
true
);
expect( isValidQueryString( 'query=something%20with%20spaces' ) ).toBe(
true
);
expect(
isValidQueryString(
'source=hp&ei=tP7kW8-_FoK89QORoa2QBQ&q=test+url&oq=test+url&gs_l=psy-ab.3..0l10'
)
).toBe( true );
} );
it( 'returns false if the query string is invalid', () => {
expect( isValidQueryString() ).toBe( false );
expect( isValidQueryString( '' ) ).toBe( false );
expect( isValidQueryString( '?test=false' ) ).toBe( false );
expect( isValidQueryString( ' test=false ' ) ).toBe( false );
expect( isValidQueryString( 'test = false' ) ).toBe( false );
expect( isValidQueryString( 'test=f?alse' ) ).toBe( false );
expect( isValidQueryString( 'test=f#alse' ) ).toBe( false );
expect( isValidQueryString( 'test=f/alse' ) ).toBe( false );
expect( isValidQueryString( 'test=f?alse' ) ).toBe( false );
expect( isValidQueryString( '/test=false' ) ).toBe( false );
expect( isValidQueryString( 'test=false/' ) ).toBe( false );
} );
} );
describe( 'getFragment', () => {
it( 'returns the fragment of a URL', () => {
expect(
getFragment(
'https://user:[email protected]:1020/test-path/file.extension#fragment?query=params&more'
)
).toBe( '#fragment' );
expect(
getFragment(
'http://user:[email protected]:1020/test-path/file.extension?query=params&more#fragment'
)
).toBe( '#fragment' );
expect( getFragment( 'relative/url/#fragment' ) ).toBe( '#fragment' );
expect( getFragment( '/absolute/url/#fragment' ) ).toBe( '#fragment' );
} );
it( 'returns undefined when the provided does not contain a url fragment', () => {
expect( getFragment( '' ) ).toBeUndefined();
expect(
getFragment( 'https://wordpress.org/test-path?query' )
).toBeUndefined();
expect(
getFragment( 'https://wordpress.org/test-path' )
).toBeUndefined();
expect(
getFragment( 'https://wordpress.org/this%20is%20a%20test' )
).toBeUndefined();
expect(
getFragment(
'https://www.google.com/search?source=hp&ei=tP7kW8-_FoK89QORoa2QBQ&q=test+url&oq=test+url&gs_l=psy-ab.3..0l10'
)
).toBeUndefined();
expect( getFragment( 'https://wordpress.org' ) ).toBeUndefined();
expect( getFragment( 'https://localhost:8080' ) ).toBeUndefined();
expect( getFragment( 'https://' ) ).toBeUndefined();
expect( getFragment( 'https:///test' ) ).toBeUndefined();
expect( getFragment( 'https://?' ) ).toBeUndefined();
expect( getFragment( 'test.com' ) ).toBeUndefined();
} );
} );
describe( 'isValidFragment', () => {
it( 'returns true if the fragment is valid', () => {
expect( isValidFragment( '#' ) ).toBe( true );
expect( isValidFragment( '#yesitis' ) ).toBe( true );
expect( isValidFragment( '#yes_it_is' ) ).toBe( true );
expect( isValidFragment( '#yes~it~is' ) ).toBe( true );
expect( isValidFragment( '#yes-it-is' ) ).toBe( true );
} );
it( 'returns false if the fragment is invalid', () => {
expect( isValidFragment( '' ) ).toBe( false );
expect( isValidFragment( ' #no-it-isnt ' ) ).toBe( false );
expect( isValidFragment( '#no-it-isnt#' ) ).toBe( false );
expect( isValidFragment( '#no-it-#isnt' ) ).toBe( false );
expect( isValidFragment( '#no-it-isnt?' ) ).toBe( false );
expect( isValidFragment( '#no-it isnt' ) ).toBe( false );
expect( isValidFragment( '/#no-it-isnt' ) ).toBe( false );
expect( isValidFragment( '#no-it-isnt/' ) ).toBe( false );
} );
} );
describe( 'addQueryArgs', () => {
it( 'should append args to a URL without query string', () => {
const url = 'https://andalouses.example/beach';
const args = { sun: 'true', sand: 'false' };
expect( addQueryArgs( url, args ) ).toBe(
'https://andalouses.example/beach?sun=true&sand=false'
);
} );
it( 'should append args to a URL with query string', () => {
const url = 'https://andalouses.example/beach?night=false';
const args = { sun: 'true', sand: 'false' };
expect( addQueryArgs( url, args ) ).toBe(
'https://andalouses.example/beach?night=false&sun=true&sand=false'
);
} );
it( 'should update args to an URL with conflicting query string', () => {
const url =
'https://andalouses.example/beach?night=false&sun=false&sand=true';
const args = { sun: 'true', sand: 'false' };
expect( addQueryArgs( url, args ) ).toBe(
'https://andalouses.example/beach?night=false&sun=true&sand=false'
);
} );
it( 'should update args to an URL with array parameters', () => {
const url = 'https://andalouses.example/beach?time[]=10&time[]=11';
const args = { beach: [ 'sand', 'rock' ] };
expect( safeDecodeURI( addQueryArgs( url, args ) ) ).toBe(
'https://andalouses.example/beach?time[0]=10&time[1]=11&beach[0]=sand&beach[1]=rock'
);
} );
it( 'should disregard keys with undefined values', () => {
const url = 'https://andalouses.example/beach';
const args = { sun: 'true', sand: undefined };
expect( addQueryArgs( url, args ) ).toBe(
'https://andalouses.example/beach?sun=true'
);
} );
it( 'should encodes spaces by RFC 3986', () => {
const url = 'https://andalouses.example/beach';
const args = { activity: 'fun in the sun' };
expect( addQueryArgs( url, args ) ).toBe(
'https://andalouses.example/beach?activity=fun%20in%20the%20sun'
);
} );
it( 'should return only querystring when passed undefined url', () => {
const url = undefined;
const args = { sun: 'true' };
expect( addQueryArgs( url, args ) ).toBe( '?sun=true' );
} );
it( 'should return URL argument unaffected if no query arguments to append', () => {
[ '', 'https://example.com', 'https://example.com?' ].forEach(
( url ) => {
[ undefined, {} ].forEach( ( args ) => {
expect( addQueryArgs( url, args ) ).toBe( url );
} );
}
);
} );
} );
describe( 'getQueryArg', () => {
it( 'should get the value of an existing query arg', () => {
const url = 'https://andalouses.example/beach?foo=bar&bar=baz';
expect( getQueryArg( url, 'foo' ) ).toBe( 'bar' );
} );
it( 'should not return a value of an unknown query arg', () => {
const url = 'https://andalouses.example/beach?foo=bar&bar=baz';
expect( getQueryArg( url, 'baz' ) ).toBeUndefined();
} );
it( 'should get the value of an arry query arg', () => {
const url = 'https://andalouses.example/beach?foo[]=bar&foo[]=baz';
expect( getQueryArg( url, 'foo' ) ).toEqual( [ 'bar', 'baz' ] );
} );
it( 'continues to work when an anchor follows the query string', () => {
const url = 'https://andalouses.example/beach?foo=bar&bar=baz#foo';
expect( getQueryArg( url, 'foo' ) ).toEqual( 'bar' );
} );
} );
describe( 'hasQueryArg', () => {
it( 'should return true for an existing query arg', () => {
const url = 'https://andalouses.example/beach?foo=bar&bar=baz';
expect( hasQueryArg( url, 'foo' ) ).toBeTruthy();
} );
it( 'should return false for an unknown query arg', () => {
const url = 'https://andalouses.example/beach?foo=bar&bar=baz';
expect( hasQueryArg( url, 'baz' ) ).toBeFalsy();
} );
it( 'should return true for an arry query arg', () => {
const url = 'https://andalouses.example/beach?foo[]=bar&foo[]=baz';
expect( hasQueryArg( url, 'foo' ) ).toBeTruthy();
} );
} );
describe( 'removeQueryArgs', () => {
it( 'should not change URL not containing query args', () => {
const url = 'https://andalouses.example/beach?foo=bar&bar=baz';
expect( removeQueryArgs( url, 'baz', 'test' ) ).toEqual( url );
} );
it( 'should remove existing query args', () => {
const url = 'https://andalouses.example/beach?foo=bar&baz=foo&bar=baz';
expect( removeQueryArgs( url, 'foo', 'bar' ) ).toEqual(
'https://andalouses.example/beach?baz=foo'
);
} );
it( 'should remove array query arg', () => {
const url =
'https://andalouses.example/beach?foo[]=bar&foo[]=baz&bar=foobar';
expect( removeQueryArgs( url, 'foo' ) ).toEqual(
'https://andalouses.example/beach?bar=foobar'
);
} );
} );
describe( 'prependHTTP', () => {
it( 'should prepend http to a domain', () => {
const url = 'wordpress.org';
expect( prependHTTP( url ) ).toBe( 'http://' + url );
} );
it( 'shouldn’t prepend http to an email', () => {
const url = '[email protected]';
expect( prependHTTP( url ) ).toBe( url );
} );
it( 'shouldn’t prepend http to an absolute URL', () => {
const url = '/wordpress';
expect( prependHTTP( url ) ).toBe( url );
} );
it( 'shouldn’t prepend http to a relative URL', () => {
const url = './wordpress';
expect( prependHTTP( url ) ).toBe( url );
} );
it( 'shouldn’t prepend http to an anchor URL', () => {
const url = '#wordpress';
expect( prependHTTP( url ) ).toBe( url );
} );
it( 'shouldn’t prepend http to a URL that already has http', () => {
const url = 'http://wordpress.org';
expect( prependHTTP( url ) ).toBe( url );
} );
it( 'shouldn’t prepend http to a URL that already has https', () => {
const url = 'https://wordpress.org';
expect( prependHTTP( url ) ).toBe( url );
} );
it( 'shouldn’t prepend http to a URL that already has ftp', () => {
const url = 'ftp://wordpress.org';
expect( prependHTTP( url ) ).toBe( url );
} );
it( 'shouldn’t prepend http to a URL that already has mailto', () => {
const url = 'mailto:[email protected]';
expect( prependHTTP( url ) ).toBe( url );
} );
it( 'should remove leading whitespace before prepending HTTP', () => {
const url = ' wordpress.org';
expect( prependHTTP( url ) ).toBe( 'http://wordpress.org' );
} );
it( 'should not have trailing whitespaces', () => {
const url = 'wordpress.org ';
expect( prependHTTP( url ) ).toBe( 'http://wordpress.org' );
} );
} );
describe( 'safeDecodeURI', () => {
it( 'should decode URI if formed well', () => {
const encoded = 'https://mozilla.org/?x=%D1%88%D0%B5%D0%BB%D0%BB%D1%8B';
const decoded = 'https://mozilla.org/?x=шеллы';
expect( safeDecodeURI( encoded ) ).toBe( decoded );
} );
it( 'should return URI if malformed', () => {
const malformed = '%1';
expect( safeDecodeURI( malformed ) ).toBe( malformed );
} );
} );
describe( 'filterURLForDisplay', () => {
it( 'should remove protocol', () => {
let url = filterURLForDisplay( 'http://wordpress.org' );
expect( url ).toBe( 'wordpress.org' );
url = filterURLForDisplay( 'https://wordpress.org' );
expect( url ).toBe( 'wordpress.org' );
} );
it( 'should remove www subdomain', () => {
const url = filterURLForDisplay( 'http://www.wordpress.org' );
expect( url ).toBe( 'wordpress.org' );
} );
it( 'should remove single trailing slash', () => {
const url = filterURLForDisplay( 'http://www.wordpress.org/' );
expect( url ).toBe( 'wordpress.org' );
} );
it( 'should preserve slashes where the url has multiple in the path', () => {
const url = filterURLForDisplay(
'http://www.wordpress.org/something/'
);
expect( url ).toBe( 'wordpress.org/something/' );
} );
it( 'should preserve slash where the url has path after the initial slash', () => {
const url = filterURLForDisplay( 'http://www.wordpress.org/something' );
expect( url ).toBe( 'wordpress.org/something' );
} );
} );
describe( 'cleanForSlug', () => {
it( 'should return string prepared for use as url slug', () => {
expect( cleanForSlug( ' /Déjà_vu. ' ) ).toBe( 'deja-vu' );
} );
it( 'should return an empty string for missing argument', () => {
expect( cleanForSlug() ).toBe( '' );
} );
it( 'should return an empty string for falsy argument', () => {
expect( cleanForSlug( null ) ).toBe( '' );
} );
} );