|
| 1 | +import {act, renderHook} from '@testing-library/react-native'; |
| 2 | +import Onyx from 'react-native-onyx'; |
| 3 | +import useFeedKeysWithAssignedCards from '@hooks/useFeedKeysWithAssignedCards'; |
| 4 | +import useLocalize from '@hooks/useLocalize'; |
| 5 | +import useFilterFeedData from '@src/components/Search/hooks/useFilterFeedData'; |
| 6 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 7 | +import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates'; |
| 8 | + |
| 9 | +jest.mock('@hooks/useLocalize'); |
| 10 | +jest.mock('@hooks/useFeedKeysWithAssignedCards'); |
| 11 | + |
| 12 | +const mockUseLocalize = useLocalize as jest.Mock; |
| 13 | +const mockUseFeedKeysWithAssignedCards = useFeedKeysWithAssignedCards as jest.Mock; |
| 14 | + |
| 15 | +describe('useFilterFeedData', () => { |
| 16 | + beforeAll(() => { |
| 17 | + Onyx.init({keys: ONYXKEYS}); |
| 18 | + }); |
| 19 | + |
| 20 | + beforeEach(async () => { |
| 21 | + jest.clearAllMocks(); |
| 22 | + mockUseLocalize.mockReturnValue({ |
| 23 | + translate: (key: string) => key, |
| 24 | + localeCompare: (a: string, b: string) => a.localeCompare(b), |
| 25 | + }); |
| 26 | + mockUseFeedKeysWithAssignedCards.mockReturnValue({}); |
| 27 | + |
| 28 | + await Onyx.clear(); |
| 29 | + await waitForBatchedUpdates(); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should return empty values when no data is available', async () => { |
| 33 | + const {result} = renderHook(() => useFilterFeedData()); |
| 34 | + |
| 35 | + expect(result.current.feedOptions).toEqual([]); |
| 36 | + expect(result.current.feedValue).toEqual([]); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should return feed options when feeds are available in Onyx', async () => { |
| 40 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}123`, { |
| 41 | + settings: { |
| 42 | + companyCards: { |
| 43 | + vcf: {}, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + const {result} = renderHook(() => useFilterFeedData()); |
| 49 | + |
| 50 | + // Expected feed id is fundID_feed |
| 51 | + expect(result.current.feedOptions).toHaveLength(1); |
| 52 | + expect(result.current.feedOptions.at(0)?.value).toBe('123_vcf'); |
| 53 | + expect(result.current.feedValue).toHaveLength(0); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should return selected feed value when filter form has values', async () => { |
| 57 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}123`, { |
| 58 | + settings: { |
| 59 | + companyCards: { |
| 60 | + vcf: {}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }); |
| 64 | + await Onyx.merge(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM, {feed: ['123_vcf']}); |
| 65 | + |
| 66 | + const {result} = renderHook(() => useFilterFeedData()); |
| 67 | + |
| 68 | + expect(result.current.feedOptions).toHaveLength(1); |
| 69 | + expect(result.current.feedOptions.at(0)?.value).toBe('123_vcf'); |
| 70 | + expect(result.current.feedValue).toHaveLength(1); |
| 71 | + expect(result.current.feedValue.at(0)?.value).toBe('123_vcf'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should update when Onyx data changes', async () => { |
| 75 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}123`, { |
| 76 | + settings: { |
| 77 | + companyCards: { |
| 78 | + vcf: {}, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + const {result} = renderHook(() => useFilterFeedData()); |
| 84 | + |
| 85 | + expect(result.current.feedOptions).toHaveLength(1); |
| 86 | + expect(result.current.feedOptions.at(0)?.value).toBe('123_vcf'); |
| 87 | + |
| 88 | + // Add another feed |
| 89 | + await act(async () => { |
| 90 | + await Onyx.merge(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}456`, { |
| 91 | + settings: { |
| 92 | + companyCards: { |
| 93 | + cdf: {}, |
| 94 | + }, |
| 95 | + }, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + expect(result.current.feedOptions).toHaveLength(2); |
| 100 | + const values = result.current.feedOptions.map((o) => o.value); |
| 101 | + expect(values.at(0)).toBe('456_cdf'); |
| 102 | + expect(values.at(1)).toBe('123_vcf'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should include Expensify Card feeds from allCards', async () => { |
| 106 | + await Onyx.merge(ONYXKEYS.DERIVED.PERSONAL_AND_WORKSPACE_CARD_LIST, { |
| 107 | + card1: { |
| 108 | + bank: 'Expensify Card', |
| 109 | + fundID: '999', |
| 110 | + }, |
| 111 | + }); |
| 112 | + |
| 113 | + const {result} = renderHook(() => useFilterFeedData()); |
| 114 | + |
| 115 | + expect(result.current.feedOptions).toHaveLength(1); |
| 116 | + expect(result.current.feedOptions.at(0)?.value).toBe('999_Expensify Card'); |
| 117 | + }); |
| 118 | +}); |
0 commit comments