Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Dec 11, 2022
1 parent eb10eb1 commit aa18f98
Show file tree
Hide file tree
Showing 7 changed files with 1,182 additions and 213 deletions.
81 changes: 81 additions & 0 deletions x-pack/plugins/cases/public/common/use_is_user_typing.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { act, renderHook } from '@testing-library/react-hooks';
import type { AppMockRenderer } from './mock';
import { createAppMockRenderer } from './mock';
import { useIsUserTyping } from './use_is_user_typing';

describe('useIsUserTyping', () => {
let appMockRender: AppMockRenderer;

beforeEach(() => {
appMockRender = createAppMockRenderer();
});

it('set isUserTyping=false on init', () => {
const { result } = renderHook(() => useIsUserTyping(), {
wrapper: appMockRender.AppWrapper,
});

expect(result.current.isUserTyping).toBe(false);
});

it('set isUserTyping to true with setIsUserTyping', () => {
const { result } = renderHook(() => useIsUserTyping(), {
wrapper: appMockRender.AppWrapper,
});

act(() => {
result.current.setIsUserTyping(true);
});

expect(result.current.isUserTyping).toBe(true);
});

it('set isUserTyping to true onContentChange', () => {
const { result } = renderHook(() => useIsUserTyping(), {
wrapper: appMockRender.AppWrapper,
});

act(() => {
result.current.onContentChange('a value');
});

expect(result.current.isUserTyping).toBe(true);
});

it('does not set isUserTyping to true onContentChange when the value is empty', () => {
const { result } = renderHook(() => useIsUserTyping(), {
wrapper: appMockRender.AppWrapper,
});

act(() => {
result.current.onContentChange('');
});

expect(result.current.isUserTyping).toBe(false);
});

it('set isUserTyping to false onDebounce', () => {
const { result } = renderHook(() => useIsUserTyping(), {
wrapper: appMockRender.AppWrapper,
});

act(() => {
result.current.setIsUserTyping(true);
});

expect(result.current.isUserTyping).toBe(true);

act(() => {
result.current.onDebounce();
});

expect(result.current.isUserTyping).toBe(false);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type { AppMockRenderer } from '../../../common/mock';
import { createAppMockRenderer } from '../../../common/mock';
import { act, renderHook } from '@testing-library/react-hooks';
import { useAssigneesAction } from './use_assignees_action';

import * as api from '../../../containers/api';
import { basicCase } from '../../../containers/mock';

jest.mock('../../../containers/api');

describe('useAssigneesAction', () => {
let appMockRender: AppMockRenderer;
const onAction = jest.fn();
const onActionSuccess = jest.fn();

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

it('renders an action', async () => {
const { result } = renderHook(
() =>
useAssigneesAction({
onAction,
onActionSuccess,
isDisabled: false,
}),
{
wrapper: appMockRender.AppWrapper,
}
);

expect(result.current.getAction([basicCase])).toMatchInlineSnapshot(`
Object {
"data-test-subj": "cases-bulk-action-assignees",
"disabled": false,
"icon": <EuiIcon
size="m"
type="userAvatar"
/>,
"key": "cases-bulk-action-assignees",
"name": "Edit assignees",
"onClick": [Function],
}
`);
});

it('update the assignees correctly', async () => {
const updateSpy = jest.spyOn(api, 'updateCases');

const { result, waitFor } = renderHook(
() => useAssigneesAction({ onAction, onActionSuccess, isDisabled: false }),
{
wrapper: appMockRender.AppWrapper,
}
);

const action = result.current.getAction([basicCase]);

act(() => {
action.onClick();
});

expect(onAction).toHaveBeenCalled();
expect(result.current.isFlyoutOpen).toBe(true);

act(() => {
result.current.onSaveAssignees({ selectedItems: ['1'], unSelectedItems: [] });
});

await waitFor(() => {
expect(result.current.isFlyoutOpen).toBe(false);
expect(onActionSuccess).toHaveBeenCalled();
expect(updateSpy).toHaveBeenCalledWith(
[
{
assignees: [{ uid: 'u_J41Oh6L9ki-Vo2tOogS8WRTENzhHurGtRc87NgEAlkc_0' }, { uid: '1' }],
id: basicCase.id,
version: basicCase.version,
},
],
expect.anything()
);
});
});

it('shows the success toaster correctly when updating one case', async () => {
const { result, waitFor } = renderHook(
() => useAssigneesAction({ onAction, onActionSuccess, isDisabled: false }),
{
wrapper: appMockRender.AppWrapper,
}
);

const action = result.current.getAction([basicCase]);

act(() => {
action.onClick();
});

act(() => {
result.current.onSaveAssignees({ selectedItems: ['1', '1'], unSelectedItems: ['2'] });
});

await waitFor(() => {
expect(appMockRender.coreStart.notifications.toasts.addSuccess).toHaveBeenCalledWith(
'Edited case'
);
});
});

it('shows the success toaster correctly when updating multiple cases', async () => {
const { result, waitFor } = renderHook(
() => useAssigneesAction({ onAction, onActionSuccess, isDisabled: false }),
{
wrapper: appMockRender.AppWrapper,
}
);

const action = result.current.getAction([basicCase, basicCase]);

act(() => {
action.onClick();
});

act(() => {
result.current.onSaveAssignees({ selectedItems: ['1', '1'], unSelectedItems: ['2'] });
});

await waitFor(() => {
expect(appMockRender.coreStart.notifications.toasts.addSuccess).toHaveBeenCalledWith(
'Edited 2 cases'
);
});
});
});
Loading

0 comments on commit aa18f98

Please sign in to comment.