UNPKG

3.98 kBJavaScriptView Raw
1import 'jest-localstorage-mock';
2import React from 'react';
3import useLoading, {
4 handleRequest,
5 handleResponse,
6 handleError,
7} from '.';
8
9jest.useFakeTimers();
10
11jest
12 .spyOn(React, 'useState')
13 .mockImplementation((v) => [v, jest.fn()]);
14
15jest
16 .spyOn(React, 'useEffect')
17 .mockImplementation((v) => v());
18
19jest
20 .spyOn(React, 'useCallback')
21 .mockImplementation((v) => v);
22
23beforeAll(() => {
24 Object.defineProperty(window.location, 'reload', {
25 writable: true,
26 });
27
28 window.location.reload = jest.fn();
29});
30
31describe('useLoading axios hook', () => {
32 describe('requestHandler', () => {
33 const req = {
34 headers: { common: {} },
35 baseURL: 'localhost',
36 url: '/foo',
37 };
38
39 it('should query localStorage API', () => {
40 handleRequest(req);
41 expect(localStorage.getItem).toHaveBeenCalledWith(
42 'localhost/foo',
43 );
44 });
45
46 it('should attach etag headers', () => {
47 localStorage.getItem.mockReturnValue(
48 JSON.stringify({
49 'Last-Modified': 2,
50 ETag: 1,
51 data: {},
52 }),
53 );
54
55 handleRequest(req);
56 expect(req.headers.common).toMatchObject({
57 'If-Match': 1,
58 'If-Unmodified-Since': 2,
59 });
60 });
61 });
62
63 describe('handleResponse', () => {
64 const res = {
65 data: { foo: 'bar' },
66 headers: { ETag: 1, 'Last-Modified': 2 },
67 config: { url: '/path' },
68 };
69
70 it.skip('should set localStorage', () => {
71 handleResponse(res).set();
72 expect(localStorage.setItem).toHaveBeenCalledWith(
73 '/path',
74 JSON.stringify({
75 data: { foo: 'bar' },
76 ETag: 1,
77 'Last-Modified': 2,
78 }),
79 );
80 });
81
82 it('should call onSuccess', () => {
83 const stub = { onSuccess: jest.fn() };
84 const message = 'Yup!';
85
86 handleResponse({
87 config: { method: 'POST' },
88 status: 200,
89 data: { message },
90 }).notify(stub);
91
92 expect(stub.onSuccess).toHaveBeenCalledWith('Yup!');
93 });
94
95 it('should not call onSuccess', () => {
96 const stub = { onSuccess: jest.fn() };
97
98 handleResponse({
99 config: { method: 'GET' },
100 status: 200,
101 data: { message: '' },
102 }).notify(stub);
103
104 expect(stub.onSuccess).not.toHaveBeenCalledWith();
105 });
106
107 it.skip('should call onFail', () => {
108 const stub = { onFail: jest.fn() };
109 const message = 'Nope!';
110
111 handleResponse({
112 config: { method: 'GET' },
113 status: 404,
114 data: { message },
115 }).notify(stub);
116
117 expect(stub.onFail).toHaveBeenCalledWith(message);
118 });
119 });
120
121 describe('handleError', () => {
122 it.skip('should call onFail on 412', () => {
123 const stub = { onFail: jest.fn() };
124
125 handleError({
126 response: {
127 config: {},
128 status: 412,
129 },
130 }).notify(stub);
131
132 expect(stub.onFail).toHaveBeenCalledWith(
133 expect.any(String),
134 );
135 });
136 });
137
138 it.skip('should resolve from cache', () => {
139 const stub = { stuff: true };
140 localStorage.getItem.mockReturnValue(
141 JSON.stringify(stub),
142 );
143
144 return expect(
145 handleError({
146 response: {
147 config: { method: 'get' },
148 status: 304,
149 },
150 }).refresh(),
151 ).resolves.toMatchObject(stub);
152 });
153
154 it.skip('should call refresh', async () => {
155 const stub = {
156 response: {
157 config: {},
158 status: 304,
159 },
160 };
161
162 localStorage.getItem.mockReturnValue(null);
163
164 await expect(
165 handleError(stub).refresh(),
166 ).rejects.toMatchObject(stub);
167 expect(setTimeout).toHaveBeenCalledTimes(1);
168 });
169
170 describe('useLoading default export', () => {
171 it.skip('should default to false', () =>
172 expect(useLoading()).toBeFalsy());
173 });
174});