Skip to content

Commit c0fcb43

Browse files
committed
feat: add multiSearchInput test
1 parent d736d1b commit c0fcb43

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

jest.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ module.exports = {
44
globals: {
55
},
66
// setupFilesAfterEnv: ["./setupTests.js"], // enzyme adapter
7-
transformIgnorePatterns: ["/node_modules/", "lib", "dist"],
7+
transformIgnorePatterns: ["/node_modules/", "dist"
8+
// "lib",
9+
],
810
testPathIgnorePatterns: ['/node_modules/'],
911
transform: {
1012
"^.+\\.[jt]s?(x)$": "babel-jest"
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import * as React from 'react'
2+
import { render, fireEvent, cleanup } from '@testing-library/react'
3+
import MulSelectDropdown from '../index';
4+
5+
describe('mulSelectDropdown Component test', () => {
6+
afterEach(() => {
7+
cleanup()
8+
})
9+
it('custom filter rendering', () => {
10+
const { queryByTestId } = render(<MulSelectDropdown filterOptions={['front', 'tail']}/>)
11+
expect(queryByTestId('icon-front')).not.toBeNull()
12+
expect(queryByTestId('icon-tail')).not.toBeNull()
13+
expect(queryByTestId('icon-caseSensitive')).toBeNull()
14+
expect(queryByTestId('icon-precise')).toBeNull()
15+
})
16+
it('input value change', () => {
17+
const myMockChange = jest.fn((value) => value)
18+
const { queryByTestId } = render(<MulSelectDropdown
19+
searchType='tail'
20+
onChange={myMockChange}
21+
/>)
22+
fireEvent.change(queryByTestId('input'), { target: { value: '1234567891011' } });
23+
expect(myMockChange).toHaveBeenCalled();
24+
expect(myMockChange.mock.results[0].value).toBe('1234567891011');
25+
})
26+
it('input search', () => {
27+
const myMockSearch = jest.fn((value: any, searchType: any) => { return { value: value, searchType: searchType } })
28+
const { queryByTestId } = render(<MulSelectDropdown
29+
searchType='tail'
30+
value="this is value"
31+
placeholder="hello"
32+
onSearch={myMockSearch}
33+
/>)
34+
fireEvent.keyPress(queryByTestId('input'), { key: 'Enter', code: 'Enter' })
35+
expect(myMockSearch).toHaveBeenCalled();
36+
})
37+
})

src/components/multiSearchInput/index.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ const searchTypeList: any = [
2626
]
2727

2828
export interface MultiSearchInputProps {
29-
placeholder: string;
30-
style: object;
31-
value: any; // input框的值
32-
onChange: any;
33-
onSearch: any;
34-
onTypeChange: any;
35-
searchType: string; // input框中选中的筛选方式
29+
placeholder?: string;
30+
style?: object;
31+
value?: any; // input框的值
32+
onChange?: any;
33+
onSearch?: any;
34+
onTypeChange?: any;
35+
searchType?: string; // input框中选中的筛选方式
3636
filterOptions?: any[]; // 数组
3737
[propName: string]: any;
3838
}
@@ -76,6 +76,7 @@ class MultiSearchInput extends React.Component<MultiSearchInputProps, any> {
7676
}}
7777
>
7878
<Input
79+
data-testid='input'
7980
value={propsValue != null ? propsValue : value}
8081
placeholder={placeholder}
8182
style={{
@@ -87,6 +88,7 @@ class MultiSearchInput extends React.Component<MultiSearchInputProps, any> {
8788
onChange(e.target.value);
8889
}}
8990
onPressEnter={(e: any) => {
91+
console.log('执行了')
9092
onSearch(e.target.value, searchType);
9193
}}
9294
/>
@@ -107,6 +109,8 @@ class MultiSearchInput extends React.Component<MultiSearchInputProps, any> {
107109
_.map(filterList, (item: any) => {
108110
return (
109111
<div
112+
data-testid={`icon-${item.key}`}
113+
key={`icon-${item.key}`}
110114
style={{
111115
cursor: 'pointer',
112116
display: 'block',

src/stories/chromeDownload.stories.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ stories.add('chromeDownload', () => {
3535
<ChromeDownload downloadChrome={() => {}} />
3636
~~~
3737
`,
38-
TableComponent: () => PropsTable({ propDefinitions })
38+
TableComponent: () => PropsTable({ propDefinitions }),
39+
3940
}
4041
})

0 commit comments

Comments
 (0)