|
| 1 | +import axios from 'axios'; |
| 2 | +import AxiosMockAdapter from 'axios-mock-adapter'; |
| 3 | +import { RequestService } from '../RequestService'; |
| 4 | + |
| 5 | +describe('RequestService', () => { |
| 6 | + it('should perform successful GET requests', (done) => { |
| 7 | + const testUrl = 'http://localhost'; |
| 8 | + const testResponse = { testData: 'Test response data' }; |
| 9 | + |
| 10 | + const testParams = { |
| 11 | + param1: 'Param #1 value', |
| 12 | + param2: 'Param #2 value', |
| 13 | + }; |
| 14 | + const mock = new AxiosMockAdapter(axios); |
| 15 | + mock.onGet(testUrl).reply(200, testResponse); |
| 16 | + |
| 17 | + RequestService |
| 18 | + .get(testUrl, testParams) |
| 19 | + .then((responseData) => { |
| 20 | + // We should receive mocked response. |
| 21 | + expect(responseData).toBeDefined(); |
| 22 | + expect(responseData.data).toEqual(testResponse); |
| 23 | + }) |
| 24 | + .catch(() => { |
| 25 | + // We should not get here. |
| 26 | + expect(true).toBe(false); |
| 27 | + }) |
| 28 | + .finally(() => { |
| 29 | + done(); |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should perform not successful GET requests', (done) => { |
| 34 | + const testUrl = 'http://localhost'; |
| 35 | + const mock = new AxiosMockAdapter(axios); |
| 36 | + mock.onGet(testUrl).reply(500); |
| 37 | + |
| 38 | + RequestService |
| 39 | + .get(testUrl) |
| 40 | + .then(() => { |
| 41 | + // We should not get here. |
| 42 | + expect(true).toBe(false); |
| 43 | + }) |
| 44 | + .catch((err) => { |
| 45 | + // We should get here. |
| 46 | + expect(err).toBeDefined(); |
| 47 | + }) |
| 48 | + .finally(() => { |
| 49 | + done(); |
| 50 | + }); |
| 51 | + }); |
| 52 | +}); |
0 commit comments