From 505cb287f6bbc90c6e30abe057fdb976e33e6dfc Mon Sep 17 00:00:00 2001 From: VickyStash Date: Wed, 28 Feb 2024 15:33:33 +0100 Subject: [PATCH 1/2] [TS migration] Migrate 'ReportTestUtils.js', 'deepReplaceKeysAndValuesTest.js', 'DistanceRequestUtilsTest.js', 'waitForBatchedUpdatesWithAct.js' and 'ConvertToLTRForComposerTest.js' test --- src/libs/deepReplaceKeysAndValues.ts | 2 + ...Test.js => ConvertToLTRForComposerTest.ts} | 0 ...ilsTest.js => DistanceRequestUtilsTest.ts} | 5 +- ...est.js => deepReplaceKeysAndValuesTest.ts} | 5 +- tests/utils/ReportTestUtils.js | 68 ------------------ tests/utils/ReportTestUtils.ts | 70 +++++++++++++++++++ ...Act.js => waitForBatchedUpdatesWithAct.ts} | 10 ++- 7 files changed, 82 insertions(+), 78 deletions(-) rename tests/unit/{ConvertToLTRForComposerTest.js => ConvertToLTRForComposerTest.ts} (100%) rename tests/unit/{DistanceRequestUtilsTest.js => DistanceRequestUtilsTest.ts} (54%) rename tests/unit/{deepReplaceKeysAndValuesTest.js => deepReplaceKeysAndValuesTest.ts} (91%) delete mode 100644 tests/utils/ReportTestUtils.js create mode 100644 tests/utils/ReportTestUtils.ts rename tests/utils/{waitForBatchedUpdatesWithAct.js => waitForBatchedUpdatesWithAct.ts} (87%) diff --git a/src/libs/deepReplaceKeysAndValues.ts b/src/libs/deepReplaceKeysAndValues.ts index 58a96a58a2c0..7fa7adfaa707 100644 --- a/src/libs/deepReplaceKeysAndValues.ts +++ b/src/libs/deepReplaceKeysAndValues.ts @@ -48,3 +48,5 @@ function deepReplaceKeysAndValues(target: T, oldVal: } export default deepReplaceKeysAndValues; + +export type {ReplaceableValue}; diff --git a/tests/unit/ConvertToLTRForComposerTest.js b/tests/unit/ConvertToLTRForComposerTest.ts similarity index 100% rename from tests/unit/ConvertToLTRForComposerTest.js rename to tests/unit/ConvertToLTRForComposerTest.ts diff --git a/tests/unit/DistanceRequestUtilsTest.js b/tests/unit/DistanceRequestUtilsTest.ts similarity index 54% rename from tests/unit/DistanceRequestUtilsTest.js rename to tests/unit/DistanceRequestUtilsTest.ts index 4b657841f2be..d6db9c824537 100644 --- a/tests/unit/DistanceRequestUtilsTest.js +++ b/tests/unit/DistanceRequestUtilsTest.ts @@ -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); }); }); diff --git a/tests/unit/deepReplaceKeysAndValuesTest.js b/tests/unit/deepReplaceKeysAndValuesTest.ts similarity index 91% rename from tests/unit/deepReplaceKeysAndValuesTest.js rename to tests/unit/deepReplaceKeysAndValuesTest.ts index ecb918026048..246ca6833f19 100644 --- a/tests/unit/deepReplaceKeysAndValuesTest.js +++ b/tests/unit/deepReplaceKeysAndValuesTest.ts @@ -1,4 +1,5 @@ -import deepReplaceKeysAndValues from '../../src/libs/deepReplaceKeysAndValues'; +import deepReplaceKeysAndValues from '@libs/deepReplaceKeysAndValues'; +import type {ReplaceableValue} from '@libs/deepReplaceKeysAndValues'; describe('deepReplaceKeysAndValues', () => { test.each([ @@ -121,7 +122,7 @@ describe('deepReplaceKeysAndValues', () => { someOtherKey: 2, }, ], - ])('deepReplaceKeysAndValues(%s)', (input, expected) => { + ])('deepReplaceKeysAndValues(%s)', (input: ReplaceableValue, expected: ReplaceableValue) => { expect(deepReplaceKeysAndValues(input, 'oldVal', 'newVal')).toStrictEqual(expected); }); }); diff --git a/tests/utils/ReportTestUtils.js b/tests/utils/ReportTestUtils.js deleted file mode 100644 index 86899e4045f6..000000000000 --- a/tests/utils/ReportTestUtils.js +++ /dev/null @@ -1,68 +0,0 @@ -import _ from 'underscore'; -import createRandomReportAction from './collections/reportActions'; - -const actionNames = ['ADDCOMMENT', 'IOU', 'REPORTPREVIEW', 'CLOSED']; - -const getFakeReportAction = (index, actionName) => ({ - 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', - isDelatedParentAction: 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@test.com', - }, - ], - previousReportActionID: '1', - reportActionID: index.toString(), - reportActionTimestamp: 1696243169753, - sequenceNumber: 0, - shouldShow: true, - timestamp: 1696243169, - whisperedToAccountIDs: [], -}); - -const getMockedSortedReportActions = (length = 100) => Array.from({length}, (__, i) => getFakeReportAction(i)); - -const getMockedReportActionsMap = (length = 100) => { - const mockReports = Array.from({length}, (__, i) => { - const reportID = i + 1; - const actionName = i === 0 ? 'CREATED' : actionNames[i % actionNames.length]; - const reportAction = { - ...createRandomReportAction(reportID), - actionName, - originalMessage: { - linkedReportID: reportID.toString(), - }, - }; - - return {[reportID]: reportAction}; - }); - return _.assign({}, ...mockReports); -}; - -export {getFakeReportAction, getMockedSortedReportActions, getMockedReportActionsMap}; diff --git a/tests/utils/ReportTestUtils.ts b/tests/utils/ReportTestUtils.ts new file mode 100644 index 000000000000..59175f76108f --- /dev/null +++ b/tests/utils/ReportTestUtils.ts @@ -0,0 +1,70 @@ +import {ReportAction, ReportActions} from '@src/types/onyx'; +import {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@test.com', + }, + ], + previousReportActionID: '1', + reportActionID: index.toString(), + reportActionTimestamp: 1696243169753, + sequenceNumber: 0, + shouldShow: true, + timestamp: 1696243169, + whisperedToAccountIDs: [], + } as ReportAction); + +const getMockedSortedReportActions = (length = 100): ReportAction[] => Array.from({length}, (__, i): ReportAction => getFakeReportAction(i)); + +const getMockedReportActionsMap = (length = 100): ReportActions => { + const mockReports: ReportActions[] = Array.from({length}, (__, i): ReportActions => { + const reportID = i + 1; + const actionName: ActionName = i === 0 ? 'CREATED' : actionNames[i % actionNames.length]; + const reportAction = { + ...createRandomReportAction(reportID), + actionName, + originalMessage: { + linkedReportID: reportID.toString(), + }, + } as ReportAction; + + return {[reportID]: reportAction}; + }); + return Object.assign({}, ...mockReports); +}; + +export {getFakeReportAction, getMockedSortedReportActions, getMockedReportActionsMap}; diff --git a/tests/utils/waitForBatchedUpdatesWithAct.js b/tests/utils/waitForBatchedUpdatesWithAct.ts similarity index 87% rename from tests/utils/waitForBatchedUpdatesWithAct.js rename to tests/utils/waitForBatchedUpdatesWithAct.ts index 125cf74159b3..64f1670b208d 100644 --- a/tests/utils/waitForBatchedUpdatesWithAct.js +++ b/tests/utils/waitForBatchedUpdatesWithAct.ts @@ -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 { + await act(async (): Promise => { await waitForBatchedUpdates(); }); } + +export default waitForBatchedUpdatesWithAct; From 858de5f5ebcf321a875a225ea4d891e1357b37e2 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Wed, 28 Feb 2024 16:33:58 +0100 Subject: [PATCH 2/2] Fix lint errors --- tests/unit/deepReplaceKeysAndValuesTest.ts | 1 + tests/utils/ReportTestUtils.ts | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/unit/deepReplaceKeysAndValuesTest.ts b/tests/unit/deepReplaceKeysAndValuesTest.ts index 246ca6833f19..46c0d4b7b9e2 100644 --- a/tests/unit/deepReplaceKeysAndValuesTest.ts +++ b/tests/unit/deepReplaceKeysAndValuesTest.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/naming-convention */ import deepReplaceKeysAndValues from '@libs/deepReplaceKeysAndValues'; import type {ReplaceableValue} from '@libs/deepReplaceKeysAndValues'; diff --git a/tests/utils/ReportTestUtils.ts b/tests/utils/ReportTestUtils.ts index 59175f76108f..4a4ce89d0296 100644 --- a/tests/utils/ReportTestUtils.ts +++ b/tests/utils/ReportTestUtils.ts @@ -1,5 +1,5 @@ -import {ReportAction, ReportActions} from '@src/types/onyx'; -import {ActionName} from '@src/types/onyx/OriginalMessage'; +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']; @@ -48,12 +48,12 @@ const getFakeReportAction = (index: number, actionName?: ActionName): ReportActi whisperedToAccountIDs: [], } as ReportAction); -const getMockedSortedReportActions = (length = 100): ReportAction[] => Array.from({length}, (__, i): ReportAction => getFakeReportAction(i)); +const getMockedSortedReportActions = (length = 100): ReportAction[] => Array.from({length}, (element, index): ReportAction => getFakeReportAction(index)); const getMockedReportActionsMap = (length = 100): ReportActions => { - const mockReports: ReportActions[] = Array.from({length}, (__, i): ReportActions => { - const reportID = i + 1; - const actionName: ActionName = i === 0 ? 'CREATED' : actionNames[i % actionNames.length]; + 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, @@ -64,7 +64,7 @@ const getMockedReportActionsMap = (length = 100): ReportActions => { return {[reportID]: reportAction}; }); - return Object.assign({}, ...mockReports); + return Object.assign({}, ...mockReports) as ReportActions; }; export {getFakeReportAction, getMockedSortedReportActions, getMockedReportActionsMap};