Skip to content

Commit 4b8128d

Browse files
saimedhigithub-actions[bot]
authored andcommitted
workflow_detail unit tests (#345)
* workflow_detail tests Signed-off-by: saimedhi <saimedhi@amazon.com> * workflow_detail tests Signed-off-by: saimedhi <saimedhi@amazon.com> * workflow_detail tests Signed-off-by: saimedhi <saimedhi@amazon.com> * workflow_detail tests Signed-off-by: saimedhi <saimedhi@amazon.com> * workflow_detail tests Signed-off-by: saimedhi <saimedhi@amazon.com> * workflow_detail tests Signed-off-by: saimedhi <saimedhi@amazon.com> * workflow_detail tests Signed-off-by: saimedhi <saimedhi@amazon.com> * workflow_detail tests Signed-off-by: saimedhi <saimedhi@amazon.com> --------- Signed-off-by: saimedhi <saimedhi@amazon.com> (cherry picked from commit afb45d6)
1 parent 7dfc375 commit 4b8128d

File tree

3 files changed

+181
-4
lines changed

3 files changed

+181
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import React from 'react';
7+
import { render } from '@testing-library/react';
8+
import { Provider } from 'react-redux';
9+
import {
10+
RouteComponentProps,
11+
Route,
12+
Switch,
13+
Router,
14+
Redirect,
15+
} from 'react-router-dom';
16+
import { WorkflowDetail } from './workflow_detail';
17+
import { WorkflowDetailRouterProps } from '../../pages';
18+
import '@testing-library/jest-dom';
19+
import { mockStore, resizeObserverMock } from '../../../test/utils';
20+
import { createMemoryHistory } from 'history';
21+
import { WORKFLOW_TYPE } from '../../../common';
22+
23+
jest.mock('../../services', () => {
24+
const { mockCoreServices } = require('../../../test');
25+
return {
26+
...jest.requireActual('../../services'),
27+
...mockCoreServices,
28+
};
29+
});
30+
31+
const workflowId = '12345';
32+
const workflowName = 'test_workflow';
33+
34+
const history = createMemoryHistory({
35+
initialEntries: [`/workflow/${workflowId}`],
36+
});
37+
38+
window.ResizeObserver = resizeObserverMock;
39+
40+
const renderWithRouter = (
41+
workflowId: string,
42+
workflowName: string,
43+
workflowType: WORKFLOW_TYPE
44+
) => ({
45+
...render(
46+
<Provider store={mockStore(workflowId, workflowName, workflowType)}>
47+
<Router history={history}>
48+
<Switch>
49+
<Route
50+
path="/workflow/:workflowId"
51+
render={(props: RouteComponentProps<WorkflowDetailRouterProps>) => {
52+
return <WorkflowDetail setActionMenu={jest.fn()} {...props} />;
53+
}}
54+
/>
55+
</Switch>
56+
</Router>
57+
</Provider>
58+
),
59+
});
60+
61+
describe('WorkflowDetail', () => {
62+
Object.values(WORKFLOW_TYPE).forEach((type) => {
63+
test(`renders the page with ${type} type`, () => {
64+
const { getAllByText, getByText, getByRole } = renderWithRouter(
65+
workflowId,
66+
workflowName,
67+
type
68+
);
69+
70+
expect(getAllByText(workflowName).length).toBeGreaterThan(0);
71+
expect(getAllByText('Create an ingest pipeline').length).toBeGreaterThan(
72+
0
73+
);
74+
expect(getAllByText('Skip ingestion pipeline').length).toBeGreaterThan(0);
75+
expect(getAllByText('Define ingest pipeline').length).toBeGreaterThan(0);
76+
expect(getAllByText('Tools').length).toBeGreaterThan(0);
77+
expect(getAllByText('Preview').length).toBeGreaterThan(0);
78+
expect(getAllByText('Not started').length).toBeGreaterThan(0);
79+
expect(
80+
getAllByText((content) => content.startsWith('Last updated:')).length
81+
).toBeGreaterThan(0);
82+
expect(getAllByText('Search pipeline').length).toBeGreaterThan(0);
83+
expect(getByText('Close')).toBeInTheDocument();
84+
expect(getByText('Export')).toBeInTheDocument();
85+
expect(getByText('Visual')).toBeInTheDocument();
86+
expect(getByText('JSON')).toBeInTheDocument();
87+
expect(getByRole('tab', { name: 'Run ingestion' })).toBeInTheDocument();
88+
expect(getByRole('tab', { name: 'Run queries' })).toBeInTheDocument();
89+
expect(getByRole('tab', { name: 'Errors' })).toBeInTheDocument();
90+
expect(getByRole('tab', { name: 'Resources' })).toBeInTheDocument();
91+
});
92+
});
93+
});

public/pages/workflows/new_workflow/utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export function enrichPresetWorkflowWithUiMetadata(
6060
} as WorkflowTemplate;
6161
}
6262

63-
function fetchEmptyMetadata(): UIState {
63+
export function fetchEmptyMetadata(): UIState {
6464
return {
6565
type: WORKFLOW_TYPE.CUSTOM,
6666
config: {
@@ -125,7 +125,7 @@ function fetchEmptyMetadata(): UIState {
125125
};
126126
}
127127

128-
function fetchSemanticSearchMetadata(): UIState {
128+
export function fetchSemanticSearchMetadata(): UIState {
129129
let baseState = fetchEmptyMetadata();
130130
baseState.type = WORKFLOW_TYPE.SEMANTIC_SEARCH;
131131
baseState.config.ingest.enrich.processors = [new MLIngestProcessor().toObj()];
@@ -143,7 +143,7 @@ function fetchSemanticSearchMetadata(): UIState {
143143
return baseState;
144144
}
145145

146-
function fetchMultimodalSearchMetadata(): UIState {
146+
export function fetchMultimodalSearchMetadata(): UIState {
147147
let baseState = fetchEmptyMetadata();
148148
baseState.type = WORKFLOW_TYPE.MULTIMODAL_SEARCH;
149149
baseState.config.ingest.enrich.processors = [new MLIngestProcessor().toObj()];
@@ -163,7 +163,7 @@ function fetchMultimodalSearchMetadata(): UIState {
163163
return baseState;
164164
}
165165

166-
function fetchHybridSearchMetadata(): UIState {
166+
export function fetchHybridSearchMetadata(): UIState {
167167
let baseState = fetchEmptyMetadata();
168168
baseState.type = WORKFLOW_TYPE.HYBRID_SEARCH;
169169
baseState.config.ingest.enrich.processors = [new MLIngestProcessor().toObj()];

test/utils.ts

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { WORKFLOW_TYPE } from '../common/constants';
7+
import { UIState, Workflow } from '../common/interfaces';
8+
import {
9+
fetchEmptyMetadata,
10+
fetchHybridSearchMetadata,
11+
fetchMultimodalSearchMetadata,
12+
fetchSemanticSearchMetadata,
13+
} from '../public/pages/workflows/new_workflow/utils';
14+
15+
export function mockStore(
16+
workflowId: string,
17+
workflowName: string,
18+
workflowType: WORKFLOW_TYPE
19+
) {
20+
return {
21+
getState: () => ({
22+
opensearch: {
23+
errorMessage: '',
24+
},
25+
ml: {},
26+
workflows: {
27+
loading: false,
28+
errorMessage: '',
29+
workflows: {
30+
[workflowId]: generateWorkflow(
31+
workflowId,
32+
workflowName,
33+
workflowType
34+
),
35+
},
36+
},
37+
}),
38+
dispatch: jest.fn(),
39+
subscribe: jest.fn(),
40+
replaceReducer: jest.fn(),
41+
[Symbol.observable]: jest.fn(),
42+
};
43+
}
44+
45+
function generateWorkflow(
46+
workflowId: string,
47+
workflowName: string,
48+
workflowType: WORKFLOW_TYPE
49+
): Workflow {
50+
return {
51+
id: workflowId,
52+
name: workflowName,
53+
version: { template: '1.0.0', compatibility: ['2.17.0', '3.0.0'] },
54+
ui_metadata: getConfig(workflowType),
55+
};
56+
}
57+
function getConfig(workflowType: WORKFLOW_TYPE) {
58+
let uiMetadata = {} as UIState;
59+
switch (workflowType) {
60+
case WORKFLOW_TYPE.SEMANTIC_SEARCH: {
61+
uiMetadata = fetchSemanticSearchMetadata();
62+
break;
63+
}
64+
case WORKFLOW_TYPE.MULTIMODAL_SEARCH: {
65+
uiMetadata = fetchMultimodalSearchMetadata();
66+
break;
67+
}
68+
case WORKFLOW_TYPE.HYBRID_SEARCH: {
69+
uiMetadata = fetchHybridSearchMetadata();
70+
break;
71+
}
72+
default: {
73+
uiMetadata = fetchEmptyMetadata();
74+
break;
75+
}
76+
}
77+
return uiMetadata;
78+
}
79+
80+
export const resizeObserverMock = jest.fn().mockImplementation(() => ({
81+
observe: jest.fn(),
82+
unobserve: jest.fn(),
83+
disconnect: jest.fn(),
84+
}));

0 commit comments

Comments
 (0)