|
| 1 | +import {WRITE_COMMANDS} from '@libs/API/types'; |
| 2 | + |
| 3 | +import CONST from '@src/CONST'; |
| 4 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 5 | + |
| 6 | +import Onyx from 'react-native-onyx'; |
| 7 | + |
| 8 | +import HttpUtils from '../../src/libs/HttpUtils'; |
| 9 | + |
| 10 | +function mockFetchResponse(message: string) { |
| 11 | + global.fetch = jest.fn().mockResolvedValue({ |
| 12 | + ok: true, |
| 13 | + status: 200, |
| 14 | + headers: {get: () => null}, |
| 15 | + json: () => Promise.resolve({jsonCode: CONST.JSON_CODE.EXP_ERROR, message}), |
| 16 | + }); |
| 17 | +} |
| 18 | + |
| 19 | +beforeAll(() => { |
| 20 | + Onyx.init({ |
| 21 | + keys: ONYXKEYS, |
| 22 | + }); |
| 23 | +}); |
| 24 | + |
| 25 | +afterEach(() => { |
| 26 | + jest.restoreAllMocks(); |
| 27 | +}); |
| 28 | + |
| 29 | +describe('HttpUtils', () => { |
| 30 | + // The mapping is keyed on the server message alone, not the command. The messages are |
| 31 | + // pinned as literals so a change to the CONST values can't silently drift from what the |
| 32 | + // server really sends. |
| 33 | + it.each([ |
| 34 | + ['Transaction already created.', WRITE_COMMANDS.REQUEST_MONEY], |
| 35 | + ['The request has already been paid', WRITE_COMMANDS.PAY_MONEY_REQUEST], |
| 36 | + ])('maps the jsonCode-666 rejection "%s" to ALREADY_CREATED', async (message, command) => { |
| 37 | + mockFetchResponse(message); |
| 38 | + |
| 39 | + await expect(HttpUtils.xhr(command, {})).rejects.toMatchObject({ |
| 40 | + message: CONST.ERROR.ALREADY_CREATED, |
| 41 | + title: message, |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('leaves a jsonCode-666 response with an unrecognized message untouched', async () => { |
| 46 | + mockFetchResponse('Some other error'); |
| 47 | + |
| 48 | + await expect(HttpUtils.xhr(WRITE_COMMANDS.PAY_MONEY_REQUEST, {})).resolves.toMatchObject({jsonCode: CONST.JSON_CODE.EXP_ERROR, message: 'Some other error'}); |
| 49 | + }); |
| 50 | +}); |
0 commit comments