|
47 | 47 | - [`toHaveProp`](#tohaveprop) |
48 | 48 | - [`toHaveTextContent`](#tohavetextcontent) |
49 | 49 | - [`toHaveStyle`](#tohavestyle) |
| 50 | + - [`toBeVisible`](#tobevisible) |
50 | 51 | - [Inspiration](#inspiration) |
51 | 52 | - [Other solutions](#other-solutions) |
52 | 53 | - [Contributors](#contributors) |
@@ -298,6 +299,107 @@ expect(queryByText('Hello World')).toHaveStyle([{ color: 'black' }, { fontWeight |
298 | 299 | expect(queryByText('Hello World')).not.toHaveStyle({ color: 'white' }); |
299 | 300 | ``` |
300 | 301 |
|
| 302 | +### `toBeVisible` |
| 303 | + |
| 304 | +```typescript |
| 305 | +toBeVisible(); |
| 306 | +``` |
| 307 | + |
| 308 | +Check that the given element is visible. |
| 309 | + |
| 310 | +An element is visible if **all** the following conditions are met: |
| 311 | + |
| 312 | +- it does not have its style property `display` set to `none`. |
| 313 | +- it does not have its style property `opacity` set to `0`. |
| 314 | +- it is not a `Modal` component or it does not have the prop `visible` set to `false`. |
| 315 | +- its ancestor elements are also visible. |
| 316 | + |
| 317 | +#### Examples |
| 318 | + |
| 319 | +```javascript |
| 320 | +const { getByTestId } = render(<View testID="empty-view" />); |
| 321 | + |
| 322 | +expect(getByTestId('empty-view')).toBeVisible(); |
| 323 | +``` |
| 324 | + |
| 325 | +```javascript |
| 326 | +const { getByTestId } = render(<View testID="view-with-opacity" style={{ opacity: 0.2 }} />); |
| 327 | + |
| 328 | +expect(getByTestId('view-with-opacity')).toBeVisible(); |
| 329 | +``` |
| 330 | + |
| 331 | +```javascript |
| 332 | +const { getByTestId } = render(<Modal testID="empty-modal" />); |
| 333 | + |
| 334 | +expect(getByTestId('empty-modal')).toBeVisible(); |
| 335 | +``` |
| 336 | + |
| 337 | +```javascript |
| 338 | +const { getByTestId } = render( |
| 339 | + <Modal> |
| 340 | + <View> |
| 341 | + <View testID="view-within-modal" /> |
| 342 | + </View> |
| 343 | + </Modal>, |
| 344 | +); |
| 345 | + |
| 346 | +expect(getByTestId('view-within-modal')).toBeVisible(); |
| 347 | +``` |
| 348 | + |
| 349 | +```javascript |
| 350 | +const { getByTestId } = render(<View testID="invisible-view" style={{ opacity: 0 }} />); |
| 351 | + |
| 352 | +expect(getByTestId('invisible-view')).not.toBeVisible(); |
| 353 | +``` |
| 354 | + |
| 355 | +```javascript |
| 356 | +const { getByTestId } = render(<View testID="display-none-view" style={{ display: 'none' }} />); |
| 357 | + |
| 358 | +expect(getByTestId('display-none-view')).not.toBeVisible(); |
| 359 | +``` |
| 360 | + |
| 361 | +```javascript |
| 362 | +const { getByTestId } = render( |
| 363 | + <View style={{ opacity: 0 }}> |
| 364 | + <View> |
| 365 | + <View testID="view-within-invisible-view" /> |
| 366 | + </View> |
| 367 | + </View>, |
| 368 | +); |
| 369 | + |
| 370 | +expect(getByTestId('view-within-invisible-view')).not.toBeVisible(); |
| 371 | +``` |
| 372 | + |
| 373 | +```javascript |
| 374 | +const { getByTestId } = render( |
| 375 | + <View style={{ display: 'none' }}> |
| 376 | + <View> |
| 377 | + <View testID="view-within-display-none-view" /> |
| 378 | + </View> |
| 379 | + </View>, |
| 380 | +); |
| 381 | + |
| 382 | +expect(getByTestId('view-within-display-none-view')).not.toBeVisible(); |
| 383 | +``` |
| 384 | + |
| 385 | +```javascript |
| 386 | +const { getByTestId } = render( |
| 387 | + <Modal visible={false}> |
| 388 | + <View> |
| 389 | + <View testID="view-within-not-visible-modal" /> |
| 390 | + </View> |
| 391 | + </Modal>, |
| 392 | +); |
| 393 | + |
| 394 | +expect(getByTestId('view-within-not-visible-modal')).not.toBeVisible(); |
| 395 | +``` |
| 396 | + |
| 397 | +```javascript |
| 398 | +const { getByTestId } = render(<Modal testID="not-visible-modal" visible={false} />); |
| 399 | + |
| 400 | +expect(getByTestId('not-visible-modal')).not.toBeVisible(); |
| 401 | +``` |
| 402 | + |
301 | 403 | ## Inspiration |
302 | 404 |
|
303 | 405 | This library was made to be a companion for |
|
0 commit comments