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
5 changes: 5 additions & 0 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ function getSortedReportActions(reportActions: ReportAction[] | null, shouldSort
return (first.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED ? -1 : 1) * invertedMultiplier;
}

// Ensure that neither first's nor second's created property is undefined
if (first.created === undefined || second.created === undefined) {
return (first.created === undefined ? -1 : 1) * invertedMultiplier;
}

// Then sort by timestamp
if (first.created !== second.created) {
return (first.created < second.created ? -1 : 1) * invertedMultiplier;
Expand Down
99 changes: 97 additions & 2 deletions tests/unit/ReportActionsUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,110 @@ describe('ReportActionsUtils', () => {
},
],
],
[
[
{
created: '2022-11-09 22:27:01.825',
reportActionID: '8401445780099176',
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
},
{
created: '2022-11-09 22:27:01.600',
reportActionID: '6401435781022176',
actionName: CONST.REPORT.ACTIONS.TYPE.CREATED,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
},
// this item has no created field, so it should appear right after CONST.REPORT.ACTIONS.TYPE.CREATED
{
reportActionID: '2962390724708756',
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
},
{
created: '2022-11-09 22:26:48.889',
reportActionID: '1609646094152486',
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
},
{
created: '2022-11-09 22:26:48.989',
reportActionID: '1661970171066218',
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
},
],
[
{
created: '2022-11-09 22:27:01.600',
reportActionID: '6401435781022176',
actionName: CONST.REPORT.ACTIONS.TYPE.CREATED,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
},
{
reportActionID: '2962390724708756',
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
},
{
created: '2022-11-09 22:26:48.889',
reportActionID: '1609646094152486',
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
},
{
created: '2022-11-09 22:26:48.989',
reportActionID: '1661970171066218',
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
},
{
created: '2022-11-09 22:27:01.825',
reportActionID: '8401445780099176',
actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT,
originalMessage: {
html: 'Hello world',
whisperedTo: [],
},
},
],
],
];

test.each(cases)('sorts by created, then actionName, then reportActionID', (input, expectedOutput) => {
const result = ReportActionsUtils.getSortedReportActions(input);
const result = ReportActionsUtils.getSortedReportActions(input as ReportAction[]);
expect(result).toStrictEqual(expectedOutput);
});

test.each(cases)('in descending order', (input, expectedOutput) => {
const result = ReportActionsUtils.getSortedReportActions(input, true);
const result = ReportActionsUtils.getSortedReportActions(input as ReportAction[], true);
expect(result).toStrictEqual(expectedOutput.reverse());
});
});
Expand Down