在笑话测试中将该函数作为参数传递

时间:2018-09-17 18:21:54

标签: jestjs

我要测试此功能

export const compare = selector => (a, b) => selector(a).localeCompare(selector(b))

当前,比较函数被称为

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的方法。

1 个答案:

答案 0 :(得分:0)

我设法解决了

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)
    })
  })
})

Guidance found here