Skip to content

Instantly share code, notes, and snippets.

@jhanstra
Created July 25, 2019 03:33
Show Gist options
  • Save jhanstra/dffddf02df0a9088c7c03a9044d9687f to your computer and use it in GitHub Desktop.
Save jhanstra/dffddf02df0a9088c7c03a9044d9687f to your computer and use it in GitHub Desktop.
attempt #1 at testing hooks
import { useApiData } from '../index';
// import CandidateInterviewScheduler from '../../components/CandidateInterviewScheduler';
// import React from 'react';
// import { render, cleanup, waitForElement } from '@testing-library/react';
// describe('API integration', () => {
// afterEach(cleanup);
// it('loads interview data based on applicationId and interviewId', async () => {
// const applicationId = '01014aeb3e7ed57f8d274ddd75e280e9be239c45e3';
// const interviewId = 'ddbfeff0-fd1f-4d5f-b3c8-3e75f027c37e';
// const { } = render(<CandidateInterviewScheduler />);
// });
// });
import React from 'react';
import 'whatwg-fetch';
import { renderHook } from '@testing-library/react-hooks';
import fetchMock from 'fetch-mock';
import { act } from 'react-test-renderer';
describe('useApiData', () => {
beforeAll(() => {
global.fetch = fetch;
});
afterAll(() => {
fetchMock.restore();
});
it('should return data with a successful request', async () => {
const { result } = renderHook(() => useApiData('jfkdslaj', 'fdjdkslj'));
fetchMock.mock('test.com', {
returnedData: 'foo'
});
await act(async () => {
result.current.callApi('test.com');
});
expect(result.current.data).toBe({
returnedData: 'foo'
});
});
});
@jhanstra
Copy link
Author

beforeEach(() => {
window.history.pushState({}, 'Test', '/candidate/application/01014aeb3e7ed57f8d274ddd75e280e9be239c45e3/interviewrequest/ddbfeff0-fd1f-4d5f-b3c8-3e75f027c37e');
});

@jhanstra
Copy link
Author

it('should call prop functions with onChange event', () => {
const { container, debug } = render(<PhoneNumberField {...props} />);
const phoneNumber = container.querySelector('[name=textinput]');
fireEvent.change(phoneNumber, { target: { value: '1' } });
debug();
expect(props.form.setFieldTouched).toHaveBeenCalledTimes(1);
expect(props.form.setFieldValue).toHaveBeenCalledTimes(1);
expect(props.field.onChange).toHaveBeenCalledTimes(1);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment