|
2 | 2 | import {renderHook, waitFor} from '@testing-library/react-native'; |
3 | 3 | import Onyx from 'react-native-onyx'; |
4 | 4 | import useCardFeeds from '@hooks/useCardFeeds'; |
| 5 | +import {getCardFeedWithDomainID} from '@libs/CardUtils'; |
5 | 6 | import CONST from '@src/CONST'; |
6 | 7 | import ONYXKEYS from '@src/ONYXKEYS'; |
7 | 8 | import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates'; |
@@ -109,4 +110,67 @@ describe('useCardFeeds', () => { |
109 | 110 | }); |
110 | 111 | }); |
111 | 112 | }); |
| 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 | + }); |
112 | 176 | }); |
0 commit comments