11import { render , screen } from '@testing-library/react' ;
2- import { describe , expect , it } from 'vitest' ;
2+ import userEvent from '@testing-library/user-event' ;
3+ import { describe , expect , it , vi } from 'vitest' ;
34
45import { ClientTable } from './ClientTable' ;
56
@@ -8,7 +9,7 @@ const FIRST_BUTTON_ID = 'first-page-button';
89const PREVIOUS_BUTTON_ID = 'previous-page-button' ;
910const NEXT_BUTTON_ID = 'next-page-button' ;
1011const LAST_BUTTON_ID = 'last-page-button' ;
11-
12+ const PAGE_NUMBER_TEST_ID = 'page-numbers' ;
1213describe ( 'ClientTable' , ( ) => {
1314 it ( 'should render' , ( ) => {
1415 render ( < ClientTable columns = { [ ] } data = { [ ] } minRows = { 10 } /> ) ;
@@ -22,4 +23,65 @@ describe('ClientTable', () => {
2223 render ( < ClientTable className = "foo" columns = { [ ] } data = { [ ] } minRows = { 10 } /> ) ;
2324 expect ( screen . getByTestId ( TEST_ID ) ) . toHaveClass ( 'foo' ) ;
2425 } ) ;
26+ it ( 'should function correctly' , async ( ) => {
27+ const handleClientTableItemClick = vi . fn ( ) ;
28+ const handleClientTableDropdownClick = vi . fn ( ) ;
29+ render (
30+ < ClientTable
31+ columnDropdownOptions = { [
32+ {
33+ label : 'delete' ,
34+ onSelection : handleClientTableDropdownClick
35+ }
36+ ] }
37+ columns = { [
38+ {
39+ field : 'f1' ,
40+ label : 'Field 1'
41+ } ,
42+ {
43+ field : 'f2' ,
44+ label : 'Field 2'
45+ }
46+ ] }
47+ data = { [
48+ {
49+ f1 : 1 ,
50+ f2 : 2
51+ } ,
52+ {
53+ f1 : 23 ,
54+ f2 : 24
55+ }
56+ ] }
57+ entriesPerPage = { 1 }
58+ minRows = { 3 }
59+ onEntryClick = { handleClientTableItemClick }
60+ />
61+ ) ;
62+ expect ( screen . getByTestId ( PAGE_NUMBER_TEST_ID ) ) . toBeInTheDocument ( ) ;
63+ await userEvent . click ( screen . getByTestId ( NEXT_BUTTON_ID ) ) ;
64+ await userEvent . click ( await screen . findByText ( '23.00' ) ) ;
65+ expect ( handleClientTableItemClick ) . toBeCalled ( ) ;
66+
67+ expect ( screen . getByTestId ( PAGE_NUMBER_TEST_ID ) . textContent ) . toBe ( '2 - 2 / 2' ) ;
68+ await userEvent . click ( screen . getByTestId ( PREVIOUS_BUTTON_ID ) ) ;
69+ await userEvent . click ( await screen . findByText ( '1.00' ) ) ;
70+ expect ( handleClientTableItemClick ) . toBeCalled ( ) ;
71+
72+ expect ( screen . getByTestId ( PAGE_NUMBER_TEST_ID ) . textContent ) . toBe ( '1 - 1 / 2' ) ;
73+ await userEvent . click ( screen . getByTestId ( LAST_BUTTON_ID ) ) ;
74+ await userEvent . click ( await screen . findByText ( '23.00' ) ) ;
75+ expect ( handleClientTableItemClick ) . toBeCalled ( ) ;
76+
77+ expect ( screen . getByTestId ( PAGE_NUMBER_TEST_ID ) . textContent ) . toBe ( '2 - 2 / 2' ) ;
78+ await userEvent . click ( screen . getByTestId ( FIRST_BUTTON_ID ) ) ;
79+ await userEvent . click ( await screen . findByText ( '1.00' ) ) ;
80+ expect ( handleClientTableItemClick ) . toBeCalled ( ) ;
81+ expect ( screen . getByTestId ( PAGE_NUMBER_TEST_ID ) . textContent ) . toBe ( '1 - 1 / 2' ) ;
82+
83+ await userEvent . click ( await screen . findByText ( 'Field 1' ) ) ;
84+ await userEvent . click ( screen . getByTestId ( 'delete-test-id' ) ) ;
85+ expect ( handleClientTableDropdownClick ) . toBeCalled ( ) ;
86+ } ) ;
2587} ) ;
0 commit comments