Skip to content

Commit

Permalink
Add test cases for the toast content
Browse files Browse the repository at this point in the history
  • Loading branch information
Esteban Beltran committed Mar 2, 2022
1 parent 5147ade commit 115998c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
72 changes: 56 additions & 16 deletions x-pack/plugins/cases/public/common/use_cases_toast.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,69 @@

import { renderHook } from '@testing-library/react-hooks';
import { useToasts } from '../common/lib/kibana';
import { TestProviders } from '../common/mock';
import { useCasesToast } from './use_cases_toast';
import { AppMockRenderer, createAppMockRenderer, TestProviders } from '../common/mock';
import { CaseToastSuccessContent, useCasesToast } from './use_cases_toast';
import { mockCase } from '../containers/mock';
import React from 'react';
import userEvent from '@testing-library/user-event';

jest.mock('../common/lib/kibana');

const useToastsMock = useToasts as jest.Mock;

describe('Use cases toast hook', () => {
const successMock = jest.fn();
useToastsMock.mockImplementation(() => {
return {
addSuccess: successMock,
};
describe('Toast hook', () => {
const successMock = jest.fn();
useToastsMock.mockImplementation(() => {
return {
addSuccess: successMock,
};
});
it('should create a success tost when invoked with a case', () => {
const { result } = renderHook(
() => {
return useCasesToast();
},
{ wrapper: TestProviders }
);
result.current.showSuccessAttach(mockCase);
expect(successMock).toHaveBeenCalled();
});
});
it('should create a success tost when invoked with a case', () => {
const { result } = renderHook(
() => {
return useCasesToast();
},
{ wrapper: TestProviders }
);
result.current.showSuccessAttach(mockCase);
expect(successMock).toHaveBeenCalled();
describe('Toast content', () => {
let appMockRender: AppMockRenderer;
const onViewCaseClick = jest.fn();
beforeEach(() => {
appMockRender = createAppMockRenderer();
onViewCaseClick.mockReset();
});

it('renders a correct successfull message with synced alerts', () => {
const result = appMockRender.render(
<CaseToastSuccessContent syncAlerts={true} onViewCaseClick={onViewCaseClick} />
);
expect(result.getByTestId('toaster-content-sync-text')).toHaveTextContent(
'Alerts in this case have their status synched with the case status'
);
expect(result.getByTestId('toaster-content-case-view-link')).toHaveTextContent('View Case');
expect(onViewCaseClick).not.toHaveBeenCalled();
});

it('renders a correct successfull message with not synced alerts', () => {
const result = appMockRender.render(
<CaseToastSuccessContent syncAlerts={false} onViewCaseClick={onViewCaseClick} />
);
expect(result.queryByTestId('toaster-content-sync-text')).toBeFalsy();
expect(result.getByTestId('toaster-content-case-view-link')).toHaveTextContent('View Case');
expect(onViewCaseClick).not.toHaveBeenCalled();
});

it('Calls the onViewCaseClick when clicked', () => {
const result = appMockRender.render(
<CaseToastSuccessContent syncAlerts={false} onViewCaseClick={onViewCaseClick} />
);
userEvent.click(result.getByTestId('toaster-content-case-view-link'));
expect(onViewCaseClick).toHaveBeenCalled();
});
});
});
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/public/common/use_cases_toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const useCasesToast = () => {
},
};
};
const CaseToastSuccessContent = ({
export const CaseToastSuccessContent = ({
syncAlerts,
onViewCaseClick,
}: {
Expand Down

0 comments on commit 115998c

Please sign in to comment.