UNPKG

4.94 kBJavaScriptView Raw
1var sinon = require('sinon');
2var ChatController = require('../../src/js/controllers/chatController');
3var Conversations = require('../../src/js/collections/conversations');
4var Conversation = require('../../src/js/models/conversation');
5var vent = require('../../src/js/vent');
6var AppUser = require('../../src/js/models/appUser');
7
8var ClientScenario = require('../scenarios/clientScenario');
9
10describe('ChatController', function() {
11 var scenario;
12 var sandbox;
13 var chatController;
14 var conversations;
15 var user;
16 var getConversationSpy;
17 var initFayeSpy;
18 var initMessagingBusSpy;
19 var manageUnreadSpy;
20 var receiveSpy;
21 var renderWidgetSpy;
22 var initConversationSpy;
23
24 before(function() {
25 scenario = new ClientScenario();
26 scenario.build();
27
28 sandbox = sinon.sandbox.create();
29 });
30
31 after(function() {
32 scenario.clean();
33 });
34
35 beforeEach(function(done) {
36 conversations = new Conversations();
37 user = new AppUser({
38 givenName: 'test',
39 surname: 'user',
40 id: '12345'
41 });
42
43 sandbox.stub(user, 'save', function(attributes, options) {
44 return this._save(attributes, options);
45 });
46
47 chatController = new ChatController({
48 collection: conversations,
49 user: user
50 });
51
52 getConversationSpy = sandbox.spy(chatController, '_getConversation');
53 initFayeSpy = sandbox.stub(chatController, '_initFaye', function(conversation) {
54 chatController._fayeClient = {disconnect: function(){}};
55 return $.Deferred().resolve(conversation);
56 });
57 initMessagingBusSpy = sandbox.spy(chatController, '_initMessagingBus');
58 manageUnreadSpy = sandbox.spy(chatController, '_manageUnread');
59 renderWidgetSpy = sandbox.spy(chatController, '_renderWidget');
60 receiveSpy = sandbox.spy(chatController, '_receiveMessage');
61 initConversationSpy = sandbox.spy(chatController, '_initConversation');
62
63 chatController.getWidget().then(function() {
64 done();
65 });
66 });
67
68 afterEach(function() {
69 sandbox.restore();
70
71 chatController.destroy();
72 conversations.reset();
73 });
74
75 describe('#getWidget', function() {
76 it('should trigger the init chain', function() {
77 getConversationSpy.should.have.been.calledOnce;
78 initFayeSpy.should.have.been.calledOnce;
79 initMessagingBusSpy.should.have.been.calledOnce;
80 manageUnreadSpy.should.have.been.calledOnce;
81 renderWidgetSpy.should.have.been.calledOnce;
82 initConversationSpy.should.have.been.calledOnce;
83 });
84 });
85
86 describe('#sendMessage on existing conversation', function() {
87 var message = 'Hey!';
88 var messages;
89 var initialLength;
90
91 it('should add a message to the conversation', function(done) {
92 messages = chatController.conversation.get('messages');
93 initialLength = messages.length;
94
95 chatController.sendMessage(message).then(function() {
96 messages.length.should.equals(initialLength + 1);
97 messages.last().get('text').should.equals(message);
98 done();
99 });
100 });
101 });
102
103 describe('#sendMessage on new conversation', function() {
104 var message = 'Hey!';
105 var messages;
106 var initialLength;
107
108 beforeEach(function() {
109 chatController.conversation = new Conversation();
110 });
111
112 it('should add a message to the conversation', function(done) {
113 messages = chatController.conversation.get('messages');
114 initialLength = messages.length;
115
116 chatController.sendMessage(message).then(function() {
117 messages.length.should.equals(initialLength + 1);
118 messages.last().get('text').should.equals(message);
119 done();
120 });
121 });
122 });
123
124 describe('#_receiveMessage', function() {
125 it('should add a message to the conversation', function() {
126
127 var message = {
128 authorId: 1,
129 text: 'Hey!'
130 };
131
132 var messages = chatController.conversation.get('messages');
133 var initialLength = messages.length;
134 chatController._receiveMessage(message);
135 messages.length.should.equals(initialLength + 1);
136 messages.last().get('text').should.equals(message.text);
137 });
138
139 });
140
141 describe('vent', function() {
142 var message = {
143 authorId: 1,
144 text: 'Hey!'
145 };
146
147 it('should trigger _receiveMessage', function() {
148 vent.trigger('receive:message', message);
149 receiveSpy.should.have.been.calledOnce;
150 receiveSpy.should.have.been.calledWith(message);
151 });
152 });
153
154});