React Native testing cannot read property 'call' of undefined

194 views Asked by At

I have built an application in react native, and am now going through the process of updating some old test suites. The test running an error is a component test using react test renderer to simulate the functionality of the screen.

Error Summary: One of the expect statements is throwing an error saying

Cannot read property 'call' of undefined

When the value exists and I can print out the property call like so console.log(store.dispatch.mock.calls[6][0]); and it gives me the expected value.

CODE:

//Imports
jest.spyOn(Date, "now").mockImplementation(() => 1479427200000);
const mockStore = configureStore([]);

describe("block1", () => {
    it("test1", async done => {
        try {
            let component;
            let store = mockStore(stores.SummaryR1);
            store.dispatch = await jest.fn();
            const mockDate = new Date(1466424490000);
            const spy = jest.spyOn(global, "Date").mockImplementation(() => mockDate);
            Date.now = jest.fn(() => 1466424490000);
            await act(async () => {
                component = await renderer.create(
                    <PaperProvider>
                        <Provider store={store}>
                            <Receive_Signature />
                        </Provider>
                    </PaperProvider>
                );
            });
            const expected = await component.toJSON();
            expect(expected).toMatchSnapshot();

            await act(async () => {
                //action
            });
            await act(async () => {
                //action
            });
            await act(async () => {
                //action
            });
            await act(async () => {
                //action
            });
            await act(async () => {
                //action
            });
            expect(store.dispatch).toHaveBeenCalledTimes(8);
            expect(store.dispatch).toHaveBeenNthCalledWith(1, {results1});
            expect(store.dispatch).toHaveBeenNthCalledWith(2, {results2});
            expect(store.dispatch).toHaveBeenNthCalledWith(3, {results3});
            expect(store.dispatch).toHaveBeenNthCalledWith(4, {results4});
            expect(store.dispatch).toHaveBeenNthCalledWith(5, {results5});
            expect(store.dispatch).toHaveBeenNthCalledWith(6, {results6});
            expect(store.dispatch).toHaveBeenNthCalledWith(7, {results7} );
            expect(store.dispatch).toHaveBeenNthCalledWith(8, {results8});
            expect(navigateToScreen).toHaveBeenCalledTimes(1);
            expect(navigateToScreen.mock.calls[0][0]).toEqual("Processor_Dashboard");

            done();
        } catch (error) {
            done.fail(error);
        }
    }, 15000);

The error is forming on testing results7 on the 7th call.

Firstly I know there are 8 calls because of expect(store.dispatch).toHaveBeenCalledTimes(8);

I can then also print out results7, and see that I have the correct data for results7. But when I run it in the jest expect statement I get the error:

Cannot read property 'call' of undefined

I have no idea why there is this error since all the other expects run, and if I comment out just the one statement the rest of the suite runs fine as well. For some reason it is only erroring out on the one expect statement.

Obviously the data has been removed, but does not affect how it would run.

0

There are 0 answers