UNPKG

2.78 kBJavaScriptView Raw
1import {
2 getFn,
3 isEmpty,
4 makePath,
5 acceptCsvFiletype,
6 formatUrlPath,
7} from '../helpers';
8
9jest.mock('js-file-download');
10
11jest.mock('axios', () => ({
12 get: jest
13 .fn()
14 .mockResolvedValueOnce({
15 data: {
16 values: ['Foo', 'Bar'],
17 },
18 })
19 .mockResolvedValue({
20 data: {
21 values: [
22 { id: '1', name: 'Foo' },
23 { id: '2', name: 'Bar' },
24 ],
25 },
26 }),
27}));
28
29describe('q3-ui-rest helpers', () => {
30 describe('getFn', () => {
31 it('should throw an error on unknown function', () =>
32 expect(() =>
33 getFn({ bar: jest.fn() }, 'foo'),
34 ).toThrowError());
35
36 it('should call method', () => {
37 const foo = jest.fn();
38 getFn({ foo }, 'foo');
39 expect(foo).toHaveBeenCalled();
40 });
41 });
42
43 describe('isEmpty', () => {
44 it('should return truthy', () =>
45 expect(isEmpty({})).toBeTruthy());
46
47 it('should return falsy', () =>
48 expect(isEmpty({ foo: 'bar' })).toBeFalsy());
49 });
50
51 describe('makePath', () => {
52 it('should return nested url', () =>
53 expect(makePath(['foo', 'bar/'])).toMatch(
54 '/foo/bar',
55 ));
56
57 it('should strip leading slashes', () =>
58 expect(makePath(['/foo', '/bar'])).toMatch(
59 '/foo/bar',
60 ));
61
62 it('should preserve internal slashes', () =>
63 expect(makePath(['/foo/bar', '/quux'])).toMatch(
64 '/foo/bar/quux',
65 ));
66 });
67
68 describe('acceptCsvFiletype', () => {
69 it('should assign "accept" header', () => {
70 const headers = {};
71 acceptCsvFiletype({})({}, headers);
72 expect(headers.Accept).toMatch('text/csv');
73 });
74 });
75
76 describe('"formatUrlPath"', () => {
77 it('should do nothing', () => {
78 const str = formatUrlPath('localhost', '');
79 expect(str).toMatch('localhost');
80 });
81
82 it('should append query string', () => {
83 const str = formatUrlPath('localhost', 'foo=bar');
84 expect(str).toMatch('localhost?foo=bar');
85 });
86
87 it('should add query string to existing query', () => {
88 const str = formatUrlPath(
89 'localhost?foo=bar',
90 'quuz=garply',
91 );
92 expect(str).toMatch('localhost?foo=bar&quuz=garply');
93 });
94
95 it('should add query string to existing query', () => {
96 const str = formatUrlPath(
97 'localhost?foo=bar',
98 'quuz=garply&thunk',
99 '1,2,3',
100 );
101
102 expect(str).toMatch(
103 'localhost?foo=bar&quuz=garply&thunk&fields=1,2,3',
104 );
105 });
106
107 it('should add projection without query', () => {
108 const str = formatUrlPath('localhost', '', '1,2,3');
109 expect(str).toMatch('localhost?fields=1,2,3');
110 });
111 });
112});