containers.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /**
  2. * Copyright © 2016-2019 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, StringHashSet} 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. isStarred: false,
  36. };
  37. }
  38. function getId(c: threema.Conversation): string {
  39. return c.id;
  40. }
  41. describe('Container', () => {
  42. describe('Conversations', () => {
  43. it('find', function() {
  44. const conversations = getConversations();
  45. conversations.set([
  46. makeContactConversation('1'),
  47. makeContactConversation('2'),
  48. makeContactConversation('3'),
  49. makeContactConversation('4'),
  50. ]);
  51. const receiver1: threema.BaseReceiver = { id: '2', type: 'contact' };
  52. const receiver2: threema.BaseReceiver = { id: '5', type: 'contact' };
  53. const receiver3: threema.BaseReceiver = { id: '2', type: 'me' };
  54. expect(conversations.find(receiver1)).toEqual(makeContactConversation('2'));
  55. expect(conversations.find(receiver2)).toEqual(null);
  56. expect(conversations.find(receiver3)).toEqual(null);
  57. });
  58. describe('set', function() {
  59. it('overwrites previous data', function() {
  60. const conversations = getConversations();
  61. expect(conversations.get()).toEqual([]);
  62. conversations.add(makeContactConversation('0'));
  63. expect(conversations.get().map(getId)).toEqual(['0']);
  64. conversations.set([makeContactConversation('1')]);
  65. expect(conversations.get().map(getId)).toEqual(['1']);
  66. });
  67. it('clears position field', function() {
  68. const conversations = getConversations();
  69. conversations.set([makeContactConversation('1', 7)]);
  70. const expected = makeContactConversation('1');
  71. delete expected.position;
  72. expect((conversations as any).conversations).toEqual([expected]);
  73. });
  74. it('sets defaults', function() {
  75. const conversations = getConversations();
  76. const conversation = makeContactConversation('1', 7);
  77. delete conversation.isStarred;
  78. conversations.set([conversation]);
  79. const expected = makeContactConversation('1');
  80. expect((conversations as any).conversations[0].isStarred).toEqual(false);
  81. });
  82. });
  83. describe('add', function() {
  84. it('adds a new conversation at the correct location', function() {
  85. const conversations = getConversations();
  86. expect(conversations.get()).toEqual([]);
  87. conversations.add(makeContactConversation('0', 0));
  88. conversations.add(makeContactConversation('1', 1));
  89. expect(conversations.get().map(getId)).toEqual(['0', '1']);
  90. conversations.add(makeContactConversation('2', 1));
  91. expect(conversations.get().map(getId)).toEqual(['0', '2', '1']);
  92. });
  93. });
  94. describe('updateOrAdd', function() {
  95. it('adds a new conversation at the correct location', function() {
  96. const conversations = getConversations();
  97. conversations.set([
  98. makeContactConversation('0'),
  99. makeContactConversation('1'),
  100. ]);
  101. expect(conversations.get().map(getId)).toEqual(['0', '1']);
  102. conversations.updateOrAdd(makeContactConversation('2', 2));
  103. expect(conversations.get().map(getId)).toEqual(['0', '1', '2']);
  104. conversations.updateOrAdd(makeContactConversation('3', 2));
  105. expect(conversations.get().map(getId)).toEqual(['0', '1', '3', '2']);
  106. });
  107. it('moves an existing conversation to the correct location', function() {
  108. const conversations = getConversations();
  109. conversations.set([
  110. makeContactConversation('0'),
  111. makeContactConversation('1'),
  112. makeContactConversation('2'),
  113. ]);
  114. expect(conversations.get().map(getId)).toEqual(['0', '1', '2']);
  115. conversations.updateOrAdd(makeContactConversation('2', 1));
  116. expect(conversations.get().map(getId)).toEqual(['0', '2', '1']);
  117. conversations.updateOrAdd(makeContactConversation('1', 0));
  118. expect(conversations.get().map(getId)).toEqual(['1', '0', '2']);
  119. conversations.updateOrAdd(makeContactConversation('0', 2));
  120. expect(conversations.get().map(getId)).toEqual(['1', '2', '0']);
  121. conversations.updateOrAdd(makeContactConversation('1', 7));
  122. expect(conversations.get().map(getId)).toEqual(['2', '0', '1']);
  123. });
  124. });
  125. });
  126. describe('StringHashSet', () => {
  127. it('clearAll', function() {
  128. const shs = new StringHashSet();
  129. shs.add('hello');
  130. shs.add('hello');
  131. shs.add('bye');
  132. expect(shs.values()).toEqual(['hello', 'bye']);
  133. shs.clearAll();
  134. expect(shs.values()).toEqual([]);
  135. });
  136. });
  137. });