Skip to content

Commit 0703c97

Browse files
authored
Merge pull request #175 from nada-deriv/nada/FEQ-2405/use-advert-create
Nada/FEQ-2405/chore: unit tests for advert create, update, delete
2 parents a023670 + d153363 commit 0703c97

File tree

6 files changed

+156
-37
lines changed

6 files changed

+156
-37
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { mockAdvertValues } from '@/__mocks__/mock-data';
2+
import { useP2pAdvertUpdate } from '@deriv-com/api-hooks';
3+
import { renderHook } from '@testing-library/react';
4+
import useAdvertDelete from '../useAdvertDelete';
5+
6+
jest.mock('@deriv-com/api-hooks', () => ({
7+
useP2pAdvertUpdate: jest.fn().mockReturnValue({
8+
data: undefined,
9+
mutate: jest.fn(),
10+
}),
11+
}));
12+
13+
const mockUseP2pAdvertUpdate = useP2pAdvertUpdate as jest.Mock;
14+
15+
const mockInvalidate = jest.fn();
16+
17+
jest.mock('../../../useInvalidateQuery', () => () => mockInvalidate);
18+
19+
describe('useAdvertDelete', () => {
20+
it('should return undefined if data is not present', () => {
21+
const { result } = renderHook(() => useAdvertDelete());
22+
23+
expect(result.current.data).toEqual(undefined);
24+
});
25+
26+
it('should call invalidate when onSuccess is triggered', async () => {
27+
const { result } = renderHook(() => useAdvertDelete());
28+
29+
await result.current.mutate({
30+
id: '1234',
31+
});
32+
33+
const { onSuccess } = mockUseP2pAdvertUpdate.mock.calls[0][0];
34+
onSuccess();
35+
expect(mockInvalidate).toHaveBeenCalledWith('p2p_advert_list');
36+
expect(mockInvalidate).toHaveBeenCalledWith('p2p_advertiser_adverts');
37+
});
38+
39+
it('should return correct data if present', () => {
40+
mockUseP2pAdvertUpdate.mockReturnValueOnce({
41+
data: mockAdvertValues,
42+
});
43+
44+
const { result } = renderHook(() => useAdvertDelete());
45+
expect(result.current.data).toEqual(mockAdvertValues);
46+
});
47+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { mockAdvertValues } from '@/__mocks__/mock-data';
2+
import { useP2pAdvertUpdate } from '@deriv-com/api-hooks';
3+
import { renderHook } from '@testing-library/react';
4+
import useAdvertUpdate from '../useAdvertUpdate';
5+
6+
jest.mock('@deriv-com/api-hooks', () => ({
7+
useP2pAdvertUpdate: jest.fn().mockReturnValue({
8+
data: undefined,
9+
mutate: jest.fn(),
10+
}),
11+
}));
12+
13+
const mockUseP2pAdvertUpdate = useP2pAdvertUpdate as jest.Mock;
14+
15+
const mockInvalidate = jest.fn();
16+
17+
jest.mock('../../../useInvalidateQuery', () => () => mockInvalidate);
18+
19+
describe('useAdvertUpdate', () => {
20+
it('should return undefined if data is not present', () => {
21+
const { result } = renderHook(() => useAdvertUpdate());
22+
23+
expect(result.current.data).toEqual(undefined);
24+
});
25+
26+
it('should call invalidate when onSuccess is triggered', async () => {
27+
const { result } = renderHook(() => useAdvertUpdate());
28+
29+
await result.current.mutate({
30+
id: '1234',
31+
is_active: 0,
32+
});
33+
34+
const { onSuccess } = mockUseP2pAdvertUpdate.mock.calls[0][0];
35+
onSuccess();
36+
expect(mockInvalidate).toHaveBeenCalledWith('p2p_advert_list');
37+
});
38+
39+
it('should return correct data if present', () => {
40+
mockUseP2pAdvertUpdate.mockReturnValueOnce({
41+
data: mockAdvertValues,
42+
});
43+
44+
const { result } = renderHook(() => useAdvertUpdate());
45+
expect(result.current.data).toEqual(mockAdvertValues);
46+
});
47+
});

src/hooks/api/advert/p2p-advert/useAdvertCreate.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,16 @@ const useAdvertCreate = () => {
3333

3434
const mutate = useCallback((payload: TPayload) => _mutate(payload), [_mutate]);
3535

36-
const modified_data = useMemo(() => {
36+
const memoizedData = useMemo(() => {
3737
if (!data) return undefined;
3838

3939
return {
4040
...data,
41-
/** Indicates if this is block trade advert or not. */
42-
block_trade: Boolean(data?.block_trade),
43-
/** The advert creation time in epoch. */
44-
created_time: data?.created_time ? new Date(data?.created_time) : undefined,
45-
/** The activation status of the advert. */
46-
is_active: Boolean(data?.is_active),
47-
/** Indicates that this advert will appear on the main advert list. */
48-
is_visible: Boolean(data?.is_visible),
4941
};
5042
}, [data]);
5143

5244
return {
53-
data: modified_data,
45+
data: memoizedData,
5446
mutate,
5547
...rest,
5648
};

src/hooks/api/advert/p2p-advert/useAdvertDelete.ts

+4-14
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,16 @@ const useAdvertDelete = () => {
3636
[_mutate]
3737
);
3838

39-
const modified_data = useMemo(() => {
40-
const p2p_advert_update = data;
41-
42-
if (!p2p_advert_update) return undefined;
39+
const memoizedData = useMemo(() => {
40+
if (!data) return undefined;
4341

4442
return {
45-
...p2p_advert_update,
46-
/** The activation status of the advert. */
47-
is_active: Boolean(p2p_advert_update.is_active),
48-
/** Indicates if this is block trade advert or not. */
49-
is_block_trade: Boolean(p2p_advert_update.block_trade),
50-
/** Indicates that the advert has been deleted. */
51-
is_deleted: Boolean(p2p_advert_update.deleted),
52-
/** Indicates that this advert will appear on the main advert list. */
53-
is_visible: Boolean(p2p_advert_update.is_visible),
43+
...data,
5444
};
5545
}, [data]);
5646

5747
return {
58-
data: modified_data,
48+
data: memoizedData,
5949
mutate,
6050
...rest,
6151
};

src/hooks/api/advert/p2p-advert/useAdvertUpdate.ts

+4-13
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,16 @@ const useAdvertUpdate = () => {
2929

3030
const mutate = useCallback((payload: TPayload) => _mutate(payload), [_mutate]);
3131

32-
const modified_data = useMemo(() => {
33-
const p2p_advert_update = data;
34-
if (!p2p_advert_update) return undefined;
32+
const memoizedData = useMemo(() => {
33+
if (!data) return undefined;
3534

3635
return {
37-
...p2p_advert_update,
38-
/** The activation status of the advert. */
39-
is_active: Boolean(p2p_advert_update.is_active),
40-
/** Indicates if this is block trade advert or not. */
41-
is_block_trade: Boolean(p2p_advert_update.block_trade),
42-
/** Indicates that the advert has been deleted. */
43-
is_deleted: Boolean(p2p_advert_update.deleted),
44-
/** Indicates that this advert will appear on the main advert list. */
45-
is_visible: Boolean(p2p_advert_update.is_visible),
36+
...data,
4637
};
4738
}, [data]);
4839

4940
return {
50-
data: modified_data,
41+
data: memoizedData,
5142
mutate,
5243
...rest,
5344
};

0 commit comments

Comments
 (0)