|
1 | | -import { handleResponseType } from '../../src/utils/api'; |
| 1 | +import api, { handleResponseType } from '../../src/utils/api'; |
2 | 2 |
|
3 | 3 | describe('api', () => { |
4 | 4 | describe('handleResponseType', () => { |
@@ -29,4 +29,118 @@ describe('api', () => { |
29 | 29 | expect(handled).toEqual([true, blob]); |
30 | 30 | }); |
31 | 31 | }); |
| 32 | + |
| 33 | + describe('api client', () => { |
| 34 | + let fetchSpy; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + fetchSpy = jest.spyOn(window, 'fetch'); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + fetchSpy.mockRestore(); |
| 42 | + }); |
| 43 | + |
| 44 | + test('when there is no VERDACCIO_URL is defined', () => { |
| 45 | + const { VERDACCIO_API_URL } = window; |
| 46 | + delete window.VERDACCIO_API_URL; |
| 47 | + // @ts-ignore |
| 48 | + window.VERDACCIO_API_URL = undefined; |
| 49 | + |
| 50 | + expect(() => { |
| 51 | + api.request('https://verdaccio.tld'); |
| 52 | + }).toThrow(new Error('VERDACCIO_API_URL is not defined!')); |
| 53 | + |
| 54 | + window.VERDACCIO_API_URL = VERDACCIO_API_URL; |
| 55 | + }); |
| 56 | + |
| 57 | + test('when url is a resource url', async () => { |
| 58 | + fetchSpy.mockImplementation(() => |
| 59 | + Promise.resolve({ |
| 60 | + headers: new Headers({ |
| 61 | + 'Content-Type': 'application/json', |
| 62 | + }), |
| 63 | + ok: true, |
| 64 | + json: () => ({ a: 1 }), |
| 65 | + }) |
| 66 | + ); |
| 67 | + |
| 68 | + const response = await api.request('/resource'); |
| 69 | + |
| 70 | + expect(fetchSpy).toHaveBeenCalledWith('https://verdaccio.tld/resource', { |
| 71 | + credentials: 'same-origin', |
| 72 | + headers: {}, |
| 73 | + method: 'GET', |
| 74 | + }); |
| 75 | + expect(response).toEqual({ a: 1 }); |
| 76 | + }); |
| 77 | + |
| 78 | + test('when there is token from storage', async () => { |
| 79 | + jest.resetModules(); |
| 80 | + jest.doMock('./storage', () => ({ getItem: () => 'token-xx-xx-xx' })); |
| 81 | + |
| 82 | + fetchSpy.mockImplementation(() => |
| 83 | + Promise.resolve({ |
| 84 | + headers: new Headers({ |
| 85 | + 'Content-Type': 'application/json', |
| 86 | + }), |
| 87 | + ok: true, |
| 88 | + json: () => ({ c: 3 }), |
| 89 | + }) |
| 90 | + ); |
| 91 | + |
| 92 | + const api = require('../../src/utils/api').default; |
| 93 | + const response = await api.request('/resource', 'GET'); |
| 94 | + |
| 95 | + expect(fetchSpy).toHaveBeenCalledWith('https://verdaccio.tld/resource', { |
| 96 | + credentials: 'same-origin', |
| 97 | + headers: { |
| 98 | + Authorization: 'Bearer token-xx-xx-xx', |
| 99 | + }, |
| 100 | + method: 'GET', |
| 101 | + }); |
| 102 | + expect(response).toEqual({ c: 3 }); |
| 103 | + }); |
| 104 | + |
| 105 | + test('when url is a cross origin url', async () => { |
| 106 | + fetchSpy.mockImplementation(() => |
| 107 | + Promise.resolve({ |
| 108 | + headers: new Headers({ |
| 109 | + 'Content-Type': 'application/json', |
| 110 | + }), |
| 111 | + ok: true, |
| 112 | + json: () => ({ b: 2 }), |
| 113 | + }) |
| 114 | + ); |
| 115 | + |
| 116 | + const response = await api.request('https://verdaccio.xyz/resource'); |
| 117 | + expect(fetchSpy).toHaveBeenCalledWith('https://verdaccio.xyz/resource', { |
| 118 | + credentials: 'same-origin', |
| 119 | + headers: {}, |
| 120 | + method: 'GET', |
| 121 | + }); |
| 122 | + expect(response).toEqual({ b: 2 }); |
| 123 | + }); |
| 124 | + |
| 125 | + test('when api returns an error 3.x.x - 4.x.x', async () => { |
| 126 | + fetchSpy.mockImplementation(() => |
| 127 | + Promise.resolve({ |
| 128 | + headers: new Headers({ |
| 129 | + 'Content-Type': 'application/json', |
| 130 | + }), |
| 131 | + ok: false, |
| 132 | + json: () => {}, |
| 133 | + }) |
| 134 | + ); |
| 135 | + |
| 136 | + await expect(api.request('/resource')).rejects.toThrow(new Error('something went wrong')); |
| 137 | + }); |
| 138 | + |
| 139 | + test('when api returns an error 5.x.x', async () => { |
| 140 | + const errorMessage = 'Internal server error'; |
| 141 | + fetchSpy.mockImplementation(() => Promise.reject(new Error(errorMessage))); |
| 142 | + |
| 143 | + await expect(api.request('/resource')).rejects.toThrow(new Error(errorMessage)); |
| 144 | + }); |
| 145 | + }); |
32 | 146 | }); |
0 commit comments