I have a react component
const Upload = (props) => {
const [fileName, setFileName] = useState("no file chosen")
function handleFile(e) {
const [sFile] = e.target.files;
setFileName(sFile.name);
}
const chooseFile = (
<label>
<input id="file-upload" type="file" accept=".pdf, application/pdf" onChange={handleFile} />
choose_file
</label>
<label className='file-text'>
{fileName}
</label>
);
return (
<React.Fragment>
{chooseFile}
</React.Fragment>
);
};
I have this test for checking the value change of state fileName, so that it displays the name of the file chosen
beforeEach(() => {
wrapper = shallow(<Upload />);
});
describe('Choose File', () => {
const mockedFile = 'application.pdf';
const event = {
target: {
files: [{ name: 'application.pdf' }]
}
};
it('displays the file name when a file is selected', () => {
wrapper.find('#file-upload').simulate('change', event);
wrapper.update();
expect(wrapper.find('label').at(1).text()).toEqual(mockedFile);
});
}
But the output is always "no file chosen". Any help would be appreciated.