|
1 | 1 | import React from 'react'; |
2 | | -import { DropDown, Text } from '../src'; |
| 2 | +import * as RN from 'react-native'; |
| 3 | +import { DropDown, DropDownListContainer, gray, Text } from '../src'; |
3 | 4 | import { fireEvent, render, waitFor } from './test-utils'; |
4 | 5 |
|
5 | 6 | describe('DropDown Component', () => { |
6 | 7 | const mockInputWrapperTouchTestId = 'input-wrapper-touch-test-id'; |
| 8 | + const mockInputTestId = 'input-test-id'; |
7 | 9 |
|
8 | 10 | beforeEach(() => { |
9 | 11 | jest.clearAllMocks(); |
@@ -42,4 +44,220 @@ describe('DropDown Component', () => { |
42 | 44 | expect(mockOnDropDownClicked).toHaveBeenCalled(); |
43 | 45 | expect(mockOnDropDownClicked).toHaveBeenCalledTimes(1); |
44 | 46 | }); |
| 47 | + |
| 48 | + it('should render the input component', () => { |
| 49 | + const { getByTestId } = render(<DropDown dropDownInputTestId={mockInputTestId} />); |
| 50 | + |
| 51 | + const input = getByTestId(mockInputTestId); |
| 52 | + expect(input).toBeDefined(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should render icon input component when variation prop passed as icon', () => { |
| 56 | + const { getByTestId } = render(<DropDown variation="icon" dropDownInputTestId={mockInputTestId} />); |
| 57 | + |
| 58 | + const input = getByTestId(mockInputTestId); |
| 59 | + expect(input).toBeDefined(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should show empty value when input component mount', () => { |
| 63 | + const { getByTestId } = render(<DropDown variation="icon" dropDownInputTestId={mockInputTestId} />); |
| 64 | + |
| 65 | + const input = getByTestId(mockInputTestId); |
| 66 | + expect(input.props.value).toEqual(''); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should not render any input component when invalid variation prop passed', () => { |
| 70 | + const { queryByTestId } = render(<DropDown variation={'unknown' as any} dropDownInputTestId={mockInputTestId} />); |
| 71 | + |
| 72 | + const input = queryByTestId(mockInputTestId); |
| 73 | + expect(input).toBeNull(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should show the selected list item title', () => { |
| 77 | + const { getByTestId } = render( |
| 78 | + <DropDown |
| 79 | + data={[{ id: '1', title: 'first_item' }]} |
| 80 | + selectedListItems={[{ id: '1', title: 'first_item' }]} |
| 81 | + variation="icon" |
| 82 | + dropDownInputTestId={mockInputTestId} |
| 83 | + />, |
| 84 | + ); |
| 85 | + |
| 86 | + const input = getByTestId(mockInputTestId); |
| 87 | + |
| 88 | + expect(input.props.value).toEqual('first_item'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should show the multiselect message', () => { |
| 92 | + const { getByTestId } = render( |
| 93 | + <DropDown |
| 94 | + data={[{ id: '1', title: 'first_item' }]} |
| 95 | + selectedListItems={[{ id: '1', title: 'first_item' }]} |
| 96 | + variation="icon" |
| 97 | + multiselect |
| 98 | + dropDownInputTestId={mockInputTestId} |
| 99 | + />, |
| 100 | + ); |
| 101 | + |
| 102 | + const input = getByTestId(mockInputTestId); |
| 103 | + |
| 104 | + expect(input.props.value).toEqual('Selected items 1'); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('DropDownListContainer component', () => { |
| 109 | + const mockListItemTestId = 'mock-list-item-test-id'; |
| 110 | + |
| 111 | + const mockOnItemClickedHandler = jest.fn(); |
| 112 | + const mockOnCloseHandler = jest.fn(); |
| 113 | + |
| 114 | + beforeEach(() => { |
| 115 | + jest.clearAllMocks(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('should render properly with default props', () => { |
| 119 | + const { toJSON } = render( |
| 120 | + <DropDownListContainer |
| 121 | + inputLayoutRectangle={{ x: 0, y: 0, width: 0, height: 0 }} |
| 122 | + dropDownContainerRect={{ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }} |
| 123 | + />, |
| 124 | + ); |
| 125 | + |
| 126 | + expect(toJSON()).toMatchSnapshot(); |
| 127 | + }); |
| 128 | + |
| 129 | + it('should render the list item', () => { |
| 130 | + const { getByTestId } = render( |
| 131 | + <DropDownListContainer |
| 132 | + inputLayoutRectangle={{ x: 0, y: 0, width: 0, height: 0 }} |
| 133 | + dropDownContainerRect={{ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }} |
| 134 | + data={[{ id: '1', title: 'first_item' }]} |
| 135 | + listItemTestId={mockListItemTestId} |
| 136 | + />, |
| 137 | + ); |
| 138 | + |
| 139 | + const listItem = getByTestId(mockListItemTestId); |
| 140 | + expect(listItem).toBeDefined(); |
| 141 | + }); |
| 142 | + |
| 143 | + it('should called the onItemClicked when onItemClicked props will passed and user tap on the list item', () => { |
| 144 | + const { getByTestId } = render( |
| 145 | + <DropDownListContainer |
| 146 | + inputLayoutRectangle={{ x: 0, y: 0, width: 0, height: 0 }} |
| 147 | + dropDownContainerRect={{ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }} |
| 148 | + data={[{ id: '1', title: 'first_item' }]} |
| 149 | + onItemClicked={mockOnItemClickedHandler} |
| 150 | + listItemTestId={mockListItemTestId} |
| 151 | + />, |
| 152 | + ); |
| 153 | + |
| 154 | + const listItem = getByTestId(mockListItemTestId); |
| 155 | + |
| 156 | + fireEvent(listItem, 'press', { nativeEvent: {} }); |
| 157 | + |
| 158 | + expect(mockOnItemClickedHandler).toHaveBeenCalledTimes(1); |
| 159 | + expect(mockOnItemClickedHandler).toHaveBeenCalledWith({ id: '1', title: 'first_item' }); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should show the gray[900] color for title text', () => { |
| 163 | + const { getByText } = render( |
| 164 | + <DropDownListContainer |
| 165 | + inputLayoutRectangle={{ x: 0, y: 0, width: 0, height: 0 }} |
| 166 | + dropDownContainerRect={{ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }} |
| 167 | + data={[{ id: '1', title: 'first_item' }]} |
| 168 | + onItemClicked={mockOnItemClickedHandler} |
| 169 | + listItemTestId={mockListItemTestId} |
| 170 | + />, |
| 171 | + ); |
| 172 | + |
| 173 | + const title = getByText('first_item'); |
| 174 | + expect(title).toBeDefined(); |
| 175 | + expect(title.props.style.color).toEqual(gray[900]); |
| 176 | + }); |
| 177 | + |
| 178 | + it('should show the gray[50] color for title text if item is selected', () => { |
| 179 | + const { getByText } = render( |
| 180 | + <DropDownListContainer |
| 181 | + inputLayoutRectangle={{ x: 0, y: 0, width: 0, height: 0 }} |
| 182 | + dropDownContainerRect={{ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }} |
| 183 | + data={[{ id: '1', title: 'first_item' }]} |
| 184 | + onItemClicked={mockOnItemClickedHandler} |
| 185 | + listItemTestId={mockListItemTestId} |
| 186 | + selectedListItems={[{ id: '1', title: 'first_item' }]} |
| 187 | + showSelectedItem |
| 188 | + />, |
| 189 | + ); |
| 190 | + |
| 191 | + const title = getByText('first_item'); |
| 192 | + expect(title).toBeDefined(); |
| 193 | + expect(title.props.style.color).toEqual(gray[50]); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should show the light color of the title text when color scheme is dark', () => { |
| 197 | + jest.spyOn(RN, 'useColorScheme').mockReturnValue('dark'); |
| 198 | + |
| 199 | + const { getByText } = render( |
| 200 | + <DropDownListContainer |
| 201 | + inputLayoutRectangle={{ x: 0, y: 0, width: 0, height: 0 }} |
| 202 | + dropDownContainerRect={{ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }} |
| 203 | + data={[{ id: '1', title: 'first_item' }]} |
| 204 | + onItemClicked={mockOnItemClickedHandler} |
| 205 | + listItemTestId={mockListItemTestId} |
| 206 | + />, |
| 207 | + ); |
| 208 | + |
| 209 | + const title = getByText('first_item'); |
| 210 | + expect(title).toBeDefined(); |
| 211 | + expect(title.props.style.color).toEqual(gray[900]); |
| 212 | + }); |
| 213 | + |
| 214 | + it('should show the listItemEndAdornment item when list items is isSelected', () => { |
| 215 | + const { getByText } = render( |
| 216 | + <DropDownListContainer |
| 217 | + inputLayoutRectangle={{ x: 0, y: 0, width: 0, height: 0 }} |
| 218 | + dropDownContainerRect={{ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }} |
| 219 | + data={[{ id: '1', title: 'first_item' }]} |
| 220 | + selectedListItems={[{ id: '1', title: 'first_item' }]} |
| 221 | + listItemEndAdornment={<Text>Hello</Text>} |
| 222 | + displaySelectedAdornment |
| 223 | + showSelectedItem |
| 224 | + />, |
| 225 | + ); |
| 226 | + |
| 227 | + const listItemEndAdornment = getByText('Hello'); |
| 228 | + expect(listItemEndAdornment).toBeDefined(); |
| 229 | + }); |
| 230 | + |
| 231 | + it('should render the search bar component', () => { |
| 232 | + const { toJSON, getByPlaceholderText } = render( |
| 233 | + <DropDownListContainer |
| 234 | + inputLayoutRectangle={{ x: 0, y: 0, width: 0, height: 0 }} |
| 235 | + dropDownContainerRect={{ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }} |
| 236 | + search |
| 237 | + />, |
| 238 | + ); |
| 239 | + |
| 240 | + expect(toJSON()).toMatchSnapshot(); |
| 241 | + |
| 242 | + const search = getByPlaceholderText('Search'); |
| 243 | + expect(search).toBeDefined(); |
| 244 | + }); |
| 245 | + |
| 246 | + it('should call the onClose function when press on item', () => { |
| 247 | + const { getByText } = render( |
| 248 | + <DropDownListContainer |
| 249 | + inputLayoutRectangle={{ x: 0, y: 0, width: 0, height: 0 }} |
| 250 | + dropDownContainerRect={{ x: 0, y: 0, width: 0, height: 0, pageX: 0, pageY: 0 }} |
| 251 | + data={[{ id: '1', title: 'first_item' }]} |
| 252 | + onItemClicked={mockOnItemClickedHandler} |
| 253 | + listItemTestId={mockListItemTestId} |
| 254 | + onClose={mockOnCloseHandler} |
| 255 | + />, |
| 256 | + ); |
| 257 | + |
| 258 | + const title = getByText('first_item'); |
| 259 | + fireEvent(title, 'press', { nativeEvent: {} }); |
| 260 | + expect(mockOnCloseHandler).toHaveBeenCalled(); |
| 261 | + expect(mockOnCloseHandler).toHaveBeenCalledTimes(1); |
| 262 | + }); |
45 | 263 | }); |
0 commit comments