55 * 2.0.
66 */
77
8- import { mount , type ComponentType as EnzymeComponentType } from 'enzyme' ;
8+ import { render , waitFor } from '@testing-library/react' ;
9+ import { userEvent } from '@testing-library/user-event' ;
910import { AlertContextMenu } from './alert_context_menu' ;
1011import { TestProviders } from '../../../../common/mock' ;
1112import React from 'react' ;
@@ -104,72 +105,89 @@ jest.mock('../../../containers/detection_engine/alerts/use_alerts_privileges', (
104105 useAlertsPrivileges : jest . fn ( ) . mockReturnValue ( { hasIndexWrite : true , hasKibanaCRUD : true } ) ,
105106} ) ) ;
106107
107- const actionMenuButton = '[data-test-subj=" timeline-context-menu-button"] button' ;
108- const addToExistingCaseButton = '[data-test-subj=" add-to-existing-case-action"] ' ;
109- const addToNewCaseButton = '[data-test-subj=" add-to-new-case-action"] ' ;
110- const markAsOpenButton = '[data-test-subj=" open-alert-status"] ' ;
111- const markAsAcknowledgedButton = '[data-test-subj=" acknowledged-alert-status"] ' ;
112- const markAsClosedButton = '[data-test-subj=" close-alert-status"] ' ;
113- const addEndpointEventFilterButton = '[data-test-subj=" add-event-filter-menu-item"] ' ;
114- const applyAlertTagsButton = '[data-test-subj=" alert-tags-context-menu-item"] ' ;
115- const applyAlertAssigneesButton = '[data-test-subj=" alert-assignees-context-menu-item"] ' ;
108+ const actionMenuButton = 'timeline-context-menu-button' ;
109+ const addToExistingCaseButton = 'add-to-existing-case-action' ;
110+ const addToNewCaseButton = 'add-to-new-case-action' ;
111+ const markAsOpenButton = 'open-alert-status' ;
112+ const markAsAcknowledgedButton = 'acknowledged-alert-status' ;
113+ const markAsClosedButton = 'close-alert-status' ;
114+ const addEndpointEventFilterButton = 'add-event-filter-menu-item' ;
115+ const applyAlertTagsButton = 'alert-tags-context-menu-item' ;
116+ const applyAlertAssigneesButton = 'alert-assignees-context-menu-item' ;
116117
117118describe ( 'Alert table context menu' , ( ) => {
118119 describe ( 'Case actions' , ( ) => {
119- test ( 'it render AddToCase context menu item if timelineId === TimelineId.detectionsPage' , ( ) => {
120- const wrapper = mount ( < AlertContextMenu { ...props } scopeId = { TableId . alertsOnAlertsPage } /> , {
121- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
122- } ) ;
120+ test ( 'it render AddToCase context menu item if timelineId === TimelineId.detectionsPage' , async ( ) => {
121+ const wrapper = render (
122+ < TestProviders >
123+ < AlertContextMenu { ...props } scopeId = { TableId . alertsOnAlertsPage } />
124+ </ TestProviders >
125+ ) ;
126+
127+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
123128
124- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
125- expect ( wrapper . find ( addToExistingCaseButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
126- expect ( wrapper . find ( addToNewCaseButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
129+ await waitFor ( ( ) => {
130+ expect ( wrapper . getByTestId ( addToExistingCaseButton ) ) . toBeTruthy ( ) ;
131+ expect ( wrapper . getByTestId ( addToNewCaseButton ) ) . toBeTruthy ( ) ;
132+ } ) ;
127133 } ) ;
128134
129- test ( 'it render AddToCase context menu item if timelineId === TimelineId.detectionsRulesDetailsPage' , ( ) => {
130- const wrapper = mount (
131- < AlertContextMenu { ...props } scopeId = { TableId . alertsOnRuleDetailsPage } /> ,
132- {
133- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
134- }
135+ test ( 'it render AddToCase context menu item if timelineId === TimelineId.detectionsRulesDetailsPage' , async ( ) => {
136+ const wrapper = render (
137+ < TestProviders >
138+ < AlertContextMenu { ...props } scopeId = { TableId . alertsOnRuleDetailsPage } />
139+ </ TestProviders >
135140 ) ;
136141
137- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
138- expect ( wrapper . find ( addToExistingCaseButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
139- expect ( wrapper . find ( addToNewCaseButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
140- } ) ;
142+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
141143
142- test ( 'it render AddToCase context menu item if timelineId === TimelineId.active' , ( ) => {
143- const wrapper = mount ( < AlertContextMenu { ... props } scopeId = { TimelineId . active } /> , {
144- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
144+ await waitFor ( ( ) => {
145+ expect ( wrapper . getByTestId ( addToExistingCaseButton ) ) . toBeTruthy ( ) ;
146+ expect ( wrapper . getByTestId ( addToNewCaseButton ) ) . toBeTruthy ( ) ;
145147 } ) ;
146-
147- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
148- expect ( wrapper . find ( addToExistingCaseButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
149- expect ( wrapper . find ( addToNewCaseButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
150148 } ) ;
151149
152- test ( 'it does NOT render AddToCase context menu item when timelineId is not in the allowed list' , ( ) => {
153- const wrapper = mount ( < AlertContextMenu { ...props } scopeId = "timeline-test" /> , {
154- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
150+ test ( 'it render AddToCase context menu item if timelineId === TimelineId.active' , async ( ) => {
151+ const wrapper = render (
152+ < TestProviders >
153+ < AlertContextMenu { ...props } scopeId = { TimelineId . active } />
154+ </ TestProviders >
155+ ) ;
156+
157+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
158+
159+ await waitFor ( ( ) => {
160+ expect ( wrapper . getByTestId ( addToExistingCaseButton ) ) . toBeTruthy ( ) ;
161+ expect ( wrapper . getByTestId ( addToNewCaseButton ) ) . toBeTruthy ( ) ;
155162 } ) ;
156- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
157- expect ( wrapper . find ( addToExistingCaseButton ) . first ( ) . exists ( ) ) . toEqual ( false ) ;
158- expect ( wrapper . find ( addToNewCaseButton ) . first ( ) . exists ( ) ) . toEqual ( false ) ;
163+ } ) ;
164+
165+ test ( 'it does NOT render AddToCase context menu item when timelineId is not in the allowed list' , async ( ) => {
166+ const wrapper = render (
167+ < TestProviders >
168+ < AlertContextMenu { ...props } scopeId = "timeline-test" />
169+ </ TestProviders >
170+ ) ;
171+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
172+
173+ expect ( wrapper . queryByTestId ( addToExistingCaseButton ) ) . toBeNull ( ) ;
174+ expect ( wrapper . queryByTestId ( addToNewCaseButton ) ) . toBeNull ( ) ;
159175 } ) ;
160176 } ) ;
161177
162178 describe ( 'Alert status actions' , ( ) => {
163- test ( 'it renders the correct status action buttons' , ( ) => {
164- const wrapper = mount ( < AlertContextMenu { ...props } scopeId = { TimelineId . active } /> , {
165- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
166- } ) ;
179+ test ( 'it renders the correct status action buttons' , async ( ) => {
180+ const wrapper = render (
181+ < TestProviders >
182+ < AlertContextMenu { ...props } scopeId = { TimelineId . active } />
183+ </ TestProviders >
184+ ) ;
167185
168- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
186+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
169187
170- expect ( wrapper . find ( markAsOpenButton ) . first ( ) . exists ( ) ) . toEqual ( false ) ;
171- expect ( wrapper . find ( markAsAcknowledgedButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
172- expect ( wrapper . find ( markAsClosedButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
188+ expect ( wrapper . queryByTestId ( markAsOpenButton ) ) . toBeNull ( ) ;
189+ expect ( wrapper . getByTestId ( markAsAcknowledgedButton ) ) . toBeInTheDocument ( ) ;
190+ expect ( wrapper . getByTestId ( markAsClosedButton ) ) . toBeInTheDocument ( ) ;
173191 } ) ;
174192 } ) ;
175193
@@ -188,81 +206,87 @@ describe('Alert table context menu', () => {
188206 } ) ;
189207 } ) ;
190208
191- test ( 'it disables AddEndpointEventFilter when timeline id is not host events page' , ( ) => {
192- const wrapper = mount (
193- < AlertContextMenu { ...endpointEventProps } scopeId = { TimelineId . active } /> ,
194- {
195- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
196- }
209+ test ( 'it disables AddEndpointEventFilter when timeline id is not host events page' , async ( ) => {
210+ const wrapper = render (
211+ < TestProviders >
212+ < AlertContextMenu { ...endpointEventProps } scopeId = { TimelineId . active } />
213+ </ TestProviders >
197214 ) ;
198215
199- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
200- expect ( wrapper . find ( addEndpointEventFilterButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
201- expect ( wrapper . find ( addEndpointEventFilterButton ) . first ( ) . props ( ) . disabled ) . toEqual ( true ) ;
216+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
217+
218+ const button = wrapper . getByTestId ( addEndpointEventFilterButton ) ;
219+
220+ expect ( button ) . toBeInTheDocument ( ) ;
221+ expect ( button ) . toBeDisabled ( ) ;
202222 } ) ;
203223
204- test ( 'it enables AddEndpointEventFilter when timeline id is host events page' , ( ) => {
205- const wrapper = mount (
206- < AlertContextMenu { ...endpointEventProps } scopeId = { TableId . hostsPageEvents } /> ,
207- {
208- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
209- }
224+ test ( 'it enables AddEndpointEventFilter when timeline id is host events page' , async ( ) => {
225+ const wrapper = render (
226+ < TestProviders >
227+ < AlertContextMenu { ...endpointEventProps } scopeId = { TableId . hostsPageEvents } />
228+ </ TestProviders >
210229 ) ;
211230
212- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
213- expect ( wrapper . find ( addEndpointEventFilterButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
214- expect ( wrapper . find ( addEndpointEventFilterButton ) . first ( ) . props ( ) . disabled ) . toEqual (
215- false
216- ) ;
231+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
232+
233+ const button = wrapper . getByTestId ( addEndpointEventFilterButton ) ;
234+
235+ expect ( button ) . toBeInTheDocument ( ) ;
236+ expect ( button ) . not . toBeDisabled ( ) ;
217237 } ) ;
218238
219- test ( 'it disables AddEndpointEventFilter when timeline id is host events page but is not from endpoint' , ( ) => {
239+ test ( 'it disables AddEndpointEventFilter when timeline id is host events page but is not from endpoint' , async ( ) => {
220240 const customProps = {
221241 ...props ,
222242 ecsRowData : { ...ecsRowData , agent : { type : [ 'other' ] } , event : { kind : [ 'event' ] } } ,
223243 } ;
224- const wrapper = mount (
225- < AlertContextMenu { ...customProps } scopeId = { TableId . hostsPageEvents } /> ,
226- {
227- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
228- }
244+ const wrapper = render (
245+ < TestProviders >
246+ < AlertContextMenu { ...customProps } scopeId = { TableId . hostsPageEvents } />
247+ </ TestProviders >
229248 ) ;
230249
231- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
232- expect ( wrapper . find ( addEndpointEventFilterButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
233- expect ( wrapper . find ( addEndpointEventFilterButton ) . first ( ) . props ( ) . disabled ) . toEqual ( true ) ;
250+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
251+
252+ const button = wrapper . getByTestId ( addEndpointEventFilterButton ) ;
253+
254+ expect ( button ) . toBeInTheDocument ( ) ;
255+ expect ( button ) . toBeDisabled ( ) ;
234256 } ) ;
235257
236- test ( 'it enables AddEndpointEventFilter when timeline id is user events page' , ( ) => {
237- const wrapper = mount (
238- < AlertContextMenu { ...endpointEventProps } scopeId = { TableId . usersPageEvents } /> ,
239- {
240- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
241- }
258+ test ( 'it enables AddEndpointEventFilter when timeline id is user events page' , async ( ) => {
259+ const wrapper = render (
260+ < TestProviders >
261+ < AlertContextMenu { ...endpointEventProps } scopeId = { TableId . usersPageEvents } />
262+ </ TestProviders >
242263 ) ;
243264
244- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
245- expect ( wrapper . find ( addEndpointEventFilterButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
246- expect ( wrapper . find ( addEndpointEventFilterButton ) . first ( ) . props ( ) . disabled ) . toEqual (
247- false
248- ) ;
265+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
266+
267+ const button = wrapper . getByTestId ( addEndpointEventFilterButton ) ;
268+
269+ expect ( button ) . toBeInTheDocument ( ) ;
270+ expect ( button ) . not . toBeDisabled ( ) ;
249271 } ) ;
250272
251- test ( 'it disables AddEndpointEventFilter when timeline id is user events page but is not from endpoint' , ( ) => {
273+ test ( 'it disables AddEndpointEventFilter when timeline id is user events page but is not from endpoint' , async ( ) => {
252274 const customProps = {
253275 ...props ,
254276 ecsRowData : { ...ecsRowData , agent : { type : [ 'other' ] } , event : { kind : [ 'event' ] } } ,
255277 } ;
256- const wrapper = mount (
257- < AlertContextMenu { ...customProps } scopeId = { TableId . usersPageEvents } /> ,
258- {
259- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
260- }
278+ const wrapper = render (
279+ < TestProviders >
280+ < AlertContextMenu { ...customProps } scopeId = { TableId . usersPageEvents } />
281+ </ TestProviders >
261282 ) ;
262283
263- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
264- expect ( wrapper . find ( addEndpointEventFilterButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
265- expect ( wrapper . find ( addEndpointEventFilterButton ) . first ( ) . props ( ) . disabled ) . toEqual ( true ) ;
284+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
285+
286+ const button = wrapper . getByTestId ( addEndpointEventFilterButton ) ;
287+
288+ expect ( button ) . toBeInTheDocument ( ) ;
289+ expect ( button ) . toBeDisabled ( ) ;
266290 } ) ;
267291 } ) ;
268292
@@ -274,54 +298,60 @@ describe('Alert table context menu', () => {
274298 } ) ;
275299 } ) ;
276300
277- test ( 'it removes AddEndpointEventFilter option when timeline id is host events page but does not has write event filters privilege' , ( ) => {
278- const wrapper = mount (
279- < AlertContextMenu { ...endpointEventProps } scopeId = { TableId . hostsPageEvents } /> ,
280- {
281- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
282- }
301+ test ( 'it disables actionMenuButton when timeline id is host events page but does not has write event filters privilege' , ( ) => {
302+ const wrapper = render (
303+ < TestProviders >
304+ < AlertContextMenu { ...endpointEventProps } scopeId = { TableId . hostsPageEvents } />
305+ </ TestProviders >
283306 ) ;
284307
285- // Entire actionMenuButton is removed as there is no option available
286- expect ( wrapper . find ( actionMenuButton ) . first ( ) . exists ( ) ) . toEqual ( false ) ;
308+ // <TestProviders> Entire actionMenuButton is disabled as there is no option available
309+ expect ( wrapper . getByTestId ( actionMenuButton ) ) . toBeDisabled ( ) ;
287310 } ) ;
288311
289- test ( 'it removes AddEndpointEventFilter option when timeline id is user events page but does not has write event filters privilege' , ( ) => {
290- const wrapper = mount (
291- < AlertContextMenu { ...endpointEventProps } scopeId = { TableId . usersPageEvents } /> ,
292- {
293- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
294- }
312+ test ( 'it disables actionMenuButton when timeline id is user events page but does not has write event filters privilege' , ( ) => {
313+ const wrapper = render (
314+ < TestProviders >
315+ < AlertContextMenu { ...endpointEventProps } scopeId = { TableId . usersPageEvents } />
316+ </ TestProviders >
295317 ) ;
296318
297- // Entire actionMenuButton is removed as there is no option available
298- expect ( wrapper . find ( actionMenuButton ) . first ( ) . exists ( ) ) . toEqual ( false ) ;
319+ // <TestProviders> Entire actionMenuButton is disabled as there is no option available
320+ expect ( wrapper . getByTestId ( actionMenuButton ) ) . toBeDisabled ( ) ;
299321 } ) ;
300322 } ) ;
301323 } ) ;
302- } ) ;
303324
304- describe ( 'Apply alert tags action' , ( ) => {
305- test ( 'it renders the apply alert tags action button' , ( ) => {
306- const wrapper = mount ( < AlertContextMenu { ...props } scopeId = { TimelineId . active } /> , {
307- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
308- } ) ;
325+ describe ( 'Apply alert tags action' , ( ) => {
326+ test ( 'it renders the apply alert tags action button' , async ( ) => {
327+ const wrapper = render (
328+ < TestProviders >
329+ < AlertContextMenu { ...props } scopeId = { TimelineId . active } />
330+ </ TestProviders >
331+ ) ;
309332
310- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
333+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
311334
312- expect ( wrapper . find ( applyAlertTagsButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
335+ await waitFor ( ( ) => {
336+ expect ( wrapper . getByTestId ( applyAlertTagsButton ) ) . toBeTruthy ( ) ;
337+ } ) ;
338+ } ) ;
313339 } ) ;
314- } ) ;
315340
316- describe ( 'Assign alert action' , ( ) => {
317- test ( 'it renders the assign alert action button' , ( ) => {
318- const wrapper = mount ( < AlertContextMenu { ...props } scopeId = { TimelineId . active } /> , {
319- wrappingComponent : TestProviders as EnzymeComponentType < { } > ,
320- } ) ;
341+ describe ( 'Assign alert action' , ( ) => {
342+ test ( 'it renders the assign alert action button' , async ( ) => {
343+ const wrapper = render (
344+ < TestProviders >
345+ < AlertContextMenu { ...props } scopeId = { TimelineId . active } />
346+ </ TestProviders >
347+ ) ;
321348
322- wrapper . find ( actionMenuButton ) . simulate ( 'click' ) ;
349+ await userEvent . click ( wrapper . getByTestId ( actionMenuButton ) ) ;
323350
324- expect ( wrapper . find ( applyAlertAssigneesButton ) . first ( ) . exists ( ) ) . toEqual ( true ) ;
351+ await waitFor ( ( ) => {
352+ expect ( wrapper . getByTestId ( applyAlertAssigneesButton ) ) . toBeTruthy ( ) ;
353+ } ) ;
354+ } ) ;
325355 } ) ;
326356 } ) ;
327357} ) ;
0 commit comments