1
1
import { Row } from '@tanstack/react-table' ;
2
2
import { act , renderHook } from '@testing-library/react' ;
3
3
4
+ import { ColumnEditorType } from '@/types' ;
5
+ import { createColumnAccessorFn , createColumnMeta } from '@/utils' ;
6
+
4
7
import { useEditableData } from './useEditableData' ;
5
8
6
9
/**
@@ -22,8 +25,15 @@ describe('useEditableData', () => {
22
25
...row ,
23
26
} ) as any ;
24
27
28
+ /**
29
+ * Default Table
30
+ */
31
+ const defaultTable = {
32
+ getAllColumns : ( ) => [ ] ,
33
+ } ;
34
+
25
35
it ( 'Should allow to start edit' , async ( ) => {
26
- const { result } = renderHook ( ( ) => useEditableData ( { table : { } as any , onUpdateRow : jest . fn ( ) } ) ) ;
36
+ const { result } = renderHook ( ( ) => useEditableData ( { table : defaultTable as any , onUpdateRow : jest . fn ( ) } ) ) ;
27
37
28
38
const row = createRow ( { id : '1' , original : { name : 'abc' } } ) ;
29
39
await act ( async ( ) => result . current . onStartEdit ( row ) ) ;
@@ -32,7 +42,7 @@ describe('useEditableData', () => {
32
42
} ) ;
33
43
34
44
it ( 'Should allow to cancel edit' , async ( ) => {
35
- const { result } = renderHook ( ( ) => useEditableData ( { table : { } as any , onUpdateRow : jest . fn ( ) } ) ) ;
45
+ const { result } = renderHook ( ( ) => useEditableData ( { table : defaultTable as any , onUpdateRow : jest . fn ( ) } ) ) ;
36
46
37
47
const row = createRow ( { id : '1' , original : { name : 'abc' } } ) ;
38
48
@@ -44,7 +54,7 @@ describe('useEditableData', () => {
44
54
} ) ;
45
55
46
56
it ( 'Should allow to change row data' , async ( ) => {
47
- const { result } = renderHook ( ( ) => useEditableData ( { table : { } as any , onUpdateRow : jest . fn ( ) } ) ) ;
57
+ const { result } = renderHook ( ( ) => useEditableData ( { table : defaultTable as any , onUpdateRow : jest . fn ( ) } ) ) ;
48
58
49
59
const row = createRow ( { id : '1' , original : { name : 'abc' } } ) ;
50
60
@@ -61,7 +71,7 @@ describe('useEditableData', () => {
61
71
62
72
it ( 'Should allow to save row data' , async ( ) => {
63
73
const onUpdateRow = jest . fn ( ) ;
64
- const { result } = renderHook ( ( ) => useEditableData ( { table : { } as any , onUpdateRow } ) ) ;
74
+ const { result } = renderHook ( ( ) => useEditableData ( { table : defaultTable as any , onUpdateRow } ) ) ;
65
75
66
76
const row = createRow ( { id : '1' , original : { name : 'abc' } } ) ;
67
77
@@ -88,7 +98,7 @@ describe('useEditableData', () => {
88
98
89
99
it ( 'Should reset saving state if update error' , async ( ) => {
90
100
const onUpdateRow = jest . fn ( ) . mockRejectedValue ( null ) ;
91
- const { result } = renderHook ( ( ) => useEditableData ( { table : { } as any , onUpdateRow } ) ) ;
101
+ const { result } = renderHook ( ( ) => useEditableData ( { table : defaultTable as any , onUpdateRow } ) ) ;
92
102
93
103
const row = createRow ( { id : '1' , original : { name : 'abc' } } ) ;
94
104
@@ -109,4 +119,55 @@ describe('useEditableData', () => {
109
119
expect ( result . current . row ) . toEqual ( row ) ;
110
120
expect ( result . current . isSaving ) . toBeFalsy ( ) ;
111
121
} ) ;
122
+
123
+ it ( 'Should allow to start edit and replace value for TextArea editors' , async ( ) => {
124
+ const columns = [
125
+ {
126
+ id : 'name' ,
127
+ accessorFn : createColumnAccessorFn ( 'name' ) ,
128
+ columnDef : {
129
+ meta : createColumnMeta ( {
130
+ editable : false ,
131
+ } ) ,
132
+ } ,
133
+ } ,
134
+ {
135
+ id : 'value' ,
136
+ accessorFn : createColumnAccessorFn ( 'value' ) ,
137
+ columnDef : {
138
+ meta : createColumnMeta ( {
139
+ editable : true ,
140
+ editor : {
141
+ type : ColumnEditorType . NUMBER ,
142
+ } ,
143
+ } ) ,
144
+ } ,
145
+ } ,
146
+ {
147
+ id : 'text' ,
148
+ accessorFn : createColumnAccessorFn ( 'text' ) ,
149
+ columnDef : {
150
+ meta : createColumnMeta ( {
151
+ editable : true ,
152
+ editor : {
153
+ type : ColumnEditorType . TEXTAREA ,
154
+ } ,
155
+ } ) ,
156
+ } ,
157
+ } ,
158
+ ] ;
159
+
160
+ const currentTable = {
161
+ getAllColumns : ( ) => columns ,
162
+ } ;
163
+
164
+ const { result } = renderHook ( ( ) => useEditableData ( { table : currentTable as any , onUpdateRow : jest . fn ( ) } ) ) ;
165
+
166
+ const row = createRow ( { id : '1' , original : { name : 'abc' , value : 10 , text : 'text test\n' } } ) ;
167
+ const expectedRow = createRow ( { id : '1' , original : { name : 'abc' , value : 10 , text : 'text test\\n' } } ) ;
168
+
169
+ await act ( async ( ) => result . current . onStartEdit ( row ) ) ;
170
+
171
+ expect ( result . current . row ) . toEqual ( expectedRow ) ;
172
+ } ) ;
112
173
} ) ;
0 commit comments