chatroom.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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("ChatRooms", $.proxy(function (mock, utils) {
  11. describe("A Chat Room", $.proxy(function () {
  12. beforeEach(function () {
  13. runs(function () {
  14. utils.closeAllChatBoxes();
  15. });
  16. waits(250);
  17. runs(function () {
  18. utils.openControlBox();
  19. });
  20. waits(250);
  21. runs(function () {
  22. utils.openRoomsPanel();
  23. });
  24. waits(501);
  25. runs(function () {
  26. // Open a new chatroom
  27. var roomspanel = converse.chatboxviews.get('controlbox').roomspanel;
  28. var $input = roomspanel.$el.find('input.new-chatroom-name');
  29. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  30. var $server = roomspanel.$el.find('input.new-chatroom-server');
  31. $input.val('lounge');
  32. $nick.val('dummy');
  33. $server.val('muc.localhost');
  34. roomspanel.$el.find('form').submit();
  35. });
  36. waits(250);
  37. runs(function () {
  38. utils.closeControlBox();
  39. });
  40. waits(250);
  41. runs(function () {});
  42. });
  43. it("shows users currently present in the room", $.proxy(function () {
  44. var chatroomview = this.chatboxviews.get('lounge@muc.localhost'),
  45. $participant_list;
  46. var roster = {}, room = {}, i;
  47. for (i=0; i<mock.chatroom_names.length-1; i++) {
  48. roster[mock.chatroom_names[i]] = {};
  49. chatroomview.onChatRoomRoster(roster, room);
  50. $participant_list = chatroomview.$el.find('.participant-list');
  51. expect($participant_list.find('li').length).toBe(1+i);
  52. expect($($participant_list.find('li')[i]).text()).toBe(mock.chatroom_names[i]);
  53. }
  54. roster[converse.bare_jid] = {};
  55. chatroomview.onChatRoomRoster(roster, room);
  56. }, converse));
  57. it("indicates moderators by means of a special css class and tooltip", $.proxy(function () {
  58. var chatroomview = this.chatboxviews.get('lounge@muc.localhost');
  59. var roster = {}, idx = mock.chatroom_names.length-1;
  60. roster[mock.chatroom_names[idx]] = {};
  61. roster[mock.chatroom_names[idx]].role = 'moderator';
  62. chatroomview.onChatRoomRoster(roster, {});
  63. var occupant = chatroomview.$el.find('.participant-list').find('li');
  64. expect(occupant.length).toBe(1);
  65. expect($(occupant).text()).toBe(mock.chatroom_names[idx]);
  66. expect($(occupant).attr('class')).toBe('moderator');
  67. expect($(occupant).attr('title')).toBe('This user is a moderator');
  68. }, converse));
  69. it("shows received groupchat messages", $.proxy(function () {
  70. spyOn(converse, 'emit');
  71. var view = this.chatboxviews.get('lounge@muc.localhost');
  72. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  73. var nick = mock.chatroom_names[0];
  74. var text = 'This is a received message';
  75. var message = $msg({
  76. from: 'lounge@muc.localhost/'+nick,
  77. id: '1',
  78. to: 'dummy@localhost',
  79. type: 'groupchat'
  80. }).c('body').t(text);
  81. view.onChatRoomMessage(message.nodeTree);
  82. var $chat_content = view.$el.find('.chat-content');
  83. expect($chat_content.find('.chat-message').length).toBe(1);
  84. expect($chat_content.find('.chat-message-content').text()).toBe(text);
  85. expect(converse.emit).toHaveBeenCalledWith('onMessage', message.nodeTree);
  86. }, converse));
  87. it("shows sent groupchat messages", $.proxy(function () {
  88. spyOn(converse, 'emit');
  89. var view = this.chatboxviews.get('lounge@muc.localhost');
  90. if (!view.$el.find('.chat-area').length) { view.renderChatArea(); }
  91. var nick = mock.chatroom_names[0];
  92. var text = 'This is a sent message';
  93. view.$el.find('.chat-textarea').text(text);
  94. view.$el.find('textarea.chat-textarea').trigger($.Event('keypress', {keyCode: 13}));
  95. expect(converse.emit).toHaveBeenCalledWith('onMessageSend', text);
  96. var message = $msg({
  97. from: 'lounge@muc.localhost/dummy',
  98. id: '2',
  99. to: 'dummy@localhost.com',
  100. type: 'groupchat'
  101. }).c('body').t(text);
  102. view.onChatRoomMessage(message.nodeTree);
  103. var $chat_content = view.$el.find('.chat-content');
  104. expect($chat_content.find('.chat-message').length).toBe(1);
  105. expect($chat_content.find('.chat-message-content').last().text()).toBe(text);
  106. // We don't emit an event if it's our own message
  107. expect(converse.emit.callCount, 1);
  108. }, converse));
  109. it("can be saved to, and retrieved from, localStorage", $.proxy(function () {
  110. // We instantiate a new ChatBoxes collection, which by default
  111. // will be empty.
  112. spyOn(this.chatboxviews, 'trimOpenChats');
  113. utils.openControlBox();
  114. var newchatboxes = new this.ChatBoxes();
  115. expect(newchatboxes.length).toEqual(0);
  116. // The chatboxes will then be fetched from localStorage inside the
  117. // onConnected method
  118. newchatboxes.onConnected();
  119. expect(this.chatboxviews.trimOpenChats).toHaveBeenCalled();
  120. expect(newchatboxes.length).toEqual(2); // XXX: Includes controlbox, is this a bug?
  121. // Check that the chatrooms retrieved from localStorage
  122. // have the same attributes values as the original ones.
  123. attrs = ['id', 'box_id', 'visible'];
  124. for (i=0; i<attrs.length; i++) {
  125. new_attrs = _.pluck(_.pluck(newchatboxes.models, 'attributes'), attrs[i]);
  126. old_attrs = _.pluck(_.pluck(this.chatboxes.models, 'attributes'), attrs[i]);
  127. // FIXME: should have have to sort here? Order must
  128. // probably be the same...
  129. // This should be fixed once the controlbox always opens
  130. // only on the right.
  131. expect(_.isEqual(new_attrs.sort(), old_attrs.sort())).toEqual(true);
  132. }
  133. this.rosterview.render();
  134. }, converse));
  135. it("can be toggled by clicking a DOM element with class 'toggle-chatbox-button'", function () {
  136. var view = this.chatboxviews.get('lounge@muc.localhost'),
  137. chatroom = view.model, $el;
  138. spyOn(view, 'toggleChatBox').andCallThrough();
  139. spyOn(converse, 'emit');
  140. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  141. runs(function () {
  142. view.$el.find('.toggle-chatbox-button').click();
  143. });
  144. waits(250);
  145. runs(function () {
  146. expect(view.toggleChatBox).toHaveBeenCalled();
  147. expect(converse.emit).toHaveBeenCalledWith('onChatBoxToggled', jasmine.any(Object));
  148. expect(converse.emit.callCount, 2);
  149. expect(view.$el.find('.chat-body').is(':visible')).toBeFalsy();
  150. expect(view.$el.find('.toggle-chatbox-button').hasClass('icon-minus')).toBeFalsy();
  151. expect(view.$el.find('.toggle-chatbox-button').hasClass('icon-plus')).toBeTruthy();
  152. expect(view.model.get('minimized')).toBeTruthy();
  153. view.$el.find('.toggle-chatbox-button').click();
  154. });
  155. waits(250);
  156. runs(function () {
  157. expect(view.toggleChatBox).toHaveBeenCalled();
  158. expect(converse.emit).toHaveBeenCalledWith('onChatBoxToggled', jasmine.any(Object));
  159. expect(view.$el.find('.chat-body').is(':visible')).toBeTruthy();
  160. expect(view.$el.find('.toggle-chatbox-button').hasClass('icon-minus')).toBeTruthy();
  161. expect(view.$el.find('.toggle-chatbox-button').hasClass('icon-plus')).toBeFalsy();
  162. expect(view.model.get('minimized')).toBeFalsy();
  163. expect(converse.emit.callCount, 3);
  164. });
  165. }.bind(converse));
  166. it("can be closed again by clicking a DOM element with class 'close-chatbox-button'", $.proxy(function () {
  167. var view = this.chatboxviews.get('lounge@muc.localhost'), chatroom = view.model, $el;
  168. spyOn(view, 'closeChat').andCallThrough();
  169. spyOn(converse, 'emit');
  170. spyOn(converse.connection.muc, 'leave');
  171. view.delegateEvents(); // We need to rebind all events otherwise our spy won't be called
  172. runs(function () {
  173. view.$el.find('.close-chatbox-button').click();
  174. });
  175. waits(250);
  176. runs(function () {
  177. expect(view.closeChat).toHaveBeenCalled();
  178. expect(this.connection.muc.leave).toHaveBeenCalled();
  179. expect(this.emit).toHaveBeenCalledWith('onChatBoxClosed', jasmine.any(Object));
  180. }.bind(converse));
  181. }, converse));
  182. }, converse));
  183. describe("When attempting to enter a chatroom", $.proxy(function () {
  184. beforeEach($.proxy(function () {
  185. var roomspanel = this.chatboxviews.get('controlbox').roomspanel;
  186. var $input = roomspanel.$el.find('input.new-chatroom-name');
  187. var $nick = roomspanel.$el.find('input.new-chatroom-nick');
  188. var $server = roomspanel.$el.find('input.new-chatroom-server');
  189. $input.val('problematic');
  190. $nick.val('dummy');
  191. $server.val('muc.localhost');
  192. roomspanel.$el.find('form').submit();
  193. }, converse));
  194. afterEach($.proxy(function () {
  195. var view = this.chatboxviews.get('problematic@muc.localhost');
  196. view.closeChat();
  197. }, converse));
  198. it("will show an error message if the room requires a password", $.proxy(function () {
  199. var presence = $pres().attrs({
  200. from:'coven@chat.shakespeare.lit/thirdwitch',
  201. id:'n13mt3l',
  202. to:'hag66@shakespeare.lit/pda',
  203. type:'error'})
  204. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  205. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
  206. .c('not-authorized').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  207. var view = this.chatboxviews.get('problematic@muc.localhost');
  208. spyOn(view, 'renderPasswordForm').andCallThrough();
  209. runs(function () {
  210. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  211. });
  212. waits(250);
  213. runs(function () {
  214. var $chat_body = view.$el.find('.chat-body');
  215. expect(view.renderPasswordForm).toHaveBeenCalled();
  216. expect($chat_body.find('form.chatroom-form').length).toBe(1);
  217. expect($chat_body.find('legend').text()).toBe('This chatroom requires a password');
  218. });
  219. }, converse));
  220. it("will show an error message if the room is members-only and the user not included", $.proxy(function () {
  221. var presence = $pres().attrs({
  222. from:'coven@chat.shakespeare.lit/thirdwitch',
  223. id:'n13mt3l',
  224. to:'hag66@shakespeare.lit/pda',
  225. type:'error'})
  226. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  227. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
  228. .c('registration-required').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  229. var view = this.chatboxviews.get('problematic@muc.localhost');
  230. spyOn(view, 'showErrorMessage').andCallThrough();
  231. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  232. expect(view.$el.find('.chat-body p').text()).toBe('You are not on the member list of this room');
  233. }, converse));
  234. it("will show an error message if the user has been banned", $.proxy(function () {
  235. var presence = $pres().attrs({
  236. from:'coven@chat.shakespeare.lit/thirdwitch',
  237. id:'n13mt3l',
  238. to:'hag66@shakespeare.lit/pda',
  239. type:'error'})
  240. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  241. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'auth'})
  242. .c('forbidden').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  243. var view = this.chatboxviews.get('problematic@muc.localhost');
  244. spyOn(view, 'showErrorMessage').andCallThrough();
  245. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  246. expect(view.$el.find('.chat-body p').text()).toBe('You have been banned from this room');
  247. }, converse));
  248. it("will show an error message if no nickname was specified for the user", $.proxy(function () {
  249. var presence = $pres().attrs({
  250. from:'coven@chat.shakespeare.lit/thirdwitch',
  251. id:'n13mt3l',
  252. to:'hag66@shakespeare.lit/pda',
  253. type:'error'})
  254. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  255. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'modify'})
  256. .c('jid-malformed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  257. var view = this.chatboxviews.get('problematic@muc.localhost');
  258. spyOn(view, 'showErrorMessage').andCallThrough();
  259. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  260. expect(view.$el.find('.chat-body p').text()).toBe('No nickname was specified');
  261. }, converse));
  262. it("will show an error message if the user is not allowed to have created the room", $.proxy(function () {
  263. var presence = $pres().attrs({
  264. from:'coven@chat.shakespeare.lit/thirdwitch',
  265. id:'n13mt3l',
  266. to:'hag66@shakespeare.lit/pda',
  267. type:'error'})
  268. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  269. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  270. .c('not-allowed').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  271. var view = this.chatboxviews.get('problematic@muc.localhost');
  272. spyOn(view, 'showErrorMessage').andCallThrough();
  273. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  274. expect(view.$el.find('.chat-body p').text()).toBe('You are not allowed to create new rooms');
  275. }, converse));
  276. it("will show an error message if the user's nickname doesn't conform to room policy", $.proxy(function () {
  277. var presence = $pres().attrs({
  278. from:'coven@chat.shakespeare.lit/thirdwitch',
  279. id:'n13mt3l',
  280. to:'hag66@shakespeare.lit/pda',
  281. type:'error'})
  282. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  283. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  284. .c('not-acceptable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  285. var view = this.chatboxviews.get('problematic@muc.localhost');
  286. spyOn(view, 'showErrorMessage').andCallThrough();
  287. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  288. expect(view.$el.find('.chat-body p').text()).toBe("Your nickname doesn't conform to this room's policies");
  289. }, converse));
  290. it("will show an error message if the user's nickname is already taken", $.proxy(function () {
  291. var presence = $pres().attrs({
  292. from:'coven@chat.shakespeare.lit/thirdwitch',
  293. id:'n13mt3l',
  294. to:'hag66@shakespeare.lit/pda',
  295. type:'error'})
  296. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  297. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  298. .c('conflict').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  299. var view = this.chatboxviews.get('problematic@muc.localhost');
  300. spyOn(view, 'showErrorMessage').andCallThrough();
  301. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  302. expect(view.$el.find('.chat-body p').text()).toBe("Your nickname is already taken");
  303. }, converse));
  304. it("will show an error message if the room doesn't yet exist", $.proxy(function () {
  305. var presence = $pres().attrs({
  306. from:'coven@chat.shakespeare.lit/thirdwitch',
  307. id:'n13mt3l',
  308. to:'hag66@shakespeare.lit/pda',
  309. type:'error'})
  310. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  311. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  312. .c('item-not-found').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  313. var view = this.chatboxviews.get('problematic@muc.localhost');
  314. spyOn(view, 'showErrorMessage').andCallThrough();
  315. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  316. expect(view.$el.find('.chat-body p').text()).toBe("This room does not (yet) exist");
  317. }, converse));
  318. it("will show an error message if the room has reached it's maximum number of occupants", $.proxy(function () {
  319. var presence = $pres().attrs({
  320. from:'coven@chat.shakespeare.lit/thirdwitch',
  321. id:'n13mt3l',
  322. to:'hag66@shakespeare.lit/pda',
  323. type:'error'})
  324. .c('x').attrs({xmlns:'http://jabber.org/protocol/muc'}).up()
  325. .c('error').attrs({by:'coven@chat.shakespeare.lit', type:'cancel'})
  326. .c('service-unavailable').attrs({xmlns:'urn:ietf:params:xml:ns:xmpp-stanzas'}).nodeTree;
  327. var view = this.chatboxviews.get('problematic@muc.localhost');
  328. spyOn(view, 'showErrorMessage').andCallThrough();
  329. view.onChatRoomPresence(presence, {'nick': 'dummy'});
  330. expect(view.$el.find('.chat-body p').text()).toBe("This room has reached it's maximum number of occupants");
  331. }, converse));
  332. }, converse));
  333. }, converse, mock, utils));
  334. }));