1
1
/* Copyright 2024 Marimo. All rights reserved. */
2
2
3
3
import { describe , expect , it , vi } from "vitest" ;
4
- import { render , screen , fireEvent , within } from "@testing-library/react" ;
4
+ import { render , screen } from "@testing-library/react" ;
5
5
import { DataSelectionPanel } from "../data-selection" ;
6
- import type { Row } from "@tanstack/react-table" ;
7
6
import { Provider } from "jotai" ;
8
7
import { TooltipProvider } from "@/components/ui/tooltip" ;
9
- import { PanelGroup } from "react-resizable-panels" ;
8
+ import type { GetRowResult } from "@/plugins/impl/DataTablePlugin" ;
9
+ import type { FieldTypesWithExternalType } from "../../types" ;
10
10
11
11
const renderWithProviders = ( component : React . ReactNode ) => {
12
12
return render (
@@ -17,178 +17,37 @@ const renderWithProviders = (component: React.ReactNode) => {
17
17
} ;
18
18
19
19
describe ( "DataSelectionPanel" , ( ) => {
20
- const mockRows = [
21
- {
22
- getAllCells : ( ) => [
23
- {
24
- column : { id : "name" , columnDef : { meta : { dataType : "string" } } } ,
25
- getValue : ( ) => "John" ,
26
- renderValue : ( ) => "John" ,
27
- } ,
28
- {
29
- column : { id : "age" , columnDef : { meta : { dataType : "number" } } } ,
30
- getValue : ( ) => 30 ,
31
- renderValue : ( ) => "30" ,
32
- } ,
33
- ] ,
34
- } ,
35
- ] as Array < Row < unknown > > ;
36
-
37
- const mockClosePanel = vi . fn ( ) ;
38
-
39
- it ( "renders data in selection panel" , ( ) => {
40
- renderWithProviders (
41
- < DataSelectionPanel rows = { mockRows } closePanel = { mockClosePanel } /> ,
42
- ) ;
43
-
44
- expect ( screen . getByText ( "John" ) ) . toBeInTheDocument ( ) ;
45
- expect ( screen . getByText ( "30" ) ) . toBeInTheDocument ( ) ;
46
- } ) ;
47
-
48
- it ( "renders in overlay mode when isOverlay is true" , ( ) => {
49
- renderWithProviders (
50
- < PanelGroup direction = "horizontal" >
51
- < DataSelectionPanel rows = { mockRows } closePanel = { mockClosePanel } />
52
- </ PanelGroup > ,
53
- ) ;
54
-
55
- const overlayButton = screen . getByRole ( "button" , {
56
- name : / e x i t o v e r l a y m o d e / i,
20
+ const mockFieldTypes : FieldTypesWithExternalType = [
21
+ [ "name" , [ "string" , "string" ] ] ,
22
+ [ "age" , [ "number" , "number" ] ] ,
23
+ ] ;
24
+
25
+ const mockGetRow = vi
26
+ . fn ( )
27
+ . mockImplementation ( ( rowIdx : number ) : Promise < GetRowResult > => {
28
+ const mockData = [
29
+ { name : "John" , age : 30 } ,
30
+ { name : "Jane" , age : 25 } ,
31
+ { name : "Alice" , age : 35 } ,
32
+ ] ;
33
+ return Promise . resolve ( { rows : [ mockData [ rowIdx ] ] } ) ;
57
34
} ) ;
58
- fireEvent . click ( overlayButton ) ;
59
-
60
- // Check if the panel is rendered with overlay styles
61
- expect (
62
- screen . getByRole ( "button" , { name : / o v e r l a y m o d e / i } ) ,
63
- ) . toBeInTheDocument ( ) ;
64
- } ) ;
65
-
66
- it ( "calls closePanel when close button is clicked" , ( ) => {
67
- renderWithProviders (
68
- < DataSelectionPanel rows = { mockRows } closePanel = { mockClosePanel } /> ,
69
- ) ;
70
-
71
- const closeButton = screen . getByRole ( "button" , {
72
- name : / c l o s e s e l e c t i o n p a n e l / i,
73
- } ) ;
74
- fireEvent . click ( closeButton ) ;
75
-
76
- expect ( mockClosePanel ) . toHaveBeenCalled ( ) ;
77
- } ) ;
78
- } ) ;
79
35
80
- describe ( "DataSelection" , ( ) => {
81
- const JOHNS_AGE = 30 ;
82
- const mockRows = [
83
- {
84
- getAllCells : ( ) => [
85
- {
86
- column : { id : "name" , columnDef : { meta : { dataType : "string" } } } ,
87
- getValue : ( ) => "John" ,
88
- renderValue : ( ) => "John" ,
89
- } ,
90
- {
91
- column : { id : "age" , columnDef : { meta : { dataType : "number" } } } ,
92
- getValue : ( ) => JOHNS_AGE ,
93
- renderValue : ( ) => JOHNS_AGE . toString ( ) ,
94
- } ,
95
- ] ,
96
- } ,
97
- {
98
- getAllCells : ( ) => [
99
- {
100
- column : { id : "name" , columnDef : { meta : { dataType : "string" } } } ,
101
- getValue : ( ) => "Jane" ,
102
- renderValue : ( ) => "Jane" ,
103
- } ,
104
- {
105
- column : { id : "age" , columnDef : { meta : { dataType : "number" } } } ,
106
- getValue : ( ) => 25 ,
107
- renderValue : ( ) => "25" ,
108
- } ,
109
- ] ,
110
- } ,
111
- {
112
- getAllCells : ( ) => [
113
- {
114
- column : { id : "name" , columnDef : { meta : { dataType : "string" } } } ,
115
- getValue : ( ) => "Alice" ,
116
- renderValue : ( ) => "Alice" ,
117
- } ,
118
- {
119
- column : { id : "age" , columnDef : { meta : { dataType : "number" } } } ,
120
- getValue : ( ) => 35 ,
121
- renderValue : ( ) => "35" ,
122
- } ,
123
- ] ,
124
- } ,
125
- ] as Array < Row < unknown > > ;
126
-
127
- const mockClosePanel = vi . fn ( ) ;
128
-
129
- it ( "navigates between rows using navigation buttons" , ( ) => {
130
- renderWithProviders (
131
- < DataSelectionPanel rows = { mockRows } closePanel = { mockClosePanel } /> ,
132
- ) ;
133
-
134
- // Check initial state
135
- expect ( screen . getByText ( "Row 1 of 3" ) ) . toBeInTheDocument ( ) ;
136
- expect ( screen . getByText ( "John" ) ) . toBeInTheDocument ( ) ;
137
-
138
- // Navigate to next row
139
- const nextButton = screen . getByRole ( "button" , { name : "Next row" } ) ;
140
- fireEvent . click ( nextButton ) ;
141
-
142
- expect ( screen . getByText ( "Row 2 of 3" ) ) . toBeInTheDocument ( ) ;
143
- expect ( screen . getByText ( "Jane" ) ) . toBeInTheDocument ( ) ;
144
-
145
- // Navigate to previous row
146
- const prevButton = screen . getByRole ( "button" , { name : "Previous row" } ) ;
147
- fireEvent . click ( prevButton ) ;
148
-
149
- expect ( screen . getByText ( "Row 1 of 3" ) ) . toBeInTheDocument ( ) ;
150
- expect ( screen . getByText ( "John" ) ) . toBeInTheDocument ( ) ;
151
-
152
- // Navigate to last row
153
- const lastButton = screen . getByRole ( "button" , { name : "Go to last row" } ) ;
154
- fireEvent . click ( lastButton ) ;
155
-
156
- expect ( screen . getByText ( "Row 3 of 3" ) ) . toBeInTheDocument ( ) ;
157
- expect ( screen . getByText ( "Alice" ) ) . toBeInTheDocument ( ) ;
158
-
159
- // Navigate to first row
160
- const firstButton = screen . getByRole ( "button" , { name : "Go to first row" } ) ;
161
- fireEvent . click ( firstButton ) ;
162
-
163
- expect ( screen . getByText ( "Row 1 of 3" ) ) . toBeInTheDocument ( ) ;
164
- expect ( screen . getByText ( "John" ) ) . toBeInTheDocument ( ) ;
165
- } ) ;
166
-
167
- it ( "filters rows based on search input" , ( ) => {
168
- renderWithProviders (
169
- < DataSelectionPanel rows = { mockRows } closePanel = { mockClosePanel } /> ,
170
- ) ;
171
-
172
- // Search for John
173
- const searchInput = screen . getByTestId ( "selection-panel-search-input" ) ;
174
- fireEvent . change ( searchInput , { target : { value : "John" } } ) ;
175
-
176
- expect ( screen . getByText ( "John" ) ) . toBeInTheDocument ( ) ;
177
- // Check that the other column is not in the document
178
- expect ( screen . queryByText ( JOHNS_AGE . toString ( ) ) ) . not . toBeInTheDocument ( ) ;
179
- } ) ;
36
+ const mockSetRowIdx = vi . fn ( ) ;
180
37
181
- it ( "renders copy button on hover" , ( ) => {
38
+ it ( "renders data in selection panel" , async ( ) => {
182
39
renderWithProviders (
183
- < DataSelectionPanel rows = { mockRows } closePanel = { mockClosePanel } /> ,
40
+ < DataSelectionPanel
41
+ rowIdx = { 0 }
42
+ setRowIdx = { mockSetRowIdx }
43
+ totalRows = { 3 }
44
+ fieldTypes = { mockFieldTypes }
45
+ getRow = { mockGetRow }
46
+ /> ,
184
47
) ;
185
48
186
- const row = screen . getByText ( "John" ) . closest ( "tr" ) ;
187
- if ( row ) {
188
- fireEvent . mouseEnter ( row ) ;
189
- expect (
190
- within ( row ) . getByRole ( "button" , { name : / c o p y t o c l i p b o a r d / i } ) ,
191
- ) . toBeInTheDocument ( ) ;
192
- }
49
+ // Wait for the data to load
50
+ expect ( await screen . findByText ( "John" ) ) . toBeInTheDocument ( ) ;
51
+ expect ( await screen . findByText ( "30" ) ) . toBeInTheDocument ( ) ;
193
52
} ) ;
194
53
} ) ;
0 commit comments