Skip to content

Commit

Permalink
Fixing mocks and types
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-buttner committed Apr 14, 2021
1 parent 12a705d commit 0c3c544
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 15 deletions.
6 changes: 3 additions & 3 deletions x-pack/plugins/cases/server/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { CasesClientInternal, createCasesClientInternal } from './client_interna
import { createSubCasesClient, SubCasesClient } from './sub_cases/client';
import { ENABLE_CASE_CONNECTOR } from '../../common/constants';
import { ConfigureSubClient, createConfigurationSubClient } from './configure/client';
import { createStatusStatsSubClient, StatusStatsSubClient } from './status_stats/client';
import { createStatsSubClient, StatsSubClient } from './status_stats/client';

export class CasesClient {
private readonly _casesClientInternal: CasesClientInternal;
Expand All @@ -22,7 +22,7 @@ export class CasesClient {
private readonly _userActions: UserActionsSubClient;
private readonly _subCases: SubCasesClient;
private readonly _configure: ConfigureSubClient;
private readonly _stats: StatusStatsSubClient;
private readonly _stats: StatsSubClient;

constructor(args: CasesClientArgs) {
this._casesClientInternal = createCasesClientInternal(args);
Expand All @@ -31,7 +31,7 @@ export class CasesClient {
this._userActions = createUserActionsSubClient(args);
this._subCases = createSubCasesClient(args, this._casesClientInternal);
this._configure = createConfigurationSubClient(args, this._casesClientInternal);
this._stats = createStatusStatsSubClient(args);
this._stats = createStatsSubClient(args);
}

public get cases() {
Expand Down
35 changes: 32 additions & 3 deletions x-pack/plugins/cases/server/client/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

import { PublicContract, PublicMethodsOf } from '@kbn/utility-types';

import { CasesClient, CasesClientInternal } from '.';
import { CasesClient } from '.';
import { AttachmentsSubClient } from './attachments/client';
import { CasesSubClient } from './cases/client';
import { ConfigureSubClient } from './configure/client';
import { CasesClientFactory } from './factory';
import { StatsSubClient } from './status_stats/client';
import { SubCasesClient } from './sub_cases/client';
import { UserActionsSubClient } from './user_actions/client';

Expand All @@ -23,6 +25,9 @@ const createCasesSubClientMock = (): CasesSubClientMock => {
get: jest.fn(),
push: jest.fn(),
update: jest.fn(),
delete: jest.fn(),
getTags: jest.fn(),
getReporters: jest.fn(),
};
};

Expand All @@ -31,6 +36,12 @@ type AttachmentsSubClientMock = jest.Mocked<AttachmentsSubClient>;
const createAttachmentsSubClientMock = (): AttachmentsSubClientMock => {
return {
add: jest.fn(),
deleteAll: jest.fn(),
delete: jest.fn(),
find: jest.fn(),
getAll: jest.fn(),
get: jest.fn(),
update: jest.fn(),
};
};

Expand All @@ -53,7 +64,24 @@ const createSubCasesClientMock = (): SubCasesClientMock => {
};
};

type CasesClientInternalMock = jest.Mocked<CasesClientInternal>;
type ConfigureSubClientMock = jest.Mocked<ConfigureSubClient>;

const createConfigureSubClientMock = (): ConfigureSubClientMock => {
return {
get: jest.fn(),
getConnectors: jest.fn(),
update: jest.fn(),
create: jest.fn(),
};
};

type StatsSubClientMock = jest.Mocked<StatsSubClient>;

const createStatsSubClientMock = (): StatsSubClientMock => {
return {
getStatusTotalsByType: jest.fn(),
};
};

export interface CasesClientMock extends CasesClient {
cases: CasesSubClientMock;
Expand All @@ -64,11 +92,12 @@ export interface CasesClientMock extends CasesClient {

export const createCasesClientMock = (): CasesClientMock => {
const client: PublicContract<CasesClient> = {
casesClientInternal: (jest.fn() as unknown) as CasesClientInternalMock,
cases: createCasesSubClientMock(),
attachments: createAttachmentsSubClientMock(),
userActions: createUserActionsSubClientMock(),
subCases: createSubCasesClientMock(),
configure: createConfigureSubClientMock(),
stats: createStatsSubClientMock(),
};
return (client as unknown) as CasesClientMock;
};
Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/cases/server/client/status_stats/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ import { createCaseError } from '../../common/error';
import { constructQueryOptions } from '../utils';

/**
* Status statistics API contract.
* Statistics API contract.
*/
export interface StatusStatsSubClient {
get(): Promise<CasesStatusResponse>;
export interface StatsSubClient {
getStatusTotalsByType(): Promise<CasesStatusResponse>;
}

/**
* Creates the interface for retrieving the number of open, closed, and in progress cases.
*/
export function createStatusStatsSubClient(clientArgs: CasesClientArgs): StatusStatsSubClient {
export function createStatsSubClient(clientArgs: CasesClientArgs): StatsSubClient {
return Object.freeze({
get: () => get(clientArgs),
getStatusTotalsByType: () => getStatusTotalsByType(clientArgs),
});
}

async function get({
async function getStatusTotalsByType({
savedObjectsClient: soClient,
caseService,
logger,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function initGetReportersApi({ router, logger }: RouteDeps) {
try {
const client = await context.cases.getCasesClient();

return response.ok({ body: await client.reporters.get() });
return response.ok({ body: await client.cases.getReporters() });
} catch (error) {
logger.error(`Failed to get reporters in route: ${error}`);
return response.customError(wrapError(error));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function initGetCasesStatusApi({ router, logger }: RouteDeps) {
const client = await context.cases.getCasesClient();

return response.ok({
body: await client.statusStats.get(),
body: await client.stats.getStatusTotalsByType(),
});
} catch (error) {
logger.error(`Failed to get status stats in route: ${error}`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function initGetTagsApi({ router, logger }: RouteDeps) {
try {
const client = await context.cases.getCasesClient();

return response.ok({ body: await client.tags.get() });
return response.ok({ body: await client.cases.getTags() });
} catch (error) {
logger.error(`Failed to retrieve tags in route: ${error}`);
return response.customError(wrapError(error));
Expand Down

0 comments on commit 0c3c544

Please sign in to comment.