Back

Technologies:

javascriptjavascript
avatar
Tolerim
a month ago

What is the process for altering values in a mocked function?

What is the process for modifying a value in a mocked function?

To modify a value in a mocked function, you can use the jest.fn() method along with jest.mock(). By doing so, you can access the setSelectedList function in the useSelection() method and alter its value accordingly. Here's an example code:
const mockSetSelectedList = jest.fn()
jest.mock('some-lib', () => ({
  useSelection: () => ({
    setSelectedList: mockSetSelectedList,
    verifiedUser: { id: 'id' } // <- this value can be modified for different tests
  })
}))

test('should call mock function', async () => {
  expect(mockSetSelectedList).toHaveBeenCalled()
})
Suppose you need to run the same test with verifiedUser: undefined. What would be the solution?

If you need to run the same test with verifiedUser: undefined, you can use the not.toHaveBeenCalled() method to confirm that the mockSetSelectedList function was not called. Here's an example code:
test('should not call mockfunction', async () => {
  // set verifiedUser to `undefined`
  jest.mock('some-lib', () => ({
    useSelection: () => ({
      setSelectedList: mockSetSelectedList,
      verifiedUser: undefined // <- modifying to undefined
    })
  }))
  expect(mockSetSelectedList).not.toHaveBeenCalled()
})
Answers(2)
avatar
Vogi
a month ago
Verified Answer
To modify a value in a mocked function, you can use the jest.fn() method along with jest.mock(). By doing so, you can access the setSelectedList function in the useSelection() method and alter its value accordingly. Here's an example code:
jest.mock('some-lib', () => ({
  useSelection: () => ({
    setSelectedList: mockSetSelectedList,
    verifiedUser: { id: 'id' } // <- this value can be modified for different tests
  })
}))

test('should call mock function', async () => {
  expect(mockSetSelectedList).toHaveBeenCalled()
})
If you need to run the same test with verifiedUser: undefined, you can use the not.toHaveBeenCalled() method to confirm that the mockSetSelectedList function was not called. Here's an example code:
// set verifiedUser to `undefined`
  jest.mock('some-lib', () => ({
    useSelection: () => ({
      setSelectedList: mockSetSelectedList,
      verifiedUser: undefined // <- modifying to undefined
    })
  }))
  expect(mockSetSelectedList).not.toHaveBeenCalled()
})
I hope this helps! Let me know if you have any other questions.
avatar
Tolerim
a month ago
To change the value of verifiedUser in the mocked function, you can simply pass a different object with a different id value. For example, to run the test with verifiedUser as undefined, you can update the useSelection mock like this:
jest.mock('some-lib', () => ({
  useSelection: () => ({
    setSelectedList: mockSetSelectedList,
    verifiedUser: undefined
  })
}))
This will set verifiedUser to undefined in the mocked function, and now your test for not calling mockSetSelectedList should pass.
;