From d7008a13688f99f14a78463e559d22147b224d82 Mon Sep 17 00:00:00 2001 From: Brian Ford Date: Mon, 15 Dec 2014 10:42:39 -0800 Subject: [PATCH] test(inspectedApp): add tests for messaging --- .../components/inspected-app/inspected-app.js | 18 ++-- .../inspected-app/inspected-app.spec.js | 97 +++++++++++++++---- 2 files changed, 85 insertions(+), 30 deletions(-) diff --git a/panel/components/inspected-app/inspected-app.js b/panel/components/inspected-app/inspected-app.js index 16fcb35..baf1e44 100644 --- a/panel/components/inspected-app/inspected-app.js +++ b/panel/components/inspected-app/inspected-app.js @@ -77,16 +77,14 @@ function inspectedAppService($rootScope, $q) { if (hint.message) { hints.push(hint); } else if (hint.event) { - if (hint.id) { - if (hint.event === 'scope:new') { - addNewScope(hint); - } else if (scopes[hint.id]) { - if (hint.event === 'model:change') { - scopes[hint.id].models[hint.path] = (typeof hint.value === 'undefined') ? - undefined : JSON.parse(hint.value); - } else if (hint.event === 'scope:link') { - scopes[hint.id].descriptor = hint.descriptor; - } + if (hint.event === 'scope:new') { + addNewScope(hint); + } else if (hint.id && scopes[hint.id]) { + if (hint.event === 'model:change') { + scopes[hint.id].models[hint.path] = (typeof hint.value === 'undefined') ? + undefined : JSON.parse(hint.value); + } else if (hint.event === 'scope:link') { + scopes[hint.id].descriptor = hint.descriptor; } } $rootScope.$broadcast(hint.event, hint); diff --git a/panel/components/inspected-app/inspected-app.spec.js b/panel/components/inspected-app/inspected-app.spec.js index 83db298..448d3db 100644 --- a/panel/components/inspected-app/inspected-app.spec.js +++ b/panel/components/inspected-app/inspected-app.spec.js @@ -1,13 +1,45 @@ +'use strict'; + describe('inspectedApp', function() { - var inspectedApp; + var inspectedApp, port; - beforeEach(module('batarang.inspected-app')); beforeEach(function() { + module('batarang.inspected-app') window.chrome = createMockChrome(); + inject(function(_inspectedApp_) { + inspectedApp = _inspectedApp_; + }); }); - beforeEach(inject(function(_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 () { it('should call chrome devtools APIs', function() { @@ -23,26 +55,51 @@ 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() { + return { + extension: { + connect: function () { + return port = createMockSocket(); + } + }, + devtools: { + inspectedWindow: { + tabId: 1, + eval: jasmine.createSpy('inspectedWindowEval') + } + } + }; + } }); -function createMockChrome() { - return { - extension: { - connect: createMockSocket +function createListenerSpy(name) { + var symbol = '_' + name; + + var listener = { + addListener: function (fn) { + listener[symbol].push(fn); }, - devtools: { - inspectedWindow: { - tabId: 1, - eval: jasmine.createSpy('inspectedWindowEval') - } + removeListener: function (fn) { + listener[symbol].splice(fn, 1); + }, + trigger: function () { + var args = arguments; + listener[symbol].forEach(function (fn) { + fn.apply(listener, args); + }); } }; -} -function createListenerSpy(name) { - return { - addListener: jasmine.createSpy(name) - }; + listener[symbol] = []; + return listener; } function createMockSocket() { @@ -51,4 +108,4 @@ function createMockSocket() { postMessage: jasmine.createSpy('postMessageFunction'), onDisconnect: createListenerSpy('onDisconnect') }; -} \ No newline at end of file +}