Zuzu JH
Zuzu JH

Reputation: 627

React-native test with Jest and Enzyme not calling prop function

I have the following test case

//to mock props
const createTestProps = (props) => ({
    ...props,
    processDecision: jest.fn(),
    navigation: {
        getParam: jest.fn(),
        navigate: jest.fn(),
    },
});

describe('TermsAndConditionsScreen', () => {
    let props, wrapper;
    beforeEach(() => {
        props = createTestProps({});
        wrapper = shallow(<TermsAndConditionsScreen {...props} />);
    });


    it('should invoke the processDecision twice', () => {

        wrapper = shallow(<TermsAndConditionsScreen {...props} />);
        wrapper.dive().find(Button).first().props().onPress();
        wrapper.dive().find(Button).last().props().onPress();
        expect(props.processDecision).toHaveBeenCalledTimes(2);

    });
});

For some reason it's not detecting props.processDecision. but if I put it this way, it works totally fine:

 it('should invoke the processDecision twice', () => {
            let mockFn = jest.fn();
            TermsAndConditionsScreen.prototype.processDecision = mockFn;        
            wrapper = shallow(<TermsAndConditionsScreen {...props} />);
            wrapper.dive().find(Button).first().props().onPress();
            wrapper.dive().find(Button).last().props().onPress();
            expect(mockFn).toHaveBeenCalledTimes(2);

        });

What is the difference please? Thank you in advance

Upvotes: 0

Views: 304

Answers (1)

Zuzu JH
Zuzu JH

Reputation: 627

Just in case somebody faces the same difficulty. In the first example it's consider processDecision a prop (Which is not true). In the second example it's considering processDecision a prototype (which is true) and that's why it works.

Upvotes: 1

Related Questions