From 955a711a06ed0b332ddce9c52b7758324df2c3f8 Mon Sep 17 00:00:00 2001 From: KJ21-ENG <140263938+KJ21-ENG@users.noreply.github.com> Date: Wed, 12 Aug 2026 18:55:35 +0530 Subject: [PATCH] Remove unsafe type assertions from tests --- tests/unit/DistanceRateTest.ts | 7 ++- .../DynamicContactMethodDetailsPageTest.tsx | 5 +- tests/unit/FlashListTest.tsx | 14 ++++-- .../ScreenWrapperPreventRemoveTest.tsx | 10 ++-- tests/unit/getTimeDifferenceIntervalsTest.ts | 6 ++- .../goBackFromWorkspaceSettingPagesTest.ts | 4 +- tests/unit/hooks/useCachedImageSource.test.ts | 13 +++-- .../hooks/useConfirmationCtaText.test.tsx | 3 +- tests/unit/hooks/useContainerWidth.test.ts | 4 +- ...DismissOnMoneyRequestReportRemoval.test.ts | 4 +- tests/unit/hooks/useDomainGroupFilter.test.ts | 12 +++-- tests/unit/hooks/useExpenseSubmission.test.ts | 3 +- ...eExpensifyCardFeedsForFeedSelector.test.ts | 48 +++++++++---------- tests/unit/hooks/useReceiptTraining.test.ts | 4 +- .../useSearchShouldCalculateTotals.test.ts | 38 ++++++--------- 15 files changed, 94 insertions(+), 81 deletions(-) diff --git a/tests/unit/DistanceRateTest.ts b/tests/unit/DistanceRateTest.ts index 816ff4e43e49..493f465ae44b 100644 --- a/tests/unit/DistanceRateTest.ts +++ b/tests/unit/DistanceRateTest.ts @@ -5,8 +5,6 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, Transaction, TransactionViolations} from '@src/types/onyx'; -import type {OnyxKey} from 'react-native-onyx'; - import Onyx from 'react-native-onyx'; import createRandomPolicy from '../utils/collections/policies'; @@ -148,12 +146,13 @@ describe('DistanceRate', () => { } await waitForBatchedUpdates(); const onyxPolicy = await new Promise((resolve) => { + const policyKey = `${ONYXKEYS.COLLECTION.POLICY}${policy.id}` as const; Onyx.connect({ - key: `${ONYXKEYS.COLLECTION.POLICY}${policy.id}` as OnyxKey, + key: policyKey, // eslint-disable-next-line rulesdir/prefer-early-return callback: (value) => { if (value !== undefined) { - resolve(value as Policy); + resolve(value); } }, }); diff --git a/tests/unit/DynamicContactMethodDetailsPageTest.tsx b/tests/unit/DynamicContactMethodDetailsPageTest.tsx index 38a94e40a9bd..12ea15c4a91f 100644 --- a/tests/unit/DynamicContactMethodDetailsPageTest.tsx +++ b/tests/unit/DynamicContactMethodDetailsPageTest.tsx @@ -85,8 +85,9 @@ describe('DynamicContactMethodDetailsPage', () => { }); }); beforeEach(() => { - global.fetch = getGlobalFetchMock(); - mockFetch = fetch as MockFetch; + const globalFetchMock = getGlobalFetchMock(); + global.fetch = globalFetchMock; + mockFetch = globalFetchMock; return Onyx.clear().then(waitForBatchedUpdates); }); diff --git a/tests/unit/FlashListTest.tsx b/tests/unit/FlashListTest.tsx index 5c8b63f0e1a0..f76e02aeefbf 100644 --- a/tests/unit/FlashListTest.tsx +++ b/tests/unit/FlashListTest.tsx @@ -6,6 +6,8 @@ import {FlashList} from '@shopify/flash-list'; import React from 'react'; import {View} from 'react-native'; +import createMock from '../utils/createMock'; + type CellProps = { index: number; onLayout: (event: LayoutChangeEvent) => void; @@ -72,11 +74,13 @@ describe('FlashList - stale onLayout after rapid data change', () => { // Pre-fix: this throws "index out of bounds, not enough layouts" // Post-fix: validateItemSize bails when tryGetLayout returns undefined expect(() => { - staleOnLayout?.({ - nativeEvent: { - layout: {x: 0, y: 0, width: 100, height: 50}, - }, - } as LayoutChangeEvent); + staleOnLayout?.( + createMock({ + nativeEvent: { + layout: {x: 0, y: 0, width: 100, height: 50}, + }, + }), + ); }).not.toThrow(); }); }); diff --git a/tests/unit/components/ScreenWrapperPreventRemoveTest.tsx b/tests/unit/components/ScreenWrapperPreventRemoveTest.tsx index 42c8cc46dfa2..d49e4dfdc8a3 100644 --- a/tests/unit/components/ScreenWrapperPreventRemoveTest.tsx +++ b/tests/unit/components/ScreenWrapperPreventRemoveTest.tsx @@ -2,6 +2,7 @@ import ScreenWrapper from '@components/ScreenWrapper'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {usePreventRemove} from '@react-navigation/native'; import type ReactTestRendererType from 'react-test-renderer'; // These rules are disabled because `require()` is used inside jest.mock factories for module resolution, @@ -12,7 +13,6 @@ import Onyx from 'react-native-onyx'; import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates'; -// eslint-disable-next-line const ReactTestRenderer: typeof ReactTestRendererType = require('react-test-renderer'); Onyx.init({keys: ONYXKEYS}); @@ -120,12 +120,16 @@ function getNavigationMock() { } function getUsePreventRemove() { - return jest.requireMock<{usePreventRemove: jest.Mock}>('@react-navigation/native').usePreventRemove; + return jest.requireMock<{usePreventRemove: jest.MockedFunction}>('@react-navigation/native').usePreventRemove; } function lastPreventRemoveCondition(): boolean { const mock = getUsePreventRemove(); - return mock.mock.calls.at(-1)?.[0] as boolean; + const lastCall = mock.mock.calls.at(-1); + if (!lastCall) { + throw new Error('Expected usePreventRemove to have been called'); + } + return lastCall[0]; } async function renderScreenWrapper(): Promise { diff --git a/tests/unit/getTimeDifferenceIntervalsTest.ts b/tests/unit/getTimeDifferenceIntervalsTest.ts index 490399316f3c..7f6dcea6fd0e 100644 --- a/tests/unit/getTimeDifferenceIntervalsTest.ts +++ b/tests/unit/getTimeDifferenceIntervalsTest.ts @@ -6,9 +6,11 @@ import type {OnyxEntry} from 'react-native-onyx'; import {addDays, differenceInDays, differenceInMinutes, startOfDay} from 'date-fns'; +import createMock from '../utils/createMock'; + describe('getTimeDifferenceIntervals', () => { const createMockTransaction = (startDate: string, endDate: string): OnyxEntry => - ({ + createMock({ comment: { customUnit: { attributes: { @@ -19,7 +21,7 @@ describe('getTimeDifferenceIntervals', () => { }, }, }, - }) as OnyxEntry; + }); it('calculates hours for same-day transactions', () => { // Given a transaction that starts and ends on the same day diff --git a/tests/unit/goBackFromWorkspaceSettingPagesTest.ts b/tests/unit/goBackFromWorkspaceSettingPagesTest.ts index a7c9bf9cc2fd..2384e2735201 100644 --- a/tests/unit/goBackFromWorkspaceSettingPagesTest.ts +++ b/tests/unit/goBackFromWorkspaceSettingPagesTest.ts @@ -8,6 +8,8 @@ import type {NavigationState, PartialState} from '@react-navigation/native'; import {StackActions} from '@react-navigation/native'; +import createMock from '../utils/createMock'; + jest.mock('@libs/Navigation/Navigation', () => ({ __esModule: true, default: { @@ -33,7 +35,7 @@ function mockRootState(routes: Array<{name: string; state?: PartialState(state)); } describe('goBackFromWorkspaceSettingPages', () => { diff --git a/tests/unit/hooks/useCachedImageSource.test.ts b/tests/unit/hooks/useCachedImageSource.test.ts index 634fe26a55a7..1b7b38535bb8 100644 --- a/tests/unit/hooks/useCachedImageSource.test.ts +++ b/tests/unit/hooks/useCachedImageSource.test.ts @@ -6,6 +6,8 @@ import CONST from '@src/CONST'; import type {ImageSource} from 'expo-image'; +import createMock from '../../utils/createMock'; + const MOCK_URI = 'https://example.com/image.png'; // eslint-disable-next-line @typescript-eslint/naming-convention const MOCK_HEADERS = {'X-Auth-Token': 'token123'}; @@ -20,13 +22,14 @@ let mockCreateObjectURL: jest.Mock; let mockRevokeObjectURL: jest.Mock; const createMockResponse = (ok = true) => { - const response = { + const clone = jest.fn(); + const response = createMock({ ok, blob: jest.fn().mockResolvedValue(MOCK_BLOB), - clone: jest.fn(), - }; - response.clone.mockReturnValue(response); - return response as unknown as Response; + clone, + }); + clone.mockReturnValue(response); + return response; }; beforeEach(() => { diff --git a/tests/unit/hooks/useConfirmationCtaText.test.tsx b/tests/unit/hooks/useConfirmationCtaText.test.tsx index 2e7d3d2b2f6f..927fe215c522 100644 --- a/tests/unit/hooks/useConfirmationCtaText.test.tsx +++ b/tests/unit/hooks/useConfirmationCtaText.test.tsx @@ -9,6 +9,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import React from 'react'; import Onyx from 'react-native-onyx'; +import createMock from '../../utils/createMock'; import waitForBatchedUpdatesWithAct from '../../utils/waitForBatchedUpdatesWithAct'; type Params = Parameters[0]; @@ -86,7 +87,7 @@ describe('useConfirmationCtaText', () => { }); it('includes formatted amount in sendInvoice copy when invoicing details are present', () => { - const policy = {invoice: {companyName: 'Acme', companyWebsite: 'acme.com'}} as Params['policy']; + const policy = createMock>({invoice: {companyName: 'Acme', companyWebsite: 'acme.com'}}); const {result} = renderHook(() => useConfirmationCtaText({...baseParams, isTypeInvoice: true, iouType: CONST.IOU.TYPE.INVOICE, policy, formattedAmount: '$50.00'}), { wrapper: Wrapper, }); diff --git a/tests/unit/hooks/useContainerWidth.test.ts b/tests/unit/hooks/useContainerWidth.test.ts index b2f7c65ed190..2bdf0c2e33a9 100644 --- a/tests/unit/hooks/useContainerWidth.test.ts +++ b/tests/unit/hooks/useContainerWidth.test.ts @@ -4,8 +4,10 @@ import useContainerWidth from '@hooks/useContainerWidth'; import type {LayoutChangeEvent} from 'react-native'; +import createMock from '../../utils/createMock'; + function createLayoutEvent(width: number): LayoutChangeEvent { - return {nativeEvent: {layout: {x: 0, y: 0, width, height: 0}}} as LayoutChangeEvent; + return createMock({nativeEvent: {layout: {x: 0, y: 0, width, height: 0}}}); } describe('useContainerWidth', () => { diff --git a/tests/unit/hooks/useDismissOnMoneyRequestReportRemoval.test.ts b/tests/unit/hooks/useDismissOnMoneyRequestReportRemoval.test.ts index e12f339dd79a..e5eb4e2f692f 100644 --- a/tests/unit/hooks/useDismissOnMoneyRequestReportRemoval.test.ts +++ b/tests/unit/hooks/useDismissOnMoneyRequestReportRemoval.test.ts @@ -14,13 +14,13 @@ import Onyx from 'react-native-onyx'; import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates'; -const mockUseIsFocused = jest.fn().mockReturnValue(true); +const mockUseIsFocused = jest.fn().mockReturnValue(true); jest.mock('@react-navigation/native', () => { const actualNav = jest.requireActual('@react-navigation/native'); return { ...actualNav, - useIsFocused: () => mockUseIsFocused() as boolean, + useIsFocused: () => mockUseIsFocused(), }; }); diff --git a/tests/unit/hooks/useDomainGroupFilter.test.ts b/tests/unit/hooks/useDomainGroupFilter.test.ts index 0448eef0fa62..1d7b23096d34 100644 --- a/tests/unit/hooks/useDomainGroupFilter.test.ts +++ b/tests/unit/hooks/useDomainGroupFilter.test.ts @@ -8,32 +8,36 @@ import useDomainGroupFilter from '@hooks/useDomainGroupFilter'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type Domain from '@src/types/onyx/Domain'; +import type {DomainSecurityGroup, SecurityGroupKey} from '@src/types/onyx/Domain'; import Onyx from 'react-native-onyx'; +import createMock from '../../utils/createMock'; import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates'; const DOMAIN_ACCOUNT_ID = 99999; const SECURITY_GROUP_PREFIX = CONST.DOMAIN.DOMAIN_SECURITY_GROUP_PREFIX; function buildDomain(groups: Record; name: string}>): Domain { - const domain: Record = { + const domain = createMock({ validated: true, accountID: DOMAIN_ACCOUNT_ID, email: 'admin@test.com', domain_defaultSecurityGroupID: Object.keys(groups).at(0) ?? '', - }; + }); for (const [groupID, group] of Object.entries(groups)) { - domain[`${SECURITY_GROUP_PREFIX}${groupID}`] = { + const securityGroupKey: SecurityGroupKey = `${SECURITY_GROUP_PREFIX}${groupID}`; + const securityGroup: DomainSecurityGroup = { shared: group.members, name: group.name, enableRestrictedPrimaryLogin: false, enableRestrictedPolicyCreation: false, }; + domain[securityGroupKey] = securityGroup; } - return domain as unknown as Domain; + return domain; } function buildMemberRow(accountID: number): DomainMemberRowData { diff --git a/tests/unit/hooks/useExpenseSubmission.test.ts b/tests/unit/hooks/useExpenseSubmission.test.ts index 0e7784ce8532..06bcf70c405a 100644 --- a/tests/unit/hooks/useExpenseSubmission.test.ts +++ b/tests/unit/hooks/useExpenseSubmission.test.ts @@ -11,6 +11,7 @@ import Onyx from 'react-native-onyx'; import type * as Split from '../../../src/libs/actions/IOU/Split'; +import createMock from '../../utils/createMock'; import waitForBatchedUpdatesWithAct from '../../utils/waitForBatchedUpdatesWithAct'; const mockRequestMoneyAction = jest.fn(); @@ -174,7 +175,7 @@ function buildParams(overrides: Partial[ receiptFiles: {}, report: {reportID: REPORT_ID, type: CONST.REPORT.TYPE.CHAT} as Report, reportID: REPORT_ID, - policy: {id: 'policy-1'} as Policy, + policy: createMock({id: 'policy-1'}), policyCategories: {} as PolicyCategories, isDraftPolicy: false, currentUserPersonalDetails: {accountID: CURRENT_USER_ACCOUNT_ID, login: 'me@test.com', email: 'me@test.com'}, diff --git a/tests/unit/hooks/useExpensifyCardFeedsForFeedSelector.test.ts b/tests/unit/hooks/useExpensifyCardFeedsForFeedSelector.test.ts index 852e08532bed..c9e4be74b28d 100644 --- a/tests/unit/hooks/useExpensifyCardFeedsForFeedSelector.test.ts +++ b/tests/unit/hooks/useExpensifyCardFeedsForFeedSelector.test.ts @@ -1,6 +1,7 @@ import {renderHook} from '@testing-library/react-native'; import useExpensifyCardFeedsForFeedSelector from '@hooks/useExpensifyCardFeedsForFeedSelector'; +import useOnyx from '@hooks/useOnyx'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; @@ -12,12 +13,9 @@ const otherPolicyID = 'policy_other'; const currentUserAccountID = 1001; const orphanDomainFundID = 5555; -const mockUseOnyx = jest.fn(); +jest.mock('@hooks/useOnyx', () => jest.fn()); -jest.mock('@hooks/useOnyx', () => ({ - __esModule: true, - default: (...args: unknown[]): [unknown, {status?: string}] => mockUseOnyx(...args) as [unknown, {status?: string}], -})); +const mockUseOnyx = jest.mocked(useOnyx); jest.mock('@hooks/useCurrentUserPersonalDetails', () => ({ __esModule: true, @@ -54,7 +52,7 @@ function configuredCardSettings(overrides: Record = {}) { describe('useExpensifyCardFeedsForFeedSelector', () => { beforeEach(() => { jest.clearAllMocks(); - mockUseOnyx.mockImplementation((key: string) => { + mockUseOnyx.mockImplementation((key) => { if (key === ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS) { return [{}, {status: 'loaded'}]; } @@ -64,7 +62,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { if (key === ONYXKEYS.COLLECTION.DOMAIN) { return [{}, {status: 'loaded'}]; } - return [undefined, {}]; + return [undefined, {status: 'loaded'}]; }); }); @@ -89,7 +87,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { }); it('ignores preferredPolicy: visible feeds with no linkedPolicyIDs are never primary', () => { - mockUseOnyx.mockImplementation((key: string) => { + mockUseOnyx.mockImplementation((key) => { if (key === ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS) { return [ { @@ -111,7 +109,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { if (key === ONYXKEYS.COLLECTION.DOMAIN) { return [{...domainWithAdmin(111, currentUserAccountID), ...domainWithAdmin(222, currentUserAccountID)}, {status: 'loaded'}]; } - return [undefined, {}]; + return [undefined, {status: 'loaded'}]; }); const {result} = renderHook(() => useExpensifyCardFeedsForFeedSelector(currentPolicyID)); @@ -123,7 +121,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { }); it('partitions by linkedPolicyIDs for feeds that define them (per feed, not global)', () => { - mockUseOnyx.mockImplementation((key: string) => { + mockUseOnyx.mockImplementation((key) => { if (key === ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS) { return [ { @@ -147,7 +145,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { if (key === ONYXKEYS.COLLECTION.DOMAIN) { return [{...domainWithAdmin(10, currentUserAccountID), ...domainWithAdmin(20, currentUserAccountID)}, {status: 'loaded'}]; } - return [undefined, {}]; + return [undefined, {status: 'loaded'}]; }); const {result} = renderHook(() => useExpensifyCardFeedsForFeedSelector(currentPolicyID)); @@ -160,7 +158,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { }); it('resolves linkedPolicyIDs nested under US (not only on settings root)', () => { - mockUseOnyx.mockImplementation((key: string) => { + mockUseOnyx.mockImplementation((key) => { if (key === ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS) { return [ { @@ -182,7 +180,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { if (key === ONYXKEYS.COLLECTION.DOMAIN) { return [domainWithAdmin(77, currentUserAccountID), {status: 'loaded'}]; } - return [undefined, {}]; + return [undefined, {status: 'loaded'}]; }); const {result} = renderHook(() => useExpensifyCardFeedsForFeedSelector(currentPolicyID)); @@ -195,7 +193,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { it('resolves linkedPolicyIDs (API spelling) and matches policyID case-insensitively', () => { const policyIdUpper = 'BF0EEF42D8D1036B'; const policyIdLower = policyIdUpper.toLowerCase(); - mockUseOnyx.mockImplementation((key: string) => { + mockUseOnyx.mockImplementation((key) => { if (key === ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS) { return [ { @@ -217,7 +215,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { if (key === ONYXKEYS.COLLECTION.DOMAIN) { return [domainWithAdmin(88, currentUserAccountID), {status: 'loaded'}]; } - return [undefined, {}]; + return [undefined, {status: 'loaded'}]; }); const {result} = renderHook(() => useExpensifyCardFeedsForFeedSelector(policyIdLower)); @@ -228,7 +226,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { }); it('includes feeds visible via linkedPolicyIDs when preferredPolicy is a different workspace', () => { - mockUseOnyx.mockImplementation((key: string) => { + mockUseOnyx.mockImplementation((key) => { if (key === ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS) { return [ { @@ -247,7 +245,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { if (key === ONYXKEYS.COLLECTION.DOMAIN) { return [domainWithAdmin(7, currentUserAccountID), {status: 'loaded'}]; } - return [undefined, {}]; + return [undefined, {status: 'loaded'}]; }); const {result} = renderHook(() => useExpensifyCardFeedsForFeedSelector(currentPolicyID)); @@ -259,7 +257,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { }); it('excludes feeds that are not visible to admin (single-key settings)', () => { - mockUseOnyx.mockImplementation((key: string) => { + mockUseOnyx.mockImplementation((key) => { if (key === ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS) { return [ { @@ -271,7 +269,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { if (key === ONYXKEYS.COLLECTION.POLICY) { return [adminPolicy(currentPolicyID), {status: 'loaded'}]; } - return [undefined, {}]; + return [undefined, {status: 'loaded'}]; }); const {result} = renderHook(() => useExpensifyCardFeedsForFeedSelector(currentPolicyID)); @@ -282,7 +280,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { }); it('includes orphan feeds in otherFeeds when user is domain admin', () => { - mockUseOnyx.mockImplementation((key: string) => { + mockUseOnyx.mockImplementation((key) => { if (key === ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS) { return [ { @@ -303,7 +301,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { if (key === ONYXKEYS.CARD_LIST) { return [{card1: {bank: CONST.EXPENSIFY_CARD.BANK, fundID: orphanDomainFundID.toString()}}, {status: 'loaded'}]; } - return [undefined, {}]; + return [undefined, {status: 'loaded'}]; }); const {result} = renderHook(() => useExpensifyCardFeedsForFeedSelector(currentPolicyID)); @@ -316,7 +314,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { it('includes orphan feeds in otherFeeds when fundID matches current workspaceAccountID', () => { const workspaceAccountID = 9001; - mockUseOnyx.mockImplementation((key: string) => { + mockUseOnyx.mockImplementation((key) => { if (key === ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS) { return [ { @@ -345,7 +343,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { if (key === ONYXKEYS.CARD_LIST) { return [{card1: {bank: CONST.EXPENSIFY_CARD.BANK, fundID: workspaceAccountID.toString()}}, {status: 'loaded'}]; } - return [undefined, {}]; + return [undefined, {status: 'loaded'}]; }); const {result} = renderHook(() => useExpensifyCardFeedsForFeedSelector(currentPolicyID)); @@ -357,7 +355,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { }); it('excludes orphan feeds when user is not domain admin and not workspace admin for fundID', () => { - mockUseOnyx.mockImplementation((key: string) => { + mockUseOnyx.mockImplementation((key) => { if (key === ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS) { return [ { @@ -375,7 +373,7 @@ describe('useExpensifyCardFeedsForFeedSelector', () => { if (key === ONYXKEYS.COLLECTION.DOMAIN) { return [{}, {status: 'loaded'}]; } - return [undefined, {}]; + return [undefined, {status: 'loaded'}]; }); const {result} = renderHook(() => useExpensifyCardFeedsForFeedSelector(currentPolicyID)); diff --git a/tests/unit/hooks/useReceiptTraining.test.ts b/tests/unit/hooks/useReceiptTraining.test.ts index 994b4292f5be..0d8c89b4eba1 100644 --- a/tests/unit/hooks/useReceiptTraining.test.ts +++ b/tests/unit/hooks/useReceiptTraining.test.ts @@ -4,6 +4,8 @@ import useReceiptTraining from '@components/MoneyRequestConfirmationList/hooks/u import type * as OnyxTypes from '@src/types/onyx'; +import createMock from '../../utils/createMock'; + jest.mock('@components/ProductTrainingContext', () => ({ useProductTrainingContext: () => ({ shouldShowProductTrainingTooltip: false, @@ -12,7 +14,7 @@ jest.mock('@components/ProductTrainingContext', () => ({ })); function makeTransaction(receipt: Partial> = {}): OnyxTypes.Transaction { - return {transactionID: 'txn1', receipt} as unknown as OnyxTypes.Transaction; + return createMock({transactionID: 'txn1', receipt}); } describe('useReceiptTraining', () => { diff --git a/tests/unit/hooks/useSearchShouldCalculateTotals.test.ts b/tests/unit/hooks/useSearchShouldCalculateTotals.test.ts index 108eace944e1..9aeb9b1e596e 100644 --- a/tests/unit/hooks/useSearchShouldCalculateTotals.test.ts +++ b/tests/unit/hooks/useSearchShouldCalculateTotals.test.ts @@ -1,34 +1,24 @@ import {renderHook} from '@testing-library/react-native'; +import useOnyx from '@hooks/useOnyx'; import useSearchShouldCalculateTotals, {getSearchRequestOffsetForMissingAllMatchingCount} from '@hooks/useSearchShouldCalculateTotals'; import CONST from '@src/CONST'; -import ONYXKEYS from '@src/ONYXKEYS'; - -const onyxData: Record = {}; - -const mockUseOnyx = jest.fn( - ( - key: string, - options?: { - selector?: (value: unknown) => unknown; - }, - ) => { - const value = onyxData[key]; - const selectedValue = options?.selector ? options.selector(value as never) : value; - return [selectedValue]; - }, -); - -jest.mock('@hooks/useOnyx', () => ({ - __esModule: true, - default: (key: string, options?: {selector?: (value: unknown) => unknown}) => mockUseOnyx(key, options), -})); +import type ONYXKEYS from '@src/ONYXKEYS'; + +import type {OnyxValue} from 'react-native-onyx'; + +jest.mock('@hooks/useOnyx', () => jest.fn()); + +const mockUseOnyx = jest.mocked(useOnyx); + +let savedSearches: OnyxValue; describe('useSearchShouldCalculateTotals', () => { beforeEach(() => { - onyxData[ONYXKEYS.SAVED_SEARCHES] = undefined; + savedSearches = undefined; mockUseOnyx.mockClear(); + mockUseOnyx.mockImplementation(() => [savedSearches, {status: 'loaded'}]); }); it('returns false when disabled', () => { @@ -50,7 +40,7 @@ describe('useSearchShouldCalculateTotals', () => { }); it('returns true for saved searches that match the hash', () => { - onyxData[ONYXKEYS.SAVED_SEARCHES] = { + savedSearches = { // eslint-disable-next-line @typescript-eslint/naming-convention 456: { name: 'My search', @@ -64,7 +54,7 @@ describe('useSearchShouldCalculateTotals', () => { }); it('returns false when saved searches do not match the hash', () => { - onyxData[ONYXKEYS.SAVED_SEARCHES] = { + savedSearches = { // eslint-disable-next-line @typescript-eslint/naming-convention 456: { name: 'My search',