Comments (2)
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.
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)
- Investigate Provider support
- Throw errors on other unsupported APIs provided by react-redux
- Support default imports HOT 1
- Errors in older node environments for export * syntax HOT 1
- This should really be named mock-react-redux
- Consider allowing usage outside describe()s to set up base state
- Create some kind of "docs/Philosophy.md" page
- Migrate to create-typescript-app@1 HOT 1
- Allow inline selectors HOT 1
- Investigate support for other test frameworks/runners
- Add support for Vitest
- Support connect() usage HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mock-react-redux.