2

Hello I'm trying to test the createSelector function in jest but I'm stuck. So the problem is that never enters in the if of the function getContent and I need to enter, for the coverage.

File

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
}

Testing file

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

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

Any suggestions?

Jonathan
  • 469
  • 1
  • 6
  • 20

1 Answers1

2

selectDetailsObject needs a state object, not a function. So you can either just create a mockStore object, or call it when you hand it in:

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

test('should select the content of the table', () => {
    const result = selectDetailsObject(mockStore())
    expect(result.objects).toBe()
})
Will Jenkins
  • 9,507
  • 1
  • 27
  • 46