diff --git a/packages/checkout/sdk/src/availability/availability.test.ts b/packages/checkout/sdk/src/availability/availability.test.ts index 602ca10e11..9f73cf7bc0 100644 --- a/packages/checkout/sdk/src/availability/availability.test.ts +++ b/packages/checkout/sdk/src/availability/availability.test.ts @@ -11,10 +11,8 @@ describe('availabilityService', () => { }); describe('checkDexAvailability', () => { - it('should return true when status is 204', async () => { - const mockResponse = { - status: 204, - }; + it('should return true when status is 2xx', async () => { + const mockResponse = {}; mockedAxios.post.mockResolvedValueOnce(mockResponse); const response = await availabilityService(true, false).checkDexAvailability(); @@ -26,7 +24,7 @@ describe('availabilityService', () => { const mockResponse = { status: 403, }; - mockedAxios.post.mockResolvedValueOnce(mockResponse); + mockedAxios.post.mockRejectedValueOnce({ response: mockResponse }); const response = await availabilityService(true, false).checkDexAvailability(); expect(mockedAxios.post).toHaveBeenCalledTimes(1); @@ -38,7 +36,7 @@ describe('availabilityService', () => { status: 500, statusText: 'error message', }; - mockedAxios.post.mockResolvedValueOnce(mockResponse); + mockedAxios.post.mockRejectedValueOnce({ response: mockResponse }); await expect(availabilityService(true, false).checkDexAvailability()) .rejects diff --git a/packages/checkout/sdk/src/availability/availability.ts b/packages/checkout/sdk/src/availability/availability.ts index 09f22a00c1..13cb8c376a 100644 --- a/packages/checkout/sdk/src/availability/availability.ts +++ b/packages/checkout/sdk/src/availability/availability.ts @@ -24,19 +24,20 @@ export const availabilityService = ( try { response = await axios.post(`${postEndpoint()}/v1/availability/checkout/swap`); } catch (error: any) { + // The request was made and the server responded with a status code + // that falls out of the range of 2xx response = error.response; - } - if (response.status === 403) { - return false; - } - if (response.status === 204) { - return true; + // If 403 then the service is geo-blocked + if (response.status === 403) return false; + + throw new CheckoutError( + `Error fetching from api: ${response.status} ${response.statusText}`, + CheckoutErrorType.API_ERROR, + ); } - throw new CheckoutError( - `Error fetching from api: ${response.status} ${response.statusText}`, - CheckoutErrorType.API_ERROR, - ); + + return true; }; return {