Skip to content

Commit

Permalink
Test utils
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Apr 9, 2020
1 parent 82e048a commit f941761
Show file tree
Hide file tree
Showing 2 changed files with 357 additions and 0 deletions.
13 changes: 13 additions & 0 deletions x-pack/plugins/case/server/routes/api/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { CasePostRequest } from '../../../common/api';

export const newCase: CasePostRequest = {
title: 'My new case',
description: 'A description',
tags: ['new', 'case'],
};
344 changes: 344 additions & 0 deletions x-pack/plugins/case/server/routes/api/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,344 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import {
transformNewCase,
transformNewComment,
wrapError,
transformCases,
flattenCaseSavedObjects,
flattenCaseSavedObject,
flattenCommentSavedObjects,
transformComments,
flattenCommentSavedObject,
} from './utils';
import { newCase } from './mock';
import { isBoom, boomify } from 'boom';
import { mockCases, mockCaseComments } from './__fixtures__/mock_saved_objects';

describe('Utils', () => {
describe('transformNewCase', () => {
it('transform correctly', () => {
const myCase = {
newCase,
createdDate: '2020-04-09T09:43:51.778Z',
email: 'elastic@elastic.co',
full_name: 'Elastic',
username: 'elastic',
};

const res = transformNewCase(myCase);

expect(res).toEqual({
...myCase.newCase,
closed_at: null,
closed_by: null,
created_at: '2020-04-09T09:43:51.778Z',
created_by: { email: 'elastic@elastic.co', full_name: 'Elastic', username: 'elastic' },
external_service: null,
status: 'open',
updated_at: null,
updated_by: null,
});
});

it('transform correctly without optional fields', () => {
const myCase = {
newCase,
createdDate: '2020-04-09T09:43:51.778Z',
};

const res = transformNewCase(myCase);

expect(res).toEqual({
...myCase.newCase,
closed_at: null,
closed_by: null,
created_at: '2020-04-09T09:43:51.778Z',
created_by: { email: undefined, full_name: undefined, username: undefined },
external_service: null,
status: 'open',
updated_at: null,
updated_by: null,
});
});

it('transform correctly with optional fields as null', () => {
const myCase = {
newCase,
createdDate: '2020-04-09T09:43:51.778Z',
email: null,
full_name: null,
username: null,
};

const res = transformNewCase(myCase);

expect(res).toEqual({
...myCase.newCase,
closed_at: null,
closed_by: null,
created_at: '2020-04-09T09:43:51.778Z',
created_by: { email: null, full_name: null, username: null },
external_service: null,
status: 'open',
updated_at: null,
updated_by: null,
});
});
});

describe('transformNewComment', () => {
it('transforms correctly', () => {
const comment = {
comment: 'A comment',
createdDate: '2020-04-09T09:43:51.778Z',
email: 'elastic@elastic.co',
full_name: 'Elastic',
username: 'elastic',
};

const res = transformNewComment(comment);
expect(res).toEqual({
comment: 'A comment',
created_at: '2020-04-09T09:43:51.778Z',
created_by: { email: 'elastic@elastic.co', full_name: 'Elastic', username: 'elastic' },
pushed_at: null,
pushed_by: null,
updated_at: null,
updated_by: null,
});
});

it('transform correctly without optional fields', () => {
const comment = {
comment: 'A comment',
createdDate: '2020-04-09T09:43:51.778Z',
};

const res = transformNewComment(comment);

expect(res).toEqual({
comment: 'A comment',
created_at: '2020-04-09T09:43:51.778Z',
created_by: { email: undefined, full_name: undefined, username: undefined },
pushed_at: null,
pushed_by: null,
updated_at: null,
updated_by: null,
});
});

it('transform correctly with optional fields as null', () => {
const comment = {
comment: 'A comment',
createdDate: '2020-04-09T09:43:51.778Z',
email: null,
full_name: null,
username: null,
};

const res = transformNewComment(comment);

expect(res).toEqual({
comment: 'A comment',
created_at: '2020-04-09T09:43:51.778Z',
created_by: { email: null, full_name: null, username: null },
pushed_at: null,
pushed_by: null,
updated_at: null,
updated_by: null,
});
});
});

describe('wrapError', () => {
it('wraps an error', () => {
const error = new Error('Something happened');
const res = wrapError(error);

expect(isBoom(res.body as Error)).toBe(true);
});

it('it set statusCode to 500', () => {
const error = new Error('Something happened');
const res = wrapError(error);

expect(res.statusCode).toBe(500);
});

it('it set statusCode to errors status code', () => {
const error = new Error('Something happened') as any;
error.statusCode = 404;
const res = wrapError(error);

expect(res.statusCode).toBe(404);
});

it('it accepts a boom error', () => {
const error = boomify(new Error('Something happened'));
const res = wrapError(error);

// Utils returns the same boom error as body
expect(res.body).toBe(error);
});

it('it accepts a boom error with status code', () => {
const error = boomify(new Error('Something happened'), { statusCode: 404 });
const res = wrapError(error);

expect(res.statusCode).toBe(404);
});

it('it returns empty headers', () => {
const error = new Error('Something happened');
const res = wrapError(error);

expect(res.headers).toEqual({});
});
});

describe('transformCases', () => {
it('transforms correctly', () => {
const totalCommentsByCase = [
{ caseId: mockCases[0].id, totalComments: 2 },
{ caseId: mockCases[1].id, totalComments: 2 },
{ caseId: mockCases[2].id, totalComments: 2 },
{ caseId: mockCases[3].id, totalComments: 2 },
];

const res = transformCases(
{ saved_objects: mockCases, total: mockCases.length, per_page: 10, page: 1 },
2,
2,
totalCommentsByCase
);
expect(res).toEqual({
page: 1,
per_page: 10,
total: mockCases.length,
cases: flattenCaseSavedObjects(mockCases, totalCommentsByCase),
count_open_cases: 2,
count_closed_cases: 2,
});
});
});

describe('flattenCaseSavedObjects', () => {
it('flattens correctly', () => {
const totalCommentsByCase = [
{ caseId: mockCases[0].id, totalComments: 2 },
{ caseId: mockCases[1].id, totalComments: 1 },
];
const cases = [{ ...mockCases[0] }, { ...mockCases[1] }];
const res = flattenCaseSavedObjects(cases, totalCommentsByCase);
expect(res).toEqual([
flattenCaseSavedObject(cases[0], [], totalCommentsByCase[0].totalComments),
flattenCaseSavedObject(cases[1], [], totalCommentsByCase[1].totalComments),
]);
});

it('it handles total comments correctly', () => {
const totalCommentsByCase = [{ caseId: 'not-exist', totalComments: 2 }];
const cases = [{ ...mockCases[0] }];
const res = flattenCaseSavedObjects(cases, totalCommentsByCase);
expect(res).toEqual([flattenCaseSavedObject(cases[0], [], 0)]);
});
});

describe('flattenCaseSavedObject', () => {
it('flattens correctly', () => {
const myCase = { ...mockCases[0] };
const res = flattenCaseSavedObject(myCase, [], 2);
expect(res).toEqual({
id: myCase.id,
version: myCase.version,
comments: [],
totalComment: 2,
...myCase.attributes,
});
});

it('flattens correctly without version', () => {
const myCase = { ...mockCases[0] };
myCase.version = undefined;
const res = flattenCaseSavedObject(myCase, [], 2);
expect(res).toEqual({
id: myCase.id,
version: '0',
comments: [],
totalComment: 2,
...myCase.attributes,
});
});

it('flattens correctly with comments', () => {
const myCase = { ...mockCases[0] };
const comments = [{ ...mockCaseComments[0] }];
const res = flattenCaseSavedObject(myCase, comments, 2);
expect(res).toEqual({
id: myCase.id,
version: myCase.version,
comments: flattenCommentSavedObjects(comments),
totalComment: 2,
...myCase.attributes,
});
});
});

describe('transformComments', () => {
it('transforms correctly', () => {
const comments = {
saved_objects: mockCaseComments,
total: mockCaseComments.length,
per_page: 10,
page: 1,
};

const res = transformComments(comments);
expect(res).toEqual({
page: 1,
per_page: 10,
total: mockCaseComments.length,
comments: flattenCommentSavedObjects(comments.saved_objects),
});
});
});

describe('flattenCommentSavedObjects', () => {
it('flattens correctly', () => {
const comments = [{ ...mockCaseComments[0] }, { ...mockCaseComments[1] }];
const res = flattenCommentSavedObjects(comments);
expect(res).toEqual([
flattenCommentSavedObject(comments[0]),
flattenCommentSavedObject(comments[1]),
]);
});
});

describe('flattenCommentSavedObject', () => {
it('flattens correctly', () => {
const comment = { ...mockCaseComments[0] };
const res = flattenCommentSavedObject(comment);
expect(res).toEqual({
id: comment.id,
version: comment.version,
...comment.attributes,
});
});

it('flattens correctly without version', () => {
const comment = { ...mockCaseComments[0] };
comment.version = undefined;
const res = flattenCommentSavedObject(comment);
expect(res).toEqual({
id: comment.id,
version: '0',
...comment.attributes,
});
});
});
});

0 comments on commit f941761

Please sign in to comment.