@@ -8,11 +8,6 @@ import createRandomPolicy from '../../utils/collections/policies';
88
99const mockPolicyID = '123456' ;
1010
11- const delay = ( ms : number ) =>
12- new Promise ( ( resolve ) => {
13- setTimeout ( resolve , ms ) ;
14- } ) ;
15-
1611const mockPolicy = { ...createRandomPolicy ( Number ( mockPolicyID ) , CONST . POLICY . TYPE . TEAM , 'TestPolicy' ) , policyID : mockPolicyID , workspaceAccountID : Number ( mockPolicyID ) } ;
1712
1813const mockCardFeeds = {
@@ -41,8 +36,14 @@ jest.mock('@hooks/useCardFeeds', () => ({
4136} ) ) ;
4237describe ( 'useIsBlockedToAddFeed' , ( ) => {
4338 beforeEach ( async ( ) => {
39+ await Onyx . clear ( ) ;
4440 await Onyx . merge ( `${ ONYXKEYS . COLLECTION . POLICY } ${ mockPolicy ?. policyID } ` , mockPolicy ) ;
4541 } ) ;
42+
43+ afterEach ( async ( ) => {
44+ await Onyx . clear ( ) ;
45+ jest . clearAllMocks ( ) ;
46+ } ) ;
4647 it ( 'should return true if collect policy and feed already exists' , ( ) => {
4748 ( useCardFeeds as jest . Mock ) . mockReturnValue ( [ mockCardFeeds , { status : 'loaded' } ] ) ;
4849 const { result} = renderHook ( ( ) => useIsBlockedToAddFeed ( mockPolicyID ) ) ;
@@ -56,26 +57,21 @@ describe('useIsBlockedToAddFeed', () => {
5657 expect ( result ?. current . isBlockedToAddNewFeeds ) . toBe ( false ) ;
5758 } ) ;
5859
59- it ( 'should return isBlockedToAddNewFeeds as false if collect policy and new feed added' , async ( ) => {
60- ( useCardFeeds as jest . Mock ) . mockReturnValue ( [ { } , { status : 'loaded' } ] ) ;
61- const { result, rerender} = renderHook ( ( ) => useIsBlockedToAddFeed ( mockPolicyID ) ) ;
62- expect ( result . current . isBlockedToAddNewFeeds ) . toBe ( false ) ;
63- // Set initial empty state and wait for new connection to be established
64- await delay ( 2000 ) ;
60+ it ( 'should return isBlockedToAddNewFeeds as false if collect policy and CSV feed exists (allows adding another feed)' , async ( ) => {
6561 ( useCardFeeds as jest . Mock ) . mockReturnValue ( [
6662 {
67- ins : {
68- feed : 'ins' ,
69- customFeedName : 'Regions Bank cards' ,
70- accountList : [ 'Plaid Checking 0000' , 'Plaid Credit Card 3333' ] ,
63+ // eslint-disable-next-line @typescript-eslint/naming-convention
64+ 'csv#123456' : {
65+ feed : 'csv#123456' ,
66+ customFeedName : 'CSV Upload' ,
67+ accountList : [ 'Card 0000' ] ,
7168 } ,
7269 } ,
7370 { status : 'loaded' } ,
7471 ] ) ;
75- // Wait to set state happened
76- await delay ( 2000 ) ;
72+ const { result} = renderHook ( ( ) => useIsBlockedToAddFeed ( mockPolicyID ) ) ;
7773
78- rerender ( mockPolicyID ) ;
74+ // CSV feeds don't count toward the limit, so user can add another feed
7975 expect ( result . current . isBlockedToAddNewFeeds ) . toBe ( false ) ;
8076 } ) ;
8177
@@ -125,7 +121,90 @@ describe('useIsBlockedToAddFeed', () => {
125121 expect ( result . current . isBlockedToAddNewFeeds ) . toBe ( false ) ;
126122
127123 ( useCardFeeds as jest . Mock ) . mockReturnValue ( [ mockCardFeeds , { status : 'loaded' } , { isLoading : false } ] ) ;
128- rerender ( { policyID : mockPolicyID } ) ;
124+ rerender ( mockPolicyID ) ;
125+ expect ( result . current . isBlockedToAddNewFeeds ) . toBe ( true ) ;
126+ } ) ;
127+
128+ it ( 'should return true if collect policy and pending feed exists (pending feeds count toward limit)' , ( ) => {
129+ ( useCardFeeds as jest . Mock ) . mockReturnValue ( [
130+ {
131+ // eslint-disable-next-line @typescript-eslint/naming-convention
132+ 'plaid.ins_19#123456' : {
133+ feed : 'plaid.ins_19' ,
134+ domainID : 123456 ,
135+ pending : true ,
136+ liabilityType : 'corporate' ,
137+ } ,
138+ } ,
139+ { status : 'loaded' } ,
140+ ] ) ;
141+ const { result} = renderHook ( ( ) => useIsBlockedToAddFeed ( mockPolicyID ) ) ;
142+
143+ // Pending feeds count toward the limit, so user should be blocked from adding another feed
144+ expect ( result . current . isBlockedToAddNewFeeds ) . toBe ( true ) ;
145+ } ) ;
146+
147+ it ( 'should return false if collect policy and pending CSV feed exists (pending CSV feeds do not count toward limit)' , ( ) => {
148+ ( useCardFeeds as jest . Mock ) . mockReturnValue ( [
149+ {
150+ // eslint-disable-next-line @typescript-eslint/naming-convention
151+ 'csv#123456' : {
152+ feed : 'csv#123456' ,
153+ customFeedName : 'CSV Upload' ,
154+ accountList : [ 'Card 0000' ] ,
155+ pending : true ,
156+ } ,
157+ } ,
158+ { status : 'loaded' } ,
159+ ] ) ;
160+ const { result} = renderHook ( ( ) => useIsBlockedToAddFeed ( mockPolicyID ) ) ;
161+
162+ // Pending CSV feeds don't count toward the limit, so user can add another feed
163+ expect ( result . current . isBlockedToAddNewFeeds ) . toBe ( false ) ;
164+ } ) ;
165+
166+ it ( 'should return true if collect policy has both regular feed and pending feed' , ( ) => {
167+ ( useCardFeeds as jest . Mock ) . mockReturnValue ( [
168+ {
169+ // eslint-disable-next-line @typescript-eslint/naming-convention
170+ 'plaid.ins_19#123456' : {
171+ feed : 'plaid.ins_19' ,
172+ domainID : 123456 ,
173+ pending : false ,
174+ liabilityType : 'corporate' ,
175+ } ,
176+ // eslint-disable-next-line @typescript-eslint/naming-convention
177+ 'oauth.chase.com#123456' : {
178+ feed : 'oauth.chase.com' ,
179+ domainID : 123456 ,
180+ pending : true ,
181+ liabilityType : 'corporate' ,
182+ } ,
183+ } ,
184+ { status : 'loaded' } ,
185+ ] ) ;
186+ const { result} = renderHook ( ( ) => useIsBlockedToAddFeed ( mockPolicyID ) ) ;
187+
188+ // Both regular and pending feeds count, so user should be blocked
189+ expect ( result . current . isBlockedToAddNewFeeds ) . toBe ( true ) ;
190+ } ) ;
191+
192+ it ( 'should return true if collect policy has only pending feed (no regular feeds)' , ( ) => {
193+ ( useCardFeeds as jest . Mock ) . mockReturnValue ( [
194+ {
195+ // eslint-disable-next-line @typescript-eslint/naming-convention
196+ 'oauth.chase.com#123456' : {
197+ feed : 'oauth.chase.com' ,
198+ domainID : 123456 ,
199+ pending : true ,
200+ liabilityType : 'corporate' ,
201+ } ,
202+ } ,
203+ { status : 'loaded' } ,
204+ ] ) ;
205+ const { result} = renderHook ( ( ) => useIsBlockedToAddFeed ( mockPolicyID ) ) ;
206+
207+ // Pending feeds count toward the limit, so even with only a pending feed, user should be blocked
129208 expect ( result . current . isBlockedToAddNewFeeds ) . toBe ( true ) ;
130209 } ) ;
131210} ) ;
0 commit comments