This answer shows how to access all the different parts of the code using jest. However, this does not necessarily mean that you need to test all of these parts in this way.
The test code is essentially the same as in the question, except that I replaced addEventListener('load', ...
with onload = ...
, and I deleted the console.log
line:
MyComponent.js
import React from 'react'; class MyComponent extends React.Component { constructor(props) { super(props); this.state = {fileContents: ''}; this.changeHandler = this.changeHandler.bind(this); } changeHandler(evt) { const reader = new FileReader(); reader.addEventListener('load', () => { this.setState({fileContents: reader.result}); }); reader.readAsText(evt.target.files[0]); } render() { return <input type="file" onChange={this.changeHandler}/>; } } export default MyComponent;
I believe that I was able to test almost everything in the code test (with one exception noted in the comments and discussed below) with the following:
MyComponent.test.js
import React from 'react'; import {mount} from 'enzyme'; import MyComponent from './temp01'; it('should test handler', () => { const componentWrapper = mount(<MyComponent/>); const component = componentWrapper.get(0); // should the line above use `componentWrapper.instance()` instead? const fileContents = 'file contents'; const expectedFinalState = {fileContents: fileContents}; const file = new Blob([fileContents], {type : 'text/plain'}); const readAsText = jest.fn(); const addEventListener = jest.fn((_, evtHandler) => { evtHandler(); }); const dummyFileReader = {addEventListener, readAsText, result: fileContents}; window.FileReader = jest.fn(() => dummyFileReader); spyOn(component, 'setState').and.callThrough(); // spyOn(component, 'changeHandler').and.callThrough(); // not yet working componentWrapper.find('input').simulate('change', {target: {files: [file]}}); expect(FileReader ).toHaveBeenCalled ( ); expect(addEventListener ).toHaveBeenCalledWith('load', jasmine.any(Function)); expect(readAsText ).toHaveBeenCalledWith(file ); expect(component.setState).toHaveBeenCalledWith(expectedFinalState ); expect(component.state ).toEqual (expectedFinalState ); // expect(component.changeHandler).toHaveBeenCalled(); // not yet working });
The only thing I have not yet verified explicitly is whether or not changeHandler
is changeHandler
. It seems to be easy, but for some reason it still eludes me. This was clearly caused because it was confirmed that the other mocking functions inside it were called, but I still could not check whether it was called by itself, either using jest.fn()
, or even Jasmine spyOn
. I asked this other question on SO to try to solve this remaining problem.
source share