1 | import React from 'react';
|
2 | import PropTypes from 'prop-types';
|
3 | import { waitFor, cleanup } from '@testing-library/react';
|
4 | import { avOrganizationsApi } from '@availity/api-axios';
|
5 | import { QueryClient } from 'react-query';
|
6 | import renderWithClient from './util';
|
7 | import { useOrganizations } 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 = useOrganizations({}, { 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('useOrganizations', () => {
|
53 | test('should return an error', async () => {
|
54 | avOrganizationsApi.getOrganizations.mockRejectedValueOnce(
|
55 | 'An error occurred'
|
56 | );
|
57 |
|
58 | const { getByText } = renderWithClient(
|
59 | queryClient,
|
60 | <Component log={pushState} />
|
61 | );
|
62 |
|
63 | getByText('Status: loading');
|
64 | await waitFor(() => {
|
65 | const el = getByText('Status: error');
|
66 | expect(el).toBeDefined();
|
67 | });
|
68 | await waitFor(() => {
|
69 | const el = getByText('Error: An error occurred');
|
70 | expect(el).toBeDefined();
|
71 | });
|
72 | });
|
73 |
|
74 | test('should return organizations', async () => {
|
75 | avOrganizationsApi.getOrganizations.mockResolvedValueOnce({
|
76 | data: {
|
77 | organizations: [
|
78 | {
|
79 | links: {
|
80 | permissions: { href: 'test' },
|
81 | patients: { href: 'test' },
|
82 | self: { href: 'test' },
|
83 | admin: { href: 'test' },
|
84 | businessArrangements: { href: 'test' },
|
85 | users: { href: 'test' },
|
86 | },
|
87 | },
|
88 | ],
|
89 | },
|
90 | });
|
91 |
|
92 | const { getByText } = renderWithClient(
|
93 | queryClient,
|
94 | <Component log={pushState} />
|
95 | );
|
96 |
|
97 | getByText('Status: loading');
|
98 | await waitFor(() => {
|
99 | const el = getByText(
|
100 | `Data: ${JSON.stringify({
|
101 | data: {
|
102 | organizations: [
|
103 | {
|
104 | links: {
|
105 | permissions: { href: 'test' },
|
106 | patients: { href: 'test' },
|
107 | self: { href: 'test' },
|
108 | admin: { href: 'test' },
|
109 | businessArrangements: { href: 'test' },
|
110 | users: { href: 'test' },
|
111 | },
|
112 | },
|
113 | ],
|
114 | },
|
115 | })}`
|
116 | );
|
117 | expect(el).toBeDefined();
|
118 | });
|
119 | });
|
120 | });
|