Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions tests/unit/LHNAvatarPipelineTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
createWorkspaceTaskReport,
createWorkspaceThread,
} from '../utils/collections/reports';
import createMock from '../utils/createMock';
import {translateLocal} from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

Expand Down Expand Up @@ -195,15 +196,18 @@ describe('LHN Avatar Pipeline', () => {
} as Report);

// Receiver policy (for B2B invoice cases)
await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${RECEIVER_POLICY_ID}`, {
id: RECEIVER_POLICY_ID,
name: 'Receiver Workspace',
type: CONST.POLICY.TYPE.TEAM,
role: CONST.POLICY.ROLE.ADMIN,
owner: 'user2@test.com',
ownerAccountID: 2,
outputCurrency: 'USD',
} as Policy);
await Onyx.set(
`${ONYXKEYS.COLLECTION.POLICY}${RECEIVER_POLICY_ID}`,
createMock<Policy>({
id: RECEIVER_POLICY_ID,
name: 'Receiver Workspace',
type: CONST.POLICY.TYPE.TEAM,
role: CONST.POLICY.ROLE.ADMIN,
owner: 'user2@test.com',
ownerAccountID: 2,
outputCurrency: 'USD',
}),
);

// B2B invoice room (for B2B invoice report cases)
await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${B2B_INVOICE_ROOM_ID}`, {
Expand Down Expand Up @@ -549,11 +553,11 @@ describe('LHN Avatar Pipeline', () => {

// ── Case 12c: Personal task (parent is DM) → SINGLE ──────────────
it('Task Report (personal, parent is DM) → single', () => {
const report = {
const report = createMock<Report>({
...createWorkspaceTaskReport(123, [CURRENT_USER_ACCOUNT_ID, 2], PARENT_DM_REPORT_ID),
policyID: undefined,
ownerAccountID: 2,
} as unknown as Report;
});
const result = computeAvatarResult({report, policy: null});

expect(result.shouldShowSubscript).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {findFocusedRoute} from '@react-navigation/native';
import {useEffect, useMemo} from 'react';

import createRandomTransaction from '../utils/collections/transaction';
import createMock from '../utils/createMock';

// Mock the TransactionThreadNavigation module
jest.mock('@libs/actions/TransactionThreadNavigation', () => ({
Expand Down Expand Up @@ -62,11 +63,12 @@ describe('MoneyRequestReportTransactionList - Active Transaction IDs Effect', ()
const mockClearActiveTransactionIDs = jest.mocked(clearActiveTransactionIDs);
const mockGetActiveTransactionIDs = jest.mocked(getActiveTransactionIDs);
const mockFindFocusedRoute = jest.mocked(findFocusedRoute);
const mockGetRootState = jest.spyOn(navigationRef, 'getRootState');

beforeEach(() => {
jest.clearAllMocks();
mockGetActiveTransactionIDs.mockReturnValue({ids: null, descriptors: null});
(navigationRef.getRootState as jest.Mock).mockReturnValue({} as ReturnType<typeof navigationRef.getRootState>);
mockGetRootState.mockReturnValue(createMock<NonNullable<ReturnType<typeof navigationRef.getRootState>>>({}));
});

it('should call setActiveTransactionIDs when focused route is SEARCH_REPORT', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jest.mock('@libs/Navigation/Navigation', () => ({
jest.mock('@libs/Navigation/helpers/isSearchTopmostFullScreenRoute', () => jest.fn());

describe('getReportRouteForCurrentContext', () => {
const mockGetActiveRoute = Navigation.getActiveRoute as jest.MockedFunction<typeof Navigation.getActiveRoute>;
const mockIsSearchTopmostFullScreenRoute = isSearchTopmostFullScreenRoute as jest.MockedFunction<typeof isSearchTopmostFullScreenRoute>;
const mockGetActiveRoute = jest.mocked(Navigation.getActiveRoute);
const mockIsSearchTopmostFullScreenRoute = jest.mocked(isSearchTopmostFullScreenRoute);

beforeEach(() => {
jest.clearAllMocks();
Expand Down
8 changes: 5 additions & 3 deletions tests/unit/NavigationFocusReturnTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,9 @@ describe('shouldSkipAutoFocusDueToExistingFocus', () => {
}

it('returns false when body holds focus (nothing else claimed)', () => {
(document.activeElement as HTMLElement | null)?.blur();
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur();
}
expect(shouldSkipAutoFocusDueToExistingFocus()).toBe(false);
});

Expand Down Expand Up @@ -3010,8 +3012,8 @@ describe('teardown / setup lifecycle', () => {
const originalAddListener = navigationRef.addListener.bind(navigationRef);
const originalIsReady = navigationRef.isReady.bind(navigationRef);
const originalGetRootState = navigationRef.getRootState.bind(navigationRef);
const addListenerSpy = jest.fn(() => () => {});
navigationRef.addListener = addListenerSpy as unknown as typeof navigationRef.addListener;
const addListenerSpy = jest.fn<ReturnType<typeof navigationRef.addListener>, Parameters<typeof navigationRef.addListener>>(() => () => {});
navigationRef.addListener = addListenerSpy;
navigationRef.isReady = () => false;
navigationRef.getRootState = () => undefined;
try {
Expand Down
25 changes: 20 additions & 5 deletions tests/unit/NetworkStateReachabilityTest.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
// cspell:ignore bssid
import CONST from '@src/CONST';
import type Middleware from '@src/libs/Middleware/types';
import type * as NetworkState from '@src/libs/NetworkState';
import type Request from '@src/types/onyx/Request';

import type {NetInfoState} from '@react-native-community/netinfo';
import type {NetInfoState, NetInfoWifiState} from '@react-native-community/netinfo';
import type {OnyxKey} from 'react-native-onyx';

import {NetInfoStateType} from '@react-native-community/netinfo';

let netInfoListener: ((state: NetInfoState) => void) | null = null;
const mockOnyxCallbacks = new Map<string, (value: unknown) => void>();

jest.mock('@react-native-community/netinfo', () => ({
NetInfoStateType: {wifi: 'wifi'},
addEventListener: jest.fn((cb: (state: NetInfoState) => void) => {
netInfoListener = cb;
return () => {
Expand All @@ -28,17 +32,28 @@ jest.mock('react-native-onyx', () => ({
}),
}));

function fireNetInfoState(overrides: Partial<NetInfoState>) {
function fireNetInfoState(overrides: Partial<NetInfoWifiState>) {
if (!netInfoListener) {
throw new Error('NetInfo listener not registered');
}
netInfoListener({
isConnected: true,
isInternetReachable: true,
type: 'wifi' as NetInfoState['type'],
details: null,
type: NetInfoStateType.wifi,
details: {
isConnectionExpensive: false,
ssid: null,
bssid: null,
strength: null,
ipAddress: null,
subnet: null,
frequency: null,
linkSpeed: null,
rxLinkSpeed: null,
txLinkSpeed: null,
},
...overrides,
} as NetInfoState);
});
}

function fireSessionChange(accountID: number) {
Expand Down
15 changes: 8 additions & 7 deletions tests/unit/NextStepUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import type {Policy, Report, ReportAction, Transaction, TransactionViolations} f
import type {ReportNextStep} from '@src/types/onyx/Report';
import {toCollectionDataSet} from '@src/types/utils/CollectionDataSet';

import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import type {OnyxCollection} from 'react-native-onyx';

import {format} from 'date-fns';
import Onyx from 'react-native-onyx';

import createMock from '../utils/createMock';
import {formatPhoneNumber, getCurrencyDecimalsLocal, translateLocal} from '../utils/TestHelper';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';

Expand Down Expand Up @@ -895,12 +896,12 @@ describe('libs/NextStepUtils', () => {
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
} as Report;

const transaction: Transaction = {
const transaction = createMock<Transaction>({
transactionID: 'txn-1',
reportID: report.reportID,
amount: -500,
currency: CONST.CURRENCY.USD,
} as Transaction;
});

const transactionViolations: OnyxCollection<TransactionViolations> = {
[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`]: [
Expand All @@ -914,7 +915,7 @@ describe('libs/NextStepUtils', () => {
const result = getReportNextStep({
moneyRequestReport: report,
moneyRequestReportOwnerLogin: currentUserEmail,
transactions: [transaction] as Array<OnyxEntry<Transaction>>,
transactions: [transaction],
policy: undefined,
transactionViolations,
currentUserEmail,
Expand Down Expand Up @@ -1021,12 +1022,12 @@ describe('libs/NextStepUtils', () => {
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
} as Report;

const transaction: Transaction = {
const transaction = createMock<Transaction>({
transactionID: 'txn-2',
reportID: report.reportID,
amount: -500,
currency: CONST.CURRENCY.USD,
} as Transaction;
});

const transactionViolations: OnyxCollection<TransactionViolations> = {
[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transaction.transactionID}`]: [
Expand All @@ -1043,7 +1044,7 @@ describe('libs/NextStepUtils', () => {
const result = getReportNextStep({
moneyRequestReport: report,
moneyRequestReportOwnerLogin: currentUserEmail,
transactions: [transaction] as Array<OnyxEntry<Transaction>>,
transactions: [transaction],
policy,
transactionViolations,
currentUserEmail,
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/PaymentCardFormTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import AddPaymentCard from '@pages/settings/Subscription/PaymentCard';
import ONYXKEYS from '@src/ONYXKEYS';
import SCREENS from '@src/SCREENS';

import type ReactNative from 'react-native';

import {PortalProvider} from '@gorhom/portal';
import {NavigationContainer} from '@react-navigation/native';
import {createStackNavigator} from '@react-navigation/stack';
Expand All @@ -22,8 +24,8 @@ import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct';

jest.mock('@components/RenderHTML', () => {
const ReactMock = require('react') as typeof React;
const {Text} = require('react-native') as {Text: React.ComponentType<{children?: React.ReactNode}>};
const ReactMock = jest.requireActual<typeof React>('react');
const {Text} = jest.requireActual<typeof ReactNative>('react-native');

return ({html}: {html: string}) => {
const plainText = html.replaceAll(/<[^>]*>/g, '');
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/ReimbursementAccountUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {ACHDataReimbursementAccount} from '../../src/types/onyx/Reimburseme
import CONST from '../../src/CONST';
import {getBankAccountIDAsNumber} from '../../src/libs/ReimbursementAccountUtils';
import ONYXKEYS from '../../src/ONYXKEYS';
import createMock from '../utils/createMock';

Onyx.init({keys: ONYXKEYS});

Expand All @@ -21,15 +22,15 @@ describe('ReimbursementAccountUtils', () => {
it('should return DEFAULT_NUMBER_ID when bankAccountID is undefined on achData', () => {
// Given achData without bank account id
// When resolving the numeric bank account id
const result = getBankAccountIDAsNumber({} as ACHDataReimbursementAccount);
const result = getBankAccountIDAsNumber(createMock<ACHDataReimbursementAccount>({}));
// Then we default to CONST.DEFAULT_NUMBER_ID
expect(result).toBe(CONST.DEFAULT_NUMBER_ID);
});

it('should return the existing bankAccountID when present', () => {
// Given achData with a bank account id
// When resolving the numeric bank account id
const result = getBankAccountIDAsNumber({bankAccountID: 42} as ACHDataReimbursementAccount);
const result = getBankAccountIDAsNumber(createMock<ACHDataReimbursementAccount>({bankAccountID: 42}));
// Then we return that id (not the default)
expect(result).toBe(42);
});
Expand Down
9 changes: 5 additions & 4 deletions tests/unit/libs/MoneyRequestReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {ReportAction, Transaction} from '@src/types/onyx';

import {actionR14932, actionR98765} from '../../../__mocks__/reportData/actions';
import {transactionR14932, transactionR98765} from '../../../__mocks__/reportData/transactions';
import createMock from '../../utils/createMock';

describe('getThreadReportIDsForTransactions', () => {
test('returns empty list for no transactions', () => {
Expand Down Expand Up @@ -37,17 +38,17 @@ describe('getThreadReportIDsForTransactions', () => {

describe('isActionVisibleOnMoneyRequestReport', () => {
test('hides created action by default', () => {
const createdAction = {
const createdAction = createMock<ReportAction>({
actionName: CONST.REPORT.ACTIONS.TYPE.CREATED,
} as ReportAction;
});

expect(isActionVisibleOnMoneyRequestReport(createdAction)).toBe(false);
});

test('shows created action when explicitly allowed', () => {
const createdAction = {
const createdAction = createMock<ReportAction>({
actionName: CONST.REPORT.ACTIONS.TYPE.CREATED,
} as ReportAction;
});

expect(isActionVisibleOnMoneyRequestReport(createdAction, true)).toBe(true);
});
Expand Down
Loading
Loading