lundi 24 août 2020

How to return different values for mocked named import function using jest

I would like to mock a named import function and return different values for the imported promise for every test.

list.spec.tsx

import React from 'react'
import { render } from '@testing-library/react'
import List from '../List'
import { useListPage } from '@app/data-access-list-page'

jest.mock('@app/data-access-list-page', () => ({
    useListPage: () => jest.fn(() => Promise.resolve())
}))

describe('List', () => {
    test('should call mocked function', () => {
        // useListPage.mockReturnValue('something')
        render(<List />)
        expect(useListPage).toHaveBeenCalled()
    })
})

I do get the error Matcher error: received value must be a mock or spy function

Also adding the mockReturnValue() seems to be the wrong syntax

data-access-list-page.tsx

import { useListQuery } from '@app/graphql'

export const useListPage = () => {
  const param: object = {
    options: () => ({
    pollInterval: 1000
    })
  }
  const list = useListQuery(param)
  return list
}

Aucun commentaire:

Enregistrer un commentaire