test(inspectedApp): add tests for messaging

test-unit-sauce
Brian Ford 10 years ago
parent d3e7bd6ec4
commit d7008a1368

@ -77,10 +77,9 @@ function inspectedAppService($rootScope, $q) {
if (hint.message) { if (hint.message) {
hints.push(hint); hints.push(hint);
} else if (hint.event) { } else if (hint.event) {
if (hint.id) {
if (hint.event === 'scope:new') { if (hint.event === 'scope:new') {
addNewScope(hint); addNewScope(hint);
} else if (scopes[hint.id]) { } else if (hint.id && scopes[hint.id]) {
if (hint.event === 'model:change') { if (hint.event === 'model:change') {
scopes[hint.id].models[hint.path] = (typeof hint.value === 'undefined') ? scopes[hint.id].models[hint.path] = (typeof hint.value === 'undefined') ?
undefined : JSON.parse(hint.value); undefined : JSON.parse(hint.value);
@ -88,7 +87,6 @@ function inspectedAppService($rootScope, $q) {
scopes[hint.id].descriptor = hint.descriptor; scopes[hint.id].descriptor = hint.descriptor;
} }
} }
}
$rootScope.$broadcast(hint.event, hint); $rootScope.$broadcast(hint.event, hint);
} }
} }

@ -1,13 +1,45 @@
'use strict';
describe('inspectedApp', function() { describe('inspectedApp', function() {
var inspectedApp; var inspectedApp, port;
beforeEach(module('batarang.inspected-app'));
beforeEach(function() { beforeEach(function() {
module('batarang.inspected-app')
window.chrome = createMockChrome(); window.chrome = createMockChrome();
}); inject(function(_inspectedApp_) {
beforeEach(inject(function(_inspectedApp_) {
inspectedApp = _inspectedApp_; inspectedApp = _inspectedApp_;
});
});
describe('when instantiated', function () {
it('should post a message with the inspected tabId', function () {
expect(port.postMessage).
toHaveBeenCalledWith(window.chrome.devtools.inspectedWindow.tabId);
});
});
describe('messaging', function () {
it('should track hints', inject(function ($browser) {
port.onMessage.trigger(JSON.stringify({ message: 'hi' }));
$browser.defer.flush();
expect(inspectedApp.hints).toEqual([{message: 'hi'}]);
}));
it('should track new scopes', inject(function ($browser) {
port.onMessage.trigger(JSON.stringify({ event: 'scope:new', child: 1 }));
$browser.defer.flush();
expect(inspectedApp.scopes).toEqual({ 1: { parent: undefined, children: [], models: {} } });
}));
it('should track updates to scope descriptors', inject(function ($browser) {
port.onMessage.trigger(JSON.stringify({ event: 'scope:new', child: 1 }));
port.onMessage.trigger(JSON.stringify({ event: 'scope:link', id: 1, descriptor: 'pasta' }));
$browser.defer.flush();
expect(inspectedApp.scopes[1].descriptor).toBe('pasta');
})); }));
})
describe('watch', function () { describe('watch', function () {
it('should call chrome devtools APIs', function() { it('should call chrome devtools APIs', function() {
@ -23,12 +55,20 @@ describe('inspectedApp', function() {
}); });
}); });
describe('inspectScope', function () {
it('should call chrome devtools APIs', function() {
inspectedApp.inspectScope(2);
expect(chrome.devtools.inspectedWindow.eval).toHaveBeenCalledWith('angular.hint.inspectScope(2,"")');
});
}); });
function createMockChrome() { function createMockChrome() {
return { return {
extension: { extension: {
connect: createMockSocket connect: function () {
return port = createMockSocket();
}
}, },
devtools: { devtools: {
inspectedWindow: { inspectedWindow: {
@ -38,11 +78,28 @@ function createMockChrome() {
} }
}; };
} }
});
function createListenerSpy(name) { function createListenerSpy(name) {
return { var symbol = '_' + name;
addListener: jasmine.createSpy(name)
var listener = {
addListener: function (fn) {
listener[symbol].push(fn);
},
removeListener: function (fn) {
listener[symbol].splice(fn, 1);
},
trigger: function () {
var args = arguments;
listener[symbol].forEach(function (fn) {
fn.apply(listener, args);
});
}
}; };
listener[symbol] = [];
return listener;
} }
function createMockSocket() { function createMockSocket() {