containers.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. // tslint:disable:no-reference
  20. /// <reference path="../../src/threema.d.ts" />
  21. import {ReceiverService} from '../../src/services/receiver';
  22. import {Conversations} from '../../src/threema/container';
  23. function getConversations(): Conversations {
  24. const receiverService = new ReceiverService();
  25. return new Conversations(receiverService);
  26. }
  27. function makeContactConversation(id: string, position: number): threema.ConversationWithPosition {
  28. return {
  29. type: 'contact',
  30. id: id,
  31. position: position,
  32. messageCount: 5,
  33. unreadCount: 0,
  34. latestMessage: null,
  35. };
  36. }
  37. function simplifyConversation(c: threema.Conversation): Array<string | number> {
  38. return [c.id, c.position];
  39. }
  40. describe('Container', () => {
  41. describe('Conversations', () => {
  42. it('find', function() {
  43. const conversations = getConversations();
  44. conversations.set([
  45. makeContactConversation('1', 0),
  46. makeContactConversation('2', 1),
  47. makeContactConversation('3', 2),
  48. makeContactConversation('4', 3),
  49. ]);
  50. const receiver1: threema.BaseReceiver = { id: '2', type: 'contact' };
  51. const receiver2: threema.BaseReceiver = { id: '5', type: 'contact' };
  52. const receiver3: threema.BaseReceiver = { id: '2', type: 'me' };
  53. expect(conversations.find(receiver1)).toEqual(makeContactConversation('2', 1));
  54. expect(conversations.find(receiver2)).toEqual(null);
  55. expect(conversations.find(receiver3)).toEqual(null);
  56. });
  57. describe('set', function() {
  58. it('overwrites previous data', function() {
  59. const conversations = getConversations();
  60. expect(conversations.get()).toEqual([]);
  61. conversations.add(makeContactConversation('0', 0));
  62. expect(conversations.get().map(simplifyConversation)).toEqual([['0', 0]]);
  63. conversations.set([makeContactConversation('1', 1)]);
  64. expect(conversations.get().map(simplifyConversation)).toEqual([['1', 1]]);
  65. });
  66. });
  67. describe('add', function() {
  68. it('adds a conversation at the correct location', function() {
  69. const conversations = getConversations();
  70. expect(conversations.get()).toEqual([]);
  71. conversations.add(makeContactConversation('0', 0));
  72. conversations.add(makeContactConversation('1', 1));
  73. expect(conversations.get().map(simplifyConversation)).toEqual([
  74. ['0', 0],
  75. ['1', 1],
  76. ]);
  77. conversations.add(makeContactConversation('2', 1));
  78. expect(conversations.get().map(simplifyConversation)).toEqual([
  79. ['0', 0],
  80. ['2', 1],
  81. ['1', 1],
  82. ]);
  83. });
  84. });
  85. });
  86. });