signal-desktop/ts/test-node/components/media-gallery/groupMessagesByDate_test.ts

272 lines
7.6 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2018 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2018-04-13 00:45:14 +00:00
import { assert } from 'chai';
2018-04-13 20:27:06 +00:00
import { shuffle } from 'lodash';
2018-04-13 00:45:14 +00:00
import { IMAGE_JPEG } from '../../../types/MIME';
import type { Section } from '../../../components/conversation/media-gallery/groupMediaItemsByDate';
import { groupMediaItemsByDate } from '../../../components/conversation/media-gallery/groupMediaItemsByDate';
import type { MediaItemType } from '../../../types/MediaItem';
import { fakeAttachment } from '../../../test-both/helpers/fakeAttachment';
2018-04-13 00:45:14 +00:00
const testDate = (
year: number,
month: number,
day: number,
hour: number,
minute: number,
second = 0
): Date => new Date(Date.UTC(year, month - 1, day, hour, minute, second, 0));
const toMediaItem = (date: Date): MediaItemType => ({
objectURL: date.toUTCString(),
index: 0,
message: {
2021-08-23 23:14:53 +00:00
conversationId: '1234',
id: 'id',
received_at: date.getTime(),
2021-03-04 21:44:57 +00:00
received_at_ms: date.getTime(),
attachments: [],
2021-08-26 20:42:25 +00:00
sent_at: date.getTime(),
},
attachment: fakeAttachment({
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
}),
2018-04-13 00:45:14 +00:00
});
describe('groupMediaItemsByDate', () => {
it('should group mediaItems', () => {
const referenceTime = testDate(2018, 4, 12, 18, 0, 0).getTime(); // Thu
const input: Array<MediaItemType> = shuffle([
2018-04-13 00:45:14 +00:00
// Today
toMediaItem(testDate(2018, 4, 12, 12, 0)), // Thu
toMediaItem(testDate(2018, 4, 12, 0, 1)), // Thu
2018-04-13 00:45:14 +00:00
// This week
toMediaItem(testDate(2018, 4, 11, 23, 59)), // Wed
toMediaItem(testDate(2018, 4, 9, 0, 1)), // Mon
2018-04-13 00:45:14 +00:00
// This month
toMediaItem(testDate(2018, 4, 8, 23, 59)), // Sun
toMediaItem(testDate(2018, 4, 1, 0, 1)),
2018-04-13 00:45:14 +00:00
// March 2018
toMediaItem(testDate(2018, 3, 31, 23, 59)),
toMediaItem(testDate(2018, 3, 1, 14, 0)),
2018-04-13 00:45:14 +00:00
// February 2011
toMediaItem(testDate(2011, 2, 28, 23, 59)),
toMediaItem(testDate(2011, 2, 1, 10, 0)),
2018-04-13 20:27:06 +00:00
]);
2018-04-13 00:45:14 +00:00
2018-04-13 20:27:06 +00:00
const expected: Array<Section> = [
{
type: 'today',
mediaItems: [
2018-04-13 20:27:06 +00:00
{
objectURL: 'Thu, 12 Apr 2018 12:00:00 GMT',
index: 0,
message: {
2021-08-23 23:14:53 +00:00
conversationId: '1234',
id: 'id',
received_at: 1523534400000,
2021-03-04 21:44:57 +00:00
received_at_ms: 1523534400000,
attachments: [],
2021-08-26 20:42:25 +00:00
sent_at: 1523534400000,
},
attachment: fakeAttachment({
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
}),
2018-04-13 00:45:14 +00:00
},
2018-04-13 20:27:06 +00:00
{
objectURL: 'Thu, 12 Apr 2018 00:01:00 GMT',
index: 0,
message: {
2021-08-23 23:14:53 +00:00
conversationId: '1234',
id: 'id',
received_at: 1523491260000,
2021-03-04 21:44:57 +00:00
received_at_ms: 1523491260000,
attachments: [],
2021-08-26 20:42:25 +00:00
sent_at: 1523491260000,
},
attachment: fakeAttachment({
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
}),
2018-04-13 00:45:14 +00:00
},
2018-04-13 20:27:06 +00:00
],
},
{
type: 'yesterday',
mediaItems: [
2018-04-13 20:27:06 +00:00
{
objectURL: 'Wed, 11 Apr 2018 23:59:00 GMT',
index: 0,
message: {
2021-08-23 23:14:53 +00:00
conversationId: '1234',
id: 'id',
received_at: 1523491140000,
2021-03-04 21:44:57 +00:00
received_at_ms: 1523491140000,
attachments: [],
2021-08-26 20:42:25 +00:00
sent_at: 1523491140000,
},
attachment: fakeAttachment({
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
}),
2018-04-13 00:45:14 +00:00
},
2018-04-13 20:27:06 +00:00
],
},
{
type: 'thisWeek',
mediaItems: [
2018-04-13 20:27:06 +00:00
{
objectURL: 'Mon, 09 Apr 2018 00:01:00 GMT',
index: 0,
message: {
2021-08-23 23:14:53 +00:00
conversationId: '1234',
id: 'id',
received_at: 1523232060000,
2021-03-04 21:44:57 +00:00
received_at_ms: 1523232060000,
attachments: [],
2021-08-26 20:42:25 +00:00
sent_at: 1523232060000,
},
attachment: fakeAttachment({
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
}),
2018-04-13 00:45:14 +00:00
},
2018-04-13 20:27:06 +00:00
],
},
{
type: 'thisMonth',
mediaItems: [
2018-04-13 20:27:06 +00:00
{
objectURL: 'Sun, 08 Apr 2018 23:59:00 GMT',
index: 0,
message: {
2021-08-23 23:14:53 +00:00
conversationId: '1234',
id: 'id',
received_at: 1523231940000,
2021-03-04 21:44:57 +00:00
received_at_ms: 1523231940000,
attachments: [],
2021-08-26 20:42:25 +00:00
sent_at: 1523231940000,
},
attachment: fakeAttachment({
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
}),
2018-04-13 00:45:14 +00:00
},
2018-04-13 20:27:06 +00:00
{
objectURL: 'Sun, 01 Apr 2018 00:01:00 GMT',
index: 0,
message: {
2021-08-23 23:14:53 +00:00
conversationId: '1234',
id: 'id',
received_at: 1522540860000,
2021-03-04 21:44:57 +00:00
received_at_ms: 1522540860000,
attachments: [],
2021-08-26 20:42:25 +00:00
sent_at: 1522540860000,
},
attachment: fakeAttachment({
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
}),
2018-04-13 00:45:14 +00:00
},
2018-04-13 20:27:06 +00:00
],
},
{
type: 'yearMonth',
year: 2018,
month: 2,
mediaItems: [
2018-04-13 20:27:06 +00:00
{
objectURL: 'Sat, 31 Mar 2018 23:59:00 GMT',
index: 0,
message: {
2021-08-23 23:14:53 +00:00
conversationId: '1234',
id: 'id',
received_at: 1522540740000,
2021-03-04 21:44:57 +00:00
received_at_ms: 1522540740000,
attachments: [],
2021-08-26 20:42:25 +00:00
sent_at: 1522540740000,
},
attachment: fakeAttachment({
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
}),
2018-04-13 00:45:14 +00:00
},
2018-04-13 20:27:06 +00:00
{
objectURL: 'Thu, 01 Mar 2018 14:00:00 GMT',
index: 0,
message: {
2021-08-23 23:14:53 +00:00
conversationId: '1234',
id: 'id',
received_at: 1519912800000,
2021-03-04 21:44:57 +00:00
received_at_ms: 1519912800000,
attachments: [],
2021-08-26 20:42:25 +00:00
sent_at: 1519912800000,
},
attachment: fakeAttachment({
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
}),
2018-04-13 00:45:14 +00:00
},
2018-04-13 20:27:06 +00:00
],
},
{
type: 'yearMonth',
year: 2011,
month: 1,
mediaItems: [
2018-04-13 20:27:06 +00:00
{
objectURL: 'Mon, 28 Feb 2011 23:59:00 GMT',
index: 0,
message: {
2021-08-23 23:14:53 +00:00
conversationId: '1234',
id: 'id',
received_at: 1298937540000,
2021-03-04 21:44:57 +00:00
received_at_ms: 1298937540000,
attachments: [],
2021-08-26 20:42:25 +00:00
sent_at: 1298937540000,
},
attachment: fakeAttachment({
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
}),
2018-04-13 00:45:14 +00:00
},
2018-04-13 20:27:06 +00:00
{
objectURL: 'Tue, 01 Feb 2011 10:00:00 GMT',
index: 0,
message: {
2021-08-23 23:14:53 +00:00
conversationId: '1234',
id: 'id',
received_at: 1296554400000,
2021-03-04 21:44:57 +00:00
received_at_ms: 1296554400000,
attachments: [],
2021-08-26 20:42:25 +00:00
sent_at: 1296554400000,
},
attachment: fakeAttachment({
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
}),
2018-04-13 00:45:14 +00:00
},
2018-04-13 20:27:06 +00:00
],
},
];
2018-04-13 00:45:14 +00:00
const actual = groupMediaItemsByDate(referenceTime, input);
2018-04-13 00:45:14 +00:00
assert.deepEqual(actual, expected);
});
});