Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[No QA] Migrate 'OptionsListUtilsTest.js', 'DateUtilsTest.js', 'SidebarLinks.perf-test.js', 'markdown.js' and 'ReportUtilsTest.js' to Typescript #39206

2 changes: 1 addition & 1 deletion src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ function formatToLongDateWithWeekday(datetime: string | Date): string {
* @returns Sunday
*/
function formatToDayOfWeek(datetime: Date): string {
return format(new Date(datetime), CONST.DATE.WEEKDAY_TIME_FORMAT);
return format(datetime, CONST.DATE.WEEKDAY_TIME_FORMAT);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/libs/OptionsListUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2141,4 +2141,4 @@ export {
getTaxRatesSection,
};

export type {MemberForList, CategorySection, GetOptions, OptionList, SearchOption, PayeePersonalDetails, Category, TaxRatesOption};
export type {MemberForList, CategorySection, CategoryTreeSection, GetOptions, OptionList, SearchOption, PayeePersonalDetails, Category, TaxRatesOption};
6 changes: 5 additions & 1 deletion src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type {
ReportMetadata,
Session,
Task,
TaxRate,
Transaction,
TransactionViolation,
UserWallet,
Expand Down Expand Up @@ -416,6 +417,9 @@ type OptionData = {
isDisabled?: boolean | null;
name?: string | null;
isSelfDM?: boolean | null;
reportID?: string;
enabled?: boolean;
data?: Partial<TaxRate>;
} & Report;

type OnyxDataTaskAssigneeChat = {
Expand Down Expand Up @@ -991,7 +995,7 @@ function filterReportsByPolicyIDAndMemberAccountIDs(reports: Report[], policyMem
/**
* Given an array of reports, return them sorted by the last read timestamp.
*/
function sortReportsByLastRead(reports: Report[], reportMetadata: OnyxCollection<ReportMetadata>): Array<OnyxEntry<Report>> {
function sortReportsByLastRead(reports: Array<OnyxEntry<Report>>, reportMetadata: OnyxCollection<ReportMetadata>): Array<OnyxEntry<Report>> {
return reports
.filter((report) => !!report?.reportID && !!(reportMetadata?.[`${ONYXKEYS.COLLECTION.REPORT_METADATA}${report.reportID}`]?.lastVisitTime ?? report?.lastReadTime))
.sort((a, b) => {
Expand Down
4 changes: 3 additions & 1 deletion tests/e2e/compare/output/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ type Entry = {
type Data = {
significance: Entry[];
meaningless: Entry[];
errors?: string[];
warnings?: string[];
};

const printRegularLine = (entry: Entry) => {
Expand All @@ -36,4 +38,4 @@ export default (data: Data) => {
console.debug('');
};

export type {Entry};
export type {Data, Entry};
Original file line number Diff line number Diff line change
@@ -1,80 +1,85 @@
// From: https://raw.githubusercontent.com/callstack/reassure/main/packages/reassure-compare/src/output/markdown.ts
import fs from 'node:fs/promises';
import path from 'path';
import _ from 'underscore';
import type {Stats} from 'tests/e2e/measure/math';
import * as Logger from '../../utils/logger';
import type {Data, Entry} from './console';
import * as format from './format';
import markdownTable from './markdownTable';

const tableHeader = ['Name', 'Duration'];

const collapsibleSection = (title, content) => `<details>\n<summary>${title}</summary>\n\n${content}\n</details>\n\n`;
const collapsibleSection = (title: string, content: string) => `<details>\n<summary>${title}</summary>\n\n${content}\n</details>\n\n`;

const buildDurationDetails = (title, entry) => {
const buildDurationDetails = (title: string, entry: Stats) => {
const relativeStdev = entry.stdev / entry.mean;

return _.filter(
[
`**${title}**`,
`Mean: ${format.formatDuration(entry.mean)}`,
`Stdev: ${format.formatDuration(entry.stdev)} (${format.formatPercent(relativeStdev)})`,
entry.entries ? `Runs: ${entry.entries.join(' ')}` : '',
],
Boolean,
).join('<br/>');
return [
`**${title}**`,
`Mean: ${format.formatDuration(entry.mean)}`,
`Stdev: ${format.formatDuration(entry.stdev)} (${format.formatPercent(relativeStdev)})`,
entry.entries ? `Runs: ${entry.entries.join(' ')}` : '',
]
.filter(Boolean)
.join('<br/>');
};

const buildDurationDetailsEntry = (entry) =>
_.filter(['baseline' in entry ? buildDurationDetails('Baseline', entry.baseline) : '', 'current' in entry ? buildDurationDetails('Current', entry.current) : ''], Boolean).join(
'<br/><br/>',
);
const buildDurationDetailsEntry = (entry: Entry) =>
['baseline' in entry ? buildDurationDetails('Baseline', entry.baseline) : '', 'current' in entry ? buildDurationDetails('Current', entry.current) : '']
.filter(Boolean)
.join('<br/><br/>');

const formatEntryDuration = (entry: Entry): string => {
let formattedDuration = '';

const formatEntryDuration = (entry) => {
if ('baseline' in entry && 'current' in entry) {
return format.formatDurationDiffChange(entry);
formattedDuration = format.formatDurationDiffChange(entry);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a bit more verbose, wondering why we needed this vs what we had before, did TS complain?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@youssef-lr Yeah, TS complained before!

}

if ('baseline' in entry) {
return format.formatDuration(entry.baseline.mean);
formattedDuration = format.formatDuration(entry.baseline.mean);
}

if ('current' in entry) {
return format.formatDuration(entry.current.mean);
formattedDuration = format.formatDuration(entry.current.mean);
}
return '';

return formattedDuration;
};

const buildDetailsTable = (entries) => {
const buildDetailsTable = (entries: Entry[]) => {
if (!entries.length) {
return '';
}

const rows = _.map(entries, (entry) => [entry.name, buildDurationDetailsEntry(entry)]);
const rows = entries.map((entry) => [entry.name, buildDurationDetailsEntry(entry)]);
const content = markdownTable([tableHeader, ...rows]);

return collapsibleSection('Show details', content);
};

const buildSummaryTable = (entries, collapse = false) => {
const buildSummaryTable = (entries: Entry[], collapse = false) => {
if (!entries.length) {
return '_There are no entries_';
}

const rows = _.map(entries, (entry) => [entry.name, formatEntryDuration(entry)]);
const rows = entries.map((entry) => [entry.name, formatEntryDuration(entry)]);
const content = markdownTable([tableHeader, ...rows]);

return collapse ? collapsibleSection('Show entries', content) : content;
};

const buildMarkdown = (data) => {
const buildMarkdown = (data: Data) => {
let result = '## Performance Comparison Report 📊';

if (data.errors && data.errors.length) {
if (data.errors?.length) {
result += '\n\n### Errors\n';
data.errors.forEach((message) => {
result += ` 1. 🛑 ${message}\n`;
});
}

if (data.warnings && data.warnings.length) {
if (data.warnings?.length) {
result += '\n\n### Warnings\n';
data.warnings.forEach((message) => {
result += ` 1. 🟡 ${message}\n`;
Expand All @@ -92,7 +97,7 @@ const buildMarkdown = (data) => {
return result;
};

const writeToFile = (filePath, content) =>
const writeToFile = (filePath: string, content: string) =>
fs
.writeFile(filePath, content)
.then(() => {
Expand All @@ -106,7 +111,7 @@ const writeToFile = (filePath, content) =>
throw error;
});

const writeToMarkdown = (filePath, data) => {
const writeToMarkdown = (filePath: string, data: Data) => {
const markdown = buildMarkdown(data);
return writeToFile(filePath, markdown).catch((error) => {
console.error(error);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
import {fireEvent, screen} from '@testing-library/react-native';
import Onyx from 'react-native-onyx';
import {measurePerformance} from 'reassure';
import _ from 'underscore';
import CONST from '../../src/CONST';
import ONYXKEYS from '../../src/ONYXKEYS';
import variables from '../../src/styles/variables';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import * as LHNTestUtils from '../utils/LHNTestUtils';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates';

jest.mock('../../src/libs/Permissions');
jest.mock('../../src/hooks/usePermissions.ts');
jest.mock('../../src/libs/Navigation/Navigation');
jest.mock('../../src/components/Icon/Expensicons');
jest.mock('@libs/Permissions');
jest.mock('@hooks/usePermissions.ts');
jest.mock('@libs/Navigation/Navigation');
jest.mock('@components/Icon/Expensicons');

jest.mock('@react-navigation/native');

const getMockedReportsMap = (length = 100) => {
const mockReports = Array.from({length}, (__, i) => {
const reportID = i + 1;
const participants = [1, 2];
const reportKey = `${ONYXKEYS.COLLECTION.REPORT}${reportID}`;
const report = LHNTestUtils.getFakeReport(participants, 1, true);

return {[reportKey]: report};
});

return _.assign({}, ...mockReports);
const mockReports = Object.fromEntries(
Array.from({length}, (value, index) => {
const reportID = index + 1;
const participants = [1, 2];
const reportKey = `${ONYXKEYS.COLLECTION.REPORT}${reportID}`;
const report = LHNTestUtils.getFakeReport(participants, 1, true);

return [reportKey, report];
}),
);

return mockReports;
};

const mockedResponseMap = getMockedReportsMap(500);
Expand All @@ -36,11 +37,9 @@ describe('SidebarLinks', () => {
Onyx.init({
keys: ONYXKEYS,
safeEvictionKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS],
registerStorageEventListener: () => {},
});

Onyx.multiSet({
[ONYXKEYS.NVP_PRIORITY_MODE]: CONST.PRIORITY_MODE.DEFAULT,
[ONYXKEYS.PERSONAL_DETAILS_LIST]: LHNTestUtils.fakePersonalDetails,
[ONYXKEYS.BETAS]: [CONST.BETAS.DEFAULT_ROOMS],
[ONYXKEYS.NVP_PRIORITY_MODE]: CONST.PRIORITY_MODE.GSD,
Expand Down
Loading
Loading