gpt4 book ai didi

redux - 如何从嵌套 reducer 测试重新选择选择器?

转载 作者:行者123 更新时间:2023-12-04 15:40:12 27 4
gpt4 key购买 nike

我使用 Reselect 库创建了一个简单的 Redux 选择器。选择器获取数组中的最后一个对象。

Reselect docs ,测试示例是使用 Assert 库而不是 Expect 库构建的,但我的应用程序源自 React Create App,因此我包含了 Expect 和 Jest。

无论如何,无论我如何编写选择器测试,它都会卡在嵌套的 reducer 上:TypeError: Cannot read property 'arrayOfObj' of undefined .

Selectors.js

import { createSelector } from 'reselect'

const getArrayOfObj = (state) => state.reducer1.arrayOfObj

export const getLastObj = createSelector(
[ getArrayOfObj ],
(arrayOfObj) => {
return arrayOfObj[arrayOfObj.length-1]
}
)

Selectors.test.js
import * as selectors from 'Selectors'

const mockState = {
reducer1: {
arrayOfObj: [
{id: 1},
{id: 2},
{id: 3},
{id: 4},
],
...
},
reducer2: {
...
}
}

describe('selectors', () => {
it('getLastObj should get last obj in array', () => {
expect(selectors.getLastObj(mockState)).toDeepEqual({id: 4});
});
});

最佳答案

我能够通过更改 expect 来解决该错误声明来自 .toDeepEqual().toEqual()

关于redux - 如何从嵌套 reducer 测试重新选择选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45395616/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com