1 | import React from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import { waitFor, cleanup } from '@testing-library/react';
|
4 | import { avProvidersApi } from '@availity/api-axios';
|
5 | import { QueryClient } from 'react-query';
|
6 | import renderWithClient from './util';
|
7 | import { useProviders } from '..';
|
8 |
|
9 | jest.mock('@availity/api-axios');
|
10 |
|
11 | let queryStates = [];
|
12 | beforeEach(() => {
|
13 | queryStates = [];
|
14 | });
|
15 |
|
16 | const queryClient = new QueryClient();
|
17 |
|
18 | afterEach(() => {
|
19 | jest.clearAllMocks();
|
20 | cleanup();
|
21 | queryClient.clear();
|
22 | queryStates = [];
|
23 | });
|
24 |
|
25 | const pushState = (state) => {
|
26 | queryStates.push(state);
|
27 | };
|
28 |
|
29 | const Component = ({ log }) => {
|
30 |
|
31 |
|
32 | const state = useProviders({}, { cacheTime: 0, retry: false });
|
33 |
|
34 |
|
35 | if (log) log(state);
|
36 |
|
37 | const { data, error, status } = state;
|
38 |
|
39 | return (
|
40 | <div>
|
41 | <h1>Status: {status}</h1>
|
42 | <h1>Data: {JSON.stringify(data)}</h1>
|
43 | <h1>Error: {error}</h1>
|
44 | </div>
|
45 | );
|
46 | };
|
47 |
|
48 | Component.propTypes = {
|
49 | log: PropTypes.func,
|
50 | };
|
51 |
|
52 | describe('useProviders', () => {
|
53 | test('should return an error', async () => {
|
54 | avProvidersApi.getProviders.mockRejectedValueOnce('An error occurred');
|
55 |
|
56 | const { getByText } = renderWithClient(
|
57 | queryClient,
|
58 | <Component log={pushState} />
|
59 | );
|
60 |
|
61 | getByText('Status: loading');
|
62 | await waitFor(() => {
|
63 | const el = getByText('Status: error');
|
64 | expect(el).toBeDefined();
|
65 | });
|
66 | await waitFor(() => {
|
67 | const el = getByText('Error: An error occurred');
|
68 | expect(el).toBeDefined();
|
69 | });
|
70 | });
|
71 |
|
72 | test('should return providers', async () => {
|
73 | avProvidersApi.getProviders.mockResolvedValueOnce({
|
74 | data: {
|
75 | providers: [
|
76 | {
|
77 | id: 'test',
|
78 | lastName: 'test',
|
79 | firstName: 'test',
|
80 | middleName: 'test',
|
81 | uiDisplayName: 'test',
|
82 | },
|
83 | ],
|
84 | },
|
85 | });
|
86 |
|
87 | const { getByText } = renderWithClient(
|
88 | queryClient,
|
89 | <Component log={pushState} />
|
90 | );
|
91 |
|
92 | getByText('Status: loading');
|
93 | await waitFor(() => {
|
94 | const el = getByText(
|
95 | `Data: ${JSON.stringify({
|
96 | data: {
|
97 | providers: [
|
98 | {
|
99 | id: 'test',
|
100 | lastName: 'test',
|
101 | firstName: 'test',
|
102 | middleName: 'test',
|
103 | uiDisplayName: 'test',
|
104 | },
|
105 | ],
|
106 | },
|
107 | })}`
|
108 | );
|
109 | expect(el).toBeDefined();
|
110 | });
|
111 | });
|
112 | });
|