signal-desktop/ts/test-both/state/selectors/conversations_test.ts

513 lines
13 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2019-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2019-01-14 21:49:58 +00:00
import { assert } from 'chai';
import {
ConversationLookupType,
ConversationType,
getEmptyState,
} from '../../../state/ducks/conversations';
2019-01-14 21:49:58 +00:00
import {
_getConversationComparator,
2019-03-12 00:20:16 +00:00
_getLeftPaneLists,
getConversationSelector,
getPlaceholderContact,
getSelectedConversation,
getSelectedConversationId,
2019-01-14 21:49:58 +00:00
} from '../../../state/selectors/conversations';
import { noopAction } from '../../../state/ducks/noop';
import { StateType, reducer as rootReducer } from '../../../state/reducer';
2019-01-14 21:49:58 +00:00
describe('both/state/selectors/conversations', () => {
const getEmptyRootState = (): StateType => {
return rootReducer(undefined, noopAction());
};
function getDefaultConversation(id: string): ConversationType {
return {
id,
type: 'direct',
title: `${id} title`,
};
}
describe('#getConversationSelector', () => {
it('returns empty placeholder if falsey id provided', () => {
const state = getEmptyRootState();
const selector = getConversationSelector(state);
const actual = selector(undefined);
assert.deepEqual(actual, getPlaceholderContact());
});
it('returns empty placeholder if no match', () => {
const state = {
...getEmptyRootState(),
};
const selector = getConversationSelector(state);
const actual = selector('random-id');
assert.deepEqual(actual, getPlaceholderContact());
});
it('returns conversation by e164 first', () => {
const id = 'id';
const conversation = getDefaultConversation(id);
const wrongConversation = getDefaultConversation('wrong');
const state = {
...getEmptyRootState(),
conversations: {
...getEmptyState(),
conversationLookup: {
[id]: wrongConversation,
},
conversationsByE164: {
[id]: conversation,
},
conversationsByUuid: {
[id]: wrongConversation,
},
conversationsByGroupId: {
[id]: wrongConversation,
},
},
};
const selector = getConversationSelector(state);
const actual = selector(id);
assert.strictEqual(actual, conversation);
});
it('returns conversation by uuid', () => {
const id = 'id';
const conversation = getDefaultConversation(id);
const wrongConversation = getDefaultConversation('wrong');
const state = {
...getEmptyRootState(),
conversations: {
...getEmptyState(),
conversationLookup: {
[id]: wrongConversation,
},
conversationsByUuid: {
[id]: conversation,
},
conversationsByGroupId: {
[id]: wrongConversation,
},
},
};
const selector = getConversationSelector(state);
const actual = selector(id);
assert.strictEqual(actual, conversation);
});
it('returns conversation by groupId', () => {
const id = 'id';
const conversation = getDefaultConversation(id);
const wrongConversation = getDefaultConversation('wrong');
const state = {
...getEmptyRootState(),
conversations: {
...getEmptyState(),
conversationLookup: {
[id]: wrongConversation,
},
conversationsByGroupId: {
[id]: conversation,
},
},
};
const selector = getConversationSelector(state);
const actual = selector(id);
assert.strictEqual(actual, conversation);
});
it('returns conversation by conversationId', () => {
const id = 'id';
const conversation = getDefaultConversation(id);
const state = {
...getEmptyRootState(),
conversations: {
...getEmptyState(),
conversationLookup: {
[id]: conversation,
},
},
};
const selector = getConversationSelector(state);
const actual = selector(id);
assert.strictEqual(actual, conversation);
});
// Less important now, given that all prop-generation for conversations is in
// models/conversation.getProps() and not here.
it('does proper caching of result', () => {
const id = 'id';
const conversation = getDefaultConversation(id);
const state = {
...getEmptyRootState(),
conversations: {
...getEmptyState(),
conversationLookup: {
[id]: conversation,
},
},
};
const selector = getConversationSelector(state);
const actual = selector(id);
const secondState = {
...state,
conversations: {
...getEmptyState(),
conversationLookup: {
[id]: conversation,
},
},
};
const secondSelector = getConversationSelector(secondState);
const secondActual = secondSelector(id);
assert.strictEqual(actual, secondActual);
const thirdState = {
...state,
conversations: {
...getEmptyState(),
conversationLookup: {
[id]: getDefaultConversation('third'),
},
},
};
const thirdSelector = getConversationSelector(thirdState);
const thirdActual = thirdSelector(id);
assert.notStrictEqual(actual, thirdActual);
});
});
2019-01-14 21:49:58 +00:00
describe('#getLeftPaneList', () => {
it('sorts conversations based on timestamp then by intl-friendly title', () => {
2019-03-12 00:20:16 +00:00
const data: ConversationLookupType = {
2019-01-14 21:49:58 +00:00
id1: {
id: 'id1',
e164: '+18005551111',
2019-01-14 21:49:58 +00:00
activeAt: Date.now(),
name: 'No timestamp',
timestamp: 0,
inboxPosition: 0,
2019-01-14 21:49:58 +00:00
phoneNumber: 'notused',
2019-03-12 00:20:16 +00:00
isArchived: false,
markedUnread: false,
2019-01-14 21:49:58 +00:00
type: 'direct',
isMe: false,
lastUpdated: Date.now(),
title: 'No timestamp',
2019-01-14 21:49:58 +00:00
unreadCount: 1,
isSelected: false,
typingContact: {
name: 'Someone There',
color: 'blue',
phoneNumber: '+18005551111',
},
2020-05-27 21:37:06 +00:00
acceptedMessageRequest: true,
2019-01-14 21:49:58 +00:00
},
id2: {
id: 'id2',
e164: '+18005551111',
2019-01-14 21:49:58 +00:00
activeAt: Date.now(),
name: 'B',
timestamp: 20,
inboxPosition: 21,
2019-01-14 21:49:58 +00:00
phoneNumber: 'notused',
2019-03-12 00:20:16 +00:00
isArchived: false,
markedUnread: false,
2019-01-14 21:49:58 +00:00
type: 'direct',
isMe: false,
lastUpdated: Date.now(),
title: 'B',
2019-01-14 21:49:58 +00:00
unreadCount: 1,
isSelected: false,
typingContact: {
name: 'Someone There',
color: 'blue',
phoneNumber: '+18005551111',
},
2020-05-27 21:37:06 +00:00
acceptedMessageRequest: true,
2019-01-14 21:49:58 +00:00
},
id3: {
id: 'id3',
e164: '+18005551111',
2019-01-14 21:49:58 +00:00
activeAt: Date.now(),
name: 'C',
timestamp: 20,
inboxPosition: 22,
2019-01-14 21:49:58 +00:00
phoneNumber: 'notused',
2019-03-12 00:20:16 +00:00
isArchived: false,
markedUnread: false,
2019-01-14 21:49:58 +00:00
type: 'direct',
isMe: false,
lastUpdated: Date.now(),
title: 'C',
2019-01-14 21:49:58 +00:00
unreadCount: 1,
isSelected: false,
typingContact: {
name: 'Someone There',
color: 'blue',
phoneNumber: '+18005551111',
},
2020-05-27 21:37:06 +00:00
acceptedMessageRequest: true,
2019-01-14 21:49:58 +00:00
},
id4: {
id: 'id4',
e164: '+18005551111',
2019-01-14 21:49:58 +00:00
activeAt: Date.now(),
name: 'Á',
timestamp: 20,
inboxPosition: 20,
2019-01-14 21:49:58 +00:00
phoneNumber: 'notused',
2019-03-12 00:20:16 +00:00
isArchived: false,
markedUnread: false,
2019-01-14 21:49:58 +00:00
type: 'direct',
isMe: false,
lastUpdated: Date.now(),
title: 'A',
2019-01-14 21:49:58 +00:00
unreadCount: 1,
isSelected: false,
typingContact: {
name: 'Someone There',
color: 'blue',
phoneNumber: '+18005551111',
},
2020-05-27 21:37:06 +00:00
acceptedMessageRequest: true,
2019-01-14 21:49:58 +00:00
},
id5: {
id: 'id5',
e164: '+18005551111',
2019-01-14 21:49:58 +00:00
activeAt: Date.now(),
name: 'First!',
timestamp: 30,
inboxPosition: 30,
2019-01-14 21:49:58 +00:00
phoneNumber: 'notused',
2019-03-12 00:20:16 +00:00
isArchived: false,
markedUnread: false,
2019-01-14 21:49:58 +00:00
type: 'direct',
isMe: false,
lastUpdated: Date.now(),
title: 'First!',
2019-01-14 21:49:58 +00:00
unreadCount: 1,
isSelected: false,
typingContact: {
name: 'Someone There',
color: 'blue',
phoneNumber: '+18005551111',
},
2020-05-27 21:37:06 +00:00
acceptedMessageRequest: true,
2019-01-14 21:49:58 +00:00
},
};
2020-07-24 01:35:32 +00:00
const comparator = _getConversationComparator();
2019-03-12 00:20:16 +00:00
const { conversations } = _getLeftPaneLists(data, comparator);
2019-01-14 21:49:58 +00:00
2019-03-12 00:20:16 +00:00
assert.strictEqual(conversations[0].name, 'First!');
assert.strictEqual(conversations[1].name, 'Á');
assert.strictEqual(conversations[2].name, 'B');
assert.strictEqual(conversations[3].name, 'C');
assert.strictEqual(conversations[4].name, 'No timestamp');
2019-01-14 21:49:58 +00:00
});
2020-10-10 14:25:17 +00:00
describe('given pinned conversations', () => {
it('sorts pinned conversations based on order in storage', () => {
const data: ConversationLookupType = {
pin2: {
id: 'pin2',
e164: '+18005551111',
activeAt: Date.now(),
name: 'Pin Two',
timestamp: 30,
inboxPosition: 30,
phoneNumber: 'notused',
isArchived: false,
isPinned: true,
markedUnread: false,
2020-10-10 14:25:17 +00:00
type: 'direct',
isMe: false,
lastUpdated: Date.now(),
title: 'Pin Two',
unreadCount: 1,
isSelected: false,
typingContact: {
name: 'Someone There',
color: 'blue',
phoneNumber: '+18005551111',
},
acceptedMessageRequest: true,
},
pin3: {
id: 'pin3',
e164: '+18005551111',
activeAt: Date.now(),
name: 'Pin Three',
timestamp: 30,
inboxPosition: 30,
phoneNumber: 'notused',
isArchived: false,
isPinned: true,
markedUnread: false,
2020-10-10 14:25:17 +00:00
type: 'direct',
isMe: false,
lastUpdated: Date.now(),
title: 'Pin Three',
unreadCount: 1,
isSelected: false,
typingContact: {
name: 'Someone There',
color: 'blue',
phoneNumber: '+18005551111',
},
acceptedMessageRequest: true,
},
pin1: {
id: 'pin1',
e164: '+18005551111',
activeAt: Date.now(),
name: 'Pin One',
timestamp: 30,
inboxPosition: 30,
phoneNumber: 'notused',
isArchived: false,
isPinned: true,
markedUnread: false,
2020-10-10 14:25:17 +00:00
type: 'direct',
isMe: false,
lastUpdated: Date.now(),
title: 'Pin One',
unreadCount: 1,
isSelected: false,
typingContact: {
name: 'Someone There',
color: 'blue',
phoneNumber: '+18005551111',
},
acceptedMessageRequest: true,
},
};
const pinnedConversationIds = ['pin1', 'pin2', 'pin3'];
2020-10-10 14:25:17 +00:00
const comparator = _getConversationComparator();
const { pinnedConversations } = _getLeftPaneLists(
data,
comparator,
undefined,
pinnedConversationIds
);
2020-10-10 14:25:17 +00:00
assert.strictEqual(pinnedConversations[0].name, 'Pin One');
assert.strictEqual(pinnedConversations[1].name, 'Pin Two');
assert.strictEqual(pinnedConversations[2].name, 'Pin Three');
});
});
2019-01-14 21:49:58 +00:00
});
describe('#getSelectedConversationId', () => {
it('returns undefined if no conversation is selected', () => {
const state = {
...getEmptyRootState(),
conversations: {
...getEmptyState(),
conversationLookup: {
abc123: getDefaultConversation('abc123'),
},
},
};
assert.isUndefined(getSelectedConversationId(state));
});
it('returns the selected conversation ID', () => {
const state = {
...getEmptyRootState(),
conversations: {
...getEmptyState(),
conversationLookup: {
abc123: getDefaultConversation('abc123'),
},
selectedConversationId: 'abc123',
},
};
assert.strictEqual(getSelectedConversationId(state), 'abc123');
});
});
describe('#getSelectedConversation', () => {
it('returns undefined if no conversation is selected', () => {
const state = {
...getEmptyRootState(),
conversations: {
...getEmptyState(),
conversationLookup: {
abc123: getDefaultConversation('abc123'),
},
},
};
assert.isUndefined(getSelectedConversation(state));
});
it('returns the selected conversation ID', () => {
const state = {
...getEmptyRootState(),
conversations: {
...getEmptyState(),
conversationLookup: {
abc123: getDefaultConversation('abc123'),
},
selectedConversationId: 'abc123',
},
};
assert.deepEqual(
getSelectedConversation(state),
getDefaultConversation('abc123')
);
});
});
2019-01-14 21:49:58 +00:00
});