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

128 lines
3.3 KiB
TypeScript
Raw Normal View History

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
2018-04-13 20:27:06 +00:00
import {
groupMessagesByDate,
Section,
} from '../../../components/conversation/media-gallery/groupMessagesByDate';
import { Message } from '../../../components/conversation/media-gallery/types/Message';
2018-04-13 00:45:14 +00:00
const toMessage = (date: Date): Message => ({
2018-04-24 16:23:03 +00:00
id: date.toUTCString(),
2018-04-13 00:45:14 +00:00
received_at: date.getTime(),
attachments: [],
});
describe('groupMessagesByDate', () => {
it('should group messages', () => {
2018-04-13 14:53:15 +00:00
const referenceTime = new Date('2018-04-12T18:00Z').getTime(); // Thu
2018-04-13 20:27:06 +00:00
const input: Array<Message> = shuffle([
2018-04-13 00:45:14 +00:00
// Today
2018-04-13 14:53:15 +00:00
toMessage(new Date('2018-04-12T12:00Z')), // Thu
toMessage(new Date('2018-04-12T00:01Z')), // Thu
2018-04-13 00:45:14 +00:00
// This week
2018-04-13 14:53:15 +00:00
toMessage(new Date('2018-04-11T23:59Z')), // Wed
toMessage(new Date('2018-04-09T00:01Z')), // Mon
2018-04-13 00:45:14 +00:00
// This month
2018-04-13 14:53:15 +00:00
toMessage(new Date('2018-04-08T23:59Z')), // Sun
2018-04-13 00:45:14 +00:00
toMessage(new Date('2018-04-01T00:01Z')),
// March 2018
toMessage(new Date('2018-03-31T23:59Z')),
toMessage(new Date('2018-03-01T14:00Z')),
// February 2011
toMessage(new Date('2011-02-28T23:59Z')),
toMessage(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',
messages: [
{
2018-04-24 16:23:03 +00:00
id: 'Thu, 12 Apr 2018 12:00:00 GMT',
2018-04-13 00:45:14 +00:00
received_at: 1523534400000,
attachments: [],
},
2018-04-13 20:27:06 +00:00
{
2018-04-24 16:23:03 +00:00
id: 'Thu, 12 Apr 2018 00:01:00 GMT',
2018-04-13 00:45:14 +00:00
received_at: 1523491260000,
attachments: [],
},
2018-04-13 20:27:06 +00:00
],
},
{
type: 'yesterday',
messages: [
{
2018-04-24 16:23:03 +00:00
id: 'Wed, 11 Apr 2018 23:59:00 GMT',
2018-04-13 00:45:14 +00:00
received_at: 1523491140000,
attachments: [],
},
2018-04-13 20:27:06 +00:00
],
},
{
type: 'thisWeek',
messages: [
{
2018-04-24 16:23:03 +00:00
id: 'Mon, 09 Apr 2018 00:01:00 GMT',
2018-04-13 00:45:14 +00:00
received_at: 1523232060000,
attachments: [],
},
2018-04-13 20:27:06 +00:00
],
},
{
type: 'thisMonth',
messages: [
{
2018-04-24 16:23:03 +00:00
id: 'Sun, 08 Apr 2018 23:59:00 GMT',
2018-04-13 14:53:15 +00:00
received_at: 1523231940000,
2018-04-13 00:45:14 +00:00
attachments: [],
},
2018-04-13 20:27:06 +00:00
{
2018-04-24 16:23:03 +00:00
id: 'Sun, 01 Apr 2018 00:01:00 GMT',
2018-04-13 00:45:14 +00:00
received_at: 1522540860000,
attachments: [],
},
2018-04-13 20:27:06 +00:00
],
},
{
type: 'yearMonth',
year: 2018,
month: 2,
messages: [
{
2018-04-24 16:23:03 +00:00
id: 'Sat, 31 Mar 2018 23:59:00 GMT',
2018-04-13 00:45:14 +00:00
received_at: 1522540740000,
attachments: [],
},
2018-04-13 20:27:06 +00:00
{
2018-04-24 16:23:03 +00:00
id: 'Thu, 01 Mar 2018 14:00:00 GMT',
2018-04-13 00:45:14 +00:00
received_at: 1519912800000,
attachments: [],
},
2018-04-13 20:27:06 +00:00
],
},
{
type: 'yearMonth',
year: 2011,
month: 1,
messages: [
{
2018-04-24 16:23:03 +00:00
id: 'Mon, 28 Feb 2011 23:59:00 GMT',
2018-04-13 00:45:14 +00:00
received_at: 1298937540000,
attachments: [],
},
2018-04-13 20:27:06 +00:00
{
2018-04-24 16:23:03 +00:00
id: 'Tue, 01 Feb 2011 10:00:00 GMT',
2018-04-13 00:45:14 +00:00
received_at: 1296554400000,
attachments: [],
},
2018-04-13 20:27:06 +00:00
],
},
];
2018-04-13 00:45:14 +00:00
const actual = groupMessagesByDate(referenceTime, input);
assert.deepEqual(actual, expected);
});
});