|
1 | 1 | import React from 'react'; |
2 | | -import { render, screen } from '@testing-library/react'; |
| 2 | +import { fireEvent, render, screen } from '@testing-library/react'; |
3 | 3 | import '@testing-library/jest-dom'; |
4 | 4 |
|
5 | 5 | import { Table, Thead, Tbody, Tr, Th, Td } from '../src'; |
@@ -382,4 +382,61 @@ describe('SuperResponsiveTable UniqueCase', () => { |
382 | 382 | expect(cellWithRowSpan).toBeInTheDocument(); |
383 | 383 | expect(cellWithRowSpan).toHaveAttribute('rowSpan', '2'); |
384 | 384 | }); |
| 385 | + |
| 386 | + it('should handle events and pass React HTMLAttributes props to Tr, Th, and Td components', () => { |
| 387 | + const handleClick = jest.fn(); |
| 388 | + const handleMouseEnter = jest.fn(); |
| 389 | + const handleDoubleClick = jest.fn(); |
| 390 | + const handleKeyDown = jest.fn(); |
| 391 | + |
| 392 | + const { getByTestId } = render( |
| 393 | + <Table> |
| 394 | + <Thead> |
| 395 | + <Tr> |
| 396 | + <Th |
| 397 | + data-testid="header" |
| 398 | + onMouseEnter={handleMouseEnter} |
| 399 | + onDoubleClick={handleDoubleClick} |
| 400 | + onKeyDown={handleKeyDown} |
| 401 | + > |
| 402 | + Header 1 |
| 403 | + </Th> |
| 404 | + <Th>Header 2</Th> |
| 405 | + </Tr> |
| 406 | + </Thead> |
| 407 | + <Tbody> |
| 408 | + <Tr data-testid="clickable-row" onClick={handleClick}> |
| 409 | + <Td |
| 410 | + data-testid="cell" |
| 411 | + onMouseEnter={handleMouseEnter} |
| 412 | + onDoubleClick={handleDoubleClick} |
| 413 | + onKeyDown={handleKeyDown} |
| 414 | + > |
| 415 | + Cell 1 |
| 416 | + </Td> |
| 417 | + <Td>Cell 2</Td> |
| 418 | + </Tr> |
| 419 | + </Tbody> |
| 420 | + </Table> |
| 421 | + ); |
| 422 | + |
| 423 | + const row = getByTestId('clickable-row'); |
| 424 | + const header = getByTestId('header'); |
| 425 | + const cell = getByTestId('cell'); |
| 426 | + |
| 427 | + fireEvent.click(row); |
| 428 | + expect(handleClick).toHaveBeenCalledTimes(1); |
| 429 | + |
| 430 | + fireEvent.mouseEnter(header); |
| 431 | + fireEvent.mouseEnter(cell); |
| 432 | + expect(handleMouseEnter).toHaveBeenCalledTimes(2); |
| 433 | + |
| 434 | + fireEvent.doubleClick(header); |
| 435 | + fireEvent.doubleClick(cell); |
| 436 | + expect(handleDoubleClick).toHaveBeenCalledTimes(2); |
| 437 | + |
| 438 | + fireEvent.keyDown(header, { key: 'Enter', code: 'Enter' }); |
| 439 | + fireEvent.keyDown(cell, { key: 'Enter', code: 'Enter' }); |
| 440 | + expect(handleKeyDown).toHaveBeenCalledTimes(2); |
| 441 | + }); |
385 | 442 | }); |
0 commit comments