forked from meanjs/mean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.client.controller.tests.js
More file actions
100 lines (83 loc) · 2.86 KB
/
Copy pathchat.client.controller.tests.js
File metadata and controls
100 lines (83 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Chat client controller tests
*/
(function () {
'use strict';
describe('ChatController', function () {
// Initialize global variables
var $scope,
Socket,
ChatController,
$timeout,
$state,
Authentication,
$httpBackend;
// Load the main application module
beforeEach(module(ApplicationConfiguration.applicationModuleName));
beforeEach(inject(function ($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$state_) {
$scope = $rootScope.$new();
Socket = _Socket_;
$timeout = _$timeout_;
$state = _$state_;
Authentication = _Authentication_;
}));
describe('when user logged out', function () {
beforeEach(inject(function ($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$state_) {
Authentication.user = undefined;
spyOn($state, 'go');
ChatController = $controller('ChatController as vm', {
$scope: $scope
});
}));
it('should redirect logged out user to /', function () {
expect($state.go).toHaveBeenCalledWith('home');
});
});
describe('when user logged in', function () {
beforeEach(inject(function ($controller, $rootScope, _$httpBackend_, _Socket_, _Authentication_, _$timeout_, _$state_) {
Authentication.user = {
name: 'user',
roles: ['user']
};
$httpBackend = _$httpBackend_;
// Ignore parent template get on state transitions
$httpBackend.whenGET('/modules/core/client/views/home.client.view.html').respond(200, '');
ChatController = $controller('ChatController as vm', {
$scope: $scope
});
}));
it('should make sure socket is connected', function () {
expect(Socket.socket).toBeTruthy();
});
it('should define messages array', function () {
expect($scope.vm.messages).toBeDefined();
expect($scope.vm.messages.length).toBe(0);
});
describe('sendMessage', function () {
var text = 'hello world!';
beforeEach(function () {
$scope.vm.messageText = text;
$scope.vm.sendMessage();
$timeout.flush();
});
it('should add message to messages', function () {
expect($scope.vm.messages.length).toBe(1);
});
it('should add message with proper text attribute set', function () {
expect($scope.vm.messages[0].text).toBe(text);
});
it('should clear messageText', function () {
expect($scope.vm.messageText).toBe('');
});
});
describe('$destroy()', function () {
beforeEach(function () {
$scope.$destroy();
});
it('should remove chatMessage listener', function () {
expect(Socket.socket.cbs.chatMessage).toBeUndefined();
});
});
});
});
}());