Skip to content

Commit

Permalink
Update tests to match previous bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
przucidlo committed Apr 27, 2021
1 parent e769f20 commit 0a2176d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
20 changes: 18 additions & 2 deletions test/unit/controllers/controllers-registrator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { ControllersRegistrator } from '../../../lib/controllers/controllers-registrator';
import { HandlerParamsMapper } from '../../../lib/controllers/handler-params-mapper';
import { RequestExecutor } from '../../../lib/controllers/request-executor';
import { RequestExecutorInjector } from '../../../lib/controllers/request-executor-injector';
import { ControllerMetadata } from '../../../lib/interfaces/controller-metadata.interface';
import { IpcTransport } from '../../../lib/interfaces/ipc-transport.interface';
import { getMockBrokerEventData } from '../__mocks__/get-mock-broker-event-data';
import { getMockIpcTransport } from '../__mocks__/get-mock-ipc-transport';
import { getMockTestControllerMetadata, MOCK_TEST_CONTROLLER_PATTERN } from '../__mocks__/mock-test-controller';
import { getMockExecutionContext } from './__mocks__/get-mock-execution-context';

describe('ControllersRegistrator', () => {
let requestExecutorInjector: RequestExecutorInjector;
Expand All @@ -12,7 +16,17 @@ describe('ControllersRegistrator', () => {
let ipcTransport: IpcTransport;

beforeEach(() => {
requestExecutorInjector = <RequestExecutorInjector>(<unknown>{ injectIntoControllers: jest.fn() });
const executionContext = getMockExecutionContext(
getMockTestControllerMetadata(),
MOCK_TEST_CONTROLLER_PATTERN,
getMockBrokerEventData(),
);
const requestExecutor = new RequestExecutor([], [], () => <any>{}, <HandlerParamsMapper>{});

requestExecutorInjector = new RequestExecutorInjector(
() => executionContext,
() => requestExecutor,
);
ipcTransport = getMockIpcTransport();
controllerMetadata = getMockTestControllerMetadata();

Expand All @@ -23,9 +37,11 @@ describe('ControllersRegistrator', () => {

describe('register', () => {
it('Should inject request executor into handlers', () => {
const executorInjectorSpy = jest.spyOn(requestExecutorInjector, 'injectIntoControllersHandlers');

controllerRegistrator.register();

expect(requestExecutorInjector.injectIntoControllers).toBeCalled();
expect(executorInjectorSpy).toBeCalled();
});

it('Should register handlers in IpcTransport', () => {
Expand Down
14 changes: 6 additions & 8 deletions test/unit/controllers/request-executor-injector.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ControllerMetadata } from '../../../lib/interfaces/controller-metadata.
import { getMockBrokerEventData } from '../__mocks__/get-mock-broker-event-data';
import { getMockTestControllerMetadata, MOCK_TEST_CONTROLLER_PATTERN } from '../__mocks__/mock-test-controller';
import { ControllerHandlerMetadata } from '../../../lib/interfaces/controller-handler-metadata.interface';
import { getMockExecutionContext } from './__mocks__/get-mock-execution-context';

describe('RequestExecutorInjector', () => {
let handlerMetadata: ControllerHandlerMetadata;
Expand All @@ -20,10 +21,7 @@ describe('RequestExecutorInjector', () => {
handlerMetadata = controllerMetadata.messageHandlers[MOCK_TEST_CONTROLLER_PATTERN];
mockEventData = getMockBrokerEventData();

executionContext = new ExecutionContext(
{ controller: handlerMetadata.controller, handler: handlerMetadata.handler, middleware: [], paramsMetadata: [] },
mockEventData,
);
executionContext = getMockExecutionContext(controllerMetadata, MOCK_TEST_CONTROLLER_PATTERN, mockEventData);

requestExecutor = Object.create(RequestExecutor);
requestExecutor.executeRequest = jest.fn();
Expand All @@ -36,19 +34,19 @@ describe('RequestExecutorInjector', () => {

describe('inject', () => {
it('Should wrap message handlers with request executor', () => {
requestInjector.injectIntoControllers([controllerMetadata]);
const messageHandlers = requestInjector.injectIntoControllersHandlers([controllerMetadata]);

handlerMetadata.handler(mockEventData);
messageHandlers[0].handler(mockEventData);

expect(requestExecutor.executeRequest).toBeCalled();
});

it('Should make the controller accept only the requests', () => {
requestInjector.injectIntoControllers([controllerMetadata]);
const messageHandlers = requestInjector.injectIntoControllersHandlers([controllerMetadata]);

mockEventData.type = 'RESPONSE';

handlerMetadata.handler(mockEventData);
messageHandlers[0].handler(mockEventData);

expect(requestExecutor.executeRequest).not.toBeCalled();
});
Expand Down
4 changes: 3 additions & 1 deletion test/unit/modes/mocks/mock-controllers-registrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { getMockIpcTransport } from '../../__mocks__/get-mock-ipc-transport';
import { getMockTestControllerMetadata } from '../../__mocks__/mock-test-controller';

export function getMockControllersRegistrator(): ControllersRegistrator {
const requestExecutorInjector = <RequestExecutorInjector>(<unknown>{ injectIntoControllers: jest.fn() });
const requestExecutorInjector = <RequestExecutorInjector>(
(<unknown>{ injectIntoControllersHandlers: jest.fn().mockReturnValue([]) })
);

return new ControllersRegistrator(requestExecutorInjector, getMockIpcTransport(), () => [
getMockTestControllerMetadata(),
Expand Down

0 comments on commit 0a2176d

Please sign in to comment.