如何在 jest 中使用 reselect createSelector 函数进行测试?

How to test with the reselect createSelector function in jest?

您好,我正在尝试测试 jest 中的 createSelector 函数,但我卡住了。所以问题是永远不会进入函数 getContentif 而我需要进入,以便覆盖。

文件

export const selectDetailsObject = createSelector(
    [
        (state) => state.objectId,
        (state) => state.objects,
    ],
    (
        objectId,
        objects,
    ) => getContent(objectId, objects)
)

function getContent (objectId, objects) {
    const result = {}
    if (objectId && objectId !== '') {
        result.objectId = objectId
        result.objectInfo = objects[objectId]
        result.objectDetails = objectDetails[objectId]
    }
    return result
}

测试文件

const mockStore = () => ({
    objectId: '123',
    objects: {},
})

test('should select the content of the table', () => {
    const result = selectDetailsObject(mockStore)
    expect(result.objects).toBe()
})

有什么建议吗?

selectDetailsObject 需要状态对象,而不是函数。因此,您可以只创建一个 mockStore 对象,也可以在提交时调用它:

const mockStore = () => ({
    objectId: '123',
    objects: {},
})

test('should select the content of the table', () => {
    const result = selectDetailsObject(mockStore())
    expect(result.objects).toBe()
})