2014-11-10 23:12:33 +02:00
|
|
|
/**
|
|
|
|
|
* Chat client controller tests
|
|
|
|
|
*/
|
2015-07-25 16:53:11 -04:00
|
|
|
(function () {
|
2015-12-30 02:34:54 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('ChatController', function () {
|
2015-12-10 20:31:51 +01:00
|
|
|
// Initialize global variables
|
2015-12-30 02:34:54 -05:00
|
|
|
var $scope,
|
2015-07-28 17:52:03 -06:00
|
|
|
Socket,
|
|
|
|
|
ChatController,
|
|
|
|
|
$timeout,
|
2015-12-30 02:34:54 -05:00
|
|
|
$state,
|
2016-10-05 17:51:53 -07:00
|
|
|
Authentication,
|
|
|
|
|
$httpBackend;
|
2015-07-28 17:52:03 -06:00
|
|
|
|
|
|
|
|
// Load the main application module
|
|
|
|
|
beforeEach(module(ApplicationConfiguration.applicationModuleName));
|
|
|
|
|
|
2015-12-30 02:34:54 -05:00
|
|
|
beforeEach(inject(function ($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$state_) {
|
|
|
|
|
$scope = $rootScope.$new();
|
2015-07-28 17:52:03 -06:00
|
|
|
Socket = _Socket_;
|
|
|
|
|
$timeout = _$timeout_;
|
2015-12-30 02:34:54 -05:00
|
|
|
$state = _$state_;
|
2015-07-28 17:52:03 -06:00
|
|
|
Authentication = _Authentication_;
|
|
|
|
|
}));
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('when user logged out', function () {
|
2015-12-30 02:34:54 -05:00
|
|
|
beforeEach(inject(function ($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$state_) {
|
2015-07-28 17:52:03 -06:00
|
|
|
Authentication.user = undefined;
|
2015-12-30 02:34:54 -05:00
|
|
|
spyOn($state, 'go');
|
|
|
|
|
ChatController = $controller('ChatController as vm', {
|
|
|
|
|
$scope: $scope
|
2015-07-28 17:52:03 -06:00
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
it('should redirect logged out user to /', function () {
|
2015-12-30 02:34:54 -05:00
|
|
|
expect($state.go).toHaveBeenCalledWith('home');
|
2015-07-28 17:52:03 -06:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('when user logged in', function () {
|
2016-10-05 17:51:53 -07:00
|
|
|
beforeEach(inject(function ($controller, $rootScope, _$httpBackend_, _Socket_, _Authentication_, _$timeout_, _$state_) {
|
2015-07-28 17:52:03 -06:00
|
|
|
Authentication.user = {
|
|
|
|
|
name: 'user',
|
|
|
|
|
roles: ['user']
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-05 17:51:53 -07:00
|
|
|
$httpBackend = _$httpBackend_;
|
|
|
|
|
|
|
|
|
|
// Ignore parent template get on state transitions
|
|
|
|
|
$httpBackend.whenGET('/modules/core/client/views/home.client.view.html').respond(200, '');
|
|
|
|
|
|
2015-12-30 02:34:54 -05:00
|
|
|
ChatController = $controller('ChatController as vm', {
|
|
|
|
|
$scope: $scope
|
2015-07-28 17:52:03 -06:00
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
it('should make sure socket is connected', function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
expect(Socket.socket).toBeTruthy();
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
it('should define messages array', function () {
|
2015-12-30 02:34:54 -05:00
|
|
|
expect($scope.vm.messages).toBeDefined();
|
|
|
|
|
expect($scope.vm.messages.length).toBe(0);
|
2015-07-28 17:52:03 -06:00
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('sendMessage', function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
var text = 'hello world!';
|
2015-07-25 16:53:11 -04:00
|
|
|
beforeEach(function () {
|
2015-12-30 02:34:54 -05:00
|
|
|
$scope.vm.messageText = text;
|
|
|
|
|
$scope.vm.sendMessage();
|
2015-07-28 17:52:03 -06:00
|
|
|
$timeout.flush();
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
it('should add message to messages', function () {
|
2015-12-30 02:34:54 -05:00
|
|
|
expect($scope.vm.messages.length).toBe(1);
|
2015-07-28 17:52:03 -06:00
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
it('should add message with proper text attribute set', function () {
|
2015-12-30 02:34:54 -05:00
|
|
|
expect($scope.vm.messages[0].text).toBe(text);
|
2015-07-28 17:52:03 -06:00
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
it('should clear messageText', function () {
|
2015-12-30 02:34:54 -05:00
|
|
|
expect($scope.vm.messageText).toBe('');
|
2015-07-28 17:52:03 -06:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
describe('$destroy()', function () {
|
|
|
|
|
beforeEach(function () {
|
2015-12-30 02:34:54 -05:00
|
|
|
$scope.$destroy();
|
2015-07-28 17:52:03 -06:00
|
|
|
});
|
|
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
it('should remove chatMessage listener', function () {
|
2015-07-28 17:52:03 -06:00
|
|
|
expect(Socket.socket.cbs.chatMessage).toBeUndefined();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-12-10 20:31:51 +01:00
|
|
|
}());
|