|
| 1 | +import { createClientFromOperations } from '../client'; |
| 2 | +import * as mock from '../mocker'; |
| 3 | +import { IHttpConfig } from '../types'; |
| 4 | + |
| 5 | +describe('User Http Client', () => { |
| 6 | + describe('with mocking set to true', () => { |
| 7 | + describe('get a resource', () => { |
| 8 | + let client: ReturnType<typeof createClientFromOperations>; |
| 9 | + |
| 10 | + const config: IHttpConfig = { |
| 11 | + mock: { dynamic: false }, |
| 12 | + validateRequest: true, |
| 13 | + validateResponse: true, |
| 14 | + checkSecurity: true, |
| 15 | + }; |
| 16 | + |
| 17 | + beforeAll(async () => { |
| 18 | + jest.spyOn(mock, 'default'); |
| 19 | + |
| 20 | + client = await createClientFromOperations( |
| 21 | + [ |
| 22 | + { |
| 23 | + id: 'operation', |
| 24 | + method: 'get', |
| 25 | + path: '/pet', |
| 26 | + servers: [ |
| 27 | + { |
| 28 | + url: 'https://www.google.it', |
| 29 | + }, |
| 30 | + ], |
| 31 | + responses: [ |
| 32 | + { |
| 33 | + code: '200', |
| 34 | + }, |
| 35 | + ], |
| 36 | + }, |
| 37 | + ], |
| 38 | + config, |
| 39 | + ); |
| 40 | + |
| 41 | + jest.spyOn(client, 'request'); |
| 42 | + }); |
| 43 | + |
| 44 | + afterAll(() => jest.clearAllMocks()); |
| 45 | + |
| 46 | + describe('when calling with no options', () => { |
| 47 | + beforeAll(() => client.get('/pet')); |
| 48 | + |
| 49 | + test('shall call the mocker with the default options', () => |
| 50 | + expect(mock.default).toHaveBeenCalledWith({ input: expect.anything(), resource: expect.anything(), config })); |
| 51 | + |
| 52 | + test('shall ultimately call the main request method with the current HTTP Method', () => |
| 53 | + expect(client.request).toHaveBeenCalledWith('/pet', { method: 'get' }, undefined)); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('when overriding a config parameter on the request level', () => { |
| 57 | + beforeAll(() => client.get('/pet', { checkSecurity: false })); |
| 58 | + |
| 59 | + test('shall call the mocker with the modified options', () => |
| 60 | + expect(mock.default).toHaveBeenCalledWith({ |
| 61 | + input: expect.anything(), |
| 62 | + resource: expect.anything(), |
| 63 | + config: { ...config, checkSecurity: false }, |
| 64 | + })); |
| 65 | + }); |
| 66 | + |
| 67 | + describe('when calling a method with overridden url', () => { |
| 68 | + beforeAll(() => client.get('/pet', { baseUrl: 'https://www.google.it' })); |
| 69 | + |
| 70 | + test('should call the mocker with the replaced full url', () => { |
| 71 | + expect(mock.default).toBeCalledWith({ |
| 72 | + resource: expect.anything(), |
| 73 | + input: expect.objectContaining({ |
| 74 | + data: expect.objectContaining({ |
| 75 | + url: expect.objectContaining({ |
| 76 | + baseUrl: 'https://www.google.it', |
| 77 | + path: '/pet', |
| 78 | + }), |
| 79 | + }), |
| 80 | + }), |
| 81 | + config: expect.anything(), |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('when calling a method with a full url', () => { |
| 87 | + beforeAll(() => client.get('https://www.google.it/pet')); |
| 88 | + |
| 89 | + test('should call the mocker with the replaced full url', () => { |
| 90 | + expect(mock.default).toBeCalledWith({ |
| 91 | + resource: expect.anything(), |
| 92 | + input: expect.objectContaining({ |
| 93 | + data: expect.objectContaining({ |
| 94 | + url: expect.objectContaining({ |
| 95 | + baseUrl: 'https://www.google.it', |
| 96 | + path: '/pet', |
| 97 | + }), |
| 98 | + }), |
| 99 | + }), |
| 100 | + config: expect.anything(), |
| 101 | + }); |
| 102 | + }); |
| 103 | + }); |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments