signal-desktop/ts/test-node/quill/memberRepository_test.ts

160 lines
5.1 KiB
TypeScript
Raw Normal View History

2021-05-07 22:21:10 +00:00
// Copyright 2020-2021 Signal Messenger, LLC
2020-11-04 22:04:48 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { ConversationType } from '../../state/ducks/conversations';
import { MemberRepository } from '../../quill/memberRepository';
2021-05-07 22:21:10 +00:00
import { getDefaultConversation } from '../../test-both/helpers/getDefaultConversation';
2020-11-04 22:04:48 +00:00
2021-05-07 22:21:10 +00:00
const memberMahershala: ConversationType = getDefaultConversation({
2020-11-04 22:04:48 +00:00
id: '555444',
uuid: 'abcdefg',
title: 'Pal',
firstName: 'Mahershala',
profileName: 'Mr Ali',
name: 'Friend',
type: 'direct',
lastUpdated: Date.now(),
markedUnread: false,
areWeAdmin: false,
2021-05-07 22:21:10 +00:00
});
2020-11-04 22:04:48 +00:00
2021-05-07 22:21:10 +00:00
const memberShia: ConversationType = getDefaultConversation({
2020-11-04 22:04:48 +00:00
id: '333222',
uuid: 'hijklmno',
title: 'Buddy',
firstName: 'Shia',
profileName: 'Sr LaBeouf',
name: 'Duder',
type: 'direct',
lastUpdated: Date.now(),
markedUnread: false,
areWeAdmin: false,
2021-05-07 22:21:10 +00:00
});
2020-11-04 22:04:48 +00:00
const members: Array<ConversationType> = [memberMahershala, memberShia];
2021-05-07 22:21:10 +00:00
const singleMember: ConversationType = getDefaultConversation({
2020-11-04 22:04:48 +00:00
id: '666777',
uuid: 'pqrstuv',
title: 'The Guy',
firstName: 'Jeff',
profileName: 'Jr Klaus',
name: 'Him',
type: 'direct',
lastUpdated: Date.now(),
markedUnread: false,
areWeAdmin: false,
2021-05-07 22:21:10 +00:00
});
2020-11-04 22:04:48 +00:00
describe('MemberRepository', () => {
describe('#updateMembers', () => {
it('updates with given members', () => {
const memberRepository = new MemberRepository(members);
assert.deepEqual(memberRepository.getMembers(), members);
const updatedMembers = [...members, singleMember];
memberRepository.updateMembers(updatedMembers);
assert.deepEqual(memberRepository.getMembers(), updatedMembers);
});
});
describe('#getMemberById', () => {
it('returns undefined when there is no search id', () => {
const memberRepository = new MemberRepository(members);
assert.isUndefined(memberRepository.getMemberById());
});
it('returns a matched member', () => {
const memberRepository = new MemberRepository(members);
assert.isDefined(memberRepository.getMemberById('555444'));
});
it('returns undefined when it does not have the member', () => {
const memberRepository = new MemberRepository(members);
assert.isUndefined(memberRepository.getMemberById('nope'));
});
});
describe('#getMemberByUuid', () => {
it('returns undefined when there is no search uuid', () => {
const memberRepository = new MemberRepository(members);
assert.isUndefined(memberRepository.getMemberByUuid());
});
it('returns a matched member', () => {
const memberRepository = new MemberRepository(members);
assert.isDefined(memberRepository.getMemberByUuid('abcdefg'));
});
it('returns undefined when it does not have the member', () => {
const memberRepository = new MemberRepository(members);
assert.isUndefined(memberRepository.getMemberByUuid('nope'));
});
});
describe('#search', () => {
describe('given a prefix-matching string on last name', () => {
it('returns the match', () => {
const memberRepository = new MemberRepository(members);
const results = memberRepository.search('a');
assert.deepEqual(results, [memberMahershala]);
});
});
describe('given a prefix-matching string on first name', () => {
it('returns the match', () => {
const memberRepository = new MemberRepository(members);
const results = memberRepository.search('ma');
assert.deepEqual(results, [memberMahershala]);
});
});
describe('given a prefix-matching string on profile name', () => {
it('returns the match', () => {
const memberRepository = new MemberRepository(members);
const results = memberRepository.search('sr');
assert.deepEqual(results, [memberShia]);
});
});
2021-08-17 18:55:25 +00:00
describe('given a prefix-matching string on name', () => {
it('returns the match', () => {
const memberRepository = new MemberRepository(members);
const results = memberRepository.search('dude');
assert.deepEqual(results, [memberShia]);
});
});
2020-11-04 22:04:48 +00:00
describe('given a prefix-matching string on title', () => {
it('returns the match', () => {
const memberRepository = new MemberRepository(members);
2021-08-17 18:55:25 +00:00
const results = memberRepository.search('bud');
2020-11-04 22:04:48 +00:00
assert.deepEqual(results, [memberShia]);
});
2021-08-17 18:55:25 +00:00
it('handles titles with Unicode bidi characters, which some contacts have', () => {
const memberShiaBidi: ConversationType = {
...memberShia,
title: '\u2086Buddyo\u2069',
};
const memberRepository = new MemberRepository([
memberMahershala,
memberShiaBidi,
]);
const results = memberRepository.search('bud');
assert.deepEqual(results, [memberShiaBidi]);
});
2020-11-04 22:04:48 +00:00
});
describe('given a match in the middle of a name', () => {
it('returns zero matches', () => {
const memberRepository = new MemberRepository(members);
const results = memberRepository.search('e');
assert.deepEqual(results, []);
});
});
});
});