使用apollo useLazyQuery的customPollingHook的单元测试

时间:2019-10-21 17:00:42

标签: node.js reactjs jestjs react-apollo react-hooks-testing-library

因此,我编写了一个使用useContext和useLazyQuery挂钩的自定义轮询挂钩。我想为此编写一个单元测试,其中应涵盖其返回值的状态和副作用。

到目前为止,我已经做了很多事情,但是我不确定如何继续前进。有提示吗?

export const useUploadActivityPolling = (
  teId: TeIdType
): UploadActivityPollingResult => {
  const { dispatch, uploadActivityId }: StoreContextType = useAppContext();

  const [fetchActivityStatus, { error: UploadActivityError, data: UploadActivityData, stopPolling }] = useLazyQuery(
    GET_UPLOAD_ACTIVITY,
    {
      pollInterval: 3000,
      fetchPolicy: 'network-only',
      variables: { teId, activityId: uploadActivityId },
    }
  );

  useEffect(() => {
    if (UploadActivityData) {
      setUploadActivityId(
        UploadActivityData.getUploadActivityStatus.activity_id,
        dispatch
      );
      updateActivityStateAction(UploadActivityData.getExcelUploadActivityStatus.status, dispatch);
    }
  }, [UploadActivityData]);

  return { fetchActivityStatus, stopPolling, UploadActivityError };
};

import React from 'react';
import { mount } from 'enzyme';

const TestCustomHook = ({ callback }) => {
  callback();
  return null;
};

export const testCustomHook = callback => {
  mount(<TestCustomHook callback={callback} />);
};


describe('useUploadActivityPolling', () => {
  let pollingResult;
  const teId = 'some id';

  beforeEach(() => {
    testCustomHook(() => {
      pollingResult = useUploadActivityPolling(teId);
    });
  });

  test('should have an fetchActivityStatus function', () => {
    expect(pollingResult.fetchActivityStatus).toBeInstanceOf(Function);
  });
});

0 个答案:

没有答案