英文:
Redux mock store is not being updated on react test environment using Jest/RTL
问题
以下是翻译好的代码部分:
I'm new on testing and I'm trying to implement a unit test to check if a counter is being correctly updated after a button is clicked. Everything works fine on dev and production environments, so I'm pretty sure it has the correct behavior, but on the test environment, I couldn't figure out how to make it work so far.
My code is too big and complex to put it all here, but this is my test setup:
import { render, screen, fireEvent, act } from '@testing-library/react';
import { Provider } from 'react-redux'
import configureStore from 'redux-mock-store';
import ProductsListQtde from './ProductsListQtde';
import * as useInitProductsModule from 'hooks/useInitProducts';
import { ProductsList } from 'common/types';
const mockStore = configureStore([]);
const initialState = {
productsDataState: [
{
id: 1,
nome: "Produto 1",
qtde: 2,
valor: 10,
valorTotal: 20
},
{
id: 2,
nome: "Produto 2",
qtde: 5,
valor: 20,
valorTotal: 60
},
],
};
const storeMock = mockStore(initialState);
describe('quantity component', () => {
test('plus button is clicked the quantity is incremented by 1', async () => {
const id = 1;
const mockedInitProducts = jest.fn();
jest.spyOn(useInitProductsModule, 'default').mockReturnValue({
initProducts: mockedInitProducts,
});
const { container } = render(
<Provider store={storeMock}>
<ProductsListQtde qtde={2} id={id} />
</Provider>
);
const plusButton = screen.getByLabelText('plus');
fireEvent.click(plusButton);
const updatedQtdeValues = [
{
id: 1,
nome: "Produto 1",
qtde: 3,
valor: 10,
valorTotal: 30
},
{
id: 2,
nome: "Produto 2",
qtde: 5,
valor: 20,
valorTotal: 60
},
]
expect(mockedInitProducts).toHaveBeenCalledTimes(1);
expect(mockedInitProducts).toHaveBeenCalledWith('updQtde', updatedQtdeValues);
const updatedState = storeMock.getState().productsDataState;
const updatedQtde = updatedState.find((product: ProductsList) => product.id === id)?.qtde;
expect(updatedQtde).toBe(3);
});
});
请注意,这只是代码的翻译,不包括问题的回答。如果您有任何其他疑问或需要进一步的帮助,请随时提出。
英文:
I'm new on testing and I'm trying to implement a unit test to check if a counter is being correctly updated after a button is clicked. Everything works fine on dev and production environments, so I'm pretty sure it has the correct behavior, but on test environment I couldn't figure out how to make it work so far.
My code is too big and complex to put it all here, but this is my test setup:
import { render, screen, fireEvent, act } from '@testing-library/react';
import { Provider } from 'react-redux'
import configureStore from 'redux-mock-store';
import ProductsListQtde from './ProductsListQtde';
import * as useInitProductsModule from 'hooks/useInitProducts';
import { ProductsList } from 'common/types';
const mockStore = configureStore([]);
const initialState = {
productsDataState: [
{
id: 1,
nome: "Produto 1",
qtde: 2,
valor: 10,
valorTotal: 20
},
{
id: 2,
nome: "Produto 2",
qtde: 5,
valor: 20,
valorTotal: 60
},
],
};
const storeMock = mockStore(initialState);
describe('quantity component', () => {
test('plus button is clicked the quantity is incremented by 1', async () => {
const id = 1;
const mockedInitProducts = jest.fn();
jest.spyOn(useInitProductsModule, 'default').mockReturnValue({
initProducts: mockedInitProducts,
});
const { container } = render(
<Provider store={storeMock}>
<ProductsListQtde qtde={2} id={id} />
</Provider>
);
const plusButton = screen.getByLabelText('plus');
fireEvent.click(plusButton);
const updatedQtdeValues = [
{
id: 1,
nome: "Produto 1",
qtde: 3,
valor: 10,
valorTotal: 30
},
{
id: 2,
nome: "Produto 2",
qtde: 5,
valor: 20,
valorTotal: 60
},
]
expect(mockedInitProducts).toHaveBeenCalledTimes(1);
expect(mockedInitProducts).toHaveBeenCalledWith('updQtde', updatedQtdeValues);
const updatedState = storeMock.getState().productsDataState;
const updatedQtde = updatedState.find((product: ProductsList) => product.id === id)?.qtde;
expect(updatedQtde).toBe(3);
});
});
Every assertion passes but the last one, the function is being called with the correct values, but the store is not being updated. qtde property starts as 2 and should be updated to 3.
Is there anything wrong with my approach or am I missing something?
I'm not using react toolkit, this is an old project and I'm implementing tests on it only for educational purposes.
Please, let me know if I need to add more information to make it clearer.
答案1
得分: 1
从redux-mock-store
文档中:
请注意,该库旨在测试与操作相关的逻辑,而不是与 reducer 相关的逻辑。换句话说,它不会更新 Redux 存储。
查看redux-mock-store
包的getState()
方法,传递给mockStore
函数的状态对象将不经过任何操作而返回。
在测试 Redux 动作和 reducer 时,您有一些选择。
- redux-actions-assertions
- 没有任何库,只需使用 Redux 的createStore来创建模拟存储,分派动作,获取状态树(
store.getState()
),然后进行断言。
英文:
From redux-mock-store
doc:
> Please note that this library is designed to test the action-related logic, not the reducer-related one. In other words, it does not update the Redux store.
Take a look at getState()
method of redux-mock-store
package, the state object passed in mockStore
function is returned without any operations.
You have some choices for testing redux actions and reducers.
- redux-actions-assertions
- Without any library, just use redux createStore to create the mock store, dispatch the actions, get the state tree(
store.getState()
), and make assertions.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论