11import { MemoryRouter } from 'react-router-dom' ;
22import { Form } from 'react-final-form' ;
3- import { FieldArray } from 'react- final-form-arrays' ;
3+ import arrayMutators from 'final-form-arrays' ;
44
5- import { fireEvent , screen , waitFor } from '@folio/jest-config-stripes/testing-library/react' ;
5+ import { screen , waitFor } from '@folio/jest-config-stripes/testing-library/react' ;
66import userEvent from '@folio/jest-config-stripes/testing-library/user-event' ;
77import { StripesContext , useStripes } from '@folio/stripes/core' ;
88
99import renderWithIntlConfiguration from '../../../../test/jest/helpers/renderWithIntlConfiguration' ;
10- import FilterForm from '../FilterForm' ;
1110import FilterFileForm from './FilterFileForm' ;
12- import FILTER from '../../../../test/fixtures/filter' ;
1311
1412const onToggle = jest . fn ( ) ;
15- const onDelete = jest . fn ( ) ;
16- const onClose = jest . fn ( ) ;
17- const handleSubmit = jest . fn ( ) ;
1813const onSubmit = jest . fn ( ) ;
19- const onUploadFile = jest . fn ( ) ;
20- const onDownloadFile = jest . fn ( ) ;
2114
2215const file = new File ( [ 'foo' ] , 'file.json' , { type : 'text/plain' } ) ;
2316
24- const renderFilterFileForm = ( stripes , initialValues = FILTER ) => {
17+ const mockPost = jest . fn ( ( ) => Promise . resolve ( {
18+ ok : true ,
19+ json : ( ) => Promise . resolve ( { file } ) ,
20+ } ) ) ;
21+
22+ const renderFilterFileForm = ( stripes ) => {
2523 return renderWithIntlConfiguration (
2624 < StripesContext . Provider value = { stripes } >
2725 < MemoryRouter >
2826 < Form
2927 onSubmit = { onSubmit }
28+ mutators = { arrayMutators }
3029 render = { ( ) => (
31- < FilterForm
32- initialValues = { initialValues }
33- handlers = { { onClose, onDelete } }
34- handleSubmit = { handleSubmit }
35- onSubmit = { onSubmit }
36- onDelete = { onDelete }
37- >
38- < FilterFileForm
39- accordionId = "accordionId"
40- expanded
41- onToggle = { onToggle }
42- stripes = { stripes }
43- >
44- < FieldArray
45- name = "filterFiles"
46- onDownloadFile = { onDownloadFile }
47- onUploadFile = { onUploadFile }
48- />
49- </ FilterFileForm >
50- </ FilterForm >
30+ < FilterFileForm
31+ accordionId = "accordionId"
32+ expanded
33+ onToggle = { onToggle }
34+ />
5135 ) }
5236 />
5337 </ MemoryRouter >
@@ -58,43 +42,52 @@ const renderFilterFileForm = (stripes, initialValues = FILTER) => {
5842jest . unmock ( 'react-intl' ) ;
5943
6044describe ( 'FilterFileForm' , ( ) => {
61- let stripes ;
45+ beforeEach ( ( ) => {
46+ jest . clearAllMocks ( ) ;
47+ global . fetch = mockPost ;
48+ } ) ;
6249
6350 describe ( 'render FilterFileForm' , ( ) => {
6451 beforeEach ( ( ) => {
65- stripes = useStripes ( ) ;
52+ const stripes = useStripes ( ) ;
6653 renderFilterFileForm ( stripes ) ;
6754 } ) ;
6855
56+ afterEach ( ( ) => {
57+ delete global . fetch ;
58+ } ) ;
59+
6960 test ( 'Add file button is rendered' , ( ) => {
70- const selectFile = screen . getByRole ( 'button' , {
71- name : 'Add file to filter' ,
72- } ) ;
61+ const selectFile = screen . getByRole ( 'button' , { name : 'Add file to filter' } ) ;
7362 expect ( selectFile ) . toBeInTheDocument ( ) ;
7463 } ) ;
7564
7665 describe ( 'Click add file button' , ( ) => {
7766 beforeEach ( async ( ) => {
78- const selectFile = screen . getByRole ( 'button' , {
79- name : 'Add file to filter' ,
80- } ) ;
67+ const selectFile = screen . getByRole ( 'button' , { name : 'Add file to filter' } ) ;
8168 await userEvent . click ( selectFile ) ;
8269 } ) ;
8370
84- test ( 'should render filter file upload button ' , ( ) => {
85- expect ( document . querySelector ( '#filter-file-label-1 ' ) ) . toBeInTheDocument ( ) ;
71+ it ( 'should render filter file upload card ' , ( ) => {
72+ expect ( document . querySelector ( '#filter-file-label-0 ' ) ) . toBeInTheDocument ( ) ;
8673 expect ( document . querySelector ( '#filter-file-upload-button' ) ) . toBeInTheDocument ( ) ;
8774 } ) ;
8875
89- test ( 'should render filter file upload button ' , async ( ) => {
90- const filenameInput = document . querySelector ( '#filter-file-label-1 ' ) ;
76+ test ( 'upload file should call fetch ' , async ( ) => {
77+ const filenameInput = document . querySelector ( '#filter-file-label-0 ' ) ;
9178 const uploadFileInput = document . querySelector ( '#filter-file-input' ) ;
92- const saveButton = screen . getByRole ( 'button' , { name : 'Save & close' } ) ;
9379
9480 await userEvent . type ( filenameInput , 'my filename' ) ;
95- fireEvent . change ( uploadFileInput , { target : { filterFiles : [ file ] } } ) ;
81+ await userEvent . upload ( uploadFileInput , file ) ;
82+
9683 await waitFor ( ( ) => {
97- expect ( saveButton ) . toBeEnabled ( ) ;
84+ expect ( mockPost ) . toHaveBeenCalledWith (
85+ expect . stringContaining ( '/finc-select/files' ) ,
86+ expect . objectContaining ( {
87+ method : 'POST' ,
88+ body : file ,
89+ } )
90+ ) ;
9891 } ) ;
9992 } ) ;
10093 } ) ;
0 commit comments