GithubHelp home page GithubHelp logo

Comments (2)

JoshuaKGoldberg avatar JoshuaKGoldberg commented on June 16, 2024 1

Hi @AntonyFagundez, thanks for the issue! Totally clear & understandable English; what you're asking for makes sense.

This is an annoying quirk we found around working with dispatched thunks, yeah. I'm guessing your newContact function looks something like:

export const newContact = (values) => (dispatch, getState) => {
  /* ... */
};
expect(dispatch).toHaveBeenCalledWith(newContact(values)); // New function ❌

Structure like that means newContact returns a new [Function anonymous] every time it's called. I'll add an example to the docs.

In the meantime...

Strategy: Same Function

One solution would be to make a function that stays the same between them:

export const newContentThunk = (dispatch, getState) => {
  /* ... */
};

export const newContent = () => newContentThunk;
expect(dispatch).toHaveBeenCalledWith(newContentThunk()); // Same function ✔

...though that direct line of code doesn't support an action prop the way your newContact takes in a values.

Strategy: Memoized Function

If you want to support parameters, you could use something like Lodash's memoize to cache the function returned by newContent:

import { memoize } from "lodash";

export const newContent = memoize((values) => {
  return (dispatch, getState) => {
    /* ... */
  };
});
const values = {
  /* ... */
};

dispatch(newContent(values));

expect(dispatch).toHaveBeenCalledWith(newContent(values)); // Same function ✔

Note that Lodash by default does a strict equality check, so while the above snippet works, newContent({}) === newContent({}) would result in false because the {} is different between those two calls.

from mock-react-redux.

AntonyFagundez avatar AntonyFagundez commented on June 16, 2024 1

Hi @JoshuaKGoldberg thanks for the detailed answer>

I was indeed making a mistake comparing two references in memory.

With your help I finally get a solution (with memoize from lodash)

  test("Should dispatch if all fields exists and were validated", async () => {
    const { dispatch, state } = mockReactRedux();

    const sameFunction = memoize(() => newContact(values)(dispatch, state));

    const mockedSubmit = jest.fn().mockImplementation(() => {
      dispatch(sameFunction());
    });

    render(<ContactForm onSubmit={mockedSubmit} />);

    userEvent.type(screen.getByPlaceholderText("Nombre"), "John");
    userEvent.type(screen.getByPlaceholderText("Email"), "[email protected]");
    userEvent.type(screen.getByPlaceholderText("Teléfono"), "23456789");
    userEvent.type(screen.getByPlaceholderText("Escribe tu mensaje"), "Lorem ipsum dolor sit");

    userEvent.click(screen.getByTestId("contactButton"));

    await waitFor(() => {
      expect(dispatch).toHaveBeenCalled();
      expect(dispatch).toHaveBeenCalledWith(sameFunction());
    });
  });

Thanks again!!

from mock-react-redux.

Related Issues (13)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.