-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathquerystring.test.ts
More file actions
81 lines (63 loc) · 2.78 KB
/
querystring.test.ts
File metadata and controls
81 lines (63 loc) · 2.78 KB
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
import { describe, expect, it } from 'vitest';
import { camelToSnake } from '../../../underscore';
import { getQueryParams, stringifyQueryParams } from '../querystring';
describe('getQueryParams(string)', () => {
it('parses an emtpy querystring', () => {
const res = getQueryParams('');
expect(res).toEqual({});
});
it('parses a querystring into a URLSearchParams instance', () => {
const res = getQueryParams('foo=42&bar=43');
expect(res).toEqual({ foo: '42', bar: '43' });
});
it('parses a querystring into a URLSearchParams instance even when prefixed with ?', () => {
const res = getQueryParams('?foo=42&bar=43');
expect(res).toEqual({ foo: '42', bar: '43' });
});
it('parses multiple occurances of the same key as an array', () => {
const res = getQueryParams('?foo=42&foo=43&bar=1');
expect(res).toEqual({ foo: ['42', '43'], bar: '1' });
});
});
describe('stringifyQueryParams(object)', () => {
it('handles null values', () => {
expect(stringifyQueryParams(null)).toBe('');
});
it('handles undefined values', () => {
expect(stringifyQueryParams(undefined)).toBe('');
});
it('handles string values', () => {
expect(stringifyQueryParams('hello')).toBe('');
});
it('handles empty string values', () => {
expect(stringifyQueryParams('')).toBe('');
});
it('converts an object to querystring', () => {
expect(stringifyQueryParams({ foo: '42', bar: '43' })).toBe('foo=42&bar=43');
});
it('converts an object to querystring when value is an array', () => {
expect(stringifyQueryParams({ foo: ['42', '22'], bar: '43' })).toBe('foo=42&foo=22&bar=43');
});
it('converts an object to querystring when value is undefined', () => {
expect(stringifyQueryParams({ foo: ['42', '22'], bar: undefined })).toBe('foo=42&foo=22');
});
it('converts an object to querystring when value is null', () => {
expect(stringifyQueryParams({ foo: null })).toBe('foo=');
});
it('converts an object to querystring when value is an object', () => {
expect(stringifyQueryParams({ unsafe_metadata: { bar: '1' } })).toBe('unsafe_metadata=%7B%22bar%22%3A%221%22%7D');
});
it('converts an object to querystring when value contains invalid url symbols', () => {
expect(stringifyQueryParams({ test: 'ena=duo' })).toBe('test=ena%3Dduo');
});
it('handles false value', () => {
expect(stringifyQueryParams({ test: false, boo: true })).toBe('test=false&boo=true');
});
it('converts an object to querystring when key is camelCase', () => {
expect(stringifyQueryParams({ barFoo: '1' }, { keyEncoder: camelToSnake })).toBe('bar_foo=1');
expect(stringifyQueryParams({ unsafeMetadata: { bar: '1' } }, { keyEncoder: camelToSnake })).toBe(
'unsafe_metadata=%7B%22bar%22%3A%221%22%7D',
);
});
});
//test=ena%3Dduo