@@ -2,36 +2,65 @@ import { ColumnsType } from './models/column';
2
2
import { DSVImport } from '.' ;
3
3
import React from 'react' ;
4
4
import { render , fireEvent } from '@testing-library/react' ;
5
+ import { useDSVImport } from './features/context' ;
6
+ import { ValidationError } from './models/validation' ;
5
7
6
8
describe ( 'DSVImport' , ( ) => {
7
9
type TestType = { forename : string ; surname : string ; email : string } ;
8
10
9
11
const columns : ColumnsType < TestType > = [
10
12
{ key : 'forename' , label : 'Forename' } ,
11
13
{ key : 'surname' , label : 'Surname' } ,
12
- { key : 'email' , label : 'Email' }
14
+ { key : 'email' , label : 'Email' , rules : [ { constraint : { unique : true } , message : 'Contains duplicates' } ] }
13
15
] ;
14
16
15
- const onChangeMock = jest . fn ( ) ;
17
+ interface Props < T > {
18
+ onChange ?: ( value : T [ ] ) => void ;
19
+ onValidation ?: ( errors : ValidationError < T > [ ] ) => void ;
20
+ columns : ColumnsType < T > ;
21
+ }
16
22
17
- const renderComponent = ( ) => {
23
+ const MinimalTextareaInput : React . FC = ( ) => {
24
+ const [ , dispatch ] = useDSVImport ( ) ;
25
+
26
+ const handleChange = ( event : React . ChangeEvent < HTMLTextAreaElement > ) => {
27
+ dispatch ( { type : 'setRaw' , raw : event . target . value } ) ;
28
+ } ;
29
+
30
+ return < textarea onChange = { handleChange } > </ textarea > ;
31
+ } ;
32
+
33
+ const renderComponent = ( props : Props < TestType > ) => {
18
34
return render (
19
- < DSVImport < TestType > columns = { columns } onChange = { onChangeMock } >
20
- < DSVImport . TextareaInput />
21
- < DSVImport . TablePreview />
35
+ < DSVImport < TestType > { ...props } >
36
+ < MinimalTextareaInput />
22
37
</ DSVImport >
23
38
) ;
24
39
} ;
25
40
26
- it ( 'should invoke the onChange callback on context change' , ( ) => {
27
- const { container } = renderComponent ( ) ;
41
+ it ( 'should invoke the onChange callback' , ( ) => {
42
+ const onChangeMock = jest . fn ( ) ;
43
+ const { container } = renderComponent ( { columns, onChange : onChangeMock } ) ;
28
44
const textarea = container . querySelector ( 'textarea' ) ;
29
45
30
46
if ( textarea ) {
31
47
fireEvent . change ( textarea , { target : { value : 'Max' } } ) ;
32
48
}
33
49
34
- expect ( onChangeMock ) . toBeCalledTimes ( 1 ) ;
35
50
expect ( onChangeMock ) . toBeCalledWith ( [ { email : undefined , forename : 'Max' , surname : undefined } ] ) ;
36
51
} ) ;
52
+
53
+ it ( 'should invoke the onValidation callback' , ( ) => {
54
+ const onValidationMock = jest . fn ( ) ;
55
+ const { container } = renderComponent ( { columns, onValidation : onValidationMock } ) ;
56
+ const textarea = container . querySelector ( 'textarea' ) ;
57
+
58
+ if ( textarea ) {
59
+ fireEvent . change ( textarea , {
60
+ target :
{ value :
'Max,Muster,[email protected] \nMoritz,Muster,[email protected] ' }
61
+ } ) ;
62
+ }
63
+
64
+ expect ( onValidationMock ) . toBeCalledWith ( [ { column : 'email' , message : 'Contains duplicates' } ] ) ;
65
+ } ) ;
37
66
} ) ;
0 commit comments