Timeline: repair oldest/newest metrics if we fetch nothing

This commit is contained in:
Scott Nonnenberg 2020-12-04 12:41:40 -08:00 committed by GitHub
parent 56ae4a41eb
commit 6832b8acca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 579 additions and 173 deletions

View file

@ -0,0 +1,208 @@
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import React from 'react';
import { assert } from 'chai';
import { LeftPane, RowType, HeaderType } from '../../components/LeftPane';
import { setup as setupI18n } from '../../../js/modules/i18n';
import enMessages from '../../../_locales/en/messages.json';
const i18n = setupI18n('en', enMessages);
describe('LeftPane', () => {
const defaultProps = {
archivedConversations: [],
conversations: [],
i18n,
openConversationInternal: () => null,
pinnedConversations: [],
renderExpiredBuildDialog: () => <div />,
renderMainHeader: () => <div />,
renderMessageSearchResult: () => <div />,
renderNetworkStatus: () => <div />,
renderRelinkDialog: () => <div />,
renderUpdateDialog: () => <div />,
showArchivedConversations: () => null,
showInbox: () => null,
startNewConversation: () => null,
};
describe('getRowFromIndex', () => {
describe('given only pinned chats', () => {
it('returns pinned chats, not headers', () => {
const leftPane = new LeftPane({
...defaultProps,
pinnedConversations: [
{
id: 'philly-convo',
isPinned: true,
isSelected: false,
lastUpdated: Date.now(),
markedUnread: false,
title: 'Philip Glass',
type: 'direct',
},
{
id: 'robbo-convo',
isPinned: true,
isSelected: false,
lastUpdated: Date.now(),
markedUnread: false,
title: 'Robert Moog',
type: 'direct',
},
],
});
assert.deepEqual(leftPane.getRowFromIndex(0), {
index: 0,
type: RowType.PinnedConversation,
});
assert.deepEqual(leftPane.getRowFromIndex(1), {
index: 1,
type: RowType.PinnedConversation,
});
});
});
describe('given only non-pinned chats', () => {
it('returns conversations, not headers', () => {
const leftPane = new LeftPane({
...defaultProps,
conversations: [
{
id: 'fred-convo',
isSelected: false,
lastUpdated: Date.now(),
markedUnread: false,
title: 'Fred Willard',
type: 'direct',
},
{
id: 'robbo-convo',
isPinned: false,
isSelected: false,
lastUpdated: Date.now(),
markedUnread: false,
title: 'Robert Moog',
type: 'direct',
},
],
});
assert.deepEqual(leftPane.getRowFromIndex(0), {
index: 0,
type: RowType.Conversation,
});
assert.deepEqual(leftPane.getRowFromIndex(1), {
index: 1,
type: RowType.Conversation,
});
});
});
describe('given only pinned and non-pinned chats', () => {
it('returns headers and conversations', () => {
const leftPane = new LeftPane({
...defaultProps,
conversations: [
{
id: 'fred-convo',
isSelected: false,
lastUpdated: Date.now(),
markedUnread: false,
title: 'Fred Willard',
type: 'direct',
},
],
pinnedConversations: [
{
id: 'philly-convo',
isPinned: true,
isSelected: false,
lastUpdated: Date.now(),
markedUnread: false,
title: 'Philip Glass',
type: 'direct',
},
],
});
assert.deepEqual(leftPane.getRowFromIndex(0), {
headerType: HeaderType.Pinned,
type: RowType.Header,
});
assert.deepEqual(leftPane.getRowFromIndex(1), {
index: 0,
type: RowType.PinnedConversation,
});
assert.deepEqual(leftPane.getRowFromIndex(2), {
headerType: HeaderType.Chats,
type: RowType.Header,
});
assert.deepEqual(leftPane.getRowFromIndex(3), {
index: 0,
type: RowType.Conversation,
});
});
});
describe('given not showing archive with archived conversation', () => {
it('returns an archive button last', () => {
const leftPane = new LeftPane({
...defaultProps,
archivedConversations: [
{
id: 'jerry-convo',
isSelected: false,
lastUpdated: Date.now(),
markedUnread: false,
title: 'Jerry Jordan',
type: 'direct',
},
],
conversations: [
{
id: 'fred-convo',
isSelected: false,
lastUpdated: Date.now(),
markedUnread: false,
title: 'Fred Willard',
type: 'direct',
},
],
showArchived: false,
});
assert.deepEqual(leftPane.getRowFromIndex(1), {
type: RowType.ArchiveButton,
});
});
});
describe('given showing archive and archive chats', () => {
it('returns archived conversations', () => {
const leftPane = new LeftPane({
...defaultProps,
archivedConversations: [
{
id: 'fred-convo',
isSelected: false,
lastUpdated: Date.now(),
markedUnread: false,
title: 'Fred Willard',
type: 'direct',
},
],
showArchived: true,
});
assert.deepEqual(leftPane.getRowFromIndex(0), {
index: 0,
type: RowType.ArchivedConversation,
});
});
});
});
});

View file

@ -0,0 +1,230 @@
// Copyright 2018-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { shuffle } from 'lodash';
import { IMAGE_JPEG } from '../../../types/MIME';
import {
groupMediaItemsByDate,
Section,
} from '../../../components/conversation/media-gallery/groupMediaItemsByDate';
import { MediaItemType } from '../../../components/LightboxGallery';
const toMediaItem = (date: Date): MediaItemType => ({
objectURL: date.toUTCString(),
index: 0,
message: {
id: 'id',
received_at: date.getTime(),
attachments: [],
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
});
describe('groupMediaItemsByDate', () => {
it('should group mediaItems', () => {
const referenceTime = new Date('2018-04-12T18:00Z').getTime(); // Thu
const input: Array<MediaItemType> = shuffle([
// Today
toMediaItem(new Date('2018-04-12T12:00Z')), // Thu
toMediaItem(new Date('2018-04-12T00:01Z')), // Thu
// This week
toMediaItem(new Date('2018-04-11T23:59Z')), // Wed
toMediaItem(new Date('2018-04-09T00:01Z')), // Mon
// This month
toMediaItem(new Date('2018-04-08T23:59Z')), // Sun
toMediaItem(new Date('2018-04-01T00:01Z')),
// March 2018
toMediaItem(new Date('2018-03-31T23:59Z')),
toMediaItem(new Date('2018-03-01T14:00Z')),
// February 2011
toMediaItem(new Date('2011-02-28T23:59Z')),
toMediaItem(new Date('2011-02-01T10:00Z')),
]);
const expected: Array<Section> = [
{
type: 'today',
mediaItems: [
{
objectURL: 'Thu, 12 Apr 2018 12:00:00 GMT',
index: 0,
message: {
id: 'id',
received_at: 1523534400000,
attachments: [],
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
},
{
objectURL: 'Thu, 12 Apr 2018 00:01:00 GMT',
index: 0,
message: {
id: 'id',
received_at: 1523491260000,
attachments: [],
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
},
],
},
{
type: 'yesterday',
mediaItems: [
{
objectURL: 'Wed, 11 Apr 2018 23:59:00 GMT',
index: 0,
message: {
id: 'id',
received_at: 1523491140000,
attachments: [],
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
},
],
},
{
type: 'thisWeek',
mediaItems: [
{
objectURL: 'Mon, 09 Apr 2018 00:01:00 GMT',
index: 0,
message: {
id: 'id',
received_at: 1523232060000,
attachments: [],
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
},
],
},
{
type: 'thisMonth',
mediaItems: [
{
objectURL: 'Sun, 08 Apr 2018 23:59:00 GMT',
index: 0,
message: {
id: 'id',
received_at: 1523231940000,
attachments: [],
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
},
{
objectURL: 'Sun, 01 Apr 2018 00:01:00 GMT',
index: 0,
message: {
id: 'id',
received_at: 1522540860000,
attachments: [],
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
},
],
},
{
type: 'yearMonth',
year: 2018,
month: 2,
mediaItems: [
{
objectURL: 'Sat, 31 Mar 2018 23:59:00 GMT',
index: 0,
message: {
id: 'id',
received_at: 1522540740000,
attachments: [],
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
},
{
objectURL: 'Thu, 01 Mar 2018 14:00:00 GMT',
index: 0,
message: {
id: 'id',
received_at: 1519912800000,
attachments: [],
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
},
],
},
{
type: 'yearMonth',
year: 2011,
month: 1,
mediaItems: [
{
objectURL: 'Mon, 28 Feb 2011 23:59:00 GMT',
index: 0,
message: {
id: 'id',
received_at: 1298937540000,
attachments: [],
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
},
{
objectURL: 'Tue, 01 Feb 2011 10:00:00 GMT',
index: 0,
message: {
id: 'id',
received_at: 1296554400000,
attachments: [],
},
attachment: {
fileName: 'fileName',
contentType: IMAGE_JPEG,
url: 'url',
},
},
],
},
];
const actual = groupMediaItemsByDate(referenceTime, input);
assert.deepEqual(actual, expected);
});
});