@availity/hooks 6.0.0 → 7.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,49 @@
1
+ import React from 'react';
2
+ import { waitFor, cleanup } from '@testing-library/react';
3
+ import { QueryClient } from '@tanstack/react-query';
4
+ import { http, HttpResponse } from 'msw';
5
+ import { server } from '@availity/mock/src/server';
6
+ import { USER_A2 } from '@availity/mock/src/routes';
7
+ import renderWithClient from './util';
8
+ import { useOrganizations } from '../src/index';
9
+
10
+ const queryClient = new QueryClient();
11
+
12
+ afterEach(() => {
13
+ cleanup();
14
+ queryClient.clear();
15
+ });
16
+
17
+ const Component = () => {
18
+ const { data, error, status } = useOrganizations({}, { gcTime: 0, retry: false });
19
+
20
+ return (
21
+ <div>
22
+ <h1>Status: {status}</h1>
23
+ <h1>Data: {JSON.stringify(data?.data)}</h1>
24
+ <h1>Error: {error?.message}</h1>
25
+ </div>
26
+ );
27
+ };
28
+
29
+ describe('useOrganizations', () => {
30
+ test('should return an error', async () => {
31
+ server.use(http.get(USER_A2, () => HttpResponse.error()));
32
+
33
+ const { getByText } = renderWithClient(queryClient, <Component />);
34
+
35
+ getByText('Status: pending');
36
+ await waitFor(() => {
37
+ expect(getByText('Status: error')).toBeDefined();
38
+ });
39
+ });
40
+
41
+ test('should return organizations', async () => {
42
+ const { getByText } = renderWithClient(queryClient, <Component />);
43
+
44
+ getByText('Status: pending');
45
+ await waitFor(() => {
46
+ expect(getByText(/Techmania/)).toBeDefined();
47
+ });
48
+ });
49
+ });
@@ -0,0 +1,52 @@
1
+ import React from 'react';
2
+ import { waitFor, cleanup } from '@testing-library/react';
3
+ import { QueryClient } from '@tanstack/react-query';
4
+ import { http, HttpResponse } from 'msw';
5
+ import { server } from '@availity/mock/src/server';
6
+ import { PERMISSIONS } from '@availity/mock/src/routes';
7
+ import renderWithClient from './util';
8
+ import { usePermissions } from '../src/index';
9
+
10
+ const queryClient = new QueryClient();
11
+
12
+ afterEach(() => {
13
+ cleanup();
14
+ queryClient.clear();
15
+ });
16
+
17
+ const Component = () => {
18
+ const { data, error, status } = usePermissions({}, { gcTime: 0, retry: false });
19
+
20
+ return (
21
+ <div>
22
+ <h1>Status: {status}</h1>
23
+ <h1>Data: {JSON.stringify(data?.data)}</h1>
24
+ <h1>Error: {error?.message}</h1>
25
+ </div>
26
+ );
27
+ };
28
+
29
+ describe('usePermissions', () => {
30
+ test('should return an error', async () => {
31
+ server.use(http.get(PERMISSIONS, () => HttpResponse.error()));
32
+
33
+ const { getByText } = renderWithClient(queryClient, <Component />);
34
+
35
+ getByText('Status: pending');
36
+ await waitFor(() => {
37
+ expect(getByText('Status: error')).toBeDefined();
38
+ });
39
+ });
40
+
41
+ test('should return permissions', async () => {
42
+ const { getByText } = renderWithClient(queryClient, <Component />);
43
+
44
+ getByText('Status: pending');
45
+ await waitFor(() => {
46
+ expect(getByText(/"id":"123"/)).toBeDefined();
47
+ });
48
+ await waitFor(() => {
49
+ expect(getByText(/Do the thing/)).toBeDefined();
50
+ });
51
+ });
52
+ });
@@ -0,0 +1,49 @@
1
+ import React from 'react';
2
+ import { waitFor, cleanup } from '@testing-library/react';
3
+ import { QueryClient } from '@tanstack/react-query';
4
+ import { http, HttpResponse } from 'msw';
5
+ import { server } from '@availity/mock/src/server';
6
+ import { PROVIDERS } from '@availity/mock/src/routes';
7
+ import renderWithClient from './util';
8
+ import { useProviders } from '../src/index';
9
+
10
+ const queryClient = new QueryClient();
11
+
12
+ afterEach(() => {
13
+ cleanup();
14
+ queryClient.clear();
15
+ });
16
+
17
+ const Component = () => {
18
+ const { data, error, status } = useProviders({}, { gcTime: 0, retry: false });
19
+
20
+ return (
21
+ <div>
22
+ <h1>Status: {status}</h1>
23
+ <h1>Data: {JSON.stringify(data?.data)}</h1>
24
+ <h1>Error: {error?.message}</h1>
25
+ </div>
26
+ );
27
+ };
28
+
29
+ describe('useProviders', () => {
30
+ test('should return an error', async () => {
31
+ server.use(http.get(PROVIDERS, () => HttpResponse.error()));
32
+
33
+ const { getByText } = renderWithClient(queryClient, <Component />);
34
+
35
+ getByText('Status: pending');
36
+ await waitFor(() => {
37
+ expect(getByText('Status: error')).toBeDefined();
38
+ });
39
+ });
40
+
41
+ test('should return providers', async () => {
42
+ const { getByText } = renderWithClient(queryClient, <Component />);
43
+
44
+ getByText('Status: pending');
45
+ await waitFor(() => {
46
+ expect(getByText(/BITREX/)).toBeDefined();
47
+ });
48
+ });
49
+ });
@@ -0,0 +1,58 @@
1
+ import React from 'react';
2
+ import { waitFor, cleanup } from '@testing-library/react';
3
+ import { QueryClient } from '@tanstack/react-query';
4
+ import { http, HttpResponse } from 'msw';
5
+ import { server } from '@availity/mock/src/server';
6
+ import { STASH } from '@availity/mock/src/routes';
7
+ import { useStash } from '../src';
8
+ import renderWithClient from './util';
9
+
10
+ const queryClient = new QueryClient();
11
+
12
+ afterEach(() => {
13
+ cleanup();
14
+ queryClient.clear();
15
+ });
16
+
17
+ const Component = ({ sessionId }: { sessionId: string }) => {
18
+ const { data, error, status } = useStash(sessionId, { gcTime: 0, retry: false });
19
+
20
+ return (
21
+ <div>
22
+ <h1>Status: {status}</h1>
23
+ <h1>Data: {JSON.stringify(data)}</h1>
24
+ <h1>Error: {error?.message}</h1>
25
+ </div>
26
+ );
27
+ };
28
+
29
+ describe('useStash', () => {
30
+ test('should return an error', async () => {
31
+ server.use(http.get(STASH, () => HttpResponse.error()));
32
+
33
+ const { getByText } = renderWithClient(queryClient, <Component sessionId="test-session-id" />);
34
+
35
+ getByText('Status: pending');
36
+ await waitFor(() => {
37
+ expect(getByText('Status: error')).toBeDefined();
38
+ });
39
+ });
40
+
41
+ test('should return stash data', async () => {
42
+ const { getByText } = renderWithClient(queryClient, <Component sessionId="test-session-id" />);
43
+
44
+ getByText('Status: pending');
45
+ await waitFor(() => {
46
+ expect(getByText(/"key":"value"/)).toBeDefined();
47
+ });
48
+ await waitFor(() => {
49
+ expect(getByText(/"nested":\{"foo":"bar"\}/)).toBeDefined();
50
+ });
51
+ });
52
+
53
+ test('should not fetch when sessionId is empty', () => {
54
+ const { getByText } = renderWithClient(queryClient, <Component sessionId="" />);
55
+
56
+ expect(getByText('Status: pending')).toBeDefined();
57
+ });
58
+ });
@@ -4,8 +4,7 @@ import { useToggle } from '../src/index';
4
4
 
5
5
  afterEach(cleanup);
6
6
 
7
- // eslint-disable-next-line react/prop-types
8
- const Component = ({ initialToggle = false }) => {
7
+ const Component = ({ initialToggle = false }: { initialToggle?: boolean }) => {
9
8
  const [isToggled, toggle] = useToggle(initialToggle);
10
9
 
11
10
  return (
@@ -1,5 +1,6 @@
1
+ import React from 'react';
1
2
  import { render, screen, fireEvent } from '@testing-library/react';
2
- import { MemoryRouter, useNavigate, Routes, Route } from 'react-router-dom';
3
+ import { MemoryRouter, useNavigate, Routes, Route } from 'react-router';
3
4
  import avMessageMock from '@availity/message-core';
4
5
 
5
6
  import useUpdateNav from '../src/useUpdateNav';
@@ -6,7 +6,6 @@ const Component = () => {
6
6
  const { height, width } = useWindowDimensions();
7
7
  return (
8
8
  <div data-testid="window_dimensions">
9
- {' '}
10
9
  <span data-testid="window_height">{height}</span>
11
10
  <span data-testid="window_width">{width}</span>
12
11
  </div>
package/tests/util.tsx ADDED
@@ -0,0 +1,7 @@
1
+ import React from 'react';
2
+ import { render } from '@testing-library/react';
3
+ import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
4
+
5
+ export default function renderWithClient(client: QueryClient, ui: React.ReactElement) {
6
+ return render(<QueryClientProvider client={client}>{ui}</QueryClientProvider>);
7
+ }
@@ -1,96 +0,0 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import { waitFor, cleanup } from '@testing-library/react';
4
- import { avRegionsApi } from '@availity/api-axios';
5
- import { QueryClient } from '@tanstack/react-query';
6
- import { useCurrentRegion } from '../src/index';
7
- import renderWithClient from './util';
8
-
9
- vi.mock('@availity/api-axios');
10
-
11
- let queryStates = [];
12
- beforeEach(() => {
13
- queryStates = [];
14
- });
15
-
16
- const queryClient = new QueryClient();
17
-
18
- afterEach(() => {
19
- vi.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
- // 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 = useCurrentRegion({ gcTime: 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
-
48
- Component.propTypes = {
49
- log: PropTypes.func,
50
- };
51
-
52
- describe('useCurrentRegion', () => {
53
- test('handle error', async () => {
54
- avRegionsApi.getCurrentRegion.mockRejectedValueOnce('An error occurred');
55
- const { getByText } = renderWithClient(queryClient, <Component log={pushState} />);
56
-
57
- getByText('Status: pending');
58
- await waitFor(() => {
59
- const el = getByText('Status: error');
60
- expect(el).toBeDefined();
61
- });
62
- await waitFor(() => {
63
- const el = getByText('Error: An error occurred');
64
- expect(el).toBeDefined();
65
- });
66
- });
67
-
68
- test('handle success', async () => {
69
- avRegionsApi.getCurrentRegion.mockResolvedValueOnce({
70
- config: { polling: false },
71
- data: {
72
- regions: [
73
- {
74
- id: 'FL',
75
- value: 'Florida',
76
- },
77
- ],
78
- },
79
- status: 200,
80
- statusText: 'Ok',
81
- });
82
-
83
- const { getByText } = renderWithClient(queryClient, <Component log={pushState} />);
84
-
85
- getByText('Status: pending');
86
- await waitFor(() => {
87
- const el = getByText(
88
- `Data: ${JSON.stringify({
89
- code: 'FL',
90
- value: 'Florida',
91
- })}`
92
- );
93
- expect(el).toBeDefined();
94
- });
95
- });
96
- });
@@ -1,94 +0,0 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import { waitFor, cleanup } from '@testing-library/react';
4
- import { avUserApi } from '@availity/api-axios';
5
- import { QueryClient } from '@tanstack/react-query';
6
- import { useCurrentUser } from '../src/index';
7
- import renderWithClient from './util';
8
-
9
- vi.mock('@availity/api-axios');
10
-
11
- let queryStates = [];
12
- beforeEach(() => {
13
- queryStates = [];
14
- });
15
-
16
- const queryClient = new QueryClient();
17
-
18
- afterEach(() => {
19
- vi.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
- // 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 = useCurrentUser({ gcTime: 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
-
48
- Component.propTypes = {
49
- log: PropTypes.func,
50
- };
51
-
52
- describe('useCurrentUser', () => {
53
- test('should set error on rejected promise', async () => {
54
- avUserApi.me.mockRejectedValueOnce('An error occurred');
55
-
56
- const { getByText } = renderWithClient(queryClient, <Component log={pushState} />);
57
-
58
- getByText('Status: pending');
59
- await waitFor(() => {
60
- const el = getByText('Status: error');
61
- expect(el).toBeDefined();
62
- });
63
- await waitFor(() => {
64
- const el = getByText('Error: An error occurred');
65
- expect(el).toBeDefined();
66
- });
67
- });
68
-
69
- test('should return user', async () => {
70
- avUserApi.me.mockResolvedValueOnce({
71
- id: 'aka12345',
72
- userId: 'testExample',
73
- akaname: 'aka12345',
74
- lastName: 'Last',
75
- firstName: 'First',
76
- });
77
-
78
- const { getByText } = renderWithClient(queryClient, <Component log={pushState} />);
79
-
80
- getByText('Status: pending');
81
- await waitFor(() => {
82
- const el = getByText(
83
- `Data: ${JSON.stringify({
84
- id: 'aka12345',
85
- userId: 'testExample',
86
- akaname: 'aka12345',
87
- lastName: 'Last',
88
- firstName: 'First',
89
- })}`
90
- );
91
- expect(el).toBeDefined();
92
- });
93
- });
94
- });
@@ -1,112 +0,0 @@
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 '@tanstack/react-query';
6
- import renderWithClient from './util';
7
- import { useOrganizations } from '../src/index';
8
-
9
- vi.mock('@availity/api-axios');
10
-
11
- let queryStates = [];
12
- beforeEach(() => {
13
- queryStates = [];
14
- });
15
-
16
- const queryClient = new QueryClient();
17
-
18
- afterEach(() => {
19
- vi.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
- // 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({}, { gcTime: 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
-
48
- Component.propTypes = {
49
- log: PropTypes.func,
50
- };
51
-
52
- describe('useOrganizations', () => {
53
- test('should return an error', async () => {
54
- avOrganizationsApi.getOrganizations.mockRejectedValueOnce('An error occurred');
55
-
56
- const { getByText } = renderWithClient(queryClient, <Component log={pushState} />);
57
-
58
- getByText('Status: pending');
59
- await waitFor(() => {
60
- const el = getByText('Status: error');
61
- expect(el).toBeDefined();
62
- });
63
- await waitFor(() => {
64
- const el = getByText('Error: An error occurred');
65
- expect(el).toBeDefined();
66
- });
67
- });
68
-
69
- test('should return organizations', async () => {
70
- avOrganizationsApi.getOrganizations.mockResolvedValueOnce({
71
- data: {
72
- organizations: [
73
- {
74
- links: {
75
- permissions: { href: 'test' },
76
- patients: { href: 'test' },
77
- self: { href: 'test' },
78
- admin: { href: 'test' },
79
- businessArrangements: { href: 'test' },
80
- users: { href: 'test' },
81
- },
82
- },
83
- ],
84
- },
85
- });
86
-
87
- const { getByText } = renderWithClient(queryClient, <Component log={pushState} />);
88
-
89
- getByText('Status: pending');
90
- await waitFor(() => {
91
- const el = getByText(
92
- `Data: ${JSON.stringify({
93
- data: {
94
- organizations: [
95
- {
96
- links: {
97
- permissions: { href: 'test' },
98
- patients: { href: 'test' },
99
- self: { href: 'test' },
100
- admin: { href: 'test' },
101
- businessArrangements: { href: 'test' },
102
- users: { href: 'test' },
103
- },
104
- },
105
- ],
106
- },
107
- })}`
108
- );
109
- expect(el).toBeDefined();
110
- });
111
- });
112
- });
@@ -1,90 +0,0 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import { waitFor, cleanup } from '@testing-library/react';
4
- import { avPermissionsApi } from '@availity/api-axios';
5
- import { QueryClient } from '@tanstack/react-query';
6
- import renderWithClient from './util';
7
- import { usePermissions } from '../src/index';
8
-
9
- vi.mock('@availity/api-axios');
10
-
11
- let queryStates = [];
12
- beforeEach(() => {
13
- queryStates = [];
14
- });
15
-
16
- const queryClient = new QueryClient();
17
-
18
- afterEach(() => {
19
- vi.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
- // 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 = usePermissions({}, { gcTime: 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
-
48
- Component.propTypes = {
49
- log: PropTypes.func,
50
- };
51
-
52
- describe('usePermissions', () => {
53
- test('should return an error', async () => {
54
- avPermissionsApi.getPermissions.mockRejectedValueOnce('An error occurred');
55
-
56
- const { getByText } = renderWithClient(queryClient, <Component log={pushState} />);
57
-
58
- getByText('Status: pending');
59
- await waitFor(() => {
60
- const el = getByText('Status: error');
61
- expect(el).toBeDefined();
62
- });
63
- await waitFor(() => {
64
- const el = getByText('Error: An error occurred');
65
- expect(el).toBeDefined();
66
- });
67
- });
68
-
69
- test('should return permissions', async () => {
70
- avPermissionsApi.getPermissions.mockResolvedValueOnce({
71
- id: '44',
72
- description: 'test',
73
- links: { self: { href: 'test.com' } },
74
- });
75
-
76
- const { getByText } = renderWithClient(queryClient, <Component log={pushState} />);
77
-
78
- getByText('Status: pending');
79
- await waitFor(() => {
80
- const el = getByText(
81
- `Data: ${JSON.stringify({
82
- id: '44',
83
- description: 'test',
84
- links: { self: { href: 'test.com' } },
85
- })}`
86
- );
87
- expect(el).toBeDefined();
88
- });
89
- });
90
- });