From ad5c88976d413f31492dce0bbc155ccae1b40d41 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 6 May 2024 17:11:23 -0600 Subject: [PATCH 01/21] expose onEndReached and onEndReachedThreshold --- src/components/SelectionList/BaseSelectionList.tsx | 4 ++++ src/components/SelectionList/types.ts | 11 +++++++++++ src/libs/actions/OnyxUpdateManager/utils/index.ts | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/components/SelectionList/BaseSelectionList.tsx b/src/components/SelectionList/BaseSelectionList.tsx index 76c54d3c37cb..0982dc11b409 100644 --- a/src/components/SelectionList/BaseSelectionList.tsx +++ b/src/components/SelectionList/BaseSelectionList.tsx @@ -76,6 +76,8 @@ function BaseSelectionList( sectionTitleStyles, textInputAutoFocus = true, shouldTextInputInterceptSwipe = false, + onEndReached = () => {}, + onEndReachedThreshold, }: BaseSelectionListProps, ref: ForwardedRef, ) { @@ -618,6 +620,8 @@ function BaseSelectionList( onLayout={onSectionListLayout} style={(!maxToRenderPerBatch || (shouldHideListOnInitialRender && isInitialSectionListRender)) && styles.opacity0} ListFooterComponent={listFooterContent ?? ShowMoreButtonInstance} + onEndReached={onEndReached} + onEndReachedThreshold={onEndReachedThreshold} /> {children} diff --git a/src/components/SelectionList/types.ts b/src/components/SelectionList/types.ts index 8a2a1efdd030..538d61b61a2b 100644 --- a/src/components/SelectionList/types.ts +++ b/src/components/SelectionList/types.ts @@ -404,6 +404,17 @@ type BaseSelectionListProps = Partial & { * When false, the list will render immediately and scroll to the bottom which works great for small lists. */ shouldHideListOnInitialRender?: boolean; + + /** Called once when the scroll position gets within onEndReachedThreshold of the rendered content. */ + onEndReached?: () => void; + + /** + * How far from the end (in units of visible length of the list) the bottom edge of the + * list must be from the end of the content to trigger the `onEndReached` callback. + * Thus a value of 0.5 will trigger `onEndReached` when the end of the content is + * within half the visible length of the list. + */ + onEndReachedThreshold?: number; } & TRightHandSideComponent; type SelectionListHandle = { diff --git a/src/libs/actions/OnyxUpdateManager/utils/index.ts b/src/libs/actions/OnyxUpdateManager/utils/index.ts index 4df22d292d19..1ed7c2c0ff7e 100644 --- a/src/libs/actions/OnyxUpdateManager/utils/index.ts +++ b/src/libs/actions/OnyxUpdateManager/utils/index.ts @@ -109,7 +109,7 @@ function validateAndApplyDeferredUpdates(clientLastUpdateID?: number): Promise Date: Mon, 6 May 2024 17:27:11 -0600 Subject: [PATCH 02/21] call fetchMoreResults --- src/CONST.ts | 2 ++ src/ROUTES.ts | 4 ++-- src/components/Search.tsx | 10 ++++++++++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index ae78c2f02a05..bb7737be0add 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -4729,6 +4729,8 @@ const CONST = { CARD: 'card', DISTANCE: 'distance', }, + + SEARCH_RESULTS_PAGE_SIZE: 50, } as const; type Country = keyof typeof CONST.ALL_COUNTRIES; diff --git a/src/ROUTES.ts b/src/ROUTES.ts index d59bb9a3a981..9c428bfabeba 100644 --- a/src/ROUTES.ts +++ b/src/ROUTES.ts @@ -24,8 +24,8 @@ const ROUTES = { ALL_SETTINGS: 'all-settings', SEARCH: { - route: '/search/:query', - getRoute: (query: string) => `search/${query}` as const, + route: '/search/:query/:offset', + getRoute: (query: string, offset: number) => `search/${query}/${offset}` as const, }, SEARCH_REPORT: { diff --git a/src/components/Search.tsx b/src/components/Search.tsx index d8d3160a2633..4c18d7e3a311 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -11,6 +11,7 @@ import useCustomBackHandler from '@pages/Search/useCustomBackHandler'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; +import CONST from '@src/CONST'; import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; import SelectionList from './SelectionList'; import SearchTableHeader from './SelectionList/SearchTableHeader'; @@ -55,6 +56,13 @@ function Search({query}: SearchProps) { Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute(query, reportID)); }; + const fetchMoreResults = () => { + const currentOffset = searchResults?.search?.offset ?? 0; + const nexOffset = currentOffset + CONST.SEARCH_RESULTS_PAGE_SIZE; + + Navigation.navigate(ROUTES.SEARCH.getRoute(query, nexOffset)); + } + const ListItem = SearchUtils.getListItem(); const data = SearchUtils.getSections(searchResults?.data ?? {}); const shouldShowMerchant = SearchUtils.getShouldShowMerchant(searchResults?.data ?? {}); @@ -70,6 +78,8 @@ function Search({query}: SearchProps) { shouldPreventDefaultFocusOnSelectRow={!DeviceCapabilities.canUseTouchScreen()} listHeaderWrapperStyle={[styles.ph9, styles.pv3, styles.pb5]} containerStyle={[styles.pv0]} + onEndReachedThreshold={0.75} + onEndReached={fetchMoreResults} /> ); } From 957853df61ae3245c42f3923744ae59b2c57b3f6 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Mon, 6 May 2024 17:37:32 -0600 Subject: [PATCH 03/21] add consts, types, tweak function --- src/ROUTES.ts | 2 +- src/components/Search.tsx | 15 +++++++++------ src/libs/API/parameters/Search.ts | 1 + src/libs/actions/Search.ts | 4 ++-- src/pages/Search/SearchPage.tsx | 3 ++- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/ROUTES.ts b/src/ROUTES.ts index 9c428bfabeba..da2ee1f5701c 100644 --- a/src/ROUTES.ts +++ b/src/ROUTES.ts @@ -25,7 +25,7 @@ const ROUTES = { SEARCH: { route: '/search/:query/:offset', - getRoute: (query: string, offset: number) => `search/${query}/${offset}` as const, + getRoute: (query: string, offset = 0) => `search/${query}/${offset}` as const, }, SEARCH_REPORT: { diff --git a/src/components/Search.tsx b/src/components/Search.tsx index 4c18d7e3a311..6a09a82f5463 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -19,9 +19,10 @@ import TableListItemSkeleton from './Skeletons/TableListItemSkeleton'; type SearchProps = { query: string; + offset: number; }; -function Search({query}: SearchProps) { +function Search({query, offset}: SearchProps) { const {isOffline} = useNetwork(); const styles = useThemeStyles(); useCustomBackHandler(); @@ -34,8 +35,8 @@ function Search({query}: SearchProps) { return; } - SearchActions.search(query); - }, [query, isOffline]); + SearchActions.search(query, offset); + }, [query, offset, isOffline]); const isLoading = (!isOffline && isLoadingOnyxValue(searchResultsMeta)) || searchResults?.data === undefined; const shouldShowEmptyState = !isLoading && isEmptyObject(searchResults?.data); @@ -57,10 +58,12 @@ function Search({query}: SearchProps) { }; const fetchMoreResults = () => { - const currentOffset = searchResults?.search?.offset ?? 0; - const nexOffset = currentOffset + CONST.SEARCH_RESULTS_PAGE_SIZE; + if (!searchResults?.search?.hasMoreResults) { + return; + } - Navigation.navigate(ROUTES.SEARCH.getRoute(query, nexOffset)); + const nextOffset = offset + CONST.SEARCH_RESULTS_PAGE_SIZE; + Navigation.navigate(ROUTES.SEARCH.getRoute(query, nextOffset)); } const ListItem = SearchUtils.getListItem(); diff --git a/src/libs/API/parameters/Search.ts b/src/libs/API/parameters/Search.ts index 44e8c9f2d0fb..415c6b7cfd35 100644 --- a/src/libs/API/parameters/Search.ts +++ b/src/libs/API/parameters/Search.ts @@ -1,6 +1,7 @@ type SearchParams = { query: string; hash: number; + offset: number; }; export default SearchParams; diff --git a/src/libs/actions/Search.ts b/src/libs/actions/Search.ts index 03179fae93cc..fb6f82c0b1ab 100644 --- a/src/libs/actions/Search.ts +++ b/src/libs/actions/Search.ts @@ -2,9 +2,9 @@ import * as API from '@libs/API'; import {READ_COMMANDS} from '@libs/API/types'; import * as SearchUtils from '@libs/SearchUtils'; -function search(query: string) { +function search(query: string, offset: number) { const hash = SearchUtils.getQueryHash(query); - API.read(READ_COMMANDS.SEARCH, {query, hash}); + API.read(READ_COMMANDS.SEARCH, {query, hash, offset}); } export { diff --git a/src/pages/Search/SearchPage.tsx b/src/pages/Search/SearchPage.tsx index 499b09fd4eb9..19d857f2f656 100644 --- a/src/pages/Search/SearchPage.tsx +++ b/src/pages/Search/SearchPage.tsx @@ -18,6 +18,7 @@ type SearchPageProps = StackScreenProps - + ); From 9936959d4edfdb9bbe94b91d6635697a723a852f Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Tue, 7 May 2024 16:20:29 -0600 Subject: [PATCH 04/21] add offset --- src/pages/Search/SearchPage.tsx | 2 +- src/pages/Search/SearchPageBottomTab.tsx | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pages/Search/SearchPage.tsx b/src/pages/Search/SearchPage.tsx index 19d857f2f656..8e204dcb24df 100644 --- a/src/pages/Search/SearchPage.tsx +++ b/src/pages/Search/SearchPage.tsx @@ -18,7 +18,7 @@ type SearchPageProps = StackScreenProps - {isSmallScreenWidth && } + {isSmallScreenWidth && } ); From b235980013e867421cf8f5bbc9cbfc95a9dd273f Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 8 May 2024 10:18:41 -0600 Subject: [PATCH 05/21] add loading skeleton --- src/components/Search.tsx | 21 +++++++++---- .../SelectionList/BaseSelectionList.tsx | 3 +- src/components/SelectionList/types.ts | 2 +- .../Skeletons/ItemListSkeletonView.tsx | 9 ++++-- .../Skeletons/TableListItemSkeleton.tsx | 4 ++- src/libs/actions/Search.ts | 30 ++++++++++++++++++- src/pages/Search/SearchPage.tsx | 5 +++- src/pages/Search/SearchPageBottomTab.tsx | 7 ++++- src/types/onyx/SearchResults.ts | 1 + 9 files changed, 68 insertions(+), 14 deletions(-) diff --git a/src/components/Search.tsx b/src/components/Search.tsx index 6a09a82f5463..b23b350af5ab 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -1,4 +1,4 @@ -import React, {useEffect} from 'react'; +import React, {useEffect, useRef} from 'react'; import {useOnyx} from 'react-native-onyx'; import useNetwork from '@hooks/useNetwork'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -8,10 +8,10 @@ import * as SearchUtils from '@libs/SearchUtils'; import Navigation from '@navigation/Navigation'; import EmptySearchView from '@pages/Search/EmptySearchView'; import useCustomBackHandler from '@pages/Search/useCustomBackHandler'; +import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; -import CONST from '@src/CONST'; import isLoadingOnyxValue from '@src/types/utils/isLoadingOnyxValue'; import SelectionList from './SelectionList'; import SearchTableHeader from './SelectionList/SearchTableHeader'; @@ -38,10 +38,11 @@ function Search({query, offset}: SearchProps) { SearchActions.search(query, offset); }, [query, offset, isOffline]); - const isLoading = (!isOffline && isLoadingOnyxValue(searchResultsMeta)) || searchResults?.data === undefined; - const shouldShowEmptyState = !isLoading && isEmptyObject(searchResults?.data); + const isLoadingInitialItems = (!isOffline && isLoadingOnyxValue(searchResultsMeta)) || searchResults?.data === undefined; + const isLoadingMoreItems = !isLoadingInitialItems && searchResults?.search?.isLoading; + const shouldShowEmptyState = !isLoadingInitialItems && isEmptyObject(searchResults?.data); - if (isLoading) { + if (isLoadingInitialItems) { return ; } @@ -64,7 +65,7 @@ function Search({query, offset}: SearchProps) { const nextOffset = offset + CONST.SEARCH_RESULTS_PAGE_SIZE; Navigation.navigate(ROUTES.SEARCH.getRoute(query, nextOffset)); - } + }; const ListItem = SearchUtils.getListItem(); const data = SearchUtils.getSections(searchResults?.data ?? {}); @@ -83,6 +84,14 @@ function Search({query, offset}: SearchProps) { containerStyle={[styles.pv0]} onEndReachedThreshold={0.75} onEndReached={fetchMoreResults} + listFooterContent={ + isLoadingMoreItems ? ( + + ) : undefined + } /> ); } diff --git a/src/components/SelectionList/BaseSelectionList.tsx b/src/components/SelectionList/BaseSelectionList.tsx index 0982dc11b409..ee2f53dfcde1 100644 --- a/src/components/SelectionList/BaseSelectionList.tsx +++ b/src/components/SelectionList/BaseSelectionList.tsx @@ -78,6 +78,7 @@ function BaseSelectionList( shouldTextInputInterceptSwipe = false, onEndReached = () => {}, onEndReachedThreshold, + onContentSizeChange, }: BaseSelectionListProps, ref: ForwardedRef, ) { @@ -486,7 +487,7 @@ function BaseSelectionList( [flattenedSections.allOptions, setFocusedIndex, updateAndScrollToFocusedIndex], ); - useImperativeHandle(ref, () => ({scrollAndHighlightItem}), [scrollAndHighlightItem]); + useImperativeHandle(ref, () => ({scrollAndHighlightItem, scrollToIndex}), [scrollAndHighlightItem, scrollToIndex]); /** Selects row when pressing Enter */ useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, selectFocusedOption, { diff --git a/src/components/SelectionList/types.ts b/src/components/SelectionList/types.ts index 538d61b61a2b..5ee9871f0888 100644 --- a/src/components/SelectionList/types.ts +++ b/src/components/SelectionList/types.ts @@ -408,7 +408,7 @@ type BaseSelectionListProps = Partial & { /** Called once when the scroll position gets within onEndReachedThreshold of the rendered content. */ onEndReached?: () => void; - /** + /** * How far from the end (in units of visible length of the list) the bottom edge of the * list must be from the end of the content to trigger the `onEndReached` callback. * Thus a value of 0.5 will trigger `onEndReached` when the end of the content is diff --git a/src/components/Skeletons/ItemListSkeletonView.tsx b/src/components/Skeletons/ItemListSkeletonView.tsx index 00854471f2e3..5c46dbdddbfc 100644 --- a/src/components/Skeletons/ItemListSkeletonView.tsx +++ b/src/components/Skeletons/ItemListSkeletonView.tsx @@ -8,13 +8,14 @@ import CONST from '@src/CONST'; type ListItemSkeletonProps = { shouldAnimate?: boolean; renderSkeletonItem: (args: {itemIndex: number}) => React.ReactNode; + fixedNumItems?: number; }; -function ItemListSkeletonView({shouldAnimate = true, renderSkeletonItem}: ListItemSkeletonProps) { +function ItemListSkeletonView({shouldAnimate = true, renderSkeletonItem, fixedNumItems}: ListItemSkeletonProps) { const theme = useTheme(); const themeStyles = useThemeStyles(); - const [numItems, setNumItems] = useState(0); + const [numItems, setNumItems] = useState(fixedNumItems ?? 0); const skeletonViewItems = useMemo(() => { const items = []; for (let i = 0; i < numItems; i++) { @@ -38,6 +39,10 @@ function ItemListSkeletonView({shouldAnimate = true, renderSkeletonItem}: ListIt { + if (fixedNumItems) { + return; + } + const newNumItems = Math.ceil(event.nativeEvent.layout.height / CONST.LHN_SKELETON_VIEW_ITEM_HEIGHT); if (newNumItems === numItems) { return; diff --git a/src/components/Skeletons/TableListItemSkeleton.tsx b/src/components/Skeletons/TableListItemSkeleton.tsx index d24268521100..4b2c214921d0 100644 --- a/src/components/Skeletons/TableListItemSkeleton.tsx +++ b/src/components/Skeletons/TableListItemSkeleton.tsx @@ -4,16 +4,18 @@ import ItemListSkeletonView from './ItemListSkeletonView'; type TableListItemSkeletonProps = { shouldAnimate?: boolean; + fixedNumItems?: number; }; const barHeight = '10'; const shortBarWidth = '40'; const longBarWidth = '120'; -function TableListItemSkeleton({shouldAnimate = true}: TableListItemSkeletonProps) { +function TableListItemSkeleton({shouldAnimate = true, fixedNumItems}: TableListItemSkeletonProps) { return ( ( <> - + ); diff --git a/src/pages/Search/SearchPageBottomTab.tsx b/src/pages/Search/SearchPageBottomTab.tsx index b0b8a73f3e5a..6954a8cd1a8e 100644 --- a/src/pages/Search/SearchPageBottomTab.tsx +++ b/src/pages/Search/SearchPageBottomTab.tsx @@ -41,7 +41,12 @@ function SearchPageBottomTab() { shouldDisplaySearch={false} /> - {isSmallScreenWidth && } + {isSmallScreenWidth && ( + + )} ); diff --git a/src/types/onyx/SearchResults.ts b/src/types/onyx/SearchResults.ts index af23821d70e8..a91366f18046 100644 --- a/src/types/onyx/SearchResults.ts +++ b/src/types/onyx/SearchResults.ts @@ -6,6 +6,7 @@ type SearchResultsInfo = { offset: number; type: string; hasMoreResults: boolean; + isLoading: boolean; }; type SearchPersonalDetails = { From d56a3cda74c521bbb63d96287ef53e381580e17e Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 8 May 2024 10:57:55 -0600 Subject: [PATCH 06/21] set route params --- src/components/Search.tsx | 8 ++++++-- src/components/SelectionList/BaseSelectionList.tsx | 3 +-- src/libs/actions/OnyxUpdateManager/utils/index.ts | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/components/Search.tsx b/src/components/Search.tsx index b23b350af5ab..bc1926426f36 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -1,4 +1,4 @@ -import React, {useEffect, useRef} from 'react'; +import React, {useEffect} from 'react'; import {useOnyx} from 'react-native-onyx'; import useNetwork from '@hooks/useNetwork'; import useThemeStyles from '@hooks/useThemeStyles'; @@ -63,8 +63,12 @@ function Search({query, offset}: SearchProps) { return; } + if (isLoadingMoreItems) { + return; + } + const nextOffset = offset + CONST.SEARCH_RESULTS_PAGE_SIZE; - Navigation.navigate(ROUTES.SEARCH.getRoute(query, nextOffset)); + Navigation.setParams({offset: nextOffset}); }; const ListItem = SearchUtils.getListItem(); diff --git a/src/components/SelectionList/BaseSelectionList.tsx b/src/components/SelectionList/BaseSelectionList.tsx index ee2f53dfcde1..0982dc11b409 100644 --- a/src/components/SelectionList/BaseSelectionList.tsx +++ b/src/components/SelectionList/BaseSelectionList.tsx @@ -78,7 +78,6 @@ function BaseSelectionList( shouldTextInputInterceptSwipe = false, onEndReached = () => {}, onEndReachedThreshold, - onContentSizeChange, }: BaseSelectionListProps, ref: ForwardedRef, ) { @@ -487,7 +486,7 @@ function BaseSelectionList( [flattenedSections.allOptions, setFocusedIndex, updateAndScrollToFocusedIndex], ); - useImperativeHandle(ref, () => ({scrollAndHighlightItem, scrollToIndex}), [scrollAndHighlightItem, scrollToIndex]); + useImperativeHandle(ref, () => ({scrollAndHighlightItem}), [scrollAndHighlightItem]); /** Selects row when pressing Enter */ useKeyboardShortcut(CONST.KEYBOARD_SHORTCUTS.ENTER, selectFocusedOption, { diff --git a/src/libs/actions/OnyxUpdateManager/utils/index.ts b/src/libs/actions/OnyxUpdateManager/utils/index.ts index 0b24cf5975db..6d51d3ac02a6 100644 --- a/src/libs/actions/OnyxUpdateManager/utils/index.ts +++ b/src/libs/actions/OnyxUpdateManager/utils/index.ts @@ -114,7 +114,7 @@ function validateAndApplyDeferredUpdates(clientLastUpdateID?: number, previousPa // the initial "updatesAfterGaps" and all new deferred updates will be applied in order, // as long as there was no new gap detected. Otherwise repeat the process. - const newLastUpdateIDFromClient = 0; + const newLastUpdateIDFromClient = clientLastUpdateID ?? lastUpdateIDAppliedToClient ?? 0; deferredUpdatesProxy.deferredUpdates = {...deferredUpdatesProxy.deferredUpdates, ...updatesAfterGaps}; From 18c8069434c23a4eaefc39f66e9c2685ce7b7456 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 8 May 2024 12:35:09 -0600 Subject: [PATCH 07/21] do not use route param --- src/ROUTES.ts | 4 ++-- src/components/Search.tsx | 20 +++++++------------- src/pages/Search/SearchPage.tsx | 2 -- src/pages/Search/SearchPageBottomTab.tsx | 2 -- 4 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/ROUTES.ts b/src/ROUTES.ts index 8325dff1f6e4..9f84b3db0de1 100644 --- a/src/ROUTES.ts +++ b/src/ROUTES.ts @@ -24,8 +24,8 @@ const ROUTES = { ALL_SETTINGS: 'all-settings', SEARCH: { - route: '/search/:query/:offset', - getRoute: (query: string, offset = 0) => `search/${query}/${offset}` as const, + route: '/search/:query', + getRoute: (query: string) => `search/${query}` as const, }, SEARCH_REPORT: { diff --git a/src/components/Search.tsx b/src/components/Search.tsx index bc1926426f36..f3a895808373 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -19,10 +19,9 @@ import TableListItemSkeleton from './Skeletons/TableListItemSkeleton'; type SearchProps = { query: string; - offset: number; }; -function Search({query, offset}: SearchProps) { +function Search({query}: SearchProps) { const {isOffline} = useNetwork(); const styles = useThemeStyles(); useCustomBackHandler(); @@ -35,8 +34,8 @@ function Search({query, offset}: SearchProps) { return; } - SearchActions.search(query, offset); - }, [query, offset, isOffline]); + SearchActions.search(query, 0); + }, [query, isOffline]); const isLoadingInitialItems = (!isOffline && isLoadingOnyxValue(searchResultsMeta)) || searchResults?.data === undefined; const isLoadingMoreItems = !isLoadingInitialItems && searchResults?.search?.isLoading; @@ -59,16 +58,11 @@ function Search({query, offset}: SearchProps) { }; const fetchMoreResults = () => { - if (!searchResults?.search?.hasMoreResults) { + if (!searchResults?.search?.hasMoreResults || isLoadingInitialItems || isLoadingMoreItems) { return; } - - if (isLoadingMoreItems) { - return; - } - - const nextOffset = offset + CONST.SEARCH_RESULTS_PAGE_SIZE; - Navigation.setParams({offset: nextOffset}); + const currentOffset = searchResults?.search?.offset ?? 0; + SearchActions.search(query, currentOffset + CONST.SEARCH_RESULTS_PAGE_SIZE); }; const ListItem = SearchUtils.getListItem(); @@ -92,7 +86,7 @@ function Search({query, offset}: SearchProps) { isLoadingMoreItems ? ( ) : undefined } diff --git a/src/pages/Search/SearchPage.tsx b/src/pages/Search/SearchPage.tsx index 71ed2a066b9e..b8cf277d5a31 100644 --- a/src/pages/Search/SearchPage.tsx +++ b/src/pages/Search/SearchPage.tsx @@ -18,7 +18,6 @@ type SearchPageProps = StackScreenProps diff --git a/src/pages/Search/SearchPageBottomTab.tsx b/src/pages/Search/SearchPageBottomTab.tsx index 6954a8cd1a8e..5a43ded89e09 100644 --- a/src/pages/Search/SearchPageBottomTab.tsx +++ b/src/pages/Search/SearchPageBottomTab.tsx @@ -20,7 +20,6 @@ function SearchPageBottomTab() { const styles = useThemeStyles(); const currentQuery = activeRoute?.params && 'query' in activeRoute.params ? activeRoute?.params?.query : ''; - const offset = activeRoute?.params && 'offset' in activeRoute.params ? parseInt(activeRoute?.params?.offset as string, 10) : 0; const query = currentQuery as SearchQuery; const isValidQuery = Object.values(CONST.TAB_SEARCH).includes(query); @@ -44,7 +43,6 @@ function SearchPageBottomTab() { {isSmallScreenWidth && ( )} From 40652b458ed9bc6bb26415e3a62ef21c4e0525cf Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 8 May 2024 12:39:39 -0600 Subject: [PATCH 08/21] add default --- src/components/Search.tsx | 6 +++--- src/libs/actions/Search.ts | 2 +- src/pages/Search/SearchPage.tsx | 4 +--- src/pages/Search/SearchPageBottomTab.tsx | 6 +----- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/components/Search.tsx b/src/components/Search.tsx index f3a895808373..3b02ebbf2a49 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -34,11 +34,11 @@ function Search({query}: SearchProps) { return; } - SearchActions.search(query, 0); + SearchActions.search(query); }, [query, isOffline]); const isLoadingInitialItems = (!isOffline && isLoadingOnyxValue(searchResultsMeta)) || searchResults?.data === undefined; - const isLoadingMoreItems = !isLoadingInitialItems && searchResults?.search?.isLoading; + const isLoadingMoreItems = searchResults?.search?.isLoading ?? false; const shouldShowEmptyState = !isLoadingInitialItems && isEmptyObject(searchResults?.data); if (isLoadingInitialItems) { @@ -86,7 +86,7 @@ function Search({query}: SearchProps) { isLoadingMoreItems ? ( ) : undefined } diff --git a/src/libs/actions/Search.ts b/src/libs/actions/Search.ts index 965e7b01e5c8..8902dbf7b648 100644 --- a/src/libs/actions/Search.ts +++ b/src/libs/actions/Search.ts @@ -5,7 +5,7 @@ import {READ_COMMANDS} from '@libs/API/types'; import * as SearchUtils from '@libs/SearchUtils'; import ONYXKEYS from '@src/ONYXKEYS'; -function search(query: string, offset: number) { +function search(query: string, offset = 0) { const hash = SearchUtils.getQueryHash(query); const optimisticData: OnyxUpdate[] = [ diff --git a/src/pages/Search/SearchPage.tsx b/src/pages/Search/SearchPage.tsx index b8cf277d5a31..499b09fd4eb9 100644 --- a/src/pages/Search/SearchPage.tsx +++ b/src/pages/Search/SearchPage.tsx @@ -36,9 +36,7 @@ function SearchPage({route}: SearchPageProps) { icon={Illustrations.MoneyReceipts} shouldShowBackButton={false} /> - + ); diff --git a/src/pages/Search/SearchPageBottomTab.tsx b/src/pages/Search/SearchPageBottomTab.tsx index 5a43ded89e09..3d2ee267b7a9 100644 --- a/src/pages/Search/SearchPageBottomTab.tsx +++ b/src/pages/Search/SearchPageBottomTab.tsx @@ -40,11 +40,7 @@ function SearchPageBottomTab() { shouldDisplaySearch={false} /> - {isSmallScreenWidth && ( - - )} + {isSmallScreenWidth && } ); From d1d34d4320b3ee697280273b64f4d6e9414af6f3 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 8 May 2024 12:41:44 -0600 Subject: [PATCH 09/21] fix isLoadingMoreItems --- src/components/Search.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Search.tsx b/src/components/Search.tsx index 3b02ebbf2a49..c8efe13f7003 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -38,7 +38,7 @@ function Search({query}: SearchProps) { }, [query, isOffline]); const isLoadingInitialItems = (!isOffline && isLoadingOnyxValue(searchResultsMeta)) || searchResults?.data === undefined; - const isLoadingMoreItems = searchResults?.search?.isLoading ?? false; + const isLoadingMoreItems = !isLoadingInitialItems && searchResults?.search?.isLoading; const shouldShowEmptyState = !isLoadingInitialItems && isEmptyObject(searchResults?.data); if (isLoadingInitialItems) { From 9457168007e4fead24e0af9de6239e9b2718d9ec Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 8 May 2024 12:43:31 -0600 Subject: [PATCH 10/21] reduce number of items --- src/components/Search.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/Search.tsx b/src/components/Search.tsx index c8efe13f7003..7cc7ad7f7f9b 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -86,7 +86,7 @@ function Search({query}: SearchProps) { isLoadingMoreItems ? ( ) : undefined } From 0677a8a58754286e3e3337056163d6af7c6dc512 Mon Sep 17 00:00:00 2001 From: Carlos Martins Date: Wed, 15 May 2024 07:59:56 +0900 Subject: [PATCH 11/21] fix param order --- src/components/Search.tsx | 4 ++-- src/libs/actions/Search.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/Search.tsx b/src/components/Search.tsx index 7d8dbb2de6a1..27e87017bfee 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -36,7 +36,7 @@ function Search({query, policyIDs}: SearchProps) { return; } - SearchActions.search(hash, query, policyIDs); + SearchActions.search(hash, query, 0, policyIDs); // eslint-disable-next-line react-hooks/exhaustive-deps }, [hash, isOffline]); @@ -65,7 +65,7 @@ function Search({query, policyIDs}: SearchProps) { return; } const currentOffset = searchResults?.search?.offset ?? 0; - SearchActions.search(query, currentOffset + CONST.SEARCH_RESULTS_PAGE_SIZE); + SearchActions.search(hash, query, currentOffset + CONST.SEARCH_RESULTS_PAGE_SIZE); }; const type = SearchUtils.getSearchType(searchResults?.search); diff --git a/src/libs/actions/Search.ts b/src/libs/actions/Search.ts index bd26a1829400..fae9a427f4fa 100644 --- a/src/libs/actions/Search.ts +++ b/src/libs/actions/Search.ts @@ -4,7 +4,7 @@ import * as API from '@libs/API'; import {READ_COMMANDS} from '@libs/API/types'; import ONYXKEYS from '@src/ONYXKEYS'; -function search(hash: number, query: string, offset = 0, policyIDs?: string, ) { +function search(hash: number, query: string, offset = 0, policyIDs?: string) { const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, From 036cd79beaf5d5a502c1b874f24ac062ceabe580 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Wed, 15 May 2024 11:46:29 +0800 Subject: [PATCH 12/21] fallback merchant to (none) --- src/pages/iou/request/step/IOURequestStepMerchant.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepMerchant.tsx b/src/pages/iou/request/step/IOURequestStepMerchant.tsx index bc6f71b23228..fc7d39b49089 100644 --- a/src/pages/iou/request/step/IOURequestStepMerchant.tsx +++ b/src/pages/iou/request/step/IOURequestStepMerchant.tsx @@ -97,9 +97,9 @@ function IOURequestStepMerchant({ navigateBack(); return; } - IOU.setMoneyRequestMerchant(transactionID, newMerchant ?? '', !isEditing); + // When creating/editing an expense, newMerchant can be blank so we fall back on PARTIAL_TRANSACTION_MERCHANT + IOU.setMoneyRequestMerchant(transactionID, newMerchant || CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT, !isEditing); if (isEditing) { - // When creating a new expense, newMerchant can be blank so we fall back on PARTIAL_TRANSACTION_MERCHANT IOU.updateMoneyRequestMerchant(transactionID, reportID, newMerchant || CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT, policy, policyTags, policyCategories); } navigateBack(); From 2faa0adb676f5254b84cb8eab69d26b7b93bdbe9 Mon Sep 17 00:00:00 2001 From: Sheena Trepanier Date: Wed, 15 May 2024 15:17:17 -0700 Subject: [PATCH 13/21] Delete docs/articles/expensify-classic/workspaces/reports/Currency.md This file contains duplicate information and doesn't need to exist. I've confirmed all unique data is accounted for in the article about currency we are keeping under classic/workspaces/Currency. --- .../workspaces/reports/Currency.md | 31 ------------------- 1 file changed, 31 deletions(-) delete mode 100644 docs/articles/expensify-classic/workspaces/reports/Currency.md diff --git a/docs/articles/expensify-classic/workspaces/reports/Currency.md b/docs/articles/expensify-classic/workspaces/reports/Currency.md deleted file mode 100644 index 19fd4e8f3723..000000000000 --- a/docs/articles/expensify-classic/workspaces/reports/Currency.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -title: Currency -description: How currency works in Expensify and how to change the default currency in your Expensify workspace ---- -# Overview - -In this article, we’ll outline how to change the default currency in your account and how currency works in Expensify. -Expensify supports expenses in almost every currency in the world. Group workspace admins and individual workspace users can specify the desired output currency for employee reports. Expensify handles the currency conversion process. - -# How to change your default currency - -The default currency for all expenses added to your account is set by the primary company workspace. Just head to **Settings > Workspaces > Group > *[Workspace Name]* > Reports > Report Basics** and select your desired Report Currency. - -If you are not using a group workspace, you can change your default currency under **Settings > Workspaces > Individual > *[Workspace Name]* > Reports** and then choose your desired Report Currency. Please note that the currency selected here will be overridden should you begin reporting on a group workspace. - -# How currency works in Expensify - -When totaling expenses across multiple currencies, we convert them to a single currency, which is the "report currency" of the report's expense workspace, or your personal output currency if no workspace is in use. - -**Important notes:** - -- Currency settings on a workspace are all-or-nothing. To reflect a different output currency in reports, create a new workspace for those employees and adjust the currency settings accordingly. -- Currency settings in the workspace take precedence over a user's individual account settings. - -# How the conversion rate is determined -When converting expenses between currencies, we rely on [Open Exchange Rates](https://openexchangerates.org/) to determine the average bid and ask rate on the expense date. This rate becomes available after the market closes for that day, resulting in varying conversion rates depending on when the expense occurred and how the currencies were trading. - -If the markets were closed on the expense date (e.g., weekends), we use the daily average rate from the last open market day prior to the purchase. For future-dated expenses, we use the most recent available data. Consequently, the report's value may fluctuate until the expense date, potentially leading to unexpected results. You cannot submit reports until the markets have closed for all the expense dates on the report. - -To bypass exchange rate calculations, manually enter expenses in your default currency. These entries will only be converted when included in a report with a different default currency. - From b12858ea5078397bca91373557d6c102ec0e9336 Mon Sep 17 00:00:00 2001 From: Rocio Perez-Cano Date: Wed, 15 May 2024 18:37:40 -0400 Subject: [PATCH 14/21] Fix styles for split preview --- src/components/MoneyRequestConfirmationList.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index 5ffd9beda6fe..33f1258e60c7 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -1233,6 +1233,7 @@ function MoneyRequestConfirmationList({ shouldPreventDefaultFocusOnSelectRow footerContent={footerContent} listFooterContent={listFooterContent} + containerStyle={[styles.flexBasisAuto]} /> ); } From 89d115b1c06efa378ccd7de6cf4b7ce24b2457f8 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 11:42:13 +0200 Subject: [PATCH 15/21] Fix ts issues related with TestHelper --- tests/actions/PolicyCategoryTest.ts | 34 +++++++++++------------------ tests/actions/PolicyMemberTest.ts | 28 ++++++++++-------------- tests/actions/PolicyProfileTest.ts | 10 ++++----- 3 files changed, 29 insertions(+), 43 deletions(-) diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index 2817a1661db4..7c060b40467e 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -6,6 +6,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import createRandomPolicy from '../utils/collections/policies'; import createRandomPolicyCategories from '../utils/collections/policyCategory'; import * as TestHelper from '../utils/TestHelper'; +import type {MockFetch} from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; OnyxUpdateManager(); @@ -16,9 +17,10 @@ describe('actions/PolicyCategory', () => { }); }); + let mockFetch: MockFetch; beforeEach(() => { - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -27,8 +29,7 @@ describe('actions/PolicyCategory', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresCategory = false; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.setWorkspaceRequiresCategory(fakePolicy.id, true); await waitForBatchedUpdates(); @@ -46,8 +47,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -68,8 +68,7 @@ describe('actions/PolicyCategory', () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); const newCategoryName = 'New category'; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.createPolicyCategory(fakePolicy.id, newCategoryName); @@ -89,8 +88,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -115,8 +113,7 @@ describe('actions/PolicyCategory', () => { const fakeCategories = createRandomPolicyCategories(3); const oldCategoryName = Object.keys(fakeCategories)[0]; const newCategoryName = 'Updated category'; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.renamePolicyCategory(fakePolicy.id, { @@ -140,8 +137,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -170,8 +166,7 @@ describe('actions/PolicyCategory', () => { enabled: true, }, }; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.setWorkspaceCategoryEnabled(fakePolicy.id, categoriesToUpdate); @@ -191,8 +186,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -217,8 +211,7 @@ describe('actions/PolicyCategory', () => { const fakeCategories = createRandomPolicyCategories(3); const categoryNameToDelete = Object.keys(fakeCategories)[0]; const categoriesToDelete = [categoryNameToDelete]; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.deleteWorkspaceCategories(fakePolicy.id, categoriesToDelete); @@ -235,8 +228,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 8d982d4a1892..4f3afc51ec66 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -11,6 +11,7 @@ import createRandomPolicy from '../utils/collections/policies'; import createRandomReportAction from '../utils/collections/reportActions'; import createRandomReport from '../utils/collections/reports'; import * as TestHelper from '../utils/TestHelper'; +import type {MockFetch} from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; OnyxUpdateManager(); @@ -21,9 +22,10 @@ describe('actions/PolicyMember', () => { }); }); + let mockFetch: MockFetch; beforeEach(() => { - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -39,8 +41,7 @@ describe('actions/PolicyMember', () => { actionName: CONST.REPORT.ACTIONS.TYPE.ACTIONABLE_JOIN_REQUEST, } as ReportAction; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport); Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, { @@ -67,8 +68,7 @@ describe('actions/PolicyMember', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -101,8 +101,7 @@ describe('actions/PolicyMember', () => { }, }; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, {[fakeUser2.accountID]: fakeUser2}); await waitForBatchedUpdates(); @@ -121,8 +120,7 @@ describe('actions/PolicyMember', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -144,8 +142,7 @@ describe('actions/PolicyMember', () => { const fakeEmail = 'fake@gmail.com'; const fakeAccountID = 1; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.requestWorkspaceOwnerChange(fakePolicy.id); @@ -164,8 +161,7 @@ describe('actions/PolicyMember', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -197,8 +193,7 @@ describe('actions/PolicyMember', () => { }; const fakeAccountID = 1; - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeCard); @@ -217,8 +212,7 @@ describe('actions/PolicyMember', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyProfileTest.ts b/tests/actions/PolicyProfileTest.ts index 21ee34568100..a6bc0ea541bc 100644 --- a/tests/actions/PolicyProfileTest.ts +++ b/tests/actions/PolicyProfileTest.ts @@ -6,6 +6,7 @@ import * as ReportUtils from '@src/libs/ReportUtils'; import ONYXKEYS from '@src/ONYXKEYS'; import createRandomPolicy from '../utils/collections/policies'; import * as TestHelper from '../utils/TestHelper'; +import type {MockFetch} from '../utils/TestHelper'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; OnyxUpdateManager(); @@ -16,9 +17,10 @@ describe('actions/PolicyProfile', () => { }); }); + let mockFetch: MockFetch; beforeEach(() => { - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -29,8 +31,7 @@ describe('actions/PolicyProfile', () => { const oldDescription = fakePolicy.description ?? ''; const newDescription = 'Updated description'; const parsedDescription = ReportUtils.getParsedComment(newDescription); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - fetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.updateWorkspaceDescription(fakePolicy.id, newDescription, oldDescription); await waitForBatchedUpdates(); @@ -48,8 +49,7 @@ describe('actions/PolicyProfile', () => { }, }); }); - // @ts-expect-error TODO: Remove this once TestHelper (https://github.com/Expensify/App/issues/25318) is migrated to TypeScript. - await fetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ From 6272d95951ed4b95af6d346ceb2947c0b0b62b66 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 12:54:03 +0200 Subject: [PATCH 16/21] Fix comments --- tests/actions/IOUTest.ts | 79 +++++++++++++------------- tests/actions/PolicyCategoryTest.ts | 23 ++++---- tests/actions/PolicyMemberTest.ts | 19 +++---- tests/actions/PolicyProfileTest.ts | 7 +-- tests/actions/PolicyTagTest.ts | 67 +++++++++++----------- tests/actions/PolicyTaxTest.ts | 87 ++++++++++++++--------------- tests/actions/PolicyTest.ts | 7 ++- tests/utils/TestHelper.ts | 12 ++-- 8 files changed, 148 insertions(+), 153 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index ac320729b2b7..0f9d888b549a 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -63,8 +63,7 @@ describe('actions/IOU', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -79,7 +78,7 @@ describe('actions/IOU', () => { let transactionID: string | undefined; let transactionThread: OnyxEntry; let transactionThreadCreatedAction: OnyxEntry; - mockFetch?.pause?.(); + mockFetch.pause(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return waitForBatchedUpdates() .then( @@ -218,7 +217,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then( () => new Promise((resolve) => { @@ -267,7 +266,7 @@ describe('actions/IOU', () => { let iouAction: OnyxEntry; let iouCreatedAction: OnyxEntry; let transactionID: string | undefined; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, { @@ -378,7 +377,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -462,7 +461,7 @@ describe('actions/IOU', () => { }; let newIOUAction: OnyxEntry; let newTransaction: OnyxEntry; - mockFetch?.pause?.(); + mockFetch.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`, chatReport) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, iouReport)) .then(() => @@ -567,7 +566,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForNetworkPromises) .then( () => @@ -610,7 +609,7 @@ describe('actions/IOU', () => { let transactionID: string; let transactionThreadReport: OnyxEntry; let transactionThreadAction: OnyxEntry; - mockFetch?.pause?.(); + mockFetch.pause(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return ( waitForBatchedUpdates() @@ -721,8 +720,8 @@ describe('actions/IOU', () => { }), ) .then((): Promise => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then( () => @@ -910,7 +909,7 @@ describe('actions/IOU', () => { ) // Cleanup - .then(mockFetch?.succeed) + .then(mockFetch.succeed) ); }); }); @@ -1061,7 +1060,7 @@ describe('actions/IOU', () => { .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${julesExistingTransaction?.transactionID}`, julesExistingTransaction)) .then(() => { // When we split a bill offline - mockFetch?.pause?.(); + mockFetch.pause(); IOU.splitBill( // TODO: Migrate after the backend accepts accountIDs { @@ -1343,7 +1342,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForNetworkPromises) .then( () => @@ -1480,7 +1479,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch?.pause?.(); + mockFetch.pause(); if (chatReport && iouReport) { IOU.payMoneyRequest(CONST.IOU.PAYMENT_TYPE.ELSEWHERE, chatReport, iouReport); } @@ -1535,7 +1534,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then( () => new Promise((resolve) => { @@ -1593,7 +1592,7 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch?.resume?.(); + mockFetch.resume?.(); }); it('updates the IOU request and IOU report when offline', () => { @@ -1602,7 +1601,7 @@ describe('actions/IOU', () => { let iouAction: OnyxEntry = null; let transaction: OnyxEntry = null; - mockFetch?.pause?.(); + mockFetch.pause(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return waitForBatchedUpdates() .then(() => { @@ -1745,7 +1744,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch?.resume?.(); + mockFetch.resume?.(); }); }); @@ -1811,7 +1810,7 @@ describe('actions/IOU', () => { return waitForBatchedUpdates(); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); if (transaction) { IOU.editMoneyRequest( @@ -1904,14 +1903,14 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch?.resume?.(); + mockFetch.resume?.(); }); it('updates the expense request and expense report when paid while offline', () => { let expenseReport: OnyxEntry; let chatReport: OnyxEntry; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(ONYXKEYS.SESSION, {email: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}); return waitForBatchedUpdates() .then(() => { @@ -2068,7 +2067,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); if (chatReport && expenseReport) { IOU.payMoneyRequest('ACH', chatReport, expenseReport); } @@ -2218,7 +2217,7 @@ describe('actions/IOU', () => { it('delete an expense (IOU Action and transaction) successfully', async () => { // Given the fetch operations are paused and an expense is initiated - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { // When the expense is deleted @@ -2257,7 +2256,7 @@ describe('actions/IOU', () => { expect(t).toBeFalsy(); // Given fetch operations are resumed - mockFetch?.resume?.(); + mockFetch.resume?.(); await waitForBatchedUpdates(); // Then we recheck the IOU report action from the report actions collection @@ -2292,7 +2291,7 @@ describe('actions/IOU', () => { it('delete the IOU report when there are no visible comments left in the IOU report', async () => { // Given an IOU report and a paused fetch state - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { // When the IOU expense is deleted @@ -2315,7 +2314,7 @@ describe('actions/IOU', () => { expect(report).toBeTruthy(); // Given the resumed fetch state - mockFetch?.resume?.(); + mockFetch.resume?.(); await waitForBatchedUpdates(); report = await new Promise>((resolve) => { @@ -2367,7 +2366,7 @@ describe('actions/IOU', () => { expect(resultActionAfterUpdate?.pendingAction).toBeUndefined(); // When we attempt to delete an expense from the IOU report - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction?.transactionID, createIOUAction, false); } @@ -2393,7 +2392,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('chatReportID'); // Given the resumed fetch state - mockFetch?.resume?.(); + mockFetch.resume?.(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ @@ -2457,7 +2456,7 @@ describe('actions/IOU', () => { await waitForBatchedUpdates(); // Given Fetch is paused and timers have advanced - mockFetch?.pause?.(); + mockFetch.pause(); jest.advanceTimersByTime(10); if (transaction && createIOUAction) { @@ -2479,7 +2478,7 @@ describe('actions/IOU', () => { }); expect(report).toBeFalsy(); - mockFetch?.resume?.(); + mockFetch.resume?.(); // Then After resuming fetch, the report for the given thread ID still does not exist report = await new Promise>((resolve) => { @@ -2647,7 +2646,7 @@ describe('actions/IOU', () => { const resultActionAfter = reportActions?.[reportActionID]; expect(resultActionAfter?.pendingAction).toBeUndefined(); - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { // When deleting expense @@ -2670,7 +2669,7 @@ describe('actions/IOU', () => { // When fetch resumes // Then the transaction thread report should still exist - mockFetch?.resume?.(); + mockFetch.resume?.(); await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -2793,7 +2792,7 @@ describe('actions/IOU', () => { // Verify that our action is no longer in the loading state expect(resultActionAfterUpdate?.pendingAction).toBeUndefined(); - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { // When we delete the expense IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); @@ -2816,7 +2815,7 @@ describe('actions/IOU', () => { }); // When we resume fetch - mockFetch?.resume?.(); + mockFetch.resume?.(); // Then we expect the moneyRequestPreview to show [Deleted expense] @@ -2865,7 +2864,7 @@ describe('actions/IOU', () => { expect(ioupreview?.message?.[0]?.text).toBe('rory@expensifail.com owes $300.00'); // When we delete the first expense - mockFetch?.pause?.(); + mockFetch.pause(); jest.advanceTimersByTime(10); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); @@ -2880,7 +2879,7 @@ describe('actions/IOU', () => { expect(iouReport?.total).toBe(20000); // When we resume - mockFetch?.resume?.(); + mockFetch.resume?.(); // Then we expect the IOU report and reportPreview to update with new totals expect(iouReport).toBeTruthy(); @@ -2956,7 +2955,7 @@ describe('actions/IOU', () => { // When we delete the expense in SingleTransactionView and we should not delete the IOU report - mockFetch?.pause?.(); + mockFetch.pause(); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); @@ -2981,7 +2980,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); - mockFetch?.resume?.(); + mockFetch.resume?.(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ @@ -3307,7 +3306,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); if (expenseReport) { IOU.submitReport(expenseReport); } diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index 7c060b40467e..1fdd7f43400f 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -19,8 +19,7 @@ describe('actions/PolicyCategory', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -29,7 +28,7 @@ describe('actions/PolicyCategory', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresCategory = false; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.setWorkspaceRequiresCategory(fakePolicy.id, true); await waitForBatchedUpdates(); @@ -47,7 +46,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -68,7 +67,7 @@ describe('actions/PolicyCategory', () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); const newCategoryName = 'New category'; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.createPolicyCategory(fakePolicy.id, newCategoryName); @@ -88,7 +87,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -113,7 +112,7 @@ describe('actions/PolicyCategory', () => { const fakeCategories = createRandomPolicyCategories(3); const oldCategoryName = Object.keys(fakeCategories)[0]; const newCategoryName = 'Updated category'; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.renamePolicyCategory(fakePolicy.id, { @@ -137,7 +136,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -166,7 +165,7 @@ describe('actions/PolicyCategory', () => { enabled: true, }, }; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.setWorkspaceCategoryEnabled(fakePolicy.id, categoriesToUpdate); @@ -186,7 +185,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -211,7 +210,7 @@ describe('actions/PolicyCategory', () => { const fakeCategories = createRandomPolicyCategories(3); const categoryNameToDelete = Object.keys(fakeCategories)[0]; const categoriesToDelete = [categoryNameToDelete]; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.deleteWorkspaceCategories(fakePolicy.id, categoriesToDelete); @@ -228,7 +227,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 4f3afc51ec66..5a025829b2b4 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -24,8 +24,7 @@ describe('actions/PolicyMember', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -41,7 +40,7 @@ describe('actions/PolicyMember', () => { actionName: CONST.REPORT.ACTIONS.TYPE.ACTIONABLE_JOIN_REQUEST, } as ReportAction; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport); Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, { @@ -68,7 +67,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -101,7 +100,7 @@ describe('actions/PolicyMember', () => { }, }; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, {[fakeUser2.accountID]: fakeUser2}); await waitForBatchedUpdates(); @@ -120,7 +119,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -142,7 +141,7 @@ describe('actions/PolicyMember', () => { const fakeEmail = 'fake@gmail.com'; const fakeAccountID = 1; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.requestWorkspaceOwnerChange(fakePolicy.id); @@ -161,7 +160,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -193,7 +192,7 @@ describe('actions/PolicyMember', () => { }; const fakeAccountID = 1; - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeCard); @@ -212,7 +211,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyProfileTest.ts b/tests/actions/PolicyProfileTest.ts index a6bc0ea541bc..568b2af92dfb 100644 --- a/tests/actions/PolicyProfileTest.ts +++ b/tests/actions/PolicyProfileTest.ts @@ -19,8 +19,7 @@ describe('actions/PolicyProfile', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -31,7 +30,7 @@ describe('actions/PolicyProfile', () => { const oldDescription = fakePolicy.description ?? ''; const newDescription = 'Updated description'; const parsedDescription = ReportUtils.getParsedComment(newDescription); - mockFetch?.pause?.(); + mockFetch.pause(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.updateWorkspaceDescription(fakePolicy.id, newDescription, oldDescription); await waitForBatchedUpdates(); @@ -49,7 +48,7 @@ describe('actions/PolicyProfile', () => { }, }); }); - await mockFetch?.resume?.(); + await mockFetch.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyTagTest.ts b/tests/actions/PolicyTagTest.ts index 74ea13f3d139..db7932063b92 100644 --- a/tests/actions/PolicyTagTest.ts +++ b/tests/actions/PolicyTagTest.ts @@ -20,8 +20,7 @@ describe('actions/Policy', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -30,7 +29,7 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = false; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -55,7 +54,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -77,7 +76,7 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = true; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -102,7 +101,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -124,16 +123,16 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = true; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.setPolicyRequiresTag(fakePolicy.id, false); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -163,7 +162,7 @@ describe('actions/Policy', () => { const newTagListName = 'New tag list name'; const fakePolicyTags = createRandomPolicyTags(oldTagListName); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -199,7 +198,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -228,14 +227,14 @@ describe('actions/Policy', () => { const newTagListName = 'New tag list name'; const fakePolicyTags = createRandomPolicyTags(oldTagListName); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.renamePolicyTaglist( fakePolicy.id, @@ -247,7 +246,7 @@ describe('actions/Policy', () => { ); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -279,7 +278,7 @@ describe('actions/Policy', () => { const newTagName = 'new tag'; const fakePolicyTags = createRandomPolicyTags(tagListName); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -309,7 +308,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -339,19 +338,19 @@ describe('actions/Policy', () => { const newTagName = 'new tag'; const fakePolicyTags = createRandomPolicyTags(tagListName); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.createPolicyTag(fakePolicy.id, newTagName); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -388,7 +387,7 @@ describe('actions/Policy', () => { return acc; }, {}); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -420,7 +419,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -459,19 +458,19 @@ describe('actions/Policy', () => { return acc; }, {}); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.setWorkspaceTagEnabled(fakePolicy.id, tagsToUpdate); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -507,7 +506,7 @@ describe('actions/Policy', () => { const oldTagName = Object.keys(fakePolicyTags?.[tagListName]?.tags)[0]; const newTagName = 'New tag'; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -540,7 +539,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -571,14 +570,14 @@ describe('actions/Policy', () => { const oldTagName = Object.keys(fakePolicyTags?.[tagListName]?.tags)[0]; const newTagName = 'New tag'; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.renamePolicyTag(fakePolicy.id, { oldName: oldTagName, @@ -586,7 +585,7 @@ describe('actions/Policy', () => { }); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -618,7 +617,7 @@ describe('actions/Policy', () => { const fakePolicyTags = createRandomPolicyTags(tagListName, 2); const tagsToDelete = Object.keys(fakePolicyTags?.[tagListName]?.tags ?? {}); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -646,7 +645,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -676,19 +675,19 @@ describe('actions/Policy', () => { const fakePolicyTags = createRandomPolicyTags(tagListName, 2); const tagsToDelete = Object.keys(fakePolicyTags?.[tagListName]?.tags ?? {}); - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch?.fail?.(); + mockFetch.fail(); Policy.deletePolicyTags(fakePolicy.id, tagsToDelete); return waitForBatchedUpdates(); }) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => diff --git a/tests/actions/PolicyTaxTest.ts b/tests/actions/PolicyTaxTest.ts index b1c190f9e5ac..b6a1c9b68a1a 100644 --- a/tests/actions/PolicyTaxTest.ts +++ b/tests/actions/PolicyTaxTest.ts @@ -21,8 +21,7 @@ describe('actions/PolicyTax', () => { let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear() .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy)) .then(waitForBatchedUpdates); @@ -31,7 +30,7 @@ describe('actions/PolicyTax', () => { describe('SetPolicyCustomTaxName', () => { it('Set policy`s custom tax name', () => { const customTaxName = 'Custom tag name'; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setPolicyCustomTaxName(fakePolicy.id, customTaxName); return waitForBatchedUpdates() .then( @@ -50,7 +49,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -72,7 +71,7 @@ describe('actions/PolicyTax', () => { const customTaxName = 'Custom tag name'; const originalCustomTaxName = fakePolicy?.taxRates?.name; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setPolicyCustomTaxName(fakePolicy.id, customTaxName); return waitForBatchedUpdates() .then( @@ -92,8 +91,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -119,7 +118,7 @@ describe('actions/PolicyTax', () => { it('Set policy`s currency default tax', () => { const taxCode = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setWorkspaceCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -138,7 +137,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -160,7 +159,7 @@ describe('actions/PolicyTax', () => { const taxCode = 'id_TAX_RATE_1'; const originalDefaultExternalID = fakePolicy?.taxRates?.defaultExternalID; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setWorkspaceCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -180,8 +179,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -206,7 +205,7 @@ describe('actions/PolicyTax', () => { it('Set policy`s foreign currency default', () => { const taxCode = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setForeignCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -225,7 +224,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -248,7 +247,7 @@ describe('actions/PolicyTax', () => { const taxCode = 'id_TAX_RATE_1'; const originalDefaultForeignCurrencyID = fakePolicy?.taxRates?.foreignTaxDefault; - mockFetch?.pause?.(); + mockFetch.pause(); Policy.setForeignCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -269,8 +268,8 @@ describe('actions/PolicyTax', () => { ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -300,7 +299,7 @@ describe('actions/PolicyTax', () => { code: 'id_TAX_RATE_2', }; - mockFetch?.pause?.(); + mockFetch.pause(); createPolicyTax(fakePolicy.id, newTaxRate); return waitForBatchedUpdates() .then( @@ -321,7 +320,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -348,7 +347,7 @@ describe('actions/PolicyTax', () => { code: 'id_TAX_RATE_2', }; - mockFetch?.pause?.(); + mockFetch.pause(); createPolicyTax(fakePolicy.id, newTaxRate); return waitForBatchedUpdates() .then( @@ -370,8 +369,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -394,7 +393,7 @@ describe('actions/PolicyTax', () => { describe('SetPolicyTaxesEnabled', () => { it('Disable policy`s taxes', () => { const disableTaxID = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); setPolicyTaxesEnabled(fakePolicy.id, [disableTaxID], false); return waitForBatchedUpdates() .then( @@ -415,7 +414,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -437,7 +436,7 @@ describe('actions/PolicyTax', () => { it('Disable policy`s taxes but API returns an error, then enable policy`s taxes again', () => { const disableTaxID = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); setPolicyTaxesEnabled(fakePolicy.id, [disableTaxID], false); const originalTaxes = {...fakePolicy?.taxRates?.taxes}; return waitForBatchedUpdates() @@ -460,8 +459,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -488,7 +487,7 @@ describe('actions/PolicyTax', () => { it('Rename tax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxName = 'Tax rate 1 updated'; - mockFetch?.pause?.(); + mockFetch.pause(); renamePolicyTax(fakePolicy.id, taxID, newTaxName); return waitForBatchedUpdates() .then( @@ -509,7 +508,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -533,7 +532,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxName = 'Tax rate 1 updated'; const originalTaxRate = {...fakePolicy?.taxRates?.taxes[taxID]}; - mockFetch?.pause?.(); + mockFetch.pause(); renamePolicyTax(fakePolicy.id, taxID, newTaxName); return waitForBatchedUpdates() .then( @@ -555,8 +554,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -583,7 +582,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxValue = 10; const stringTaxValue = `${newTaxValue}%`; - mockFetch?.pause?.(); + mockFetch.pause(); updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue); return waitForBatchedUpdates() .then( @@ -604,7 +603,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -629,7 +628,7 @@ describe('actions/PolicyTax', () => { const newTaxValue = 10; const originalTaxRate = {...fakePolicy?.taxRates?.taxes[taxID]}; const stringTaxValue = `${newTaxValue}%`; - mockFetch?.pause?.(); + mockFetch.pause(); updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue); return waitForBatchedUpdates() .then( @@ -651,8 +650,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -679,7 +678,7 @@ describe('actions/PolicyTax', () => { const foreignTaxDefault = fakePolicy?.taxRates?.foreignTaxDefault; const taxID = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); deletePolicyTaxes(fakePolicy.id, [taxID]); return waitForBatchedUpdates() .then( @@ -701,7 +700,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -726,7 +725,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const firstTaxID = 'id_TAX_EXEMPT'; - mockFetch?.pause?.(); + mockFetch.pause(); return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, {taxRates: {foreignTaxDefault: 'id_TAX_RATE_1'}}) .then(() => { deletePolicyTaxes(fakePolicy.id, [taxID]); @@ -751,7 +750,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch?.resume) + .then(mockFetch.resume) .then(waitForBatchedUpdates) .then( () => @@ -776,7 +775,7 @@ describe('actions/PolicyTax', () => { const foreignTaxDefault = fakePolicy?.taxRates?.foreignTaxDefault; const taxID = 'id_TAX_RATE_1'; - mockFetch?.pause?.(); + mockFetch.pause(); deletePolicyTaxes(fakePolicy.id, [taxID]); return waitForBatchedUpdates() .then( @@ -799,8 +798,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch?.fail?.(); - return mockFetch?.resume?.() as Promise; + mockFetch.fail(); + return mockFetch.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 6dae053afac7..b6721c1b5f29 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -23,14 +23,15 @@ describe('actions/Policy', () => { }); }); + let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); describe('createWorkspace', () => { it('creates a new workspace', async () => { - (fetch as MockFetch)?.pause?.(); + mockFetch.pause(); Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID}); await waitForBatchedUpdates(); @@ -123,7 +124,7 @@ describe('actions/Policy', () => { }); // Check for success data - (fetch as MockFetch)?.resume?.(); + mockFetch.resume(); await waitForBatchedUpdates(); policy = await new Promise((resolve) => { diff --git a/tests/utils/TestHelper.ts b/tests/utils/TestHelper.ts index bd107ba6ed56..40ef19469c1b 100644 --- a/tests/utils/TestHelper.ts +++ b/tests/utils/TestHelper.ts @@ -9,10 +9,10 @@ import type {Response as OnyxResponse, PersonalDetails, Report} from '@src/types import waitForBatchedUpdates from './waitForBatchedUpdates'; type MockFetch = ReturnType & { - pause?: () => void; - fail?: () => void; - succeed?: () => void; - resume?: () => Promise; + pause: () => void; + fail: () => void; + succeed: () => void; + resume: () => Promise; }; type QueueItem = (value: Partial | PromiseLike>) => void; @@ -172,14 +172,14 @@ function getGlobalFetchMock() { json: () => Promise.resolve({jsonCode: 200}), }; - const mockFetch: MockFetch = jest.fn().mockImplementation(() => { + const mockFetch = jest.fn().mockImplementation(() => { if (!isPaused) { return Promise.resolve(getResponse()); } return new Promise((resolve) => { queue.push(resolve); }); - }); + }) as MockFetch; mockFetch.pause = () => (isPaused = true); mockFetch.resume = () => { From ce1658eadf3d05f62b49bfecdd1a6181f3cb6eaf Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 12:57:56 +0200 Subject: [PATCH 17/21] Fix mockFetch methods --- tests/actions/IOUTest.ts | 24 ++++++++++++------------ tests/actions/PolicyCategoryTest.ts | 10 +++++----- tests/actions/PolicyMemberTest.ts | 8 ++++---- tests/actions/PolicyProfileTest.ts | 2 +- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 0f9d888b549a..abd0e6998043 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -461,7 +461,7 @@ describe('actions/IOU', () => { }; let newIOUAction: OnyxEntry; let newTransaction: OnyxEntry; - mockFetch.pause?.(); + mockFetch.pause(); return Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`, chatReport) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, iouReport)) .then(() => @@ -1592,7 +1592,7 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch.resume?.(); + mockFetch.resume(); }); it('updates the IOU request and IOU report when offline', () => { @@ -1744,7 +1744,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch.resume?.(); + mockFetch.resume(); }); }); @@ -1903,7 +1903,7 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch.resume?.(); + mockFetch.resume(); }); it('updates the expense request and expense report when paid while offline', () => { @@ -2256,7 +2256,7 @@ describe('actions/IOU', () => { expect(t).toBeFalsy(); // Given fetch operations are resumed - mockFetch.resume?.(); + mockFetch.resume(); await waitForBatchedUpdates(); // Then we recheck the IOU report action from the report actions collection @@ -2314,7 +2314,7 @@ describe('actions/IOU', () => { expect(report).toBeTruthy(); // Given the resumed fetch state - mockFetch.resume?.(); + mockFetch.resume(); await waitForBatchedUpdates(); report = await new Promise>((resolve) => { @@ -2392,7 +2392,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('chatReportID'); // Given the resumed fetch state - mockFetch.resume?.(); + mockFetch.resume(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ @@ -2478,7 +2478,7 @@ describe('actions/IOU', () => { }); expect(report).toBeFalsy(); - mockFetch.resume?.(); + mockFetch.resume(); // Then After resuming fetch, the report for the given thread ID still does not exist report = await new Promise>((resolve) => { @@ -2669,7 +2669,7 @@ describe('actions/IOU', () => { // When fetch resumes // Then the transaction thread report should still exist - mockFetch.resume?.(); + mockFetch.resume(); await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -2815,7 +2815,7 @@ describe('actions/IOU', () => { }); // When we resume fetch - mockFetch.resume?.(); + mockFetch.resume(); // Then we expect the moneyRequestPreview to show [Deleted expense] @@ -2879,7 +2879,7 @@ describe('actions/IOU', () => { expect(iouReport?.total).toBe(20000); // When we resume - mockFetch.resume?.(); + mockFetch.resume(); // Then we expect the IOU report and reportPreview to update with new totals expect(iouReport).toBeTruthy(); @@ -2980,7 +2980,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); - mockFetch.resume?.(); + mockFetch.resume(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index 1fdd7f43400f..9ac4006bfc80 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -46,7 +46,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -87,7 +87,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -136,7 +136,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -185,7 +185,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -227,7 +227,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 5a025829b2b4..40e7fe34c04a 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -67,7 +67,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -119,7 +119,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -160,7 +160,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -211,7 +211,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyProfileTest.ts b/tests/actions/PolicyProfileTest.ts index 568b2af92dfb..5698c8300dec 100644 --- a/tests/actions/PolicyProfileTest.ts +++ b/tests/actions/PolicyProfileTest.ts @@ -48,7 +48,7 @@ describe('actions/PolicyProfile', () => { }, }); }); - await mockFetch.resume?.(); + await mockFetch.resume(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ From 7ada5ba423bd4aab4d56216c94c8cb4067f08d32 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 13:13:41 +0200 Subject: [PATCH 18/21] Test new changes --- tests/actions/PolicyTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index b6721c1b5f29..8e3e1ac0b862 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -109,7 +109,7 @@ describe('actions/Policy', () => { }); }); - // Each of the three reports should have a a `CREATED` action. + // Each of the three reports should have a a `CREATED` action let adminReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${adminReportID}`] ?? {}); let announceReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceReportID}`] ?? {}); let expenseReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReportID}`] ?? {}); From 6f28159b0149820d06276e4b48956599da70e7d5 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 13:48:10 +0200 Subject: [PATCH 19/21] Update tests --- tests/actions/IOUTest.ts | 3 ++- tests/actions/PolicyCategoryTest.ts | 3 ++- tests/actions/PolicyMemberTest.ts | 3 ++- tests/actions/PolicyProfileTest.ts | 7 ++++--- tests/actions/PolicyTagTest.ts | 3 ++- tests/actions/PolicyTaxTest.ts | 3 ++- tests/actions/PolicyTest.ts | 7 +++---- 7 files changed, 17 insertions(+), 12 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index abd0e6998043..543f04972bb5 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -63,7 +63,8 @@ describe('actions/IOU', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index 9ac4006bfc80..9d4a644d56cd 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -19,7 +19,8 @@ describe('actions/PolicyCategory', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 40e7fe34c04a..6c8e216cd33f 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -24,7 +24,8 @@ describe('actions/PolicyMember', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); diff --git a/tests/actions/PolicyProfileTest.ts b/tests/actions/PolicyProfileTest.ts index 5698c8300dec..a6bc0ea541bc 100644 --- a/tests/actions/PolicyProfileTest.ts +++ b/tests/actions/PolicyProfileTest.ts @@ -19,7 +19,8 @@ describe('actions/PolicyProfile', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); @@ -30,7 +31,7 @@ describe('actions/PolicyProfile', () => { const oldDescription = fakePolicy.description ?? ''; const newDescription = 'Updated description'; const parsedDescription = ReportUtils.getParsedComment(newDescription); - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.updateWorkspaceDescription(fakePolicy.id, newDescription, oldDescription); await waitForBatchedUpdates(); @@ -48,7 +49,7 @@ describe('actions/PolicyProfile', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyTagTest.ts b/tests/actions/PolicyTagTest.ts index db7932063b92..ac71f9290079 100644 --- a/tests/actions/PolicyTagTest.ts +++ b/tests/actions/PolicyTagTest.ts @@ -20,7 +20,8 @@ describe('actions/Policy', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); diff --git a/tests/actions/PolicyTaxTest.ts b/tests/actions/PolicyTaxTest.ts index b6a1c9b68a1a..b99901d01439 100644 --- a/tests/actions/PolicyTaxTest.ts +++ b/tests/actions/PolicyTaxTest.ts @@ -21,7 +21,8 @@ describe('actions/PolicyTax', () => { let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = fetch as MockFetch; return Onyx.clear() .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy)) .then(waitForBatchedUpdates); diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 8e3e1ac0b862..d90defce99f6 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -23,15 +23,14 @@ describe('actions/Policy', () => { }); }); - let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); return Onyx.clear().then(waitForBatchedUpdates); }); describe('createWorkspace', () => { it('creates a new workspace', async () => { - mockFetch.pause(); + (fetch as MockFetch).pause(); Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID}); await waitForBatchedUpdates(); @@ -124,7 +123,7 @@ describe('actions/Policy', () => { }); // Check for success data - mockFetch.resume(); + (fetch as MockFetch).resume(); await waitForBatchedUpdates(); policy = await new Promise((resolve) => { From 39c9d9b458325a8a6f99bbd2d3120a206972c959 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 14:26:34 +0200 Subject: [PATCH 20/21] Reverte changes --- tests/actions/IOUTest.ts | 76 +++++++++++++------------- tests/actions/PolicyCategoryTest.ts | 20 +++---- tests/actions/PolicyMemberTest.ts | 16 +++--- tests/actions/PolicyTagTest.ts | 64 +++++++++++----------- tests/actions/PolicyTaxTest.ts | 84 ++++++++++++++--------------- tests/actions/PolicyTest.ts | 9 ++-- tests/utils/TestHelper.ts | 8 +-- 7 files changed, 139 insertions(+), 138 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 543f04972bb5..ac320729b2b7 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -79,7 +79,7 @@ describe('actions/IOU', () => { let transactionID: string | undefined; let transactionThread: OnyxEntry; let transactionThreadCreatedAction: OnyxEntry; - mockFetch.pause(); + mockFetch?.pause?.(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return waitForBatchedUpdates() .then( @@ -218,7 +218,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then( () => new Promise((resolve) => { @@ -267,7 +267,7 @@ describe('actions/IOU', () => { let iouAction: OnyxEntry; let iouCreatedAction: OnyxEntry; let transactionID: string | undefined; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`, chatReport) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`, { @@ -378,7 +378,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -462,7 +462,7 @@ describe('actions/IOU', () => { }; let newIOUAction: OnyxEntry; let newTransaction: OnyxEntry; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`, chatReport) .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, iouReport)) .then(() => @@ -567,7 +567,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForNetworkPromises) .then( () => @@ -610,7 +610,7 @@ describe('actions/IOU', () => { let transactionID: string; let transactionThreadReport: OnyxEntry; let transactionThreadAction: OnyxEntry; - mockFetch.pause(); + mockFetch?.pause?.(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', '', RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return ( waitForBatchedUpdates() @@ -721,8 +721,8 @@ describe('actions/IOU', () => { }), ) .then((): Promise => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then( () => @@ -910,7 +910,7 @@ describe('actions/IOU', () => { ) // Cleanup - .then(mockFetch.succeed) + .then(mockFetch?.succeed) ); }); }); @@ -1061,7 +1061,7 @@ describe('actions/IOU', () => { .then(() => Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${julesExistingTransaction?.transactionID}`, julesExistingTransaction)) .then(() => { // When we split a bill offline - mockFetch.pause(); + mockFetch?.pause?.(); IOU.splitBill( // TODO: Migrate after the backend accepts accountIDs { @@ -1343,7 +1343,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForNetworkPromises) .then( () => @@ -1480,7 +1480,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch.pause(); + mockFetch?.pause?.(); if (chatReport && iouReport) { IOU.payMoneyRequest(CONST.IOU.PAYMENT_TYPE.ELSEWHERE, chatReport, iouReport); } @@ -1535,7 +1535,7 @@ describe('actions/IOU', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then( () => new Promise((resolve) => { @@ -1593,7 +1593,7 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch.resume(); + mockFetch?.resume?.(); }); it('updates the IOU request and IOU report when offline', () => { @@ -1602,7 +1602,7 @@ describe('actions/IOU', () => { let iouAction: OnyxEntry = null; let transaction: OnyxEntry = null; - mockFetch.pause(); + mockFetch?.pause?.(); IOU.requestMoney({reportID: ''}, amount, CONST.CURRENCY.USD, '', merchant, RORY_EMAIL, RORY_ACCOUNT_ID, {login: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}, comment, {}); return waitForBatchedUpdates() .then(() => { @@ -1745,7 +1745,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch.resume(); + mockFetch?.resume?.(); }); }); @@ -1811,7 +1811,7 @@ describe('actions/IOU', () => { return waitForBatchedUpdates(); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); if (transaction) { IOU.editMoneyRequest( @@ -1904,14 +1904,14 @@ describe('actions/IOU', () => { const merchant = 'NASDAQ'; afterEach(() => { - mockFetch.resume(); + mockFetch?.resume?.(); }); it('updates the expense request and expense report when paid while offline', () => { let expenseReport: OnyxEntry; let chatReport: OnyxEntry; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(ONYXKEYS.SESSION, {email: CARLOS_EMAIL, accountID: CARLOS_ACCOUNT_ID}); return waitForBatchedUpdates() .then(() => { @@ -2068,7 +2068,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); if (chatReport && expenseReport) { IOU.payMoneyRequest('ACH', chatReport, expenseReport); } @@ -2218,7 +2218,7 @@ describe('actions/IOU', () => { it('delete an expense (IOU Action and transaction) successfully', async () => { // Given the fetch operations are paused and an expense is initiated - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { // When the expense is deleted @@ -2257,7 +2257,7 @@ describe('actions/IOU', () => { expect(t).toBeFalsy(); // Given fetch operations are resumed - mockFetch.resume(); + mockFetch?.resume?.(); await waitForBatchedUpdates(); // Then we recheck the IOU report action from the report actions collection @@ -2292,7 +2292,7 @@ describe('actions/IOU', () => { it('delete the IOU report when there are no visible comments left in the IOU report', async () => { // Given an IOU report and a paused fetch state - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { // When the IOU expense is deleted @@ -2315,7 +2315,7 @@ describe('actions/IOU', () => { expect(report).toBeTruthy(); // Given the resumed fetch state - mockFetch.resume(); + mockFetch?.resume?.(); await waitForBatchedUpdates(); report = await new Promise>((resolve) => { @@ -2367,7 +2367,7 @@ describe('actions/IOU', () => { expect(resultActionAfterUpdate?.pendingAction).toBeUndefined(); // When we attempt to delete an expense from the IOU report - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction?.transactionID, createIOUAction, false); } @@ -2393,7 +2393,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('chatReportID'); // Given the resumed fetch state - mockFetch.resume(); + mockFetch?.resume?.(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ @@ -2457,7 +2457,7 @@ describe('actions/IOU', () => { await waitForBatchedUpdates(); // Given Fetch is paused and timers have advanced - mockFetch.pause(); + mockFetch?.pause?.(); jest.advanceTimersByTime(10); if (transaction && createIOUAction) { @@ -2479,7 +2479,7 @@ describe('actions/IOU', () => { }); expect(report).toBeFalsy(); - mockFetch.resume(); + mockFetch?.resume?.(); // Then After resuming fetch, the report for the given thread ID still does not exist report = await new Promise>((resolve) => { @@ -2647,7 +2647,7 @@ describe('actions/IOU', () => { const resultActionAfter = reportActions?.[reportActionID]; expect(resultActionAfter?.pendingAction).toBeUndefined(); - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { // When deleting expense @@ -2670,7 +2670,7 @@ describe('actions/IOU', () => { // When fetch resumes // Then the transaction thread report should still exist - mockFetch.resume(); + mockFetch?.resume?.(); await new Promise((resolve) => { const connectionID = Onyx.connect({ key: `${ONYXKEYS.COLLECTION.REPORT}${thread.reportID}`, @@ -2793,7 +2793,7 @@ describe('actions/IOU', () => { // Verify that our action is no longer in the loading state expect(resultActionAfterUpdate?.pendingAction).toBeUndefined(); - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { // When we delete the expense IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); @@ -2816,7 +2816,7 @@ describe('actions/IOU', () => { }); // When we resume fetch - mockFetch.resume(); + mockFetch?.resume?.(); // Then we expect the moneyRequestPreview to show [Deleted expense] @@ -2865,7 +2865,7 @@ describe('actions/IOU', () => { expect(ioupreview?.message?.[0]?.text).toBe('rory@expensifail.com owes $300.00'); // When we delete the first expense - mockFetch.pause(); + mockFetch?.pause?.(); jest.advanceTimersByTime(10); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, false); @@ -2880,7 +2880,7 @@ describe('actions/IOU', () => { expect(iouReport?.total).toBe(20000); // When we resume - mockFetch.resume(); + mockFetch?.resume?.(); // Then we expect the IOU report and reportPreview to update with new totals expect(iouReport).toBeTruthy(); @@ -2956,7 +2956,7 @@ describe('actions/IOU', () => { // When we delete the expense in SingleTransactionView and we should not delete the IOU report - mockFetch.pause(); + mockFetch?.pause?.(); if (transaction && createIOUAction) { IOU.deleteMoneyRequest(transaction.transactionID, createIOUAction, true); @@ -2981,7 +2981,7 @@ describe('actions/IOU', () => { expect(iouReport).toHaveProperty('reportID'); expect(iouReport).toHaveProperty('chatReportID'); - mockFetch.resume(); + mockFetch?.resume?.(); allReports = await new Promise>((resolve) => { const connectionID = Onyx.connect({ @@ -3307,7 +3307,7 @@ describe('actions/IOU', () => { }), ) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); if (expenseReport) { IOU.submitReport(expenseReport); } diff --git a/tests/actions/PolicyCategoryTest.ts b/tests/actions/PolicyCategoryTest.ts index 9d4a644d56cd..7c060b40467e 100644 --- a/tests/actions/PolicyCategoryTest.ts +++ b/tests/actions/PolicyCategoryTest.ts @@ -29,7 +29,7 @@ describe('actions/PolicyCategory', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresCategory = false; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Policy.setWorkspaceRequiresCategory(fakePolicy.id, true); await waitForBatchedUpdates(); @@ -47,7 +47,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -68,7 +68,7 @@ describe('actions/PolicyCategory', () => { const fakePolicy = createRandomPolicy(0); const fakeCategories = createRandomPolicyCategories(3); const newCategoryName = 'New category'; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.createPolicyCategory(fakePolicy.id, newCategoryName); @@ -88,7 +88,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -113,7 +113,7 @@ describe('actions/PolicyCategory', () => { const fakeCategories = createRandomPolicyCategories(3); const oldCategoryName = Object.keys(fakeCategories)[0]; const newCategoryName = 'Updated category'; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.renamePolicyCategory(fakePolicy.id, { @@ -137,7 +137,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -166,7 +166,7 @@ describe('actions/PolicyCategory', () => { enabled: true, }, }; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.setWorkspaceCategoryEnabled(fakePolicy.id, categoriesToUpdate); @@ -186,7 +186,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -211,7 +211,7 @@ describe('actions/PolicyCategory', () => { const fakeCategories = createRandomPolicyCategories(3); const categoryNameToDelete = Object.keys(fakeCategories)[0]; const categoriesToDelete = [categoryNameToDelete]; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${fakePolicy.id}`, fakeCategories); Policy.deleteWorkspaceCategories(fakePolicy.id, categoriesToDelete); @@ -228,7 +228,7 @@ describe('actions/PolicyCategory', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 6c8e216cd33f..4f3afc51ec66 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -41,7 +41,7 @@ describe('actions/PolicyMember', () => { actionName: CONST.REPORT.ACTIONS.TYPE.ACTIONABLE_JOIN_REQUEST, } as ReportAction; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${fakeReport.reportID}`, fakeReport); Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${fakeReport.reportID}`, { @@ -68,7 +68,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -101,7 +101,7 @@ describe('actions/PolicyMember', () => { }, }; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.set(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, {[fakeUser2.accountID]: fakeUser2}); await waitForBatchedUpdates(); @@ -120,7 +120,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -142,7 +142,7 @@ describe('actions/PolicyMember', () => { const fakeEmail = 'fake@gmail.com'; const fakeAccountID = 1; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.requestWorkspaceOwnerChange(fakePolicy.id); @@ -161,7 +161,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ @@ -193,7 +193,7 @@ describe('actions/PolicyMember', () => { }; const fakeAccountID = 1; - mockFetch.pause(); + mockFetch?.pause?.(); Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy); Onyx.merge(ONYXKEYS.SESSION, {email: fakeEmail, accountID: fakeAccountID}); Policy.addBillingCardAndRequestPolicyOwnerChange(fakePolicy.id, fakeCard); @@ -212,7 +212,7 @@ describe('actions/PolicyMember', () => { }, }); }); - await mockFetch.resume(); + await mockFetch?.resume?.(); await waitForBatchedUpdates(); await new Promise((resolve) => { const connectionID = Onyx.connect({ diff --git a/tests/actions/PolicyTagTest.ts b/tests/actions/PolicyTagTest.ts index ac71f9290079..74ea13f3d139 100644 --- a/tests/actions/PolicyTagTest.ts +++ b/tests/actions/PolicyTagTest.ts @@ -30,7 +30,7 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = false; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -55,7 +55,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -77,7 +77,7 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = true; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -102,7 +102,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -124,16 +124,16 @@ describe('actions/Policy', () => { const fakePolicy = createRandomPolicy(0); fakePolicy.requiresTag = true; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.setPolicyRequiresTag(fakePolicy.id, false); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -163,7 +163,7 @@ describe('actions/Policy', () => { const newTagListName = 'New tag list name'; const fakePolicyTags = createRandomPolicyTags(oldTagListName); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -199,7 +199,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -228,14 +228,14 @@ describe('actions/Policy', () => { const newTagListName = 'New tag list name'; const fakePolicyTags = createRandomPolicyTags(oldTagListName); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.renamePolicyTaglist( fakePolicy.id, @@ -247,7 +247,7 @@ describe('actions/Policy', () => { ); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -279,7 +279,7 @@ describe('actions/Policy', () => { const newTagName = 'new tag'; const fakePolicyTags = createRandomPolicyTags(tagListName); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -309,7 +309,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -339,19 +339,19 @@ describe('actions/Policy', () => { const newTagName = 'new tag'; const fakePolicyTags = createRandomPolicyTags(tagListName); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.createPolicyTag(fakePolicy.id, newTagName); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -388,7 +388,7 @@ describe('actions/Policy', () => { return acc; }, {}); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -420,7 +420,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -459,19 +459,19 @@ describe('actions/Policy', () => { return acc; }, {}); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.setWorkspaceTagEnabled(fakePolicy.id, tagsToUpdate); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -507,7 +507,7 @@ describe('actions/Policy', () => { const oldTagName = Object.keys(fakePolicyTags?.[tagListName]?.tags)[0]; const newTagName = 'New tag'; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -540,7 +540,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -571,14 +571,14 @@ describe('actions/Policy', () => { const oldTagName = Object.keys(fakePolicyTags?.[tagListName]?.tags)[0]; const newTagName = 'New tag'; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.renamePolicyTag(fakePolicy.id, { oldName: oldTagName, @@ -586,7 +586,7 @@ describe('actions/Policy', () => { }); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -618,7 +618,7 @@ describe('actions/Policy', () => { const fakePolicyTags = createRandomPolicyTags(tagListName, 2); const tagsToDelete = Object.keys(fakePolicyTags?.[tagListName]?.tags ?? {}); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { @@ -646,7 +646,7 @@ describe('actions/Policy', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -676,19 +676,19 @@ describe('actions/Policy', () => { const fakePolicyTags = createRandomPolicyTags(tagListName, 2); const tagsToDelete = Object.keys(fakePolicyTags?.[tagListName]?.tags ?? {}); - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, fakePolicy) .then(() => { Onyx.set(`${ONYXKEYS.COLLECTION.POLICY_TAGS}${fakePolicy.id}`, fakePolicyTags); }) .then(() => { - mockFetch.fail(); + mockFetch?.fail?.(); Policy.deletePolicyTags(fakePolicy.id, tagsToDelete); return waitForBatchedUpdates(); }) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => diff --git a/tests/actions/PolicyTaxTest.ts b/tests/actions/PolicyTaxTest.ts index b99901d01439..b1c190f9e5ac 100644 --- a/tests/actions/PolicyTaxTest.ts +++ b/tests/actions/PolicyTaxTest.ts @@ -31,7 +31,7 @@ describe('actions/PolicyTax', () => { describe('SetPolicyCustomTaxName', () => { it('Set policy`s custom tax name', () => { const customTaxName = 'Custom tag name'; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setPolicyCustomTaxName(fakePolicy.id, customTaxName); return waitForBatchedUpdates() .then( @@ -50,7 +50,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -72,7 +72,7 @@ describe('actions/PolicyTax', () => { const customTaxName = 'Custom tag name'; const originalCustomTaxName = fakePolicy?.taxRates?.name; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setPolicyCustomTaxName(fakePolicy.id, customTaxName); return waitForBatchedUpdates() .then( @@ -92,8 +92,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -119,7 +119,7 @@ describe('actions/PolicyTax', () => { it('Set policy`s currency default tax', () => { const taxCode = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setWorkspaceCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -138,7 +138,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -160,7 +160,7 @@ describe('actions/PolicyTax', () => { const taxCode = 'id_TAX_RATE_1'; const originalDefaultExternalID = fakePolicy?.taxRates?.defaultExternalID; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setWorkspaceCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -180,8 +180,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -206,7 +206,7 @@ describe('actions/PolicyTax', () => { it('Set policy`s foreign currency default', () => { const taxCode = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setForeignCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -225,7 +225,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -248,7 +248,7 @@ describe('actions/PolicyTax', () => { const taxCode = 'id_TAX_RATE_1'; const originalDefaultForeignCurrencyID = fakePolicy?.taxRates?.foreignTaxDefault; - mockFetch.pause(); + mockFetch?.pause?.(); Policy.setForeignCurrencyDefault(fakePolicy.id, taxCode); return waitForBatchedUpdates() .then( @@ -269,8 +269,8 @@ describe('actions/PolicyTax', () => { ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -300,7 +300,7 @@ describe('actions/PolicyTax', () => { code: 'id_TAX_RATE_2', }; - mockFetch.pause(); + mockFetch?.pause?.(); createPolicyTax(fakePolicy.id, newTaxRate); return waitForBatchedUpdates() .then( @@ -321,7 +321,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -348,7 +348,7 @@ describe('actions/PolicyTax', () => { code: 'id_TAX_RATE_2', }; - mockFetch.pause(); + mockFetch?.pause?.(); createPolicyTax(fakePolicy.id, newTaxRate); return waitForBatchedUpdates() .then( @@ -370,8 +370,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -394,7 +394,7 @@ describe('actions/PolicyTax', () => { describe('SetPolicyTaxesEnabled', () => { it('Disable policy`s taxes', () => { const disableTaxID = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); setPolicyTaxesEnabled(fakePolicy.id, [disableTaxID], false); return waitForBatchedUpdates() .then( @@ -415,7 +415,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -437,7 +437,7 @@ describe('actions/PolicyTax', () => { it('Disable policy`s taxes but API returns an error, then enable policy`s taxes again', () => { const disableTaxID = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); setPolicyTaxesEnabled(fakePolicy.id, [disableTaxID], false); const originalTaxes = {...fakePolicy?.taxRates?.taxes}; return waitForBatchedUpdates() @@ -460,8 +460,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -488,7 +488,7 @@ describe('actions/PolicyTax', () => { it('Rename tax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxName = 'Tax rate 1 updated'; - mockFetch.pause(); + mockFetch?.pause?.(); renamePolicyTax(fakePolicy.id, taxID, newTaxName); return waitForBatchedUpdates() .then( @@ -509,7 +509,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -533,7 +533,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxName = 'Tax rate 1 updated'; const originalTaxRate = {...fakePolicy?.taxRates?.taxes[taxID]}; - mockFetch.pause(); + mockFetch?.pause?.(); renamePolicyTax(fakePolicy.id, taxID, newTaxName); return waitForBatchedUpdates() .then( @@ -555,8 +555,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -583,7 +583,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const newTaxValue = 10; const stringTaxValue = `${newTaxValue}%`; - mockFetch.pause(); + mockFetch?.pause?.(); updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue); return waitForBatchedUpdates() .then( @@ -604,7 +604,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -629,7 +629,7 @@ describe('actions/PolicyTax', () => { const newTaxValue = 10; const originalTaxRate = {...fakePolicy?.taxRates?.taxes[taxID]}; const stringTaxValue = `${newTaxValue}%`; - mockFetch.pause(); + mockFetch?.pause?.(); updatePolicyTaxValue(fakePolicy.id, taxID, newTaxValue); return waitForBatchedUpdates() .then( @@ -651,8 +651,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( @@ -679,7 +679,7 @@ describe('actions/PolicyTax', () => { const foreignTaxDefault = fakePolicy?.taxRates?.foreignTaxDefault; const taxID = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); deletePolicyTaxes(fakePolicy.id, [taxID]); return waitForBatchedUpdates() .then( @@ -701,7 +701,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -726,7 +726,7 @@ describe('actions/PolicyTax', () => { const taxID = 'id_TAX_RATE_1'; const firstTaxID = 'id_TAX_EXEMPT'; - mockFetch.pause(); + mockFetch?.pause?.(); return Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${fakePolicy.id}`, {taxRates: {foreignTaxDefault: 'id_TAX_RATE_1'}}) .then(() => { deletePolicyTaxes(fakePolicy.id, [taxID]); @@ -751,7 +751,7 @@ describe('actions/PolicyTax', () => { }); }), ) - .then(mockFetch.resume) + .then(mockFetch?.resume) .then(waitForBatchedUpdates) .then( () => @@ -776,7 +776,7 @@ describe('actions/PolicyTax', () => { const foreignTaxDefault = fakePolicy?.taxRates?.foreignTaxDefault; const taxID = 'id_TAX_RATE_1'; - mockFetch.pause(); + mockFetch?.pause?.(); deletePolicyTaxes(fakePolicy.id, [taxID]); return waitForBatchedUpdates() .then( @@ -799,8 +799,8 @@ describe('actions/PolicyTax', () => { }), ) .then(() => { - mockFetch.fail(); - return mockFetch.resume?.() as Promise; + mockFetch?.fail?.(); + return mockFetch?.resume?.() as Promise; }) .then(waitForBatchedUpdates) .then( diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index d90defce99f6..2b8a9bfe1882 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -23,14 +23,15 @@ describe('actions/Policy', () => { }); }); + let mockFetch: MockFetch; beforeEach(() => { - global.fetch = TestHelper.getGlobalFetchMock(); + mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; return Onyx.clear().then(waitForBatchedUpdates); }); describe('createWorkspace', () => { it('creates a new workspace', async () => { - (fetch as MockFetch).pause(); + mockFetch?.pause?.(); Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID}); await waitForBatchedUpdates(); @@ -108,7 +109,7 @@ describe('actions/Policy', () => { }); }); - // Each of the three reports should have a a `CREATED` action + // Each of the three reports should have a a `CREATED` action. let adminReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${adminReportID}`] ?? {}); let announceReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${announceReportID}`] ?? {}); let expenseReportActions: ReportAction[] = Object.values(reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReportID}`] ?? {}); @@ -123,7 +124,7 @@ describe('actions/Policy', () => { }); // Check for success data - (fetch as MockFetch).resume(); + mockFetch?.resume?.(); await waitForBatchedUpdates(); policy = await new Promise((resolve) => { diff --git a/tests/utils/TestHelper.ts b/tests/utils/TestHelper.ts index 40ef19469c1b..ddf8960b9f02 100644 --- a/tests/utils/TestHelper.ts +++ b/tests/utils/TestHelper.ts @@ -9,10 +9,10 @@ import type {Response as OnyxResponse, PersonalDetails, Report} from '@src/types import waitForBatchedUpdates from './waitForBatchedUpdates'; type MockFetch = ReturnType & { - pause: () => void; - fail: () => void; - succeed: () => void; - resume: () => Promise; + pause?: () => void; + fail?: () => void; + succeed?: () => void; + resume?: () => Promise; }; type QueueItem = (value: Partial | PromiseLike>) => void; From 3a9a1a5ee5f4df9bb153734ae3393bff21cf26c1 Mon Sep 17 00:00:00 2001 From: Yauheni Date: Thu, 16 May 2024 14:28:36 +0200 Subject: [PATCH 21/21] Reverte changes x2 --- tests/actions/PolicyTest.ts | 7 +++---- tests/utils/TestHelper.ts | 4 ++-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/actions/PolicyTest.ts b/tests/actions/PolicyTest.ts index 2b8a9bfe1882..6dae053afac7 100644 --- a/tests/actions/PolicyTest.ts +++ b/tests/actions/PolicyTest.ts @@ -23,15 +23,14 @@ describe('actions/Policy', () => { }); }); - let mockFetch: MockFetch; beforeEach(() => { - mockFetch = TestHelper.getGlobalFetchMock() as MockFetch; + global.fetch = TestHelper.getGlobalFetchMock(); return Onyx.clear().then(waitForBatchedUpdates); }); describe('createWorkspace', () => { it('creates a new workspace', async () => { - mockFetch?.pause?.(); + (fetch as MockFetch)?.pause?.(); Onyx.set(ONYXKEYS.SESSION, {email: ESH_EMAIL, accountID: ESH_ACCOUNT_ID}); await waitForBatchedUpdates(); @@ -124,7 +123,7 @@ describe('actions/Policy', () => { }); // Check for success data - mockFetch?.resume?.(); + (fetch as MockFetch)?.resume?.(); await waitForBatchedUpdates(); policy = await new Promise((resolve) => { diff --git a/tests/utils/TestHelper.ts b/tests/utils/TestHelper.ts index ddf8960b9f02..bd107ba6ed56 100644 --- a/tests/utils/TestHelper.ts +++ b/tests/utils/TestHelper.ts @@ -172,14 +172,14 @@ function getGlobalFetchMock() { json: () => Promise.resolve({jsonCode: 200}), }; - const mockFetch = jest.fn().mockImplementation(() => { + const mockFetch: MockFetch = jest.fn().mockImplementation(() => { if (!isPaused) { return Promise.resolve(getResponse()); } return new Promise((resolve) => { queue.push(resolve); }); - }) as MockFetch; + }); mockFetch.pause = () => (isPaused = true); mockFetch.resume = () => {