From ba79ff1b95fac0039c226d04b8642574be1b06bb Mon Sep 17 00:00:00 2001 From: Maksim Sadym Date: Thu, 7 Oct 2021 21:39:31 +0000 Subject: [PATCH] minor change --- .../context/browsingContextProcessor.spec.ts | 84 ++++++++++--------- 1 file changed, 45 insertions(+), 39 deletions(-) diff --git a/src/bidiMapper/domains/context/browsingContextProcessor.spec.ts b/src/bidiMapper/domains/context/browsingContextProcessor.spec.ts index e39045345b..406b015392 100644 --- a/src/bidiMapper/domains/context/browsingContextProcessor.spec.ts +++ b/src/bidiMapper/domains/context/browsingContextProcessor.spec.ts @@ -31,38 +31,61 @@ import { BrowsingContext } from '../../bidiProtocolTypes'; describe('BrowsingContextProcessor', function () { let mockCdpServer: StubTransport; let browsingContextProcessor: BrowsingContextProcessor; + let cdpConnection: CdpConnection; + + const EVALUATOR_SCRIPT = 'EVALUATOR_SCRIPT'; + const NEW_CONTEXT_ID = 'NEW_CONTEXT_ID'; + const TARGET_ATTACHED_TO_TARGET_EVENT = { + method: 'Target.attachedToTarget', + params: { + sessionId: '_ANY_VALUE_', + targetInfo: { + targetId: NEW_CONTEXT_ID, + type: 'page', + title: '', + url: '', + attached: true, + canAccessOpener: false, + browserContextId: '_ANY_ANOTHER_VALUE_', + }, + waitingForDebugger: false, + }, + }; beforeEach(async function () { mockCdpServer = new StubTransport(); - - const onContextCreated = sinon.fake as unknown as ( - t: Context - ) => Promise; - const onContextDestroyed = sinon.fake as unknown as ( - t: Context - ) => Promise; - + cdpConnection = new CdpConnection(mockCdpServer); browsingContextProcessor = new BrowsingContextProcessor( - new CdpConnection(mockCdpServer), + cdpConnection, 'SELF_TARGET_ID', - onContextCreated, - onContextDestroyed, - 'EVALUATOR_SCRIPT' + sinon.fake as unknown as (t: Context) => Promise, + sinon.fake as unknown as (t: Context) => Promise, + EVALUATOR_SCRIPT ); // Actual `Context.create` logic involves several CDP calls, so mock it to avoid all the simulations. - Context.create = async ( - contextId: string, - cdpClient: CdpClient, - EVALUATOR_SCRIPT: string - ) => { - return sinon.createStubInstance(Context) as unknown as Context; - }; + Context.create = sinon.fake( + async (_1: string, _2: CdpClient, _3: string) => { + return sinon.createStubInstance(Context) as unknown as Context; + } + ); + }); + describe('handle events', async function () { + it('`Target.attachedToTarget` creates Context', async function () { + sinon.assert.notCalled(Context.create as sinon.SinonSpy); + await mockCdpServer.emulateIncomingMessage( + TARGET_ATTACHED_TO_TARGET_EVENT + ); + sinon.assert.calledOnceWithExactly( + Context.create as sinon.SinonSpy, + NEW_CONTEXT_ID, + sinon.match.any, + EVALUATOR_SCRIPT + ); + }); }); - describe('`process_browsingContext_create`', async function () { - const NEW_CONTEXT_ID = 'NEW_CONTEXT_ID'; - + describe('handle `process_browsingContext_create`', async function () { const BROWSING_CONTEXT_CREATE_COMMAND: BrowsingContext.BrowsingContextCreateCommand = { method: 'browsingContext.create', @@ -85,23 +108,6 @@ describe('BrowsingContextProcessor', function () { }, }; - const TARGET_ATTACHED_TO_TARGET_EVENT = { - method: 'Target.attachedToTarget', - params: { - sessionId: '_ANY_VALUE_', - targetInfo: { - targetId: NEW_CONTEXT_ID, - type: 'page', - title: '', - url: '', - attached: true, - canAccessOpener: false, - browserContextId: '_ANY_ANOTHER_VALUE_', - }, - waitingForDebugger: false, - }, - }; - it('Target.attachedToTarget before command finished', async function () { const createResultPromise = browsingContextProcessor.process_browsingContext_create(