Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/hooks/useFetchEligibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe('useFetchEligibility', () => {
expect(fetchFromApi).not.toHaveBeenCalled()
})

it('should returns a FAILED status if the API response is from ErrorType', async () => {
it('should returns a FAILED status if the API response contains `error_code`', async () => {
const mockedSessionStorage = {
getCache: jest.fn(),
setCache: jest.fn(),
Expand All @@ -104,6 +104,37 @@ describe('useFetchEligibility', () => {
),
)

// Status should be failed
await waitFor(() => {
expect(result.current[1]).toBe(statusResponse.FAILED)
})
// The cache should not be set
expect(mockedSessionStorage?.setCache).not.toHaveBeenCalled()
// The hook response should be empty
expect(result.current[0]).toEqual([])
})
it('should returns a FAILED status if the API response contains `errors`', async () => {
const mockedSessionStorage = {
getCache: jest.fn(),
setCache: jest.fn(),
createKey: jest.fn().mockReturnValue('mocked_key'),
clearCache: jest.fn(),
}
;(useSessionStorage as jest.Mock).mockReturnValue(mockedSessionStorage)
;(fetchFromApi as jest.Mock).mockImplementation(async () => ({
errors: 'some error',
}))

const { result } = renderHook(() =>
useFetchEligibility(
45000,
{ domain: ApiMode.TEST, merchantId: 'test_id' },
undefined,
'FR',
'FR',
),
)

// Status should be failed
await waitFor(() => {
expect(result.current[1]).toBe(statusResponse.FAILED)
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useFetchEligibility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ const useFetchEligibility = (
)
.then((res) => {
// If the response contains an error_code, we set the status to failed - for example if code is 403 unauthorized
if ('error_code' in res) {
// When purchase amount & plans are not compatible, the API returns an error {"running": {"message": "'purchase_amount'"}} which does not contain the key `error_code`
// And the widget keeps loading if we don't handle it, so we need to cover this case and returns a FAILED status
if ('error_code' in res || 'errors' in res) {
setStatus(statusResponse.FAILED)
} else {
setEligibility(res as EligibilityPlan[])
Expand Down
Loading