From 3d24711cceeedd08f065aa0e371622a21debff75 Mon Sep 17 00:00:00 2001 From: Rob Date: Tue, 27 Oct 2015 09:30:27 -0700 Subject: [PATCH] One way to control the mock instance so it can emit our events --- test/webkit/webKitDebugAdapter.test.ts | 62 +++++++++++++++++++++----- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/test/webkit/webKitDebugAdapter.test.ts b/test/webkit/webKitDebugAdapter.test.ts index e38375b..9403fe2 100644 --- a/test/webkit/webKitDebugAdapter.test.ts +++ b/test/webkit/webKitDebugAdapter.test.ts @@ -6,11 +6,15 @@ import * as mockery from 'mockery'; import * as assert from 'assert'; import * as testUtils from '../testUtils'; +import {EventEmitter} from 'events'; + /** Utilities without mocks - use for type only */ import {WebKitDebugAdapter as _WebKitDebugAdapter} from '../../webkit/webKitDebugAdapter'; const MODULE_UNDER_TEST = '../../webkit/webKitDebugAdapter'; suite('WebKitDebugAdapter', () => { + let mockEventEmitter: EventEmitter; + setup(() => { testUtils.setupUnhandledRejectionListener(); mockery.enable({ useCleanCache: true, warnOnReplace: false }); @@ -26,7 +30,7 @@ suite('WebKitDebugAdapter', () => { './v8Protocol', 'events']); - registerMockWebKitConnection(); + mockEventEmitter = registerMockWebKitConnection(); mockery.registerMock('child_process', { }); mockery.registerMock('url', { }); mockery.registerMock('path', { }); @@ -53,7 +57,7 @@ suite('WebKitDebugAdapter', () => { } }); - return wkda.attach({ address: 'localhost', 'port': 9222 }).then(() => { + return attach(wkda).then(() => { if (!initializedFired) { assert.fail('Attach completed without firing the initialized event'); } @@ -70,35 +74,71 @@ suite('WebKitDebugAdapter', () => { assert.fail('Not expecting any event in this scenario'); }); - return wkda.attach({ address: 'localhost', 'port': 9222 }).then( + return attach(wkda).then( () => assert.fail('Expecting promise to be rejected'), e => done()); }); }); + + suite('setBreakpoints()', () => { + test('works', () => { + const wkda = instantiateWKDA(); + return attach(wkda).then(() => { + mockEventEmitter.emit('Debugger.scriptParsed', { id: "id", url: "a.js" }); + wkda.setBreakpoints({ source: { path: "a.js" }, lines: [1] }); + }); + }); + }); + + suite('launch()', () => { }); + suite('setExceptionBreakpoints()', () => { }); + suite('stepping', () => { }); + suite('stackTrace()', () => { }); + suite('scopes()', () => { }); + suite('variables()', () => { }); + suite('source()', () => { }); + suite('threads()', () => { }); + suite('evaluate()', () => { }); + + suite('Debugger.resume', () => { }); + suite('Debugger.pause', () => { }); + suite('target close/error/detach', () => { }); }); -class DefaultMockWebKitConnection { - public on(eventName: string, handler: (msg: any) => void): void { - } +function attach(wkda: _WebKitDebugAdapter): Promise { + return wkda.attach({ address: 'localhost', 'port': 9222 }); +} +class DefaultMockWebKitConnection { public attach(port: number): Promise { return Promise.resolve(); } } -function registerMockWebKitConnection(partialImpl?: any): void { - const mock = () => { }; +/** + * Registers a mock WebKitConnection based off the above default impl, patched when whatever is in partialImpl + */ +function registerMockWebKitConnection(partialImpl?: any): EventEmitter { + const mockType = () => { }; Object.getOwnPropertyNames(DefaultMockWebKitConnection).forEach(name => { - mock[name] = DefaultMockWebKitConnection[name]; + mockType[name] = DefaultMockWebKitConnection[name]; }); if (partialImpl) { Object.getOwnPropertyNames(partialImpl).forEach(name => { - mock[name] = partialImpl[name]; + mockType[name] = partialImpl[name]; }); } - mockery.registerMock('./webKitConnection', { WebKitConnection: mock }); + // Instantiate the mock so we can inject an event emitter to simulate events from the WebKitConnection + const mockInstance = new mockType(); + const ee = new EventEmitter(); + mockInstance['on'] = ee.on.bind(ee); + + // Register a fake constructor so that our instance will be called when the adapter does 'new WebKitConnection' + mockery.registerMock('./webKitConnection', { WebKitConnection: () => mockInstance }); + + return ee; } function instantiateWKDA(): _WebKitDebugAdapter {