signal-desktop/ts/test-node/quill/memberRepository_test.ts
2021-05-07 17:21:10 -05:00

138 lines
4.3 KiB
TypeScript

// Copyright 2020-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { ConversationType } from '../../state/ducks/conversations';
import { MemberRepository } from '../../quill/memberRepository';
import { getDefaultConversation } from '../../test-both/helpers/getDefaultConversation';
const memberMahershala: ConversationType = getDefaultConversation({
id: '555444',
uuid: 'abcdefg',
title: 'Pal',
firstName: 'Mahershala',
profileName: 'Mr Ali',
name: 'Friend',
type: 'direct',
lastUpdated: Date.now(),
markedUnread: false,
areWeAdmin: false,
});
const memberShia: ConversationType = getDefaultConversation({
id: '333222',
uuid: 'hijklmno',
title: 'Buddy',
firstName: 'Shia',
profileName: 'Sr LaBeouf',
name: 'Duder',
type: 'direct',
lastUpdated: Date.now(),
markedUnread: false,
areWeAdmin: false,
});
const members: Array<ConversationType> = [memberMahershala, memberShia];
const singleMember: ConversationType = getDefaultConversation({
id: '666777',
uuid: 'pqrstuv',
title: 'The Guy',
firstName: 'Jeff',
profileName: 'Jr Klaus',
name: 'Him',
type: 'direct',
lastUpdated: Date.now(),
markedUnread: false,
areWeAdmin: false,
});
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]);
});
});
describe('given a prefix-matching string on title', () => {
it('returns the match', () => {
const memberRepository = new MemberRepository(members);
const results = memberRepository.search('d');
assert.deepEqual(results, [memberShia]);
});
});
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, []);
});
});
});
});