Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas committed Feb 9, 2023
1 parent 83331e4 commit d8e94d8
Show file tree
Hide file tree
Showing 2 changed files with 283 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
basicCase,
connectorsMock,
getCaseUsersMockResponse,
getUserAction,
} from '../../../containers/mock';
import React from 'react';
import type { AppMockRenderer } from '../../../common/mock';
Expand All @@ -26,9 +27,10 @@ import { useGetCaseConnectors } from '../../../containers/use_get_case_connector
import { useGetCaseUsers } from '../../../containers/use_get_case_users';
import { licensingMock } from '@kbn/licensing-plugin/public/mocks';
import { waitForComponentToUpdate } from '../../../common/test_utils';
import { waitFor } from '@testing-library/dom';
import { waitFor, within } from '@testing-library/dom';
import { getCaseConnectorsMockResponse } from '../../../common/mock/connectors';
import { defaultUseFindCaseUserActions } from '../mocks';
import { ActionTypes } from '../../../../common/api';

jest.mock('../../../containers/use_find_case_user_actions');
jest.mock('../../../containers/configure/use_get_supported_action_connectors');
Expand Down Expand Up @@ -95,14 +97,12 @@ describe('Case View Page activity tab', () => {
const caseConnectors = getCaseConnectorsMockResponse();

beforeAll(() => {
useFindCaseUserActionsMock.mockReturnValue(defaultUseFindCaseUserActions);
useGetConnectorsMock.mockReturnValue({ data: connectorsMock, isLoading: false });
usePostPushToServiceMock.mockReturnValue({ isLoading: false, pushCaseToExternalService });
useGetCaseConnectorsMock.mockReturnValue({
isLoading: false,
data: caseConnectors,
});
useGetCaseUsersMock.mockReturnValue({ isLoading: false, data: caseUsers });
});
let appMockRender: AppMockRenderer;

Expand All @@ -116,6 +116,8 @@ describe('Case View Page activity tab', () => {

beforeEach(() => {
appMockRender = createAppMockRenderer();
useFindCaseUserActionsMock.mockReturnValue(defaultUseFindCaseUserActions);
useGetCaseUsersMock.mockReturnValue({ isLoading: false, data: caseUsers });
});

it('should render the activity content and main components', async () => {
Expand Down Expand Up @@ -210,4 +212,244 @@ describe('Case View Page activity tab', () => {
expect(result.getByTestId('case-view-edit-connector')).toBeInTheDocument();
});
});

describe('Case users', () => {
describe('Participants', () => {
it('should render the participants correctly', async () => {
appMockRender = createAppMockRenderer();
const result = appMockRender.render(<CaseViewActivity {...caseProps} />);
const participantsSection = within(result.getByTestId('case-view-user-list-participants'));

await waitFor(() => {
expect(participantsSection.getByText('Participant 1')).toBeInTheDocument();
expect(participantsSection.getByText('[email protected]')).toBeInTheDocument();
expect(participantsSection.getByText('participant_3')).toBeInTheDocument();
expect(participantsSection.getByText('P4')).toBeInTheDocument();
expect(participantsSection.getByText('Participant 5')).toBeInTheDocument();
});
});

it('should render Unknown users correctly', async () => {
appMockRender = createAppMockRenderer();
const result = appMockRender.render(<CaseViewActivity {...caseProps} />);

const participantsSection = within(result.getByTestId('case-view-user-list-participants'));

await waitFor(() => {
expect(participantsSection.getByText('Unknown')).toBeInTheDocument();
});
});

it('should render assignees in the participants section', async () => {
appMockRender = createAppMockRenderer();
const result = appMockRender.render(<CaseViewActivity {...caseProps} />);

const participantsSection = within(result.getByTestId('case-view-user-list-participants'));

await waitFor(() => {
expect(participantsSection.getByText('Unknown')).toBeInTheDocument();
expect(participantsSection.getByText('Fuzzy Marten')).toBeInTheDocument();
expect(participantsSection.getByText('elastic')).toBeInTheDocument();
expect(participantsSection.getByText('Misty Mackerel')).toBeInTheDocument();
});
});
});

describe('Reporter', () => {
it('should render the reporter correctly', async () => {
appMockRender = createAppMockRenderer();
const result = appMockRender.render(<CaseViewActivity {...caseProps} />);
const reporterSection = within(result.getByTestId('case-view-user-list-reporter'));

await waitFor(() => {
expect(reporterSection.getByText('Reporter 1')).toBeInTheDocument();
expect(reporterSection.getByText('R1')).toBeInTheDocument();
});
});

it('should render a reporter without uid correctly', async () => {
useGetCaseUsersMock.mockReturnValue({
isLoading: false,
data: {
...caseUsers,
reporter: {
user: {
email: '[email protected]',
full_name: 'Reporter No UID',
username: 'reporter_no_uid',
},
},
},
});

appMockRender = createAppMockRenderer();
const result = appMockRender.render(<CaseViewActivity {...caseProps} />);
const reporterSection = within(result.getByTestId('case-view-user-list-reporter'));

await waitFor(() => {
expect(reporterSection.getByText('Reporter No UID')).toBeInTheDocument();
});
});

it('fallbacks to the caseData reporter correctly', async () => {
useGetCaseUsersMock.mockReturnValue({
isLoading: false,
data: null,
});

appMockRender = createAppMockRenderer();
const result = appMockRender.render(<CaseViewActivity {...caseProps} />);
const reporterSection = within(result.getByTestId('case-view-user-list-reporter'));

await waitFor(() => {
expect(reporterSection.getByText('Leslie Knope')).toBeInTheDocument();
});
});
});

describe('Assignees', () => {
it('should render assignees in the participants section', async () => {
appMockRender = createAppMockRenderer({ license: platinumLicense });
const result = appMockRender.render(
<CaseViewActivity
{...caseProps}
caseData={{
...caseProps.caseData,
assignees: caseUsers.assignees.map((assignee) => ({
uid: assignee.uid ?? 'not-valid',
})),
}}
/>
);

const assigneesSection = within(await result.findByTestId('case-view-assignees'));

await waitFor(() => {
expect(assigneesSection.getByText('Unknown')).toBeInTheDocument();
expect(assigneesSection.getByText('Fuzzy Marten')).toBeInTheDocument();
expect(assigneesSection.getByText('elastic')).toBeInTheDocument();
expect(assigneesSection.getByText('Misty Mackerel')).toBeInTheDocument();
});
});
});

describe('User actions', () => {
it('renders the descriptions user correctly', async () => {
appMockRender = createAppMockRenderer();
const result = appMockRender.render(<CaseViewActivity {...caseProps} />);

const description = within(result.getByTestId('description-action'));

await waitFor(() => {
expect(description.getByText('Leslie Knope')).toBeInTheDocument();
});
});

it('renders the unassigned users correctly', async () => {
useFindCaseUserActionsMock.mockReturnValue({
...defaultUseFindCaseUserActions,
data: {
userActions: [getUserAction(ActionTypes.assignees, 'delete')],
},
});

appMockRender = createAppMockRenderer();
const result = appMockRender.render(<CaseViewActivity {...caseProps} />);

const userActions = within(result.getByTestId('user-actions'));

await waitFor(() => {
expect(userActions.getByText('cases_no_connectors')).toBeInTheDocument();
expect(userActions.getByText('Valid Chimpanzee')).toBeInTheDocument();
});
});

it('renders the assigned users correctly', async () => {
useFindCaseUserActionsMock.mockReturnValue({
...defaultUseFindCaseUserActions,
data: {
userActions: [
getUserAction(ActionTypes.assignees, 'add', {
payload: {
assignees: [
{ uid: 'not-valid' },
{ uid: 'u_3OgKOf-ogtr8kJ5B0fnRcqzXs2aQQkZLtzKEEFnKaYg_0' },
],
},
}),
],
},
});

appMockRender = createAppMockRenderer();
const result = appMockRender.render(<CaseViewActivity {...caseProps} />);

const userActions = within(result.getByTestId('user-actions'));

await waitFor(() => {
expect(userActions.getByText('Fuzzy Marten')).toBeInTheDocument();
expect(userActions.getByText('Unknown')).toBeInTheDocument();
});
});

it('renders the user action users correctly', async () => {
useFindCaseUserActionsMock.mockReturnValue({
...defaultUseFindCaseUserActions,
data: {
userActions: [
getUserAction('description', 'create'),
getUserAction('description', 'update', {
createdBy: {
...caseUsers.participants[0].user,
fullName: caseUsers.participants[0].user.full_name,
profileUid: caseUsers.participants[0].uid,
},
}),
getUserAction('comment', 'update', {
createdBy: {
...caseUsers.participants[1].user,
fullName: caseUsers.participants[1].user.full_name,
profileUid: caseUsers.participants[1].uid,
},
}),
getUserAction('description', 'update', {
createdBy: {
...caseUsers.participants[2].user,
fullName: caseUsers.participants[2].user.full_name,
profileUid: caseUsers.participants[2].uid,
},
}),
getUserAction('title', 'update', {
createdBy: {
...caseUsers.participants[3].user,
fullName: caseUsers.participants[3].user.full_name,
profileUid: caseUsers.participants[3].uid,
},
}),
getUserAction('tags', 'add', {
createdBy: {
...caseUsers.participants[4].user,
fullName: caseUsers.participants[4].user.full_name,
profileUid: caseUsers.participants[4].uid,
},
}),
],
},
});

appMockRender = createAppMockRenderer();
const result = appMockRender.render(<CaseViewActivity {...caseProps} />);

const userActions = within(result.getByTestId('user-actions'));

await waitFor(() => {
expect(userActions.getByText('Participant 1')).toBeInTheDocument();
expect(userActions.getByText('[email protected]')).toBeInTheDocument();
expect(userActions.getByText('participant_3')).toBeInTheDocument();
expect(userActions.getByText('P4')).toBeInTheDocument();
expect(userActions.getByText('Participant 5')).toBeInTheDocument();
});
});
});
});
});
51 changes: 38 additions & 13 deletions x-pack/plugins/cases/public/containers/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,34 +964,59 @@ export const getCaseUsersMockResponse = (): CaseUsers => {
participants: [
{
user: {
email: '[email protected]',
full_name: 'Cases',
username: 'cases_all',
email: '[email protected]',
full_name: 'Participant 1',
username: 'participant_1',
},
},
{
user: {
email: '[email protected]',
full_name: null,
username: 'participant_2',
},
},
{
user: {
email: null,
full_name: null,
username: 'elastic',
username: 'participant_3',
},
uid: 'u_mGBROF_q5bmFCATbLXAcCwKa0k8JvONAwSruelyKA5E_0',
},
{
user: {
email: null,
full_name: null,
username: 'participant_4',
},
uid: 'participant_4_uid',
avatar: { initials: 'P4' },
},
{
user: {
email: '[email protected]',
full_name: 'Participant 5',
username: 'participant_5',
},
uid: 'participant_5_uid',
},
],
reporter: {
user: {
email: 'case_all@elastic.co',
full_name: 'Cases',
username: 'cases_all',
email: 'reporter_1@elastic.co',
full_name: 'Reporter 1',
username: 'reporter_1',
},
uid: 'reporter_1_uid',
avatar: { initials: 'R1' },
},

assignees: [
{
user: {
email: '',
full_name: '',
username: 'alerts_read_only',
email: null,
full_name: null,
username: null,
},
uid: 'u_62h24XVQzG4-MuH1-DqPmookrJY23aRa9h4fyULR6I8_0',
},
Expand Down Expand Up @@ -1027,15 +1052,15 @@ export const getCaseUsersMockResponse = (): CaseUsers => {
full_name: '',
username: 'cases_no_connectors',
},
uid: 'u_o0kPgaXwJ7odc3sNWH83yx9JMoVbou_z2CgIEhl2I8M_0',
uid: 'u_J41Oh6L9ki-Vo2tOogS8WRTENzhHurGtRc87NgEAlkc_0',
},
{
user: {
email: '[email protected]',
full_name: 'Valid Chimpanzee',
username: 'valid_chimpanzee',
},
uid: 'u_wEtPffVJIgxWIN8W79Rp84bbORN6x0wKiJ5eTsQvpBA_0',
uid: 'u_A_tM4n0wPkdiQ9smmd8o0Hr_h61XQfu8aRPh9GMoRoc_0',
},
],
};
Expand Down

0 comments on commit d8e94d8

Please sign in to comment.