Skip to content

Commit f082f75

Browse files
authored
Merge pull request #88359 from Expensify/vit-fix-domain-feed-predicate
Add useCardFeeds regression tests for empty linkedPolicyIDs
2 parents 9a3bf0d + ac27fed commit f082f75

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

tests/unit/hooks/useCardFeeds.test.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {renderHook, waitFor} from '@testing-library/react-native';
33
import Onyx from 'react-native-onyx';
44
import useCardFeeds from '@hooks/useCardFeeds';
5+
import {getCardFeedWithDomainID} from '@libs/CardUtils';
56
import CONST from '@src/CONST';
67
import ONYXKEYS from '@src/ONYXKEYS';
78
import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates';
@@ -109,4 +110,67 @@ describe('useCardFeeds', () => {
109110
});
110111
});
111112
});
113+
114+
describe('linkedPolicyIDs predicate filtering', () => {
115+
const combinedFeedKey = getCardFeedWithDomainID(oauthFeed, domainID);
116+
117+
const setupDomainFeed = async (companyCardSettings: {preferredPolicy: string; linkedPolicyIDs?: string[]}) => {
118+
await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {workspaceAccountID: 0});
119+
await Onyx.merge(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${domainID}`, {
120+
settings: {
121+
companyCards: {
122+
[oauthFeed]: {...companyCardSettings, liabilityType: 'corporate'},
123+
},
124+
oAuthAccountDetails: {
125+
[oauthFeed]: {credentials: 'xxxx', expiration: 9999999999, accountList: ['Card 1']},
126+
},
127+
},
128+
});
129+
await Onyx.merge(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${domainID}_${oauthFeed}`, {
130+
'123': {cardID: 123, cardName: 'Card 1'},
131+
});
132+
await waitForBatchedUpdates();
133+
};
134+
135+
it('includes domain feeds when linkedPolicyIDs contains only an empty string and preferredPolicy matches', async () => {
136+
await setupDomainFeed({preferredPolicy: policyID, linkedPolicyIDs: ['']});
137+
138+
const {result} = renderHook(() => useCardFeeds(policyID));
139+
await waitForBatchedUpdates();
140+
141+
// The combined feed key contains '.', so wrap it in an array to disable
142+
// jest's nested-path interpretation in toHaveProperty.
143+
await waitFor(() => expect(result.current[0]).toHaveProperty([combinedFeedKey]));
144+
});
145+
146+
it('includes domain feeds when linkedPolicyIDs is an empty array and preferredPolicy matches', async () => {
147+
await setupDomainFeed({preferredPolicy: policyID, linkedPolicyIDs: []});
148+
149+
const {result} = renderHook(() => useCardFeeds(policyID));
150+
await waitForBatchedUpdates();
151+
152+
await waitFor(() => expect(result.current[0]).toHaveProperty([combinedFeedKey]));
153+
});
154+
155+
it('excludes feeds when linkedPolicyIDs explicitly lists other policies and preferredPolicy does not match', async () => {
156+
await setupDomainFeed({preferredPolicy: 'OTHER_POLICY', linkedPolicyIDs: ['OTHER_POLICY']});
157+
158+
const {result} = renderHook(() => useCardFeeds(policyID));
159+
await waitForBatchedUpdates();
160+
161+
// Wait for the hook to finish loading before asserting absence, so the negative
162+
// assertion can't pass vacuously while Onyx is still hydrating.
163+
await waitFor(() => expect(result.current[1].status).toBe('loaded'));
164+
expect(result.current[0]).not.toHaveProperty([combinedFeedKey]);
165+
});
166+
167+
it('includes feeds when policyID is one of multiple meaningful entries in linkedPolicyIDs', async () => {
168+
await setupDomainFeed({preferredPolicy: 'OTHER_POLICY', linkedPolicyIDs: ['OTHER_POLICY', policyID]});
169+
170+
const {result} = renderHook(() => useCardFeeds(policyID));
171+
await waitForBatchedUpdates();
172+
173+
await waitFor(() => expect(result.current[0]).toHaveProperty([combinedFeedKey]));
174+
});
175+
});
112176
});

0 commit comments

Comments
 (0)