Skip to content

Commit

Permalink
merged with master and fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yctercero committed Feb 23, 2021
1 parent 39933a3 commit a12f211
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 57 deletions.
92 changes: 48 additions & 44 deletions x-pack/plugins/lists/public/exceptions/hooks/use_api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -406,57 +406,61 @@ describe('useApi', () => {
});
});

test('it invokes "addExceptionListItem" when "addExceptionListItem" used', async () => {
const payload = getExceptionListItemSchemaMock();
const itemToCreate = getCreateExceptionListItemSchemaMock();
const spyOnFetchExceptionListItemById = jest
.spyOn(api, 'addExceptionListItem')
.mockResolvedValue(payload);

await act(async () => {
const { result, waitForNextUpdate } = renderHook<HttpStart, ExceptionsApi>(() =>
useApi(mockKibanaHttpService)
);
await waitForNextUpdate();

await result.current.addExceptionListItem({
listItem: itemToCreate,
});
describe('addExceptionListItem', () => {
test('it removes exception item entry ids', async () => {
const payload = getExceptionListItemSchemaMock();
const itemToCreate = { ...getCreateExceptionListItemSchemaMock(), entries: ENTRIES_WITH_IDS };
const spyOnFetchExceptionListItemById = jest
.spyOn(api, 'addExceptionListItem')
.mockResolvedValue(payload);

await act(async () => {
const { result, waitForNextUpdate } = renderHook<HttpStart, ExceptionsApi>(() =>
useApi(mockKibanaHttpService)
);
await waitForNextUpdate();

await result.current.addExceptionListItem({
listItem: itemToCreate,
});

const expected: AddExceptionListItemProps = {
http: mockKibanaHttpService,
listItem: itemToCreate,
signal: new AbortController().signal,
};
const expected: AddExceptionListItemProps = {
http: mockKibanaHttpService,
listItem: getCreateExceptionListItemSchemaMock(),
signal: new AbortController().signal,
};

expect(spyOnFetchExceptionListItemById).toHaveBeenCalledWith(expected);
expect(spyOnFetchExceptionListItemById).toHaveBeenCalledWith(expected);
});
});
});

test('it invokes "updateExceptionListItem" when "getExceptionItem" used', async () => {
const payload = getExceptionListItemSchemaMock();
const itemToUpdate = getUpdateExceptionListItemSchemaMock();
const spyOnUpdateExceptionListItem = jest
.spyOn(api, 'updateExceptionListItem')
.mockResolvedValue(payload);

await act(async () => {
const { result, waitForNextUpdate } = renderHook<HttpStart, ExceptionsApi>(() =>
useApi(mockKibanaHttpService)
);
await waitForNextUpdate();

await result.current.updateExceptionListItem({
listItem: itemToUpdate,
});
describe('updateExceptionListItem', () => {
test('it removes exception item entry ids', async () => {
const payload = getExceptionListItemSchemaMock();
const itemToUpdate = { ...getUpdateExceptionListItemSchemaMock(), entries: ENTRIES_WITH_IDS };
const spyOnUpdateExceptionListItem = jest
.spyOn(api, 'updateExceptionListItem')
.mockResolvedValue(payload);

await act(async () => {
const { result, waitForNextUpdate } = renderHook<HttpStart, ExceptionsApi>(() =>
useApi(mockKibanaHttpService)
);
await waitForNextUpdate();

await result.current.updateExceptionListItem({
listItem: itemToUpdate,
});

const expected: UpdateExceptionListItemProps = {
http: mockKibanaHttpService,
listItem: itemToUpdate,
signal: new AbortController().signal,
};
const expected: UpdateExceptionListItemProps = {
http: mockKibanaHttpService,
listItem: getUpdateExceptionListItemSchemaMock(),
signal: new AbortController().signal,
};

expect(spyOnUpdateExceptionListItem).toHaveBeenCalledWith(expected);
expect(spyOnUpdateExceptionListItem).toHaveBeenCalledWith(expected);
});
});
});
});
22 changes: 14 additions & 8 deletions x-pack/plugins/lists/public/exceptions/hooks/use_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ import {
ExceptionListItemSchema,
ExceptionListSchema,
UpdateExceptionListItemSchema,
createExceptionListItemSchema,
} from '../../../common/schemas';
import { ApiCallFindListsItemsMemoProps, ApiCallMemoProps, ApiListExportProps } from '../types';
import { getIdsAndNamespaces } from '../utils';
import { transformInput } from '../transforms';
import { transformInput, transformOutput } from '../transforms';

export interface ExceptionsApi {
addExceptionListItem: (arg: {
Expand Down Expand Up @@ -47,12 +48,16 @@ export const useApi = (http: HttpStart): ExceptionsApi => {
listItem: CreateExceptionListItemSchema;
}): Promise<ExceptionListItemSchema> {
const abortCtrl = new AbortController();

return Api.addExceptionListItem({
http,
listItem,
signal: abortCtrl.signal,
});
const sanitizedItem = transformOutput(listItem);
if (createExceptionListItemSchema.is(sanitizedItem)) {
return Api.addExceptionListItem({
http,
listItem: sanitizedItem,
signal: abortCtrl.signal,
});
} else {
throw new Error('Unable to create exception item. Item malformed.');
}
},
async deleteExceptionItem({
id,
Expand Down Expand Up @@ -220,10 +225,11 @@ export const useApi = (http: HttpStart): ExceptionsApi => {
listItem: UpdateExceptionListItemSchema;
}): Promise<ExceptionListItemSchema> {
const abortCtrl = new AbortController();
const sanitizedItem = transformOutput(listItem);

return Api.updateExceptionListItem({
http,
listItem,
listItem: sanitizedItem,
signal: abortCtrl.signal,
});
},
Expand Down
16 changes: 11 additions & 5 deletions x-pack/plugins/lists/public/exceptions/transforms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ import { addIdToItem, removeIdFromItem } from '../../common/shared_imports';
* @returns The exceptionItem transformed from the output
*/
export const transformOutput = (
exceptionItem: CreateExceptionListItemSchema | UpdateExceptionListItemSchema
): CreateExceptionListItemSchema | UpdateExceptionListItemSchema =>
exceptionItem:
| CreateExceptionListItemSchema
| UpdateExceptionListItemSchema
| ExceptionListItemSchema
): CreateExceptionListItemSchema | UpdateExceptionListItemSchema | ExceptionListItemSchema =>
flow(removeIdFromExceptionItemsEntries)(exceptionItem);

/**
Expand Down Expand Up @@ -93,13 +96,16 @@ export const addIdToExceptionItemEntries = (
* @returns exceptionItem The exceptionItem but with id removed from the entries
*/
export const removeIdFromExceptionItemsEntries = (
exceptionItem: CreateExceptionListItemSchema | UpdateExceptionListItemSchema
): CreateExceptionListItemSchema | UpdateExceptionListItemSchema => {
exceptionItem:
| CreateExceptionListItemSchema
| UpdateExceptionListItemSchema
| ExceptionListItemSchema
): CreateExceptionListItemSchema | UpdateExceptionListItemSchema | ExceptionListItemSchema => {
const { entries, ...itemInfo } = exceptionItem;
const entriesNoId = entries.map((entry) => {
if (entry.type === 'nested') {
return removeIdFromItem({
...itemInfo,
...entry,
entries: entry.entries.map((nestedEntry) => removeIdFromItem(nestedEntry)),
});
} else {
Expand Down

0 comments on commit a12f211

Please sign in to comment.