Skip to content

Commit

Permalink
Refactor MockRouter
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonStoltz committed Feb 1, 2021
1 parent fd0809b commit db7e587
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions x-pack/plugins/enterprise_search/server/__mocks__/router.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ export class MockRouter {
};

public callRoute = async (request: MockRouterRequest) => {
const routerCalls = this.router[this.method].mock.calls as any[];
if (!routerCalls.length) throw new Error('No routes registered.');

const route = routerCalls.find(([router]: any) => router.path === this.path);
if (!route) throw new Error('No matching registered routes found - check method/path keys');

const route = this.findRouteRegistration();
const [, handler] = route;
const context = {} as jest.Mocked<RequestHandlerContext>;
await handler(context, httpServerMock.createKibanaRequest(request as any), this.response);
Expand All @@ -68,14 +63,8 @@ export class MockRouter {
public validateRoute = (request: MockRouterRequest) => {
if (!this.payload) throw new Error('Cannot validate wihout a payload type specified.');

// Ignoring a TS error here because it's tricky to resolve and unimportant to fix
// @ts-ignore
const configForMethodAndPath = this.router[this.method].mock.calls.find(
([config]: Array<{ path: string }>) => config.path === this.path
);
if (!configForMethodAndPath)
throw new Error(`No route registered for ${this.method} & ${this.path}`);
const [config] = configForMethodAndPath;
const route = this.findRouteRegistration();
const [config] = route;
const validate = config.validate as RouteValidatorConfig<{}, {}, {}>;

const payloadValidation = validate[this.payload] as { validate(request: KibanaRequest): void };
Expand All @@ -91,6 +80,16 @@ export class MockRouter {
public shouldThrow = (request: MockRouterRequest) => {
expect(() => this.validateRoute(request)).toThrow();
};

findRouteRegistration = () => {
const routerCalls = this.router[this.method].mock.calls as any[];
if (!routerCalls.length) throw new Error('No routes registered.');

const route = routerCalls.find(([router]: any) => router.path === this.path);
if (!route) throw new Error('No matching registered routes found - check method/path keys');

return route;
};
}

/**
Expand Down

0 comments on commit db7e587

Please sign in to comment.