3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
- /**
7
- * @jest -environment jsdom
8
- */
9
-
10
6
import React from 'react' ;
11
7
import { render , screen } from '@testing-library/react' ;
12
8
import { MemoryRouter , Route } from 'react-router-dom' ;
13
9
import '@testing-library/jest-dom/extend-expect' ;
14
- import { WorkloadManagement , WLM_MAIN , WLM_DETAILS } from './WorkloadManagement' ;
10
+ import { WorkloadManagement , WLM_MAIN , WLM_DETAILS , WLM_CREATE } from './WorkloadManagement' ;
15
11
import { CoreStart } from 'opensearch-dashboards/public' ;
16
12
import { QueryInsightsDashboardsPluginStartDependencies } from '../../types' ;
17
-
18
- // Mock the subcomponents
19
- jest . mock ( './WLMMain/WLMMain' , ( ) => ( ) => < div > Mocked WLM Main</ div > ) ;
20
- jest . mock ( './WLMDetails/WLMDetails' , ( ) => ( ) => < div > Mocked WLM Details</ div > ) ;
13
+ import { DataSourceContext } from './WorkloadManagement' ;
21
14
22
15
const mockCore = ( {
23
16
http : {
@@ -27,32 +20,69 @@ const mockCore = ({
27
20
uiSettings : {
28
21
get : jest . fn ( ) . mockReturnValue ( false ) ,
29
22
} ,
23
+ chrome : {
24
+ setBreadcrumbs : jest . fn ( ) ,
25
+ } ,
26
+ notifications : {
27
+ toasts : {
28
+ addSuccess : jest . fn ( ) ,
29
+ addDanger : jest . fn ( ) ,
30
+ } ,
31
+ } ,
30
32
} as unknown ) as CoreStart ;
31
33
32
- const mockDepsStart : QueryInsightsDashboardsPluginStartDependencies = { } ;
34
+ const mockDepsStart = { } as QueryInsightsDashboardsPluginStartDependencies ;
35
+
36
+ const mockDataSource = {
37
+ id : 'default' ,
38
+ name : 'default' ,
39
+ } as any ;
40
+
41
+ jest . mock ( '../../components/PageHeader' , ( ) => ( {
42
+ PageHeader : ( ) => < div data-testid = "mock-page-header" > Mocked PageHeader</ div > ,
43
+ } ) ) ;
33
44
34
- const renderWithRoute = ( initialRoute : string ) =>
35
- render (
45
+ const renderWithRoute = ( initialRoute : string ) => {
46
+ return render (
36
47
< MemoryRouter initialEntries = { [ initialRoute ] } >
37
48
< Route path = "*" >
38
- < WorkloadManagement core = { mockCore } depsStart = { mockDepsStart } />
49
+ < DataSourceContext . Provider value = { { dataSource : mockDataSource , setDataSource : jest . fn ( ) } } >
50
+ < WorkloadManagement
51
+ core = { mockCore }
52
+ depsStart = { mockDepsStart }
53
+ params = { { } as any }
54
+ dataSourceManagement = { { } as any }
55
+ />
56
+ </ DataSourceContext . Provider >
39
57
</ Route >
40
58
</ MemoryRouter >
41
59
) ;
60
+ } ;
42
61
43
62
describe ( 'WorkloadManagement Routing' , ( ) => {
63
+ beforeEach ( ( ) => {
64
+ jest . clearAllMocks ( ) ;
65
+ } ) ;
66
+
44
67
it ( 'renders WLMMain component at WLM_MAIN route' , ( ) => {
45
68
renderWithRoute ( WLM_MAIN ) ;
46
- expect ( screen . getByText ( 'Mocked WLM Main' ) ) . toBeInTheDocument ( ) ;
69
+ expect ( screen . getByRole ( 'heading' , { name : / W o r k l o a d g r o u p s / i } ) ) . toBeInTheDocument ( ) ;
47
70
} ) ;
48
71
49
- it ( 'renders WLMDetails component at WLM_DETAILS route' , ( ) => {
50
- renderWithRoute ( WLM_DETAILS ) ;
51
- expect ( screen . getByText ( 'Mocked WLM Details' ) ) . toBeInTheDocument ( ) ;
72
+ it ( 'renders WLMDetails component at WLM_DETAILS route' , async ( ) => {
73
+ renderWithRoute ( ` ${ WLM_DETAILS } ?name=DEFAULT_WORKLOAD_GROUP` ) ;
74
+ expect ( await screen . findByRole ( 'heading' , { name : / D E F A U L T _ W O R K L O A D _ G R O U P / i } ) ) . toBeInTheDocument ( ) ;
52
75
} ) ;
53
76
54
77
it ( 'redirects to WLM_MAIN for unknown routes' , ( ) => {
55
- renderWithRoute ( '/some/invalid/route' ) ;
56
- expect ( screen . getByText ( 'Mocked WLM Main' ) ) . toBeInTheDocument ( ) ;
78
+ renderWithRoute ( '/invalid/route' ) ;
79
+ expect ( screen . getByRole ( 'heading' , { name : / W o r k l o a d g r o u p s / i } ) ) . toBeInTheDocument ( ) ;
80
+ } ) ;
81
+
82
+ it ( 'renders WLMCreate component at WLM_CREATE route' , ( ) => {
83
+ renderWithRoute ( WLM_CREATE ) ;
84
+ expect ( screen . getByRole ( 'heading' , { name : / C r e a t e w o r k l o a d g r o u p / i } ) ) . toBeInTheDocument ( ) ;
57
85
} ) ;
86
+
58
87
} ) ;
88
+
0 commit comments