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
13 changes: 7 additions & 6 deletions server/victory-chart-renderer/tests/resolveCanvasSize.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type {TNode} from 'react-native-render-html';

import {describe, expect, test} from 'bun:test';
import createMock from 'tests/utils/createMock';

import resolveCanvasSize from '../src/resolveCanvasSize';

function makeChartNode(attributes: TNode['attributes'], children: TNode[] = []): TNode {
return {
return createMock<TNode>({
tagName: 'victorychart',
attributes,
children,
} as unknown as TNode;
});
}

describe('resolveCanvasSize', () => {
Expand All @@ -30,11 +31,11 @@ describe('resolveCanvasSize', () => {
describe('validation errors', () => {
test('throws when overlays exist without chart dimensions', () => {
const tnode = makeChartNode({}, [
{
createMock<TNode>({
tagName: 'victorylabel',
attributes: {x: '10', y: '20', text: 'Title'},
children: [],
} as unknown as TNode,
}),
]);

expect(() => resolveCanvasSize(tnode)).toThrow('require explicit width and height');
Expand All @@ -60,11 +61,11 @@ describe('resolveCanvasSize', () => {

test('throws when overlay has non-numeric coordinates and chart dimensions are missing', () => {
const tnode = makeChartNode({}, [
{
createMock<TNode>({
tagName: 'victorylabel',
attributes: {x: 'not-a-number', y: '20', text: 'Title'},
children: [],
} as unknown as TNode,
}),
]);

expect(() => resolveCanvasSize(tnode)).toThrow('require explicit width and height');
Expand Down
32 changes: 24 additions & 8 deletions tests/actions/IOUTest/GetUpdateMoneyRequestParamsTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {getUpdateMoneyRequestParams} from '@libs/actions/IOU/UpdateMoneyRequest';
import initOnyxDerivedValues from '@libs/actions/OnyxDerived';
import {isRecord} from '@libs/ObjectUtils';

import CONST from '@src/CONST';
import IntlStore from '@src/languages/IntlStore';
Expand Down Expand Up @@ -51,6 +52,15 @@ const REPORT_ID = 'testReportID';
const IOU_REPORT_ID = 'testIOUReportID';
const POLICY_ID = 'testPolicyID';

function isRecentlyUsedTags<TKey extends string>(value: unknown, tagListName: TKey): value is Pick<RecentlyUsedTags, TKey> {
if (!isRecord(value)) {
return false;
}

const tags = value[tagListName];
return Array.isArray(tags) && tags.every((tag: unknown): tag is string => typeof tag === 'string');
}

const transactionThreadReport: Report = {
reportID: REPORT_ID,
type: CONST.REPORT.TYPE.EXPENSE,
Expand Down Expand Up @@ -162,8 +172,10 @@ describe('getUpdateMoneyRequestParams — policyTagList', () => {
// Then the tag should appear in the recently used tags for the correct policy and tag list
const recentlyUsedTagsEntry = onyxData.optimisticData?.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${POLICY_ID}`);
expect(recentlyUsedTagsEntry).toBeDefined();
const value = recentlyUsedTagsEntry?.value as RecentlyUsedTags;
expect(value[tagListName]).toContain(tag1);
if (!recentlyUsedTagsEntry || !isRecentlyUsedTags(recentlyUsedTagsEntry.value, tagListName)) {
throw new Error('Expected recently used tags for the changed tag list');
}
expect(recentlyUsedTagsEntry.value[tagListName]).toContain(tag1);
});

it('should prepend the new tag before existing recently used tags', () => {
Expand Down Expand Up @@ -203,9 +215,11 @@ describe('getUpdateMoneyRequestParams — policyTagList', () => {
// Then the new tag should be first and the old tag should still be present
const recentlyUsedTagsEntry = onyxData.optimisticData?.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${POLICY_ID}`);
expect(recentlyUsedTagsEntry).toBeDefined();
const value = recentlyUsedTagsEntry?.value as RecentlyUsedTags;
expect(value[tagListName].at(0)).toBe(tag2);
expect(value[tagListName]).toContain(tag1);
if (!recentlyUsedTagsEntry || !isRecentlyUsedTags(recentlyUsedTagsEntry.value, tagListName)) {
throw new Error('Expected recently used tags for the changed tag list');
}
expect(recentlyUsedTagsEntry.value[tagListName].at(0)).toBe(tag2);
expect(recentlyUsedTagsEntry.value[tagListName]).toContain(tag1);
});

it('should deduplicate when the same tag is set again', () => {
Expand Down Expand Up @@ -245,9 +259,11 @@ describe('getUpdateMoneyRequestParams — policyTagList', () => {
// Then tag1 should appear exactly once and be at the front
const recentlyUsedTagsEntry = onyxData.optimisticData?.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.POLICY_RECENTLY_USED_TAGS}${POLICY_ID}`);
expect(recentlyUsedTagsEntry).toBeDefined();
const value = recentlyUsedTagsEntry?.value as RecentlyUsedTags;
expect(value[tagListName].filter((t) => t === tag1).length).toBe(1);
expect(value[tagListName].at(0)).toBe(tag1);
if (!recentlyUsedTagsEntry || !isRecentlyUsedTags(recentlyUsedTagsEntry.value, tagListName)) {
throw new Error('Expected recently used tags for the changed tag list');
}
expect(recentlyUsedTagsEntry.value[tagListName].filter((tag) => tag === tag1)).toHaveLength(1);
expect(recentlyUsedTagsEntry.value[tagListName].at(0)).toBe(tag1);
});

it('should fall back to the same behavior as passing an empty policyTagList when policyTagList is undefined and Onyx has no policy tags data', () => {
Expand Down
96 changes: 42 additions & 54 deletions tests/actions/IOUTest/UpdateMoneyRequestVendorTest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import {updateMoneyRequestVendor} from '@libs/actions/IOU/UpdateMoneyRequest';
import * as API from '@libs/API';
import * as APIActions from '@libs/API';
import {getOriginalMessage} from '@libs/ReportActionsUtils';

import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Report, Transaction, TransactionViolation} from '@src/types/onyx';
import type {Report, ReportAction, Transaction, TransactionViolation} from '@src/types/onyx';

import Onyx from 'react-native-onyx';

import {getRequiredOnyxUpdate, getRequiredOnyxUpdates, getRequiredWriteCall} from '../../utils/TestHelper';
import {isObject} from '../../utils/typeGuards';
import waitForBatchedUpdates from '../../utils/waitForBatchedUpdates';

const TRANSACTION_ID = 'txn-vendor-test';
Expand Down Expand Up @@ -34,41 +37,31 @@ const otherViolation: TransactionViolation = {
};

describe('updateMoneyRequestVendor', () => {
let writeSpy: jest.SpyInstance;
let writeSpy: jest.SpiedFunction<typeof APIActions.write>;

beforeAll(() => {
Onyx.init({keys: ONYXKEYS});
});

beforeEach(() => {
writeSpy = jest.spyOn(API, 'write').mockImplementation(jest.fn());
writeSpy = jest.spyOn(APIActions, 'write').mockImplementation(jest.fn());
});

afterEach(async () => {
writeSpy.mockRestore();
await Onyx.clear();
});

type OnyxDataArg = {
optimisticData: Array<{key: string; value: unknown}>;
successData: Array<{key: string; value: unknown}>;
failureData: Array<{key: string; value: unknown}>;
};
const getOnyxDataArg = (): OnyxDataArg | undefined => {
const firstCall = writeSpy.mock.calls.at(0) as unknown[] | undefined;
return firstCall?.at(2) as OnyxDataArg | undefined;
};
const getOnyxDataArg = () => getRequiredWriteCall(writeSpy.mock.calls, 0)[2];

it('clears an existing inactive-vendor violation optimistically when a vendor is picked', async () => {
await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${TRANSACTION_ID}`, [otherViolation, inactiveVendorViolation]);
await waitForBatchedUpdates();

updateMoneyRequestVendor({transactionID: TRANSACTION_ID, vendorID: 'v-active', transaction: baseTransaction, delegateAccountID: undefined});

const onyxData = getOnyxDataArg();
const violationsUpdate = onyxData?.optimisticData.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${TRANSACTION_ID}`);
expect(violationsUpdate).toBeDefined();
expect(violationsUpdate?.value).toEqual([otherViolation]);
const violationsUpdate = getRequiredOnyxUpdate(getOnyxDataArg(), 'optimisticData', `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${TRANSACTION_ID}`, Onyx.METHOD.SET, true);
expect(violationsUpdate.value).toEqual([otherViolation]);
});

it('clears an existing inactive-vendor violation optimistically when the vendor is cleared', async () => {
Expand All @@ -77,10 +70,8 @@ describe('updateMoneyRequestVendor', () => {

updateMoneyRequestVendor({transactionID: TRANSACTION_ID, vendorID: '', transaction: baseTransaction, delegateAccountID: undefined});

const onyxData = getOnyxDataArg();
const violationsUpdate = onyxData?.optimisticData.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${TRANSACTION_ID}`);
expect(violationsUpdate).toBeDefined();
expect(violationsUpdate?.value).toEqual([]);
const violationsUpdate = getRequiredOnyxUpdate(getOnyxDataArg(), 'optimisticData', `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${TRANSACTION_ID}`, Onyx.METHOD.SET, true);
expect(violationsUpdate.value).toEqual([]);
});

it('restores the original violation list in failureData so a server rejection rolls back cleanly', async () => {
Expand All @@ -90,9 +81,8 @@ describe('updateMoneyRequestVendor', () => {

updateMoneyRequestVendor({transactionID: TRANSACTION_ID, vendorID: 'v-active', transaction: baseTransaction, delegateAccountID: undefined});

const onyxData = getOnyxDataArg();
const failureViolations = onyxData?.failureData.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${TRANSACTION_ID}`);
expect(failureViolations?.value).toEqual(original);
const failureViolations = getRequiredOnyxUpdate(getOnyxDataArg(), 'failureData', `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${TRANSACTION_ID}`, Onyx.METHOD.SET, true);
expect(failureViolations.value).toEqual(original);
});

it('does not write a violations update when there was no inactive-vendor violation to clear', async () => {
Expand All @@ -101,9 +91,10 @@ describe('updateMoneyRequestVendor', () => {

updateMoneyRequestVendor({transactionID: TRANSACTION_ID, vendorID: 'v-active', transaction: baseTransaction, delegateAccountID: undefined});

const onyxData = getOnyxDataArg();
const violationsUpdate = onyxData?.optimisticData.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${TRANSACTION_ID}`);
expect(violationsUpdate).toBeUndefined();
const violationsUpdate = getRequiredOnyxUpdates(getOnyxDataArg(), 'optimisticData').some(
(entry) => isObject(entry) && entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${TRANSACTION_ID}`,
);
expect(violationsUpdate).toBe(false);
});

it('falls back to the Onyx-cached transaction for vendor rollback when caller omits transaction', async () => {
Expand All @@ -116,9 +107,8 @@ describe('updateMoneyRequestVendor', () => {

updateMoneyRequestVendor({transactionID: TRANSACTION_ID, vendorID: 'v-new', delegateAccountID: undefined});

const onyxData = getOnyxDataArg();
const transactionFailure = onyxData?.failureData.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`);
expect(transactionFailure?.value).toEqual({
const transactionFailure = getRequiredOnyxUpdate(getOnyxDataArg(), 'failureData', `${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`, Onyx.METHOD.MERGE, true);
expect(transactionFailure.value).toEqual({
pendingFields: {vendor: null},
comment: {vendor: previousVendor},
});
Expand All @@ -130,17 +120,15 @@ describe('updateMoneyRequestVendor', () => {
// pendingFields-clear entry still runs (so the offline indicator clears on rejection).
updateMoneyRequestVendor({transactionID: TRANSACTION_ID, vendorID: 'v-new', delegateAccountID: undefined});

const onyxData = getOnyxDataArg();
const transactionFailure = onyxData?.failureData.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`);
expect(transactionFailure?.value).toEqual({pendingFields: {vendor: null}});
const transactionFailure = getRequiredOnyxUpdate(getOnyxDataArg(), 'failureData', `${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`, Onyx.METHOD.MERGE, true);
expect(transactionFailure.value).toEqual({pendingFields: {vendor: null}});
});

it('writes pendingFields.vendor = UPDATE in optimisticData so the offline indicator surfaces', () => {
updateMoneyRequestVendor({transactionID: TRANSACTION_ID, vendorID: 'v-new', transaction: baseTransaction, delegateAccountID: undefined});

const onyxData = getOnyxDataArg();
const transactionOptimistic = onyxData?.optimisticData.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`);
expect(transactionOptimistic?.value).toEqual({
const transactionOptimistic = getRequiredOnyxUpdate(getOnyxDataArg(), 'optimisticData', `${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`, Onyx.METHOD.MERGE, true);
expect(transactionOptimistic.value).toEqual({
comment: {vendor: {externalID: 'v-new', isManuallySet: true}},
pendingFields: {vendor: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE},
});
Expand All @@ -149,19 +137,17 @@ describe('updateMoneyRequestVendor', () => {
it('clears pendingFields.vendor in successData when the server confirms the write', () => {
updateMoneyRequestVendor({transactionID: TRANSACTION_ID, vendorID: 'v-new', transaction: baseTransaction, delegateAccountID: undefined});

const onyxData = getOnyxDataArg();
const transactionSuccess = onyxData?.successData.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`);
expect(transactionSuccess?.value).toEqual({pendingFields: {vendor: null}});
const transactionSuccess = getRequiredOnyxUpdate(getOnyxDataArg(), 'successData', `${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`, Onyx.METHOD.MERGE, true);
expect(transactionSuccess.value).toEqual({pendingFields: {vendor: null}});
});

it('clears pendingFields.vendor in failureData when the server rejects the write', () => {
// Even without a prior snapshot to roll the vendor itself back, the pending indicator must
// clear on failure — otherwise the row stays stuck in "pending" forever after a server reject.
updateMoneyRequestVendor({transactionID: TRANSACTION_ID, vendorID: 'v-new', delegateAccountID: undefined});

const onyxData = getOnyxDataArg();
const transactionFailure = onyxData?.failureData.find((entry) => entry.key === `${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`);
expect(transactionFailure?.value).toMatchObject({pendingFields: {vendor: null}});
const transactionFailure = getRequiredOnyxUpdate(getOnyxDataArg(), 'failureData', `${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`, Onyx.METHOD.MERGE, true);
expect(transactionFailure.value).toMatchObject({pendingFields: {vendor: null}});
});

describe('optimistic MODIFIED_EXPENSE report action', () => {
Expand All @@ -176,14 +162,15 @@ describe('updateMoneyRequestVendor', () => {
reportName: 'Transaction thread',
} as Report;

type ReportActionMap = Record<string, {actionName?: string; originalMessage?: {vendor?: unknown; oldVendor?: unknown}}>;
type ModifiedExpenseReportAction = ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE>;
function isModifiedExpenseReportAction(value: unknown): value is ModifiedExpenseReportAction {
return isObject(value) && value.actionName === CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE && typeof value.reportActionID === 'string' && typeof value.created === 'string';
}

const findOptimisticModifiedExpense = (): {actionName?: string; originalMessage?: {vendor?: unknown; oldVendor?: unknown}} | undefined => {
const onyxData = getOnyxDataArg();
const findOptimisticModifiedExpense = () => {
const reportActionsKey = `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${TRANSACTION_THREAD_REPORT_ID}`;
const update = onyxData?.optimisticData.find((entry) => entry.key === reportActionsKey);
const value = update?.value as ReportActionMap | undefined;
return value ? Object.values(value).find((action) => action?.actionName === CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE) : undefined;
const update = getRequiredOnyxUpdate(getOnyxDataArg(), 'optimisticData', reportActionsKey, Onyx.METHOD.MERGE, true);
return Object.values(update.value).find((action): action is ModifiedExpenseReportAction => isModifiedExpenseReportAction(action));
};

it('builds an optimistic MODIFIED_EXPENSE with `vendor` set and `oldVendor` null when adding a vendor (no prior)', () => {
Expand All @@ -199,7 +186,7 @@ describe('updateMoneyRequestVendor', () => {
expect(optimisticAction).toBeDefined();
// `oldVendor: null` signals "no prior vendor". Onyx strips nested nulls on merge, which
// is fine — `ModifiedExpenseMessage` treats either key's presence as a vendor change.
expect(optimisticAction?.originalMessage).toMatchObject({
expect(getOriginalMessage(optimisticAction)).toMatchObject({
vendor: {externalID: 'v-new', isManuallySet: true},
oldVendor: null,
});
Expand All @@ -221,7 +208,7 @@ describe('updateMoneyRequestVendor', () => {
});

const optimisticAction = findOptimisticModifiedExpense();
expect(optimisticAction?.originalMessage).toMatchObject({
expect(getOriginalMessage(optimisticAction)).toMatchObject({
oldVendor: previousVendor,
vendor: {externalID: 'v-new', isManuallySet: true},
});
Expand All @@ -243,7 +230,7 @@ describe('updateMoneyRequestVendor', () => {
});

const optimisticAction = findOptimisticModifiedExpense();
expect(optimisticAction?.originalMessage).toMatchObject({
expect(getOriginalMessage(optimisticAction)).toMatchObject({
oldVendor: previousVendor,
vendor: null,
});
Expand All @@ -257,9 +244,10 @@ describe('updateMoneyRequestVendor', () => {
delegateAccountID: undefined,
});

const onyxData = getOnyxDataArg();
const reportActionsUpdate = onyxData?.optimisticData.find((entry) => entry.key.startsWith(ONYXKEYS.COLLECTION.REPORT_ACTIONS));
expect(reportActionsUpdate).toBeUndefined();
const reportActionsUpdate = getRequiredOnyxUpdates(getOnyxDataArg(), 'optimisticData').some(
(entry) => isObject(entry) && typeof entry.key === 'string' && entry.key.startsWith(ONYXKEYS.COLLECTION.REPORT_ACTIONS),
);
expect(reportActionsUpdate).toBe(false);
});
});
});
Loading
Loading