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

264 lines
7.1 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2018-2020 Signal Messenger, LLC
// 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';
2018-04-13 20:27:06 +00:00
import {
groupMediaItemsByDate,
2018-04-13 20:27:06 +00:00
Section,
} from '../../../components/conversation/media-gallery/groupMediaItemsByDate';
2021-08-23 23:14:53 +00:00
import { MediaItemType } from '../../../types/MediaItem';
2018-04-13 00:45:14 +00:00
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-25 21:08:32 +00:00
sent_at: Date.now(),
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
2018-04-13 00:45:14 +00:00
});
describe('groupMediaItemsByDate', () => {
it('should group mediaItems', () => {
2018-04-13 14:53:15 +00:00
const referenceTime = new Date('2018-04-12T18:00Z').getTime(); // Thu
const input: Array<MediaItemType> = shuffle([
2018-04-13 00:45:14 +00:00
// Today
toMediaItem(new Date('2018-04-12T12:00Z')), // Thu
toMediaItem(new Date('2018-04-12T00:01Z')), // Thu
2018-04-13 00:45:14 +00:00
// This week
toMediaItem(new Date('2018-04-11T23:59Z')), // Wed
toMediaItem(new Date('2018-04-09T00:01Z')), // Mon
2018-04-13 00:45:14 +00:00
// This month
toMediaItem(new Date('2018-04-08T23:59Z')), // Sun
toMediaItem(new Date('2018-04-01T00:01Z')),
2018-04-13 00:45:14 +00:00
// March 2018
toMediaItem(new Date('2018-03-31T23:59Z')),
toMediaItem(new Date('2018-03-01T14:00Z')),
2018-04-13 00:45:14 +00:00
// February 2011
toMediaItem(new Date('2011-02-28T23:59Z')),
toMediaItem(new Date('2011-02-01T10:00Z')),
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-25 21:08:32 +00:00
sent_at: Date.now(),
},
attachment: {
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-25 21:08:32 +00:00
sent_at: Date.now(),
},
attachment: {
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-25 21:08:32 +00:00
sent_at: Date.now(),
},
attachment: {
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-25 21:08:32 +00:00
sent_at: Date.now(),
},
attachment: {
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-25 21:08:32 +00:00
sent_at: Date.now(),
},
attachment: {
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-25 21:08:32 +00:00
sent_at: Date.now(),
},
attachment: {
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-25 21:08:32 +00:00
sent_at: Date.now(),
},
attachment: {
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-25 21:08:32 +00:00
sent_at: Date.now(),
},
attachment: {
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-25 21:08:32 +00:00
sent_at: Date.now(),
},
attachment: {
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-25 21:08:32 +00:00
sent_at: Date.now(),
},
attachment: {
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);
});
});