ChatBoxSpec.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. (function (root, factory) {
  2. define([
  3. "mock",
  4. "utils"
  5. ], function (mock, utils) {
  6. return factory(mock, utils);
  7. }
  8. );
  9. } (this, function (mock, utils) {
  10. return describe("Chatboxes", $.proxy(function(mock, utils) {
  11. describe("A Chatbox", $.proxy(function () {
  12. beforeEach(function () {
  13. utils.closeAllChatBoxes();
  14. utils.removeControlBox();
  15. converse.roster.localStorage._clear();
  16. utils.initConverse();
  17. utils.createCurrentContacts();
  18. utils.openControlBox();
  19. utils.openContactsPanel();
  20. });
  21. it("is created when you click on a roster item", $.proxy(function () {
  22. var i, $el, click, jid, view;
  23. // openControlBox was called earlier, so the controlbox is
  24. // visible, but no other chat boxes have been created.
  25. expect(this.chatboxes.length).toEqual(1);
  26. var online_contacts = this.rosterview.$el.find('dt#xmpp-contacts').siblings('dd.current-xmpp-contact.online').find('a.open-chat');
  27. for (i=0; i<online_contacts.length; i++) {
  28. $el = $(online_contacts[i]);
  29. jid = $el.text().replace(' ','.').toLowerCase() + '@localhost';
  30. view = this.rosterview.rosteritemviews[jid];
  31. spyOn(view, 'openChat').andCallThrough();
  32. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  33. $el.click();
  34. expect(view.openChat).toHaveBeenCalled();
  35. expect(this.chatboxes.length).toEqual(i+2);
  36. }
  37. }, converse));
  38. it("can be saved to, and retrieved from, localStorage", $.proxy(function () {
  39. utils.closeControlBox();
  40. // First, we open 6 more chatboxes (controlbox is already open)
  41. utils.openChatBoxes(6);
  42. // We instantiate a new ChatBoxes collection, which by default
  43. // will be empty.
  44. var newchatboxes = new this.ChatBoxes();
  45. expect(newchatboxes.length).toEqual(0);
  46. // The chatboxes will then be fetched from localStorage inside the
  47. // onConnected method
  48. newchatboxes.onConnected();
  49. expect(newchatboxes.length).toEqual(6);
  50. // Check that the chatboxes items retrieved from localStorage
  51. // have the same attributes values as the original ones.
  52. attrs = ['id', 'box_id', 'visible'];
  53. for (i=0; i<attrs.length; i++) {
  54. new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]);
  55. old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]);
  56. expect(_.isEqual(new_attrs, old_attrs)).toEqual(true);
  57. }
  58. this.rosterview.render();
  59. }, converse));
  60. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
  61. var chatbox, view, $el,
  62. num_open_chats = this.chatboxes.length;
  63. for (i=0; i<num_open_chats; i++) {
  64. chatbox = this.chatboxes.models[0];
  65. view = this.chatboxesview.views[chatbox.get('id')];
  66. spyOn(view, 'closeChat').andCallThrough();
  67. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  68. view.$el.find('.close-chatbox-button').click();
  69. expect(view.closeChat).toHaveBeenCalled();
  70. }
  71. }, converse));
  72. it("will be removed from localStorage when closed", $.proxy(function () {
  73. this.chatboxes.localStorage._clear();
  74. utils.closeControlBox();
  75. expect(converse.chatboxes.length).toEqual(0);
  76. utils.openChatBoxes(6);
  77. expect(converse.chatboxes.length).toEqual(6);
  78. utils.closeAllChatBoxes();
  79. expect(converse.chatboxes.length).toEqual(0);
  80. var newchatboxes = new this.ChatBoxes();
  81. expect(newchatboxes.length).toEqual(0);
  82. // onConnected will fetch chatboxes in localStorage, but
  83. // because there aren't any open chatboxes, there won't be any
  84. // in localStorage either.
  85. newchatboxes.onConnected();
  86. expect(newchatboxes.length).toEqual(0);
  87. }, converse));
  88. describe("A Chat Message", $.proxy(function () {
  89. it("can be received which will open a chatbox and be displayed inside it", $.proxy(function () {
  90. var message = 'This is a received message';
  91. var sender_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  92. msg = $msg({
  93. from: sender_jid,
  94. to: this.connection.jid,
  95. type: 'chat',
  96. id: (new Date()).getTime()
  97. }).c('body').t(message).up()
  98. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  99. // We don't already have an open chatbox for this user
  100. expect(this.chatboxes.get(sender_jid)).not.toBeDefined();
  101. runs($.proxy(function () {
  102. // messageReceived is a handler for received XMPP
  103. // messages
  104. this.chatboxes.messageReceived(msg);
  105. }, converse));
  106. waits(500);
  107. runs($.proxy(function () {
  108. // Check that the chatbox and its view now exist
  109. var chatbox = this.chatboxes.get(sender_jid);
  110. var chatboxview = this.chatboxesview.views[sender_jid];
  111. expect(chatbox).toBeDefined();
  112. expect(chatboxview).toBeDefined();
  113. // Check that the message was received and check the
  114. // message parameters
  115. expect(chatbox.messages.length).toEqual(1);
  116. var msg_obj = chatbox.messages.models[0];
  117. expect(msg_obj.get('message')).toEqual(message);
  118. // XXX: This is stupid, fullname is actually only the
  119. // users first name
  120. expect(msg_obj.get('fullname')).toEqual(mock.cur_names[0].split(' ')[0]);
  121. expect(msg_obj.get('sender')).toEqual('them');
  122. expect(msg_obj.get('delayed')).toEqual(false);
  123. // Now check that the message appears inside the
  124. // chatbox in the DOM
  125. var $chat_content = chatboxview.$el.find('.chat-content');
  126. var msg_txt = $chat_content.find('.chat-message').find('.chat-message-content').text();
  127. expect(msg_txt).toEqual(message);
  128. var sender_txt = $chat_content.find('span.chat-message-them').text();
  129. expect(sender_txt.match(/^[0-9][0-9]:[0-9][0-9] /)).toBeTruthy();
  130. }, converse));
  131. }, converse));
  132. it("can be sent from a chatbox, and will appear inside it", $.proxy(function () {
  133. var contact_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  134. utils.openChatBoxFor(contact_jid);
  135. var view = this.chatboxesview.views[contact_jid];
  136. var message = 'This message is sent from this chatbox';
  137. spyOn(view, 'sendMessage').andCallThrough();
  138. view.$el.find('.chat-textarea').text(message);
  139. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  140. expect(view.sendMessage).toHaveBeenCalled();
  141. expect(view.model.messages.length, 2);
  142. var txt = view.$el.find('.chat-content').find('.chat-message').last().find('.chat-message-content').text();
  143. expect(txt).toEqual(message);
  144. }, converse));
  145. }, converse));
  146. }, converse));
  147. describe("Special Messages", $.proxy(function () {
  148. it("'/clear' can be used to clear messages in a conversation", $.proxy(function () {
  149. var contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
  150. var view = this.chatboxesview.views[contact_jid];
  151. var message = 'This message is another sent from this chatbox';
  152. // Lets make sure there is at least one message already
  153. // (e.g for when this test is run on its own).
  154. view.$el.find('.chat-textarea').val(message).text(message);
  155. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  156. expect(view.model.messages.length > 0).toBeTruthy();
  157. expect(view.model.messages.localStorage.records.length > 0).toBeTruthy();
  158. message = '/clear';
  159. var old_length = view.model.messages.length;
  160. spyOn(view, 'sendMessage').andCallThrough();
  161. view.$el.find('.chat-textarea').val(message).text(message);
  162. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  163. expect(view.sendMessage).toHaveBeenCalled();
  164. expect(view.model.messages.length, 0); // The messages must be removed from the modal
  165. expect(view.model.messages.localStorage.records.length, 0); // And also from localStorage
  166. }, converse));
  167. }, converse));
  168. describe("A Message Counter", $.proxy(function () {
  169. beforeEach($.proxy(function () {
  170. converse.clearMsgCounter();
  171. }, converse));
  172. it("is incremented when the message is received and the window is not focused", $.proxy(function () {
  173. expect(this.msg_counter).toBe(0);
  174. spyOn(converse, 'incrementMsgCounter').andCallThrough();
  175. $(window).trigger('blur');
  176. var message = 'This message will increment the message counter';
  177. var sender_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  178. msg = $msg({
  179. from: sender_jid,
  180. to: this.connection.jid,
  181. type: 'chat',
  182. id: (new Date()).getTime()
  183. }).c('body').t(message).up()
  184. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  185. this.chatboxes.messageReceived(msg);
  186. expect(converse.incrementMsgCounter).toHaveBeenCalled();
  187. expect(this.msg_counter).toBe(1);
  188. }, converse));
  189. it("is cleared when the window is focused", $.proxy(function () {
  190. spyOn(converse, 'clearMsgCounter').andCallThrough();
  191. runs(function () {
  192. $(window).trigger('focus');
  193. });
  194. waits(50);
  195. runs(function () {
  196. expect(converse.clearMsgCounter).toHaveBeenCalled();
  197. });
  198. }, converse));
  199. it("is not incremented when the message is received and the window is focused", $.proxy(function () {
  200. expect(this.msg_counter).toBe(0);
  201. spyOn(converse, 'incrementMsgCounter').andCallThrough();
  202. $(window).trigger('focus');
  203. var message = 'This message will not increment the message counter';
  204. var sender_jid = mock.cur_names[0].replace(' ','.').toLowerCase() + '@localhost';
  205. msg = $msg({
  206. from: sender_jid,
  207. to: this.connection.jid,
  208. type: 'chat',
  209. id: (new Date()).getTime()
  210. }).c('body').t(message).up()
  211. .c('active', {'xmlns': 'http://jabber.org/protocol/chatstates'}).tree();
  212. this.chatboxes.messageReceived(msg);
  213. expect(converse.incrementMsgCounter).not.toHaveBeenCalled();
  214. expect(this.msg_counter).toBe(0);
  215. }, converse));
  216. }, converse));
  217. }, converse, mock, utils));
  218. }));