From d8e94d8afe2ead581708b2e85448c8a2331c7a71 Mon Sep 17 00:00:00 2001 From: Christos Nasikas Date: Thu, 9 Feb 2023 14:10:31 +0200 Subject: [PATCH] Add unit tests --- .../components/case_view_activity.test.tsx | 248 +++++++++++++++++- .../plugins/cases/public/containers/mock.ts | 51 +++- 2 files changed, 283 insertions(+), 16 deletions(-) diff --git a/x-pack/plugins/cases/public/components/case_view/components/case_view_activity.test.tsx b/x-pack/plugins/cases/public/components/case_view/components/case_view_activity.test.tsx index be19da005ba6a..fa2f89e283700 100644 --- a/x-pack/plugins/cases/public/components/case_view/components/case_view_activity.test.tsx +++ b/x-pack/plugins/cases/public/components/case_view/components/case_view_activity.test.tsx @@ -10,6 +10,7 @@ import { basicCase, connectorsMock, getCaseUsersMockResponse, + getUserAction, } from '../../../containers/mock'; import React from 'react'; import type { AppMockRenderer } from '../../../common/mock'; @@ -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'); @@ -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; @@ -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 () => { @@ -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(); + const participantsSection = within(result.getByTestId('case-view-user-list-participants')); + + await waitFor(() => { + expect(participantsSection.getByText('Participant 1')).toBeInTheDocument(); + expect(participantsSection.getByText('participant_2@elastic.co')).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(); + + 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(); + + 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(); + 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: 'reporter_no_uid@elastic.co', + full_name: 'Reporter No UID', + username: 'reporter_no_uid', + }, + }, + }, + }); + + appMockRender = createAppMockRenderer(); + const result = appMockRender.render(); + 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(); + 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( + ({ + 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(); + + 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(); + + 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(); + + 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(); + + const userActions = within(result.getByTestId('user-actions')); + + await waitFor(() => { + expect(userActions.getByText('Participant 1')).toBeInTheDocument(); + expect(userActions.getByText('participant_2@elastic.co')).toBeInTheDocument(); + expect(userActions.getByText('participant_3')).toBeInTheDocument(); + expect(userActions.getByText('P4')).toBeInTheDocument(); + expect(userActions.getByText('Participant 5')).toBeInTheDocument(); + }); + }); + }); + }); }); diff --git a/x-pack/plugins/cases/public/containers/mock.ts b/x-pack/plugins/cases/public/containers/mock.ts index 63de3dca12e13..0d51a671a3ce0 100644 --- a/x-pack/plugins/cases/public/containers/mock.ts +++ b/x-pack/plugins/cases/public/containers/mock.ts @@ -964,34 +964,59 @@ export const getCaseUsersMockResponse = (): CaseUsers => { participants: [ { user: { - email: 'case_all@elastic.co', - full_name: 'Cases', - username: 'cases_all', + email: 'participant_1@elastic.co', + full_name: 'Participant 1', + username: 'participant_1', + }, + }, + { + user: { + email: 'participant_2@elastic.co', + 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: 'participant_5@elastic.co', + 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', }, @@ -1027,7 +1052,7 @@ export const getCaseUsersMockResponse = (): CaseUsers => { full_name: '', username: 'cases_no_connectors', }, - uid: 'u_o0kPgaXwJ7odc3sNWH83yx9JMoVbou_z2CgIEhl2I8M_0', + uid: 'u_J41Oh6L9ki-Vo2tOogS8WRTENzhHurGtRc87NgEAlkc_0', }, { user: { @@ -1035,7 +1060,7 @@ export const getCaseUsersMockResponse = (): CaseUsers => { full_name: 'Valid Chimpanzee', username: 'valid_chimpanzee', }, - uid: 'u_wEtPffVJIgxWIN8W79Rp84bbORN6x0wKiJ5eTsQvpBA_0', + uid: 'u_A_tM4n0wPkdiQ9smmd8o0Hr_h61XQfu8aRPh9GMoRoc_0', }, ], };