UNPKG

3.03 kBJavaScriptView Raw
1import React from 'react';
2import PropTypes from 'prop-types';
3import { waitFor, cleanup } from '@testing-library/react';
4import { avOrganizationsApi } from '@availity/api-axios';
5import { QueryClient } from 'react-query';
6import renderWithClient from './util';
7import { useOrganizations } from '..';
8
9jest.mock('@availity/api-axios');
10
11let queryStates = [];
12beforeEach(() => {
13 queryStates = [];
14});
15
16const queryClient = new QueryClient();
17
18afterEach(() => {
19 jest.clearAllMocks();
20 cleanup();
21 queryClient.clear();
22 queryStates = [];
23});
24
25const pushState = (state) => {
26 queryStates.push(state);
27};
28
29const Component = ({ log }) => {
30 // Mirror testing methods from react-query instead of relying on timing or booleans
31 // https://github.com/tannerlinsley/react-query/blob/master/src/react/tests/useQuery.test.tsx
32 const state = useOrganizations({}, { cacheTime: 0, retry: false });
33
34 // not directly used in assertions here, but useful for debugging purposes
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
48Component.propTypes = {
49 log: PropTypes.func,
50};
51
52describe('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});