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 ' ;
11+ import fetchWithDefaultOptions from '../../DisplayUtils/fetchWithDefaultOptions ' ;
1312
1413const onToggle = jest . fn ( ) ;
15- const onDelete = jest . fn ( ) ;
16- const onClose = jest . fn ( ) ;
17- const handleSubmit = jest . fn ( ) ;
1814const onSubmit = jest . fn ( ) ;
19- const onUploadFile = jest . fn ( ) ;
20- const onDownloadFile = jest . fn ( ) ;
2115
2216const file = new File ( [ 'foo' ] , 'file.json' , { type : 'text/plain' } ) ;
2317
24- const renderFilterFileForm = ( stripes , initialValues = FILTER ) => {
18+ jest . mock ( '../../DisplayUtils/fetchWithDefaultOptions' ) ;
19+
20+ const mockPost = jest . fn ( ( ) => Promise . resolve ( {
21+ ok : true ,
22+ text : ( ) => Promise . resolve ( '34bdd9da-b765-448a-8519-11d460a4df5d' ) ,
23+ } ) ) ;
24+
25+ const renderFilterFileForm = ( stripes ) => {
2526 return renderWithIntlConfiguration (
2627 < StripesContext . Provider value = { stripes } >
2728 < MemoryRouter >
2829 < Form
2930 onSubmit = { onSubmit }
31+ mutators = { arrayMutators }
3032 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- addDocBtnLabel = "Add file to filter"
46- name = "filterFiles"
47- onDownloadFile = { onDownloadFile }
48- onUploadFile = { onUploadFile }
49- />
50- </ FilterFileForm >
51- </ FilterForm >
33+ < FilterFileForm
34+ accordionId = "accordionId"
35+ expanded
36+ onToggle = { onToggle }
37+ />
5238 ) }
5339 />
5440 </ MemoryRouter >
@@ -59,43 +45,49 @@ const renderFilterFileForm = (stripes, initialValues = FILTER) => {
5945jest . unmock ( 'react-intl' ) ;
6046
6147describe ( 'FilterFileForm' , ( ) => {
62- let stripes ;
48+ beforeEach ( ( ) => {
49+ jest . clearAllMocks ( ) ;
50+ fetchWithDefaultOptions . mockImplementation ( mockPost ) ;
51+ } ) ;
6352
6453 describe ( 'render FilterFileForm' , ( ) => {
6554 beforeEach ( ( ) => {
66- stripes = useStripes ( ) ;
55+ const stripes = useStripes ( ) ;
6756 renderFilterFileForm ( stripes ) ;
6857 } ) ;
6958
7059 test ( 'Add file button is rendered' , ( ) => {
71- const selectFile = screen . getByRole ( 'button' , {
72- name : 'Add file to filter' ,
73- } ) ;
60+ const selectFile = screen . getByRole ( 'button' , { name : 'Add file to filter' } ) ;
7461 expect ( selectFile ) . toBeInTheDocument ( ) ;
7562 } ) ;
7663
7764 describe ( 'Click add file button' , ( ) => {
7865 beforeEach ( async ( ) => {
79- const selectFile = screen . getByRole ( 'button' , {
80- name : 'Add file to filter' ,
81- } ) ;
66+ const selectFile = screen . getByRole ( 'button' , { name : 'Add file to filter' } ) ;
8267 await userEvent . click ( selectFile ) ;
8368 } ) ;
8469
85- test ( 'should render filter file upload button ' , ( ) => {
86- expect ( document . querySelector ( '#filter-file-label-1 ' ) ) . toBeInTheDocument ( ) ;
70+ it ( 'should render filter file upload card ' , ( ) => {
71+ expect ( document . querySelector ( '#filter-file-label-0 ' ) ) . toBeInTheDocument ( ) ;
8772 expect ( document . querySelector ( '#filter-file-upload-button' ) ) . toBeInTheDocument ( ) ;
8873 } ) ;
8974
90- test ( 'should render filter file upload button ' , async ( ) => {
91- const filenameInput = document . querySelector ( '#filter-file-label-1 ' ) ;
75+ test ( 'upload file should call fetch ' , async ( ) => {
76+ const filenameInput = document . querySelector ( '#filter-file-label-0 ' ) ;
9277 const uploadFileInput = document . querySelector ( '#filter-file-input' ) ;
93- const saveButton = screen . getByRole ( 'button' , { name : 'Save & close' } ) ;
9478
9579 await userEvent . type ( filenameInput , 'my filename' ) ;
96- fireEvent . change ( uploadFileInput , { target : { filterFiles : [ file ] } } ) ;
80+ await userEvent . upload ( uploadFileInput , file ) ;
81+
9782 await waitFor ( ( ) => {
98- expect ( saveButton ) . toBeEnabled ( ) ;
83+ expect ( fetchWithDefaultOptions ) . toHaveBeenCalledWith (
84+ expect . any ( Object ) ,
85+ expect . stringContaining ( '/finc-select/files' ) ,
86+ expect . objectContaining ( {
87+ method : 'POST' ,
88+ body : file ,
89+ } )
90+ ) ;
9991 } ) ;
10092 } ) ;
10193 } ) ;
0 commit comments