test(hintCtrl): add test coverage for hintCtrl functions

Add tests that demonstrate the functionality of hintCtrl formatting functions.
These tests mock the hintService to test the operation of hintCtrl
functions for splitting and using received messages.
test-unit-sauce
Erin Altenhof-Long 10 years ago
parent c432fb6930
commit 03bef161b5

@ -15,7 +15,7 @@
</head>
<body>
<div ng-controller="HintCtrl" >
<div ng-controller="HintController" >
<div class="row" style="padding:10px">
<div class="col-md-12">
<ul class="nav nav-tabs">

@ -1,16 +1,29 @@
angular.module('ngHintUI')
.controller('HintCtrl', ['$scope', '$timeout', 'hintService',
function($scope, $timeout, hintService) {
.controller('HintController', ['$scope', 'hintService',
function($scope, hintService) {
//Set the hint service to perform this action when the page is refreshed
hintService.setRefreshFunction(function() {
$scope.messageData = {
'All' : {
'Error Messages': [],
'Warning Messages': [],
'Suggestion Messages': [],
'All Messages': []
}
};
});
//Set the hint service to perform this action whenever
//a new hint message is received.
hintService.setHintFunction(function(msg) {
//If there is no scope data, initialize a new data object
$scope.messageData = $scope.messageData || {
'All' : {
'All Messages': [],
'Error Messages': [],
'Warning Messages': [],
'Suggestion Messages': []
'Suggestion Messages': [],
'All Messages': []
}
};
@ -40,6 +53,17 @@ angular.module('ngHintUI')
$scope.module, $scope.type, $scope.suppressedMessages = {}, $scope.suppressedMessagesLength = 0;
$scope.labels = ['All Messages', 'Error Messages', 'Warning Messages', 'Suggestion Messages'];
$scope.setModule = function(module) {
$scope.module = module;
};
$scope.setType = function(type) {
$scope.type = type;
};
$scope.setModule('All');
$scope.setType('All Messages');
$scope.isSuppressed = function(message) {
message = message.split(' ').slice(6,9).join('');
return message in $scope.suppressedMessages;
@ -60,16 +84,4 @@ angular.module('ngHintUI')
$scope.suppressedMessagesLength--;
delete $scope.suppressedMessages[message];
}
$scope.setModule = function(module) {
$scope.module = module;
};
$scope.setType = function(type) {
$scope.type = type;
};
$scope.setModule('All');
$scope.setType('All Messages');
}]);

@ -1,6 +1,6 @@
angular.module('ngHintUI').
service('hintService', function() {
var onHintFunction;
var onHintFunction, onRefreshFunction;
this.setHintFunction = function(hintFunction) {
onHintFunction = hintFunction;
@ -10,14 +10,18 @@ angular.module('ngHintUI').
return onHintFunction;
}
this.setRefreshFunction = function(refreshFunction) {
onRefreshFunction = refreshFunction;
}
this.getRefreshFunction = function() {
return onRefreshFunction;
}
var port = chrome.extension.connect();
port.postMessage(chrome.devtools.inspectedWindow.tabId);
port.onMessage.addListener(function(msg) {
if(msg == 'refresh') {
this.messageData = [];
return;
}
onHintFunction(msg);
msg == 'refresh' ? onRefreshFunction() : onHintFunction(msg);
});
port.onDisconnect.addListener(function (a) {

@ -37,4 +37,13 @@ describe('hintService', function() {
hintService.setHintFunction(onHintFunction);
expect(hintService.getHintFunction()).toEqual(onHintFunction);
});
it('should set the function to be executed on a refresh', function() {
var onRefreshFunction = function() {
console.log('Do this when the page is refreshed.');
};
hintService.setRefreshFunction(onRefreshFunction);
expect(hintService.getRefreshFunction()).toEqual(onRefreshFunction);
});
});

@ -0,0 +1,193 @@
describe('angularHint', function() {
var $controller, $rootScope, hintService;
beforeEach(module('ngHintUI'));
beforeEach(inject(function(_$controller_, _$rootScope_) {
$controller = _$controller_;
$rootScope = _$rootScope_;
}));
beforeEach(function() {
hintService = {
setHintFunction: function(funct) {
this.onHint = funct;
},
setRefreshFunction: function(funct) {
this.onRefresh = funct;
}
}
});
describe('on receiving a hint', function() {
it('should give the hintService onHint a helpful function to format messages', function() {
var scope = $rootScope.$new();
var ctrl = $controller('HintController', {$scope: scope, hintService: hintService});
expect(hintService.onHint.toString().indexOf("var result = msg.split('##')")).not.toEqual(-1);
});
it('should create message data arrays for each module', function() {
var scope = $rootScope.$new();
var ctrl = $controller('HintController', {$scope: scope, hintService: hintService});
var aFakeMessage = 'Directives##You spelled ng-repeat wrong!##Error Messages';
var aFakeMessage2 = 'Modules##You did not load a module!##Error Messages';
hintService.onHint(aFakeMessage);
hintService.onHint(aFakeMessage2);
var expectedMessageData = {
'Error Messages': ['You spelled ng-repeat wrong!'],
'Warning Messages': [],
'Suggestion Messages': [],
'All Messages': [{
'message': 'You spelled ng-repeat wrong!',
'type': 'Error Messages'
}]
};
var expectedMessageData2 = {
'Error Messages': ['You did not load a module!'],
'Warning Messages': [],
'Suggestion Messages': [],
'All Messages': [{
'message': 'You did not load a module!',
'type': 'Error Messages'
}]
};
expect(scope.messageData['Directives']).toEqual(expectedMessageData);
expect(scope.messageData['Modules']).toEqual(expectedMessageData2);
});
it('should create message data arrays for each type of message', function() {
var scope = $rootScope.$new();
var ctrl = $controller('HintController', {$scope: scope, hintService: hintService});
var aFakeErrorMessage = 'Modules##You did not load a module!##Error Messages';
var aFakeWarningMessage = 'Modules##You used a bad module name!##Warning Messages';
var aFakeSuggestionMessage = 'Modules##Maybe you should not make modules.##Suggestion Messages';
hintService.onHint(aFakeErrorMessage);
hintService.onHint(aFakeWarningMessage);
hintService.onHint(aFakeSuggestionMessage);
var expectedMessageData = {
'Error Messages': ['You did not load a module!'],
'Warning Messages': ['You used a bad module name!'],
'Suggestion Messages': ['Maybe you should not make modules.'],
'All Messages': [
{'message': 'You did not load a module!', 'type': 'Error Messages'},
{'message': 'You used a bad module name!', 'type': 'Warning Messages'},
{'message': 'Maybe you should not make modules.', 'type': 'Suggestion Messages'}
]
};
expect(scope.messageData['Modules']).toEqual(expectedMessageData);
});
it('should create an object to hold all recorded messages', function() {
var scope = $rootScope.$new();
var ctrl = $controller('HintController', {$scope: scope, hintService: hintService});
var aFakeErrorMessage = 'Modules##You did not load a module!##Error Messages';
var aFakeWarningMessage = 'Directives##Did you know you wrote ng-reepeet?##Warning Messages';
var aFakeSuggestionMessage = 'Controllers##Maybe you should use a better name.##Suggestion Messages';
hintService.onHint(aFakeErrorMessage);
hintService.onHint(aFakeWarningMessage);
hintService.onHint(aFakeSuggestionMessage);
var expectedAllMessagesObject = {
'Error Messages': ['You did not load a module!'],
'Warning Messages': ['Did you know you wrote ng-reepeet?'],
'Suggestion Messages': ['Maybe you should use a better name.'],
'All Messages': [
{'message': 'You did not load a module!', 'type': 'Error Messages', 'module': 'Modules'},
{'message': 'Did you know you wrote ng-reepeet?', 'type': 'Warning Messages', 'module': 'Directives'},
{'message': 'Maybe you should use a better name.', 'type': 'Suggestion Messages', 'module': 'Controllers'}
]
};
expect(scope.messageData['All']).toEqual(expectedAllMessagesObject);
});
});
describe('onRefresh', function() {
it('should use the hintService to clear the old messages on refresh', function() {
var scope = $rootScope.$new();
var ctrl = $controller('HintController', {$scope: scope, hintService: hintService});
var aFakeErrorMessage = 'Modules##You did not load a module!##Error Messages';
var aFakeWarningMessage = 'Directives##Did you know you wrote ng-reepeet?##Warning Messages';
var aFakeSuggestionMessage = 'Controllers##Maybe you should use a better name.##Suggestion Messages';
hintService.onHint(aFakeErrorMessage);
hintService.onHint(aFakeWarningMessage);
hintService.onHint(aFakeSuggestionMessage);
var expectedAllMessagesObject = {
'Error Messages': ['You did not load a module!'],
'Warning Messages': ['Did you know you wrote ng-reepeet?'],
'Suggestion Messages': ['Maybe you should use a better name.'],
'All Messages': [
{'message': 'You did not load a module!', 'type': 'Error Messages', 'module': 'Modules'},
{'message': 'Did you know you wrote ng-reepeet?', 'type': 'Warning Messages', 'module': 'Directives'},
{'message': 'Maybe you should use a better name.', 'type': 'Suggestion Messages', 'module': 'Controllers'}
]
};
expect(scope.messageData['All']).toEqual(expectedAllMessagesObject);
hintService.onRefresh();
var expectedRefreshData = {
'Error Messages': [],
'Warning Messages': [],
'Suggestion Messages': [],
'All Messages': []
}
expect(scope.messageData['All']).toEqual(expectedRefreshData);
});
});
describe('setModule', function() {
it('should to set the currently viewed module in the UI', function() {
var scope = $rootScope.$new();
var ctrl = $controller('HintController', {$scope: scope, hintService: hintService});
scope.setModule('Directives');
expect(scope.module).toEqual('Directives');
});
it('should be set to All by default', function() {
var scope = $rootScope.$new();
var ctrl = $controller('HintController', {$scope: scope, hintService: hintService});
expect(scope.module).toEqual('All');
});
});
describe('setType', function() {
it('should set the type of message being viewed in the UI', function() {
var scope = $rootScope.$new();
var ctrl = $controller('HintController', {$scope: scope, hintService: hintService});
scope.setType('Error Messages');
expect(scope.type).toEqual('Error Messages');
});
it('should be set to All Messages by default', function() {
var scope = $rootScope.$new();
var ctrl = $controller('HintController', {$scope: scope, hintService: hintService});
expect(scope.type).toEqual('All Messages');
});
});
//TO DO CARLOS WHO WROTE THESE METHODS
describe('message suppression', function() {
describe('isSuppressed', function() {
it('should detect if a message is currently suppressed', function() {
//TO DO
});
});
describe('suppressMessage', function() {
it('should put a message into the list of suppressedMessages', function() {
});
});
describe('unsuppressMessage', function() {
it('should remove a message from the list of suppressedMessages', function() {
});
});
});
});