|
| 1 | +import { mockAdvertValues } from '@/__mocks__/mock-data'; |
| 2 | +import { useP2pAdvertCreate } from '@deriv-com/api-hooks'; |
| 3 | +import { renderHook } from '@testing-library/react'; |
| 4 | +import useAdvertCreate from '../useAdvertCreate'; |
| 5 | + |
| 6 | +jest.mock('@deriv-com/api-hooks', () => ({ |
| 7 | + useP2pAdvertCreate: jest.fn().mockReturnValue({ |
| 8 | + data: undefined, |
| 9 | + mutate: jest.fn(), |
| 10 | + }), |
| 11 | +})); |
| 12 | + |
| 13 | +const mockUseP2pAdvertCreate = useP2pAdvertCreate as jest.Mock; |
| 14 | + |
| 15 | +const mockInvalidate = jest.fn(); |
| 16 | + |
| 17 | +jest.mock('../../../useInvalidateQuery', () => () => mockInvalidate); |
| 18 | + |
| 19 | +describe('useAdvertCreate', () => { |
| 20 | + it('should return undefined if data is not present', () => { |
| 21 | + const { result } = renderHook(() => useAdvertCreate()); |
| 22 | + |
| 23 | + expect(result.current.data).toEqual(undefined); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should call invalidate when onSuccess is triggered', async () => { |
| 27 | + const { result } = renderHook(() => useAdvertCreate()); |
| 28 | + |
| 29 | + await result.current.mutate({ |
| 30 | + amount: 100, |
| 31 | + description: 'Please transfer to account number 1234', |
| 32 | + max_order_amount: 50, |
| 33 | + min_order_amount: 20, |
| 34 | + payment_method: 'bank_transfer', |
| 35 | + rate: 4.25, |
| 36 | + type: 'buy', |
| 37 | + }); |
| 38 | + |
| 39 | + const { onSuccess } = mockUseP2pAdvertCreate.mock.calls[0][0]; |
| 40 | + onSuccess(); |
| 41 | + expect(mockInvalidate).toHaveBeenCalledWith('p2p_advert_list'); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return correct data if present', () => { |
| 45 | + mockUseP2pAdvertCreate.mockReturnValueOnce({ |
| 46 | + data: mockAdvertValues, |
| 47 | + }); |
| 48 | + |
| 49 | + const { result } = renderHook(() => useAdvertCreate()); |
| 50 | + expect(result.current.data).toEqual(mockAdvertValues); |
| 51 | + }); |
| 52 | +}); |
0 commit comments