containers.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Copyright © 2016-2018 Threema GmbH (https://threema.ch/).
  3. *
  4. * This file is part of Threema Web.
  5. *
  6. * Threema Web is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Affero General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with Threema Web. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. import {ReceiverService} from '../../src/services/receiver';
  20. import {Conversations} from '../../src/threema/container';
  21. function getConversations(): Conversations {
  22. const receiverService = new ReceiverService();
  23. return new Conversations(receiverService);
  24. }
  25. function makeContactConversation(id: string, position: number): threema.Conversation {
  26. return {
  27. type: 'contact',
  28. id: id,
  29. position: position,
  30. messageCount: 5,
  31. unreadCount: 0,
  32. latestMessage: null,
  33. };
  34. }
  35. function simplifyConversation(c: threema.Conversation): Array<string | number> {
  36. return [c.id, c.position];
  37. }
  38. describe('Container', () => {
  39. describe('Conversations', () => {
  40. it('find', function() {
  41. const conversations = getConversations();
  42. conversations.set([
  43. makeContactConversation('1', 0),
  44. makeContactConversation('2', 1),
  45. makeContactConversation('3', 2),
  46. makeContactConversation('4', 3),
  47. ]);
  48. const receiver1: threema.BaseReceiver = { id: '2', type: 'contact' };
  49. const receiver2: threema.BaseReceiver = { id: '5', type: 'contact' };
  50. const receiver3: threema.BaseReceiver = { id: '2', type: 'me' };
  51. expect(conversations.find(receiver1)).toEqual(makeContactConversation('2', 1));
  52. expect(conversations.find(receiver2)).toEqual(null);
  53. expect(conversations.find(receiver3)).toEqual(null);
  54. });
  55. describe('set', function() {
  56. it('overwrites previous data', function() {
  57. const conversations = getConversations();
  58. expect(conversations.get()).toEqual([]);
  59. conversations.add(makeContactConversation('0', 0));
  60. expect(conversations.get().map(simplifyConversation)).toEqual([['0', 0]]);
  61. conversations.set([makeContactConversation('1', 1)]);
  62. expect(conversations.get().map(simplifyConversation)).toEqual([['1', 1]]);
  63. });
  64. });
  65. });
  66. });