File size: 2,068 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { getNormalizedPath } from '..';

describe( 'getNormalizedPath', () => {
	test( 'should return expected results for a variety of inputs', () => {
		const pathnames = [ '/', '/shortpath', '/a/longer/path', '/specialchars/%2F%3F%26%3D%25' ];
		const queries = [
			{},
			{ a: 'query' },
			{ '?tricky=query': '' },
			{ key: '=tricky&value=;)' },
			{ emptyValue: '' },
			{ nullValue: null },
		];

		// Cartesian product: paths * queries
		pathnames.forEach( ( pathname ) =>
			queries.forEach( ( query ) =>
				expect( getNormalizedPath( pathname, query ) ).toMatchSnapshot()
			)
		);
	} );

	test( 'should return pathname for routes with no query', () => {
		const pathname = '/my/path';
		const query = {};

		expect( getNormalizedPath( pathname, query ) ).toBe( '/my/path' );
	} );

	test( 'should return normalized path for one query param', () => {
		const pathname = '/';
		const query = { cache_me: '1' };

		expect( getNormalizedPath( pathname, query ) ).toBe( '/?cache_me=1' );
	} );

	test( 'should return normalized pathname for multiple query params', () => {
		const pathname = '/';
		const query = { cache_me: '1', and_me: '2', me_too: '3' };

		expect( getNormalizedPath( pathname, query ) ).toBe( '/?and_me=2&cache_me=1&me_too=3' );
	} );

	test( 'should return a stable key pathname', () => {
		const pathname = '/';
		const query = { a: '1', b: '2' };
		const querySwapped = { b: '2', a: '1' };

		expect( getNormalizedPath( pathname, query ) ).toBe(
			getNormalizedPath( pathname, querySwapped )
		);
	} );

	test( 'should handle "empty" query params coherently', () => {
		expect( getNormalizedPath( '/', { empty: '' } ) ).toBe(
			getNormalizedPath( '/', { empty: null } )
		);
	} );

	test( 'should produce unique results', () => {
		expect( getNormalizedPath( '/', { key: 'val' } ) ).not.toBe(
			getNormalizedPath( '/', { 'key=val': '' } )
		);
	} );

	test( 'should consider paths with trailing slashes the same as paths without', () => {
		expect( getNormalizedPath( '/hello/' ) ).toBe( getNormalizedPath( '/hello' ) );
	} );
} );