refactor(hintService): move communication through developer tools to backend service
To improve testability, the backend communication with the developer tools is moved to a service that can then be mocked for testing purposes. Moving functionality to a service means that an app file is needed to create the module before the controller and the service and that the app, service, and controller should be contained in the application html page.test-unit-sauce
parent
0130c9dc8e
commit
952a4a4c73
@ -0,0 +1 @@
|
||||
angular.module('ngHintUI',[]);
|
@ -0,0 +1,26 @@
|
||||
angular.module('ngHintUI').
|
||||
service('hintService', function() {
|
||||
var onHintFunction;
|
||||
|
||||
this.setHintFunction = function(hintFunction) {
|
||||
onHintFunction = hintFunction;
|
||||
}
|
||||
|
||||
this.getHintFunction = function() {
|
||||
return onHintFunction;
|
||||
}
|
||||
|
||||
var port = chrome.extension.connect();
|
||||
port.postMessage(chrome.devtools.inspectedWindow.tabId);
|
||||
port.onMessage.addListener(function(msg) {
|
||||
if(msg == 'refresh') {
|
||||
this.messageData = [];
|
||||
return;
|
||||
}
|
||||
onHintFunction(msg);
|
||||
});
|
||||
|
||||
port.onDisconnect.addListener(function (a) {
|
||||
console.log(a);
|
||||
});
|
||||
});
|
@ -0,0 +1,40 @@
|
||||
describe('hintService', function() {
|
||||
var hintService;
|
||||
|
||||
beforeEach(module('ngHintUI'));
|
||||
beforeEach(inject(function(_hintService_) {
|
||||
hintService = _hintService_;
|
||||
}));
|
||||
|
||||
var messageFunction = {
|
||||
addListener: jasmine.createSpy('messageFunction')
|
||||
}
|
||||
var postMessageFunction = jasmine.createSpy('postMessageFunction');
|
||||
var onDisconnectFunction = {
|
||||
addListener: jasmine.createSpy('onDisconnect')
|
||||
}
|
||||
chrome = {
|
||||
extension: {
|
||||
connect: function() {
|
||||
return {
|
||||
onMessage: messageFunction,
|
||||
postMessage: postMessageFunction,
|
||||
onDisconnect: onDisconnectFunction
|
||||
};
|
||||
}
|
||||
},
|
||||
devtools: {
|
||||
inspectedWindow: {
|
||||
tabId: 1
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
it('should set the function to be executed for each hint', function() {
|
||||
var onHintFunction = function() {
|
||||
console.log('Do this when passed a hint.');
|
||||
};
|
||||
hintService.setHintFunction(onHintFunction);
|
||||
expect(hintService.getHintFunction()).toEqual(onHintFunction);
|
||||
});
|
||||
});
|
Reference in New Issue