Skip to content

Commit

Permalink
[Enterprise Search] Fix/update MockRouter helper to return specific r…
Browse files Browse the repository at this point in the history
…outes/paths (#82682)

* Fix tests failing for route files that have more than 2 router registrations of the same method

- This fix allows us to specify the route call we're testing via a path param

* Update all existing uses of MockRouter to pass path param

* Add helpful error messaging

- e.g., in case a path gets typoed
  • Loading branch information
Constance authored Nov 5, 2020
1 parent 6437139 commit 4a8f426
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 25 deletions.
18 changes: 15 additions & 3 deletions x-pack/plugins/enterprise_search/server/__mocks__/router.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type PayloadType = 'params' | 'query' | 'body';

interface IMockRouterProps {
method: MethodType;
path: string;
payload?: PayloadType;
}
interface IMockRouterRequest {
Expand All @@ -33,12 +34,14 @@ type TMockRouterRequest = KibanaRequest | IMockRouterRequest;
export class MockRouter {
public router!: jest.Mocked<IRouter>;
public method: MethodType;
public path: string;
public payload?: PayloadType;
public response = httpServerMock.createResponseFactory();

constructor({ method, payload }: IMockRouterProps) {
constructor({ method, path, payload }: IMockRouterProps) {
this.createRouter();
this.method = method;
this.path = path;
this.payload = payload;
}

Expand All @@ -47,8 +50,13 @@ export class MockRouter {
};

public callRoute = async (request: TMockRouterRequest) => {
const [, handler] = this.router[this.method].mock.calls[0];
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 [, handler] = route;
const context = {} as jest.Mocked<RequestHandlerContext>;
await handler(context, httpServerMock.createKibanaRequest(request as any), this.response);
};
Expand Down Expand Up @@ -81,7 +89,11 @@ export class MockRouter {
/**
* Example usage:
*/
// const mockRouter = new MockRouter({ method: 'get', payload: 'body' });
// const mockRouter = new MockRouter({
// method: 'get',
// path: '/api/app_search/test',
// payload: 'body'
// });
//
// beforeEach(() => {
// jest.clearAllMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ describe('credentials routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'get', payload: 'query' });
mockRouter = new MockRouter({
method: 'get',
path: '/api/app_search/credentials',
payload: 'query',
});

registerCredentialsRoutes({
...mockDependencies,
Expand Down Expand Up @@ -46,7 +50,11 @@ describe('credentials routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'post', payload: 'body' });
mockRouter = new MockRouter({
method: 'post',
path: '/api/app_search/credentials',
payload: 'body',
});

registerCredentialsRoutes({
...mockDependencies,
Expand Down Expand Up @@ -155,7 +163,11 @@ describe('credentials routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'get', payload: 'query' });
mockRouter = new MockRouter({
method: 'get',
path: '/api/app_search/credentials/details',
payload: 'query',
});

registerCredentialsRoutes({
...mockDependencies,
Expand All @@ -175,7 +187,11 @@ describe('credentials routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'put', payload: 'body' });
mockRouter = new MockRouter({
method: 'put',
path: '/api/app_search/credentials/{name}',
payload: 'body',
});

registerCredentialsRoutes({
...mockDependencies,
Expand Down Expand Up @@ -292,7 +308,11 @@ describe('credentials routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'delete', payload: 'params' });
mockRouter = new MockRouter({
method: 'delete',
path: '/api/app_search/credentials/{name}',
payload: 'params',
});

registerCredentialsRoutes({
...mockDependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ describe('engine routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'get', payload: 'query' });
mockRouter = new MockRouter({
method: 'get',
path: '/api/app_search/engines',
payload: 'query',
});

registerEnginesRoute({
...mockDependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ describe('log settings routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'get' });
mockRouter = new MockRouter({
method: 'get',
path: '/api/app_search/log_settings',
});

registerSettingsRoutes({
...mockDependencies,
Expand All @@ -36,7 +39,11 @@ describe('log settings routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'put', payload: 'body' });
mockRouter = new MockRouter({
method: 'put',
path: '/api/app_search/log_settings',
payload: 'body',
});

registerSettingsRoutes({
...mockDependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ describe('Enterprise Search Config Data API', () => {
let mockRouter: MockRouter;

beforeEach(() => {
mockRouter = new MockRouter({ method: 'get' });
mockRouter = new MockRouter({
method: 'get',
path: '/api/enterprise_search/config_data',
});

registerConfigDataRoute({
...mockDependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ describe('Enterprise Search Telemetry API', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'put', payload: 'body' });
mockRouter = new MockRouter({
method: 'put',
path: '/api/enterprise_search/stats',
payload: 'body',
});

registerTelemetryRoute({
...mockDependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ describe('groups routes', () => {
});

it('creates a request handler', () => {
mockRouter = new MockRouter({ method: 'get', payload: 'query' });
mockRouter = new MockRouter({
method: 'get',
path: '/api/workplace_search/groups',
payload: 'query',
});

registerGroupsRoute({
...mockDependencies,
Expand All @@ -43,7 +47,11 @@ describe('groups routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'post', payload: 'body' });
mockRouter = new MockRouter({
method: 'post',
path: '/api/workplace_search/groups',
payload: 'body',
});

registerGroupsRoute({
...mockDependencies,
Expand Down Expand Up @@ -71,7 +79,11 @@ describe('groups routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'post', payload: 'body' });
mockRouter = new MockRouter({
method: 'post',
path: '/api/workplace_search/groups/search',
payload: 'body',
});

registerSearchGroupsRoute({
...mockDependencies,
Expand Down Expand Up @@ -141,7 +153,11 @@ describe('groups routes', () => {
});

it('creates a request handler', () => {
mockRouter = new MockRouter({ method: 'get', payload: 'params' });
mockRouter = new MockRouter({
method: 'get',
path: '/api/workplace_search/groups/{id}',
payload: 'params',
});

registerGroupRoute({
...mockDependencies,
Expand Down Expand Up @@ -176,7 +192,11 @@ describe('groups routes', () => {
};

it('creates a request handler', () => {
mockRouter = new MockRouter({ method: 'put', payload: 'body' });
mockRouter = new MockRouter({
method: 'put',
path: '/api/workplace_search/groups/{id}',
payload: 'body',
});

registerGroupRoute({
...mockDependencies,
Expand Down Expand Up @@ -204,7 +224,11 @@ describe('groups routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'delete', payload: 'params' });
mockRouter = new MockRouter({
method: 'delete',
path: '/api/workplace_search/groups/{id}',
payload: 'params',
});

registerGroupRoute({
...mockDependencies,
Expand All @@ -227,15 +251,19 @@ describe('groups routes', () => {
});
});

describe('GET /api/workplace_search/groups/{id}/users', () => {
describe('GET /api/workplace_search/groups/{id}/group_users', () => {
let mockRouter: MockRouter;

beforeEach(() => {
jest.clearAllMocks();
});

it('creates a request handler', () => {
mockRouter = new MockRouter({ method: 'get', payload: 'params' });
mockRouter = new MockRouter({
method: 'get',
path: '/api/workplace_search/groups/{id}/group_users',
payload: 'params',
});

registerGroupUsersRoute({
...mockDependencies,
Expand All @@ -261,7 +289,11 @@ describe('groups routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'post', payload: 'body' });
mockRouter = new MockRouter({
method: 'post',
path: '/api/workplace_search/groups/{id}/share',
payload: 'body',
});

registerShareGroupRoute({
...mockDependencies,
Expand Down Expand Up @@ -291,7 +323,11 @@ describe('groups routes', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'post', payload: 'body' });
mockRouter = new MockRouter({
method: 'post',
path: '/api/workplace_search/groups/{id}/assign',
payload: 'body',
});

registerAssignGroupRoute({
...mockDependencies,
Expand Down Expand Up @@ -330,7 +366,11 @@ describe('groups routes', () => {
};

it('creates a request handler', () => {
mockRouter = new MockRouter({ method: 'put', payload: 'body' });
mockRouter = new MockRouter({
method: 'put',
path: '/api/workplace_search/groups/{id}/boosts',
payload: 'body',
});

registerBoostsGroupRoute({
...mockDependencies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ describe('Overview route', () => {

beforeEach(() => {
jest.clearAllMocks();
mockRouter = new MockRouter({ method: 'get', payload: 'query' });
mockRouter = new MockRouter({
method: 'get',
path: '/api/workplace_search/overview',
payload: 'query',
});

registerOverviewRoute({
...mockDependencies,
Expand Down

0 comments on commit 4a8f426

Please sign in to comment.