Skip to content

Commit

Permalink
Added find_list_items unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FrankHassanabad committed Aug 11, 2020
1 parent 11c0ebb commit 3e0e4bf
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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 expect from '@kbn/expect';

import { LIST_ITEM_ID, LIST_ID } from '../../../../plugins/lists/common/constants.mock';
import { getListItemResponseMockWithoutAutoGeneratedValues } from '../../../../plugins/lists/common/schemas/response/list_item_schema.mock';
import { getCreateMinimalListItemSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_list_item_schema.mock';
import { FtrProviderContext } from '../../common/ftr_provider_context';
import { LIST_URL, LIST_ITEM_URL } from '../../../../plugins/lists/common/constants';

import { getCreateMinimalListSchemaMock } from '../../../../plugins/lists/common/schemas/request/create_list_schema.mock';
import {
createListsIndex,
deleteListsIndex,
removeListItemServerGeneratedProperties,
} from '../../utils';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertest = getService('supertest');

describe('find_list_items', () => {
describe('find list items', () => {
beforeEach(async () => {
await createListsIndex(supertest);
});

afterEach(async () => {
await deleteListsIndex(supertest);
});

it('should give a validation error if the list_id is not supplied', async () => {
const { body } = await supertest
.get(`${LIST_ITEM_URL}/_find`)
.set('kbn-xsrf', 'true')
.send()
.expect(400);

expect(body).to.eql({
error: 'Bad Request',
message: '[request query]: Invalid value "undefined" supplied to "list_id"',
statusCode: 400,
});
});

it('should give a 404 if the list has not been created yet', async () => {
const { body } = await supertest
.get(`${LIST_ITEM_URL}/_find?list_id=${LIST_ITEM_ID}`)
.set('kbn-xsrf', 'true')
.send()
.expect(404);

expect(body).to.eql({
message: 'list id: "some-list-item-id" does not exist',
status_code: 404,
});
});

it('should return an empty find body correctly if no list items are loaded', async () => {
await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(200);

const { body } = await supertest
.get(`${LIST_ITEM_URL}/_find?list_id=${LIST_ID}`)
.set('kbn-xsrf', 'true')
.send()
.expect(200);

expect(body).to.eql({
cursor: 'WzBd',
data: [],
page: 1,
per_page: 20,
total: 0,
});
});

it('should return a single list item when a single list item is loaded from a find with defaults added', async () => {
await supertest
.post(LIST_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListSchemaMock())
.expect(200);

await supertest
.post(LIST_ITEM_URL)
.set('kbn-xsrf', 'true')
.send(getCreateMinimalListItemSchemaMock())
.expect(200);

const { body } = await supertest
.get(`${LIST_ITEM_URL}/_find?list_id=${LIST_ID}`)
.set('kbn-xsrf', 'true')
.send()
.expect(200);

body.data = [removeListItemServerGeneratedProperties(body.data[0])];
// cursor is a constant changing value so we have to delete it as well.
delete body.cursor;
expect(body).to.eql({
data: [getListItemResponseMockWithoutAutoGeneratedValues()],
page: 1,
per_page: 20,
total: 1,
});
});
});
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default ({ loadTestFile }: FtrProviderContext): void => {
loadTestFile(require.resolve('./delete_lists'));
loadTestFile(require.resolve('./delete_list_items'));
loadTestFile(require.resolve('./find_lists'));
loadTestFile(require.resolve('./find_list_items'));
loadTestFile(require.resolve('./import_list_items'));
});
};

0 comments on commit 3e0e4bf

Please sign in to comment.