Skip to content

Commit 0b741d1

Browse files
committed
fix(ga): issue #606 - update unit test
1 parent bb336bb commit 0b741d1

File tree

4 files changed

+523
-54
lines changed

4 files changed

+523
-54
lines changed

packages/webapp/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"esbuild-plugin-replace": "1.4.0",
3434
"esbuild-plugin-svgr": "2.1.0",
3535
"esbuild-sass-plugin": "2.16.1",
36-
"html-react-parser": "4.2.2",
36+
"html-react-parser": "5.1.12",
3737
"i18next": "23.7.16",
3838
"jest-css-modules-transform": "4.4.2",
3939
"jest-environment-jsdom": "29.7.0",

packages/webapp/test/Header/SearchField.test.tsx

+49-19
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,38 @@ import React from 'react';
33
import { useSelector } from 'react-redux';
44
import { render, fireEvent, cleanup } from '@testing-library/react';
55
import { screen } from '@testing-library/dom';
6+
import { initIcons } from '@sap-ux/ui-components';
7+
68
import { SearchField } from '../../src/webview/ui/components/Header/SearchField';
79
import { initI18n } from '../../src/webview/i18n';
810

11+
import * as actions from '../../src/webview/state/actions';
12+
13+
const mockState = {
14+
activeGuidedAnswerNode: [],
15+
guidedAnswerTreeSearchResult: {
16+
trees: [treeMock],
17+
resultSize: 1,
18+
productFilters: [],
19+
componentFilters: []
20+
},
21+
query: 'fiori tools',
22+
guideFeedback: true,
23+
selectedProductFilters: ['ProductFilter1, ProductFilter2'],
24+
selectedComponentFilters: ['ComponentFilter1', 'ComponentFilter2'],
25+
activeScreen: 'SEARCH'
26+
};
27+
928
jest.useFakeTimers();
1029
jest.spyOn(global, 'setTimeout');
1130

1231
jest.mock('../../src/webview/state', () => {
1332
return {
1433
actions: {
1534
searchTree: jest.fn(),
16-
setQueryValue: jest.fn(),
35+
setQueryValue: (newValue: string) => {
36+
mockState.query = newValue;
37+
},
1738
parseUrl: jest.fn()
1839
}
1940
};
@@ -24,21 +45,7 @@ jest.mock('react-redux', () => ({
2445
}));
2546

2647
describe('<SearchField />', () => {
27-
const mockState = {
28-
activeGuidedAnswerNode: [],
29-
guidedAnswerTreeSearchResult: {
30-
trees: [treeMock],
31-
resultSize: 1,
32-
productFilters: [],
33-
componentFilters: []
34-
},
35-
query: 'fiori tools',
36-
guideFeedback: true,
37-
selectedProductFilters: ['ProductFilter1, ProductFilter2'],
38-
selectedComponentFilters: ['ComponentFilter1', 'ComponentFilter2'],
39-
activeScreen: 'SEARCH'
40-
};
41-
48+
initIcons();
4249
initI18n();
4350
afterEach(cleanup);
4451

@@ -47,17 +54,40 @@ describe('<SearchField />', () => {
4754

4855
const { container } = render(<SearchField />);
4956
expect(container).toMatchSnapshot();
57+
});
58+
59+
it('Should render a SearchField component, on home screen', () => {
60+
(useSelector as jest.Mock).mockImplementation((selector) => selector({ ...mockState, activeScreen: 'HOME' }));
61+
62+
const { container } = render(<SearchField />);
63+
expect(container).toMatchSnapshot();
64+
});
65+
66+
it('Should render a SearchField component, search value entered', () => {
67+
(useSelector as jest.Mock).mockImplementation((selector) => selector(mockState));
68+
69+
const { container } = render(<SearchField />);
70+
expect(container).toMatchSnapshot();
5071

5172
//Test click event
5273
const element = screen.getByTestId('search-field');
5374
fireEvent.input(element, { target: { value: 'Fiori Tools' } });
54-
expect(setTimeout).toHaveBeenCalledTimes(2);
75+
expect(setTimeout).toHaveBeenCalledTimes(1);
76+
77+
expect(mockState.query).toEqual('Fiori Tools');
5578
});
5679

57-
it('Should render a SearchField component, on home screen', () => {
58-
(useSelector as jest.Mock).mockImplementation((selector) => selector({ ...mockState, activeScreen: 'HOME' }));
80+
it('Should render a SearchField component, search value entered', () => {
81+
(useSelector as jest.Mock).mockImplementation((selector) => selector(mockState));
5982

6083
const { container } = render(<SearchField />);
6184
expect(container).toMatchSnapshot();
85+
86+
//Test click event
87+
const element = screen.getByTestId('search-field');
88+
fireEvent.input(element, { target: { value: '' } });
89+
expect(setTimeout).toHaveBeenCalledTimes(1);
90+
91+
expect(mockState.query).toEqual('');
6292
});
6393
});

0 commit comments

Comments
 (0)