|
1 |
| -import jcurl from './url'; |
| 1 | +import { getParams, getParamsExtended, addParams, addParamsExtended } from './url'; |
2 | 2 |
|
3 | 3 | describe('GET', () => {
|
4 | 4 | test('getParams()', () => {
|
5 |
| - expect(jcurl.getParams('example.com')).toEqual({}); |
6 |
| - expect(jcurl.getParams('example.com?bar=1&foo')).toEqual({ bar: '1', foo: '' }); |
7 |
| - expect(jcurl.getParams('example.com?bar=1&bar=2')).toEqual({ bar: '2' }); |
8 |
| - expect(jcurl.getParams('example.com?bar[]=1&bar[]=2')).toEqual({ bar: ['1', '2'] }); |
9 |
| - expect(jcurl.getParams('example.com?bar=1&bar[]=2')).toEqual({ bar: ['2'] }); |
| 5 | + expect(getParams('example.com')).toEqual({}); |
| 6 | + expect(getParams('example.com?bar=1&foo')).toEqual({ bar: '1', foo: '' }); |
| 7 | + expect(getParams('example.com?bar=1&bar=2')).toEqual({ bar: '2' }); |
| 8 | + expect(getParams('example.com?bar[]=1&bar[]=2')).toEqual({ bar: ['1', '2'] }); |
| 9 | + expect(getParams('example.com?bar=1&bar[]=2')).toEqual({ bar: ['2'] }); |
10 | 10 | });
|
11 | 11 |
|
12 | 12 | test('getParamsExtended()', () => {
|
13 |
| - expect(jcurl.getParamsExtended('example.com?bar[t]=1&bar[j]=2')).toEqual({ bar: { t: '1', j: '2' } }); |
14 |
| - expect(jcurl.getParamsExtended('example.com?bar[t]=1&bar[j]=2&bar[j]=3')).toEqual({ bar: { t: '1', j: '3' } }); |
15 |
| - expect(jcurl.getParamsExtended('example.com?b[t]=1&b[j]=2&b[j][g]=3')) |
| 13 | + expect(getParamsExtended('example.com?bar[t]=1&bar[j]=2')).toEqual({ bar: { t: '1', j: '2' } }); |
| 14 | + expect(getParamsExtended('example.com?bar[t]=1&bar[j]=2&bar[j]=3')).toEqual({ bar: { t: '1', j: '3' } }); |
| 15 | + expect(getParamsExtended('example.com?b[t]=1&b[j]=2&b[j][g]=3')) |
16 | 16 | .toEqual({ b: { t: '1', j: { g: '3' } } });
|
17 |
| - expect(jcurl.getParamsExtended('example.com?bar=-1&bar[]=0&bar[tr]=1&bar[j]=2&bar[foo][too][poo]=3&bar[]=4&bar[foo][too][hoo]=5')) |
| 17 | + expect(getParamsExtended('example.com?bar=-1&bar[]=0&bar[tr]=1&bar[j]=2&bar[foo][too][poo]=3&bar[]=4&bar[foo][too][hoo]=5')) |
18 | 18 | .toEqual({ bar: { '1': '0', tr: '1', j: '2', foo: { too: { poo: '3', hoo: '5' } }, '5': '4' } });
|
19 | 19 | });
|
20 | 20 | });
|
21 | 21 |
|
22 | 22 | describe('ADD', () => {
|
23 | 23 | test('addParams()', () => {
|
24 |
| - expect(jcurl.addParams('example.com', { bar: 1 })).toBe('example.com?bar=1'); |
25 |
| - expect(jcurl.addParams('example.com', { bar: 1, foo: 2 })).toBe('example.com?bar=1&foo=2'); |
26 |
| - expect(jcurl.addParams('example.com?bar=1&foo', { bar: 2 })).toBe('example.com?bar=2&foo='); |
27 |
| - expect(jcurl.addParams('example.com?bar=1&foo', { bar: 2, foo: 2 })).toBe('example.com?bar=2&foo=2'); |
28 |
| - expect(jcurl.addParams('example.com?bar=1', { bar: [2, 3] })).toBe('example.com?bar[]=2&bar[]=3'); |
29 |
| - expect(jcurl.addParams('example.com?bar=1&bar[]=2', { bar: [3, 4] })).toBe('example.com?bar[]=2&bar[]=3&bar[]=4'); |
30 |
| - expect(jcurl.addParams('example.com?bar=1&bar=2', { bar: [3, 4] })).toBe('example.com?bar[]=3&bar[]=4'); |
31 |
| - expect(jcurl.addParams('example.com?bar[]=1&bar[]=2', { bar: [3, 4] })).toBe('example.com?bar[]=1&bar[]=2&bar[]=3&bar[]=4'); |
| 24 | + expect(addParams('example.com', { bar: 1 })).toBe('example.com?bar=1'); |
| 25 | + expect(addParams('example.com', { bar: 1, foo: 2 })).toBe('example.com?bar=1&foo=2'); |
| 26 | + expect(addParams('example.com?bar=1&foo', { bar: 2 })).toBe('example.com?bar=2&foo='); |
| 27 | + expect(addParams('example.com?bar=1&foo', { bar: 2, foo: 2 })).toBe('example.com?bar=2&foo=2'); |
| 28 | + expect(addParams('example.com?bar=1', { bar: [2, 3] })).toBe('example.com?bar[]=2&bar[]=3'); |
| 29 | + expect(addParams('example.com?bar=1&bar[]=2', { bar: [3, 4] })).toBe('example.com?bar[]=2&bar[]=3&bar[]=4'); |
| 30 | + expect(addParams('example.com?bar=1&bar=2', { bar: [3, 4] })).toBe('example.com?bar[]=3&bar[]=4'); |
| 31 | + expect(addParams('example.com?bar[]=1&bar[]=2', { bar: [3, 4] })).toBe('example.com?bar[]=1&bar[]=2&bar[]=3&bar[]=4'); |
32 | 32 | });
|
33 | 33 |
|
34 | 34 | test('addParamsExtended()', () => {
|
35 |
| - expect(jcurl.addParamsExtended('example.com', { bar: 1, foo: 2 })).toBe('example.com?bar=1&foo=2'); |
| 35 | + expect(addParamsExtended('example.com', { bar: 1, foo: 2 })).toBe('example.com?bar=1&foo=2'); |
36 | 36 | /**
|
37 | 37 | * BUG
|
38 | 38 | */
|
39 |
| - // expect(jcurl.addParamsExtended('example.com?bar[foo]=test&bar[joo]=2', { bar: { foo: 'new', too: 5 } })) |
| 39 | + // expect(addParamsExtended('example.com?bar[foo]=test&bar[joo]=2', { bar: { foo: 'new', too: 5 } })) |
40 | 40 | // .toBe('example.com?bar[foo]=new&bar[joo]=2&bar[too]=5');
|
41 |
| - expect(jcurl.addParamsExtended('example.com', { bar: { foo: 'test', joo: 2 } })) |
| 41 | + expect(addParamsExtended('example.com', { bar: { foo: 'test', joo: 2 } })) |
42 | 42 | .toBe('example.com?bar[foo]=test&bar[joo]=2');
|
43 |
| - expect(jcurl.addParamsExtended('example.com', { |
| 43 | + expect(addParamsExtended('example.com', { |
44 | 44 | f1: { f11: 1, f12: 2, f13: 3 }, f2: 4, f3: { f31: { f311: 6, f312: { f3121: 9 } }, f32: { f321: 2 } },
|
45 | 45 | f4: { f41: 3, f42: { f421: 2 } }, f5: [2, 4]
|
46 | 46 | })).toBe('example.com?f1[f11]=1&f1[f12]=2&f1[f13]=3&f2=4&f3[f31][f311]=6&f3[f31][f312][f3121]=9&f3[f32][f321]=2&f4[f41]=3&f4[f42][f421]=2&f5[0]=2&f5[1]=4');
|
|
0 commit comments