Skip to content

Commit

Permalink
complaints test api
Browse files Browse the repository at this point in the history
  • Loading branch information
flacoman91 committed Dec 3, 2024
1 parent ffc6416 commit adbf3c4
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 0 deletions.
168 changes: 168 additions & 0 deletions src/api/complaints.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
import { renderHook } from '@testing-library/react-hooks';
import { Provider } from 'react-redux';
import { store } from '../app/store';
import {
useGetAggregationsQuery,
useGetDocumentQuery,
useGetListQuery,
useGetMapQuery,
useGetTrendsQuery,
} from './complaints';
import { aggResponse } from '../components/List/ListPanel/fixture';
import fetchMock from 'jest-fetch-mock';
import { waitFor } from '@testing-library/react';
import { aggResponseTransformed, documentResponse } from './fixture';

fetchMock.enableMocks();

/**

Check warning on line 18 in src/api/complaints.spec.js

View workflow job for this annotation

GitHub Actions / frontend

Missing JSDoc @returns declaration
*
* @param root0

Check warning on line 20 in src/api/complaints.spec.js

View workflow job for this annotation

GitHub Actions / frontend

Missing JSDoc @param "root0" description

Check warning on line 20 in src/api/complaints.spec.js

View workflow job for this annotation

GitHub Actions / frontend

Missing JSDoc @param "root0" type
* @param root0.children

Check warning on line 21 in src/api/complaints.spec.js

View workflow job for this annotation

GitHub Actions / frontend

Missing JSDoc @param "root0.children" description

Check warning on line 21 in src/api/complaints.spec.js

View workflow job for this annotation

GitHub Actions / frontend

Missing JSDoc @param "root0.children" type
*/
// eslint-disable-next-line react/prop-types
function Wrapper({ children }) {
return <Provider store={store}>{children}</Provider>;
}

beforeEach(() => {
fetchMock.resetMocks();
});

afterEach(() => {
jest.restoreAllMocks();
});

describe('getAggregations', () => {
it('renders hook and transforms data', async () => {
fetchMock.mockResponseOnce(JSON.stringify(aggResponse));
const { result } = renderHook(
() => useGetAggregationsQuery({ foo: 'bar' }),
{
wrapper: Wrapper,
},
);
expect(result.current).toMatchObject({
status: 'pending',
endpointName: 'getAggregations',
isLoading: true,
isSuccess: false,
isError: false,
isFetching: true,
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));

expect(fetchMock).toBeCalledTimes(1);
expect(result.current.data).toEqual(aggResponseTransformed);
});
});

describe('getDocument', () => {
it('renders hook transforms data', async () => {
fetchMock.mockResponseOnce(JSON.stringify(documentResponse));
const { result } = renderHook(() => useGetDocumentQuery(12334), {
wrapper: Wrapper,
});
expect(result.current).toMatchObject({
status: 'pending',
endpointName: 'getDocument',
isLoading: true,
isSuccess: false,
isError: false,
isFetching: true,
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));

expect(fetchMock).toBeCalledTimes(1);
expect(result.current.data).toEqual({
company: 'JPMORGAN CHASE & CO.',
company_public_response: 'Company acknowledges the complaint',
company_response: 'Closed with explanation',
complaint_id: '2371744',
complaint_what_happened: 'Lorem ipsum dolor sit amet',
consumer_consent_provided: 'Consent provided',
consumer_disputed: 'Yes',
date_received: '2017-03-04T12:00:00',
date_sent_to_company: '2017-03-04T12:00:00',
has_narrative: true,
issue: 'Account opening, closing, or management',
product: 'Bank account or service',
state: 'KY',
sub_issue: 'Closing',
sub_product: 'Checking account',
submitted_via: 'Web',
tags: 'Older American',
timely: 'Yes',
zip_code: '423XX',
});
});
});

describe('getList', () => {
it('renders hook', async () => {
fetchMock.mockResponseOnce(JSON.stringify(aggResponse));
const { result } = renderHook(() => useGetListQuery({ foo: 'bar' }), {
wrapper: Wrapper,
});
expect(result.current).toMatchObject({
status: 'pending',
endpointName: 'getList',
isLoading: true,
isSuccess: false,
isError: false,
isFetching: true,
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));

expect(fetchMock).toBeCalledTimes(1);
expect(result.current.data).toEqual('foobar');
});
});

describe('getMap', () => {
it('renders hook', async () => {
fetchMock.mockResponseOnce(JSON.stringify(aggResponse));
const { result } = renderHook(() => useGetMapQuery({ foo: 'bar' }), {
wrapper: Wrapper,
});

expect(result.current).toMatchObject({
status: 'pending',
endpointName: 'getMap',
isLoading: true,
isSuccess: false,
isError: false,
isFetching: true,
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));

expect(fetchMock).toBeCalledTimes(1);
expect(result.current.data).toEqual('foobar');
});
});

describe('getTrends', () => {
it('renders hook', async () => {
fetchMock.mockResponseOnce(JSON.stringify(aggResponse));
const { result } = renderHook(() => useGetTrendsQuery({ foo: 'bar' }), {
wrapper: Wrapper,
});
expect(result.current).toMatchObject({
status: 'pending',
endpointName: 'getTrends',
isLoading: true,
isSuccess: false,
isError: false,
isFetching: true,
});

await waitFor(() => expect(result.current.isSuccess).toBe(true));

expect(fetchMock).toBeCalledTimes(1);
expect(result.current.data).toEqual('foobar');
});
});
Loading

0 comments on commit adbf3c4

Please sign in to comment.