dimanche 11 février 2018

(React Component Tests) How to unit test if a function from a different module is called in componentDidMount using jasmine

I am writing unit tests for React Component. How to write a unit test to check if a function from different module gets called in componentDidMount in jasmine. Here's my Component :

import {differentModuleFunc} from "someModule";
class MyComponent extends React.Component {

    componentDidMount() {
        differentModuleFunc();
    }

    render() {
        return 
            <div>
               TestDiv
            </div>;
    }
 }

My unit test for component looks like :

describe("MyComponent", () => {
    it("differentModuleFunc should have been called", (done) => {
        const spy = spyOn(MyComponent, "differentModuleFunc");
        myComponentDiv = TestUtils.renderIntoDocument(<MyComponent/>);
        expect(myComponentDiv).toBeDefined();
        expect(spy).toHaveBeenCalled();
    });
});

Thanks.

Aucun commentaire:

Enregistrer un commentaire