Skip to content

Commit

Permalink
Merge pull request #37427 from VickyStash/ts-migration/tests-g8
Browse files Browse the repository at this point in the history
[TS migration] Migrate 'ReportTestUtils.js', 'deepReplaceKeysAndValuesTest.js', 'DistanceRequestUtilsTest.js', 'waitForBatchedUpdatesWithAct.js' and 'ConvertToLTRForComposerTest.js' test to TypeScript
  • Loading branch information
Hayata Suenaga authored Mar 4, 2024
2 parents 35244be + 858de5f commit 9769d56
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 78 deletions.
2 changes: 2 additions & 0 deletions src/libs/deepReplaceKeysAndValues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ function deepReplaceKeysAndValues<T extends ReplaceableValue>(target: T, oldVal:
}

export default deepReplaceKeysAndValues;

export type {ReplaceableValue};
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import DistanceRequestUtils from '../../src/libs/DistanceRequestUtils';
import DistanceRequestUtils from '@libs/DistanceRequestUtils';
import type {Unit} from '@src/types/onyx/Policy';

describe('DistanceRequestUtils', () => {
describe('getDistanceRequestAmount', () => {
test.each([
[350, 8605.146, 'mi', 65.5],
[561, 8605.146, 'km', 65.1],
])('Correctly calculates amount %s for %s%s at a rate of %s per unit', (expectedResult, distance, unit, rate) => {
] as const)('Correctly calculates amount %s for %s%s at a rate of %s per unit', (expectedResult: number, distance: number, unit: Unit, rate: number) => {
expect(DistanceRequestUtils.getDistanceRequestAmount(distance, unit, rate)).toBe(expectedResult);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import deepReplaceKeysAndValues from '../../src/libs/deepReplaceKeysAndValues';
/* eslint-disable @typescript-eslint/naming-convention */
import deepReplaceKeysAndValues from '@libs/deepReplaceKeysAndValues';
import type {ReplaceableValue} from '@libs/deepReplaceKeysAndValues';

describe('deepReplaceKeysAndValues', () => {
test.each([
Expand Down Expand Up @@ -121,7 +123,7 @@ describe('deepReplaceKeysAndValues', () => {
someOtherKey: 2,
},
],
])('deepReplaceKeysAndValues(%s)', (input, expected) => {
])('deepReplaceKeysAndValues(%s)', (input: ReplaceableValue, expected: ReplaceableValue) => {
expect(deepReplaceKeysAndValues(input, 'oldVal', 'newVal')).toStrictEqual(expected);
});
});
68 changes: 0 additions & 68 deletions tests/utils/ReportTestUtils.js

This file was deleted.

70 changes: 70 additions & 0 deletions tests/utils/ReportTestUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type {ReportAction, ReportActions} from '@src/types/onyx';
import type {ActionName} from '@src/types/onyx/OriginalMessage';
import createRandomReportAction from './collections/reportActions';

const actionNames: ActionName[] = ['ADDCOMMENT', 'IOU', 'REPORTPREVIEW', 'CLOSED'];

const getFakeReportAction = (index: number, actionName?: ActionName): ReportAction =>
({
actionName,
actorAccountID: index,
automatic: false,
avatar: '',
created: '2023-09-12 16:27:35.124',
isAttachment: true,
isFirstItem: false,
lastModified: '2021-07-14T15:00:00Z',
message: [
{
html: 'hey',
isDeletedParentAction: false,
isEdited: false,
reactions: [],
text: 'test',
type: 'TEXT',
whisperedTo: [],
},
],
originalMessage: {
html: 'hey',
lastModified: '2021-07-14T15:00:00Z',
// IOUReportID: index,
linkedReportID: index.toString(),
},
pendingAction: null,
person: [
{
type: 'TEXT',
style: 'strong',
text: '[email protected]',
},
],
previousReportActionID: '1',
reportActionID: index.toString(),
reportActionTimestamp: 1696243169753,
sequenceNumber: 0,
shouldShow: true,
timestamp: 1696243169,
whisperedToAccountIDs: [],
} as ReportAction);

const getMockedSortedReportActions = (length = 100): ReportAction[] => Array.from({length}, (element, index): ReportAction => getFakeReportAction(index));

const getMockedReportActionsMap = (length = 100): ReportActions => {
const mockReports: ReportActions[] = Array.from({length}, (element, index): ReportActions => {
const reportID = index + 1;
const actionName: ActionName = index === 0 ? 'CREATED' : actionNames[index % actionNames.length];
const reportAction = {
...createRandomReportAction(reportID),
actionName,
originalMessage: {
linkedReportID: reportID.toString(),
},
} as ReportAction;

return {[reportID]: reportAction};
});
return Object.assign({}, ...mockReports) as ReportActions;
};

export {getFakeReportAction, getMockedSortedReportActions, getMockedReportActionsMap};
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ import waitForBatchedUpdates from './waitForBatchedUpdates';
* - You're not rendering any react components at all in your tests, but have some async logic you need to wait for e.g. Onyx.merge(). Use waitForBatchedUpdates().
* - You're writing UI tests but don't see any errors or warnings related to using act(). You probably don't need this in that case and should use waitForBatchedUpdates().
* - You're writing UI test and do see a warning about using act(), but there's no asynchronous code that needs to run inside act().
*
* @returns {Promise}
*/
// eslint-disable-next-line @lwc/lwc/no-async-await
export default async function waitForBatchedUpdatesWithAct() {
// eslint-disable-next-line @lwc/lwc/no-async-await
await act(async () => {
async function waitForBatchedUpdatesWithAct(): Promise<void> {
await act(async (): Promise<void> => {
await waitForBatchedUpdates();
});
}

export default waitForBatchedUpdatesWithAct;

0 comments on commit 9769d56

Please sign in to comment.