Sort by recency then alphabetically everywhere

This commit is contained in:
Jamie Kyle 2024-03-18 16:31:42 -07:00 committed by GitHub
parent 9aff86f02b
commit 53ae88c777
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 195 additions and 177 deletions

View file

@ -2,90 +2,138 @@
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { pick } from 'lodash';
import { getDefaultConversation } from '../helpers/getDefaultConversation';
import { filterAndSortConversations } from '../../util/filterAndSortConversations';
import type { ConversationType } from '../../state/ducks/conversations';
import {
filterAndSortConversationsAlphabetically,
filterAndSortConversationsByRecent,
} from '../../util/filterAndSortConversations';
type CheckProps = Pick<ConversationType, 'title' | 'activeAt' | 'e164'>;
function check({
searchTerm,
input,
expected,
}: {
searchTerm: string;
input: Array<CheckProps>;
expected: Array<CheckProps>;
}) {
const conversations = input.map(props => {
return getDefaultConversation(props);
});
const results = filterAndSortConversations(conversations, searchTerm, 'US');
const actual = results.map(convo => {
return pick(convo, 'title', 'activeAt');
});
assert.sameDeepMembers(actual, expected);
}
describe('filterAndSortConversations', () => {
const conversations = [
getDefaultConversation({
title: '+16505551234',
activeAt: 1,
}),
getDefaultConversation({
title: 'The Abraham Lincoln Club',
activeAt: 4,
}),
getDefaultConversation({
title: 'Boxing Club',
activeAt: 3,
}),
getDefaultConversation({
title: 'Not recent',
}),
getDefaultConversation({
title: 'George Washington',
e164: '+16505559876',
activeAt: 2,
}),
getDefaultConversation({
title: 'A long long long title ending with burrito',
}),
];
it('filterAndSortConversationsByRecent sorts by recency when no search term is provided', () => {
const titles = filterAndSortConversationsByRecent(
conversations,
'',
'US'
).map(contact => contact.title);
assert.sameOrderedMembers(titles, [
'The Abraham Lincoln Club',
'Boxing Club',
'George Washington',
'+16505551234',
'Not recent',
'A long long long title ending with burrito',
]);
});
it('filterAndSortConversationsAlphabetically sorts by title when no search term is provided', () => {
const titles = filterAndSortConversationsAlphabetically(
conversations,
'',
'US'
).map(contact => contact.title);
assert.sameOrderedMembers(titles, [
'A long long long title ending with burrito',
'Boxing Club',
'George Washington',
'Not recent',
'The Abraham Lincoln Club',
'+16505551234',
]);
});
it('filterAndSortConversationsAlphabetically sorts by title when a search term is provided', () => {
const titles = filterAndSortConversationsAlphabetically(
conversations,
'club',
'US'
).map(contact => contact.title);
assert.sameOrderedMembers(titles, [
'Boxing Club',
'The Abraham Lincoln Club',
]);
it('finds a conversation by title', () => {
check({
searchTerm: 'yes',
input: [{ title: 'no' }, { title: 'yes' }, { title: 'no' }],
expected: [{ title: 'yes' }],
});
});
it('finds a conversation when the search term is at the end of a long title', () => {
const titles = filterAndSortConversationsByRecent(
conversations,
'burrito',
'US'
).map(convo => convo.title);
assert.deepEqual(titles, ['A long long long title ending with burrito']);
check({
searchTerm: 'burrito',
input: [
{ title: 'no' },
{
title: 'A long long long title ending with burrito',
},
{ title: 'no' },
],
expected: [
{
title: 'A long long long title ending with burrito',
},
],
});
});
it('finds a conversation by phone number', () => {
check({
searchTerm: '9876',
input: [
{ title: 'no' },
{ title: 'yes', e164: '+16505559876' },
{ title: 'no' },
],
expected: [{ title: 'yes' }],
});
});
describe('no search term', () => {
it('sorts by recency first', () => {
check({
searchTerm: '',
input: [
{ title: 'B', activeAt: 2 },
{ title: 'A', activeAt: 1 },
{ title: 'C', activeAt: 3 },
],
expected: [
{ title: 'C', activeAt: 3 },
{ title: 'B', activeAt: 2 },
{ title: 'A', activeAt: 1 },
],
});
});
it('falls back to alphabetically', () => {
check({
searchTerm: '',
input: [
{ title: 'B', activeAt: 2 },
{ title: 'A', activeAt: 2 },
{ title: 'C', activeAt: 3 },
],
expected: [
{ title: 'C', activeAt: 3 },
{ title: 'A', activeAt: 2 },
{ title: 'B', activeAt: 2 },
],
});
});
});
describe('with search term', () => {
it('sorts by recency first', () => {
check({
searchTerm: 'yes',
input: [
{ title: 'no' },
{ title: 'yes B', activeAt: 2 },
{ title: 'yes A', activeAt: 1 },
{ title: 'yes C', activeAt: 3 },
],
expected: [
{ title: 'yes C', activeAt: 3 },
{ title: 'yes B', activeAt: 2 },
{ title: 'yes A', activeAt: 1 },
],
});
});
it('falls back to alphabetically', () => {
check({
searchTerm: 'yes',
input: [
{ title: 'no' },
{ title: 'yes B', activeAt: 2 },
{ title: 'yes A', activeAt: 2 },
{ title: 'yes C', activeAt: 3 },
],
expected: [
{ title: 'yes C', activeAt: 3 },
{ title: 'yes A', activeAt: 2 },
{ title: 'yes B', activeAt: 2 },
],
});
});
});
});