1
1
import * as React from 'react' ;
2
- import { useFetch , useSubscription } from '@deriv/api' ;
2
+ import { useSubscription } from '@deriv/api' ;
3
3
import { mockStore , StoreProvider } from '@deriv/stores' ;
4
4
import { renderHook } from '@testing-library/react-hooks' ;
5
5
import useP2PCompletedOrdersNotification from '../useP2PCompletedOrdersNotification' ;
6
6
7
7
jest . mock ( '@deriv/api' , ( ) => ( {
8
8
...jest . requireActual ( '@deriv/api' ) ,
9
- useFetch : jest . fn ( ) ,
10
9
useSubscription : jest . fn ( ) ,
11
10
} ) ) ;
12
11
13
- const mockUseFetch = useFetch as jest . MockedFunction < typeof useFetch < 'website_status' > > ;
14
12
const mockUseSubscription = useSubscription as jest . MockedFunction < typeof useSubscription < 'p2p_order_list' > > ;
15
13
16
14
describe ( 'useP2PCompletedOrdersNotification' , ( ) => {
17
- test ( 'should unsubscribe from p2p_order_list if user is not authorized ' , ( ) => {
15
+ test ( 'should not subscribe to p2p_order_list if user is not logged in ' , ( ) => {
18
16
const mock = mockStore ( {
19
17
client : {
20
18
is_authorize : false ,
19
+ is_p2p_enabled : true ,
21
20
currency : 'USD' ,
22
21
} ,
23
22
notifications : {
24
23
p2p_completed_orders : [ ] ,
25
24
} ,
26
25
} ) ;
27
26
28
- // @ts -expect-error need to come up with a way to mock the return type of useFetch
29
- mockUseFetch . mockReturnValue ( { data : { website_status : { p2p_config : { supported_currencies : [ 'usd' ] } } } } ) ;
27
+ // @ts -expect-error need to come up with a way to mock the return type of useSubscription
28
+ mockUseSubscription . mockReturnValue ( {
29
+ subscribe : jest . fn ( ) ,
30
+ unsubscribe : jest . fn ( ) ,
31
+ isSubscribed : false ,
32
+ } ) ;
33
+
34
+ const wrapper = ( { children } : { children : JSX . Element } ) => (
35
+ < StoreProvider store = { mock } > { children } </ StoreProvider >
36
+ ) ;
37
+
38
+ renderHook ( ( ) => useP2PCompletedOrdersNotification ( ) , { wrapper } ) ;
39
+ expect ( mockUseSubscription ( 'p2p_order_list' ) . subscribe ) . not . toBeCalled ( ) ;
40
+ expect ( mock . notifications . p2p_completed_orders ) . toEqual ( [ ] ) ;
41
+ } ) ;
42
+
43
+ test ( 'should not subscribe to p2p_order_list if user p2p is disabled' , ( ) => {
44
+ const mock = mockStore ( {
45
+ client : {
46
+ is_authorize : true ,
47
+ is_p2p_enabled : false ,
48
+ currency : 'EUR' ,
49
+ } ,
50
+ notifications : {
51
+ p2p_completed_orders : [ ] ,
52
+ } ,
53
+ } ) ;
30
54
31
55
// @ts -expect-error need to come up with a way to mock the return type of useSubscription
32
56
mockUseSubscription . mockReturnValue ( {
33
57
subscribe : jest . fn ( ) ,
34
58
unsubscribe : jest . fn ( ) ,
59
+ isSubscribed : false ,
35
60
} ) ;
36
61
37
62
const wrapper = ( { children } : { children : JSX . Element } ) => (
@@ -40,36 +65,66 @@ describe('useP2PCompletedOrdersNotification', () => {
40
65
41
66
renderHook ( ( ) => useP2PCompletedOrdersNotification ( ) , { wrapper } ) ;
42
67
43
- expect ( mockUseSubscription ( 'p2p_order_list' ) . unsubscribe ) . toBeCalled ( ) ;
68
+ expect ( mockUseSubscription ( 'p2p_order_list' ) . subscribe ) . not . toBeCalled ( ) ;
44
69
expect ( mock . notifications . p2p_completed_orders ) . toEqual ( [ ] ) ;
45
70
} ) ;
46
71
47
- test ( 'should unsubscribe from p2p_order_list if user p2p is disabled ' , ( ) => {
72
+ test ( 'should not call unsubscribe from p2p_order_list if user is logged in and p2p is enabled but list is not subscribed ' , ( ) => {
48
73
const mock = mockStore ( {
49
74
client : {
50
- is_authorize : false ,
51
- currency : 'EUR' ,
75
+ is_authorize : true ,
76
+ is_p2p_enabled : true ,
77
+ currency : 'USD' ,
52
78
} ,
53
79
notifications : {
54
80
p2p_completed_orders : [ ] ,
55
81
} ,
56
82
} ) ;
57
83
58
- // @ts -expect-error need to come up with a way to mock the return type of useFetch
59
- mockUseFetch . mockReturnValue ( { data : { website_status : { p2p_config : { supported_currencies : [ 'usd' ] } } } } ) ;
84
+ // @ts -expect-error need to come up with a way to mock the return type of useSubscription
85
+ mockUseSubscription . mockReturnValue ( {
86
+ subscribe : jest . fn ( ) ,
87
+ unsubscribe : jest . fn ( ) ,
88
+ isSubscribed : false ,
89
+ } ) ;
90
+
91
+ const wrapper = ( { children } : { children : JSX . Element } ) => (
92
+ < StoreProvider store = { mock } > { children } </ StoreProvider >
93
+ ) ;
94
+
95
+ const { unmount } = renderHook ( ( ) => useP2PCompletedOrdersNotification ( ) , { wrapper } ) ;
96
+
97
+ unmount ( ) ;
98
+ expect ( mockUseSubscription ( 'p2p_order_list' ) . unsubscribe ) . not . toBeCalled ( ) ;
99
+ expect ( mock . notifications . p2p_completed_orders ) . toEqual ( [ ] ) ;
100
+ } ) ;
101
+
102
+ test ( 'should unsubscribe from p2p_order_list if user is logged in and p2p is enabled and list is subscribed' , ( ) => {
103
+ const mock = mockStore ( {
104
+ client : {
105
+ is_authorize : true ,
106
+ is_p2p_enabled : true ,
107
+ currency : 'USD' ,
108
+ } ,
109
+ notifications : {
110
+ p2p_completed_orders : [ ] ,
111
+ } ,
112
+ } ) ;
60
113
61
114
// @ts -expect-error need to come up with a way to mock the return type of useSubscription
62
115
mockUseSubscription . mockReturnValue ( {
63
116
subscribe : jest . fn ( ) ,
64
117
unsubscribe : jest . fn ( ) ,
118
+ isSubscribed : true ,
65
119
} ) ;
66
120
67
121
const wrapper = ( { children } : { children : JSX . Element } ) => (
68
122
< StoreProvider store = { mock } > { children } </ StoreProvider >
69
123
) ;
70
124
71
- renderHook ( ( ) => useP2PCompletedOrdersNotification ( ) , { wrapper } ) ;
125
+ const { unmount } = renderHook ( ( ) => useP2PCompletedOrdersNotification ( ) , { wrapper } ) ;
72
126
127
+ unmount ( ) ;
73
128
expect ( mockUseSubscription ( 'p2p_order_list' ) . unsubscribe ) . toBeCalled ( ) ;
74
129
expect ( mock . notifications . p2p_completed_orders ) . toEqual ( [ ] ) ;
75
130
} ) ;
@@ -78,16 +133,14 @@ describe('useP2PCompletedOrdersNotification', () => {
78
133
const mock = mockStore ( {
79
134
client : {
80
135
is_authorize : true ,
136
+ is_p2p_enabled : true ,
81
137
currency : 'USD' ,
82
138
} ,
83
139
notifications : {
84
140
p2p_completed_orders : [ ] ,
85
141
} ,
86
142
} ) ;
87
143
88
- // @ts -expect-error need to come up with a way to mock the return type of useFetch
89
- mockUseFetch . mockReturnValue ( { data : { website_status : { p2p_config : { supported_currencies : [ 'usd' ] } } } } ) ;
90
-
91
144
const mock_p2p_order_list = [
92
145
{
93
146
account_currency : 'USD' ,
0 commit comments