|
| 1 | +import { Response } from 'node-fetch'; |
| 2 | + |
| 3 | +import { createUrlWithQuery } from './lib'; |
| 4 | +import { createFakeErrorPayload } from './createRequest'; |
| 5 | +import Api from './Api'; |
| 6 | +import { Method } from './types'; |
| 7 | + |
| 8 | +const API_URL_CORRECT = 'http://rock.prezly.test/api/v1/contacts'; |
| 9 | +const API_URL_INCORRECT = 'htp:/rock.prezly.test/api/v1/contacts'; |
| 10 | + |
| 11 | +const DEFAULT_REQUEST_PROPS = { |
| 12 | + body: undefined, |
| 13 | + headers: { |
| 14 | + Accept: 'application/json', |
| 15 | + 'Content-Type': 'application/json;charset=utf-8', |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +function successJsonResponse(body: object) { |
| 20 | + return new Response(JSON.stringify(body), { |
| 21 | + status: 200, |
| 22 | + statusText: 'OK', |
| 23 | + headers: { |
| 24 | + 'Content-Type': 'application/json', |
| 25 | + }, |
| 26 | + }); |
| 27 | +} |
| 28 | + |
| 29 | +function errorJSONResponse(body: object) { |
| 30 | + return new Response(JSON.stringify(body), { |
| 31 | + status: 500, |
| 32 | + statusText: 'Internal Server Error', |
| 33 | + headers: { |
| 34 | + 'Content-Type': 'application/json', |
| 35 | + }, |
| 36 | + }); |
| 37 | +} |
| 38 | + |
| 39 | +describe('Api', () => { |
| 40 | + it('should resolve with correct payload', async () => { |
| 41 | + const expectedPayload = { |
| 42 | + foo: 'bar', |
| 43 | + }; |
| 44 | + |
| 45 | + const expectedResponse = successJsonResponse(expectedPayload); |
| 46 | + global.fetch.mockResolvedValueOnce(expectedResponse); |
| 47 | + |
| 48 | + const actualResponse = await Api.get(API_URL_CORRECT); |
| 49 | + |
| 50 | + expect(actualResponse.status).toEqual(200); |
| 51 | + expect(actualResponse.payload).toEqual(expectedPayload); |
| 52 | + }); |
| 53 | + |
| 54 | + it('should reject with correct payload', async () => { |
| 55 | + const expectedPayload = { |
| 56 | + foo: 'bar', |
| 57 | + }; |
| 58 | + |
| 59 | + const expectedResponse = errorJSONResponse(expectedPayload); |
| 60 | + global.fetch.mockResolvedValueOnce(expectedResponse); |
| 61 | + |
| 62 | + try { |
| 63 | + await Api.get(API_URL_CORRECT); |
| 64 | + } catch ({ status, payload }) { |
| 65 | + expect(status).toEqual(500); |
| 66 | + expect(payload).toEqual(expectedPayload); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + it('should reject with Invalid URL provided', async () => { |
| 71 | + const errorMessage = 'Invalid URL provided'; |
| 72 | + // Fetch mock doesn't validate the URL so we mock the error. |
| 73 | + global.fetch.mockRejectOnce(new Error(errorMessage)); |
| 74 | + try { |
| 75 | + await Api.get(API_URL_INCORRECT); |
| 76 | + } catch ({ payload }) { |
| 77 | + const expectedErrorResponse = createFakeErrorPayload({ |
| 78 | + status: undefined, |
| 79 | + statusText: errorMessage, |
| 80 | + }); |
| 81 | + |
| 82 | + expect(payload).toEqual(expectedErrorResponse); |
| 83 | + } |
| 84 | + }); |
| 85 | + |
| 86 | + it('should create a GET request', async () => { |
| 87 | + const response = successJsonResponse({}); |
| 88 | + |
| 89 | + global.fetch.mockResolvedValueOnce(response); |
| 90 | + |
| 91 | + await Api.get(API_URL_CORRECT); |
| 92 | + |
| 93 | + const expectedUrl = createUrlWithQuery(API_URL_CORRECT); |
| 94 | + |
| 95 | + expect(global.fetch).toHaveBeenCalledWith(expectedUrl, { |
| 96 | + ...DEFAULT_REQUEST_PROPS, |
| 97 | + method: Method.GET, |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should create a GET request with query params', async () => { |
| 102 | + const response = successJsonResponse({}); |
| 103 | + global.fetch.mockResolvedValueOnce(response); |
| 104 | + |
| 105 | + const query = { foo: 'bar' }; |
| 106 | + await Api.get(API_URL_CORRECT, { query }); |
| 107 | + |
| 108 | + const expectedUrl = createUrlWithQuery(API_URL_CORRECT, query); |
| 109 | + |
| 110 | + expect(global.fetch).toHaveBeenCalledWith(expectedUrl, { |
| 111 | + ...DEFAULT_REQUEST_PROPS, |
| 112 | + method: Method.GET, |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should create a POST request', async () => { |
| 117 | + const response = successJsonResponse({}); |
| 118 | + global.fetch.mockResolvedValueOnce(response); |
| 119 | + |
| 120 | + const query = { |
| 121 | + foo: 'bar', |
| 122 | + }; |
| 123 | + |
| 124 | + const payload = { |
| 125 | + foo: 'bar', |
| 126 | + }; |
| 127 | + |
| 128 | + await Api.post(API_URL_CORRECT, { query, payload }); |
| 129 | + |
| 130 | + const expectedUrl = createUrlWithQuery(API_URL_CORRECT, query); |
| 131 | + |
| 132 | + expect(global.fetch).toHaveBeenCalledWith(expectedUrl, { |
| 133 | + ...DEFAULT_REQUEST_PROPS, |
| 134 | + method: Method.POST, |
| 135 | + body: JSON.stringify(payload), |
| 136 | + }); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should create a PUT request', async () => { |
| 140 | + const response = successJsonResponse({}); |
| 141 | + global.fetch.mockResolvedValueOnce(response); |
| 142 | + |
| 143 | + const query = { |
| 144 | + foo: 'bar', |
| 145 | + }; |
| 146 | + |
| 147 | + const payload = { |
| 148 | + foo: 'bar', |
| 149 | + }; |
| 150 | + |
| 151 | + await Api.put(API_URL_CORRECT, { query, payload }); |
| 152 | + |
| 153 | + const expectedUrl = createUrlWithQuery(API_URL_CORRECT, query); |
| 154 | + |
| 155 | + expect(global.fetch).toHaveBeenCalledWith(expectedUrl, { |
| 156 | + ...DEFAULT_REQUEST_PROPS, |
| 157 | + method: Method.PUT, |
| 158 | + body: JSON.stringify(payload), |
| 159 | + }); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should create a PATCH request', async () => { |
| 163 | + const response = successJsonResponse({}); |
| 164 | + global.fetch.mockResolvedValueOnce(response); |
| 165 | + |
| 166 | + const query = { |
| 167 | + foo: 'bar', |
| 168 | + }; |
| 169 | + |
| 170 | + const payload = { |
| 171 | + foo: 'bar', |
| 172 | + }; |
| 173 | + |
| 174 | + await Api.patch(API_URL_CORRECT, { query, payload }); |
| 175 | + |
| 176 | + const expectedUrl = createUrlWithQuery(API_URL_CORRECT, query); |
| 177 | + |
| 178 | + expect(global.fetch).toHaveBeenCalledWith(expectedUrl, { |
| 179 | + ...DEFAULT_REQUEST_PROPS, |
| 180 | + method: Method.PATCH, |
| 181 | + body: JSON.stringify(payload), |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + it('should create a DELETE request', async () => { |
| 186 | + const response = successJsonResponse({}); |
| 187 | + global.fetch.mockResolvedValueOnce(response); |
| 188 | + |
| 189 | + const query = { |
| 190 | + foo: 'bar', |
| 191 | + }; |
| 192 | + |
| 193 | + await Api.delete(API_URL_CORRECT, { query }); |
| 194 | + |
| 195 | + const expectedUrl = createUrlWithQuery(API_URL_CORRECT, query); |
| 196 | + |
| 197 | + expect(global.fetch).toHaveBeenCalledWith(expectedUrl, { |
| 198 | + ...DEFAULT_REQUEST_PROPS, |
| 199 | + method: Method.DELETE, |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should create a DELETE request (with body)', async () => { |
| 204 | + const response = successJsonResponse({}); |
| 205 | + global.fetch.mockResolvedValueOnce(response); |
| 206 | + |
| 207 | + const query = { |
| 208 | + foo: 'bar', |
| 209 | + }; |
| 210 | + |
| 211 | + const payload = { |
| 212 | + foo: 'bar', |
| 213 | + }; |
| 214 | + |
| 215 | + await Api.delete(API_URL_CORRECT, { query, payload }); |
| 216 | + |
| 217 | + const expectedUrl = createUrlWithQuery(API_URL_CORRECT, query); |
| 218 | + |
| 219 | + expect(global.fetch).toHaveBeenCalledWith(expectedUrl, { |
| 220 | + ...DEFAULT_REQUEST_PROPS, |
| 221 | + method: Method.DELETE, |
| 222 | + body: JSON.stringify(payload), |
| 223 | + }); |
| 224 | + }); |
| 225 | +}); |
0 commit comments