Jestjs 在jest测试中将函数作为参数传递

Jestjs 在jest测试中将函数作为参数传递,jestjs,Jestjs,我想测试这个函数 export const compare = selector => (a, b) => selector(a).localeCompare(selector(b)) 目前,compare函数被称为 const mapStateToProps = state => ({ items: getPolicies(state) .map(d => d) .sort(compare(d => d.name)

我想测试这个函数

export const compare = selector => (a, b) => selector(a).localeCompare(selector(b))
目前,compare函数被称为

const mapStateToProps = state => ({
        items: getPolicies(state)
        .map(d => d)
        .sort(compare(d => d.name)),
 })
我的笑话单元如下所示:

import each from 'jest-each'
import { compare } from './sorting'

describe('sorting functions test', () => {
  describe('compare tests', () => {
    const func = jest.fn(value => value.name)
    each([{ name: 'hello' }, { name: 'world' }], func)
    it('return -1', () => {
      expect(compare(func)).toBe(-1)
    })
  })
})

我只是看不到/找不到传递/模拟参数的d=>d.name的方法。

我设法解决了它

import { compare } from './sorting'

describe('sorting functions test', () => {
  describe('compare function tests', () => {
    it('returns -1', () => {
      const selector = jest.fn(value => value.name)
      const compareFunc = compare(selector)
      expect(compareFunc({ name: 'hello' }, { name: 'world' })).toBe(-1)
      expect(selector).toHaveBeenCalled()
      expect(selector).toHaveBeenCalledTimes(3)
    })
  })
})