笑话|接受多个参数的测试实用程序功能

问题描述 投票:0回答:1

我已经创建了该实用程序。它包含3个参数,(值:字符串,服务:任意which is an actual service(GET),名称:字符串)。

这里是实用程序:

import { serverErrorResponseUtil } from 'util/serverErrorResponseUtil';
import { HttpError } from 'factories/httpFactory';

const validateUniqueName = (value: string, service: any, name: string): string =>
  service()
    .then(({ data }: { data: object }) => {
      if (Array.isArray(data) && data.find(resourceData => resourceData.name === value)) {
        throw new Error(`${name} already exists`);
      }
    })
    .catch((error: HttpError) => {
      throw serverErrorResponseUtil(error);
    });

export default validateUniqueName;

[我正在尝试测试该实用程序,但是由于我需要接受service作为参数,因此我对value, name感到困惑不已。

这是我所做的:

import validateUniqueName from '../validateUniqueName';
import validationRules from '../__mocks__/validationRules';

jest.mock('../__mocks__/services.ts');

describe('validateUniqueName', () => {
  let validateUniqueNameFn: Function = jest.fn();
  beforeEach(() => {
    validateUniqueNameFn = validateUniqueName;
  });
  it('should THROW an error if the given value already exists', () => {});
  it('should ACCEPT the data if the pass name is unique', () => {});
});

这是我要介绍的两种情况。你能帮我一下吗?只是一些指南会很好。

reactjs typescript async-await jestjs
1个回答
0
投票

这里是解决方法:

© www.soinside.com 2019 - 2024. All rights reserved.