Files
meanTorrent/modules/chat/tests/client/chat.client.controller.tests.js

101 lines
2.9 KiB
JavaScript
Raw Normal View History

2014-11-10 23:12:33 +02:00
/**
* Chat client controller tests
*/
(function () {
'use strict';
describe('ChatController', function () {
// Initialize global variables
var $scope,
2015-07-28 17:52:03 -06:00
Socket,
ChatController,
$timeout,
$state,
Authentication,
$httpBackend;
2015-07-28 17:52:03 -06:00
// Load the main application module
beforeEach(module(ApplicationConfiguration.applicationModuleName));
beforeEach(inject(function ($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$state_) {
$scope = $rootScope.$new();
2015-07-28 17:52:03 -06:00
Socket = _Socket_;
$timeout = _$timeout_;
$state = _$state_;
2015-07-28 17:52:03 -06:00
Authentication = _Authentication_;
}));
describe('when user logged out', function () {
beforeEach(inject(function ($controller, $rootScope, _Socket_, _Authentication_, _$timeout_, _$state_) {
2015-07-28 17:52:03 -06:00
Authentication.user = undefined;
spyOn($state, 'go');
ChatController = $controller('ChatController as vm', {
$scope: $scope
2015-07-28 17:52:03 -06:00
});
}));
it('should redirect logged out user to /', function () {
expect($state.go).toHaveBeenCalledWith('home');
2015-07-28 17:52:03 -06:00
});
});
describe('when user logged in', function () {
beforeEach(inject(function ($controller, $rootScope, _$httpBackend_, _Socket_, _Authentication_, _$timeout_, _$state_) {
2015-07-28 17:52:03 -06:00
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
2015-07-28 17:52:03 -06:00
});
}));
it('should make sure socket is connected', function () {
2015-07-28 17:52:03 -06:00
expect(Socket.socket).toBeTruthy();
});
it('should define messages array', function () {
expect($scope.vm.messages).toBeDefined();
expect($scope.vm.messages.length).toBe(0);
2015-07-28 17:52:03 -06:00
});
describe('sendMessage', function () {
2015-07-28 17:52:03 -06:00
var text = 'hello world!';
beforeEach(function () {
$scope.vm.messageText = text;
$scope.vm.sendMessage();
2015-07-28 17:52:03 -06:00
$timeout.flush();
});
it('should add message to messages', function () {
expect($scope.vm.messages.length).toBe(1);
2015-07-28 17:52:03 -06:00
});
it('should add message with proper text attribute set', function () {
expect($scope.vm.messages[0].text).toBe(text);
2015-07-28 17:52:03 -06:00
});
it('should clear messageText', function () {
expect($scope.vm.messageText).toBe('');
2015-07-28 17:52:03 -06:00
});
});
describe('$destroy()', function () {
beforeEach(function () {
$scope.$destroy();
2015-07-28 17:52:03 -06:00
});
it('should remove chatMessage listener', function () {
2015-07-28 17:52:03 -06:00
expect(Socket.socket.cbs.chatMessage).toBeUndefined();
});
});
});
});
}());