containers.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 getId(c: threema.Conversation): string {
  38. return c.id;
  39. }
  40. describe('Container', () => {
  41. describe('Conversations', () => {
  42. it('find', function() {
  43. const conversations = getConversations();
  44. conversations.set([
  45. makeContactConversation('1'),
  46. makeContactConversation('2'),
  47. makeContactConversation('3'),
  48. makeContactConversation('4'),
  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'));
  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'));
  62. expect(conversations.get().map(getId)).toEqual(['0']);
  63. conversations.set([makeContactConversation('1')]);
  64. expect(conversations.get().map(getId)).toEqual(['1']);
  65. });
  66. it('clears position field', function() {
  67. const conversations = getConversations();
  68. conversations.set([makeContactConversation('1', 7)]);
  69. const expected = makeContactConversation('1');
  70. delete expected.position;
  71. expect((conversations as any).conversations).toEqual([expected]);
  72. });
  73. });
  74. describe('add', function() {
  75. it('adds a new conversation at the correct location', function() {
  76. const conversations = getConversations();
  77. expect(conversations.get()).toEqual([]);
  78. conversations.add(makeContactConversation('0', 0));
  79. conversations.add(makeContactConversation('1', 1));
  80. expect(conversations.get().map(getId)).toEqual(['0', '1']);
  81. conversations.add(makeContactConversation('2', 1));
  82. expect(conversations.get().map(getId)).toEqual(['0', '2', '1']);
  83. });
  84. });
  85. describe('updateOrAdd', function() {
  86. it('adds a new conversation at the correct location', function() {
  87. const conversations = getConversations();
  88. conversations.set([
  89. makeContactConversation('0'),
  90. makeContactConversation('1'),
  91. ]);
  92. expect(conversations.get().map(getId)).toEqual(['0', '1']);
  93. conversations.updateOrAdd(makeContactConversation('2', 2));
  94. expect(conversations.get().map(getId)).toEqual(['0', '1', '2']);
  95. conversations.updateOrAdd(makeContactConversation('3', 2));
  96. expect(conversations.get().map(getId)).toEqual(['0', '1', '3', '2']);
  97. });
  98. it('moves an existing conversation to the correct location', function() {
  99. const conversations = getConversations();
  100. conversations.set([
  101. makeContactConversation('0'),
  102. makeContactConversation('1'),
  103. makeContactConversation('2'),
  104. ]);
  105. expect(conversations.get().map(getId)).toEqual(['0', '1', '2']);
  106. conversations.updateOrAdd(makeContactConversation('2', 1));
  107. expect(conversations.get().map(getId)).toEqual(['0', '2', '1']);
  108. conversations.updateOrAdd(makeContactConversation('1', 0));
  109. expect(conversations.get().map(getId)).toEqual(['1', '0', '2']);
  110. conversations.updateOrAdd(makeContactConversation('0', 2));
  111. expect(conversations.get().map(getId)).toEqual(['1', '2', '0']);
  112. conversations.updateOrAdd(makeContactConversation('1', 7));
  113. expect(conversations.get().map(getId)).toEqual(['2', '0', '1']);
  114. });
  115. });
  116. });
  117. });