@availity/hooks 6.0.0 → 6.0.1

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.
@@ -1,106 +0,0 @@
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 '@tanstack/react-query';
6
- import renderWithClient from './util';
7
- import { useProviders } 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 = useProviders({}, { 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('useProviders', () => {
53
- test('should return an error', async () => {
54
- avProvidersApi.getProviders.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 providers', async () => {
70
- avProvidersApi.getProviders.mockResolvedValueOnce({
71
- data: {
72
- providers: [
73
- {
74
- id: 'test',
75
- lastName: 'test',
76
- firstName: 'test',
77
- middleName: 'test',
78
- uiDisplayName: 'test',
79
- },
80
- ],
81
- },
82
- });
83
-
84
- const { getByText } = renderWithClient(queryClient, <Component log={pushState} />);
85
-
86
- getByText('Status: pending');
87
- await waitFor(() => {
88
- const el = getByText(
89
- `Data: ${JSON.stringify({
90
- data: {
91
- providers: [
92
- {
93
- id: 'test',
94
- lastName: 'test',
95
- firstName: 'test',
96
- middleName: 'test',
97
- uiDisplayName: 'test',
98
- },
99
- ],
100
- },
101
- })}`
102
- );
103
- expect(el).toBeDefined();
104
- });
105
- });
106
- });
@@ -1,89 +0,0 @@
1
- import React from 'react';
2
- import PropTypes from 'prop-types';
3
- import { waitFor, cleanup } from '@testing-library/react';
4
- import { avStashApi } from '@availity/api-axios';
5
- import { QueryClient } from '@tanstack/react-query';
6
- import { vi, describe, test, expect, beforeEach, afterEach } from 'vitest';
7
- import { useStash } from '../src';
8
- import renderWithClient from './util';
9
-
10
- vi.mock('@availity/api-axios', () => ({
11
- avStashApi: { get: vi.fn() },
12
- }));
13
-
14
- let queryStates = [];
15
- beforeEach(() => {
16
- queryStates = [];
17
- });
18
-
19
- const queryClient = new QueryClient();
20
-
21
- afterEach(() => {
22
- vi.clearAllMocks();
23
- cleanup();
24
- queryClient.clear();
25
- queryStates = [];
26
- });
27
-
28
- const pushState = (state) => {
29
- queryStates.push(state);
30
- };
31
-
32
- const Component = ({ sessionId, log }) => {
33
- const state = useStash(sessionId, { gcTime: 0, retry: false });
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
- sessionId: PropTypes.string,
50
- log: PropTypes.func,
51
- };
52
-
53
- describe('useStash', () => {
54
- test('should return an error', async () => {
55
- avStashApi.get.mockRejectedValueOnce('An error occurred');
56
-
57
- const { getByText } = renderWithClient(queryClient, <Component sessionId="test-session-id" log={pushState} />);
58
-
59
- getByText('Status: pending');
60
- await waitFor(() => {
61
- const el = getByText('Status: error');
62
- expect(el).toBeDefined();
63
- });
64
- await waitFor(() => {
65
- const el = getByText('Error: An error occurred');
66
- expect(el).toBeDefined();
67
- });
68
- });
69
-
70
- test('should return stash data', async () => {
71
- const mockData = { key: 'value', nested: { foo: 'bar' } };
72
- avStashApi.get.mockResolvedValueOnce({ data: mockData });
73
-
74
- const { getByText } = renderWithClient(queryClient, <Component sessionId="test-session-id" log={pushState} />);
75
-
76
- getByText('Status: pending');
77
- await waitFor(() => {
78
- const el = getByText(`Data: ${JSON.stringify(mockData)}`);
79
- expect(el).toBeDefined();
80
- });
81
- });
82
-
83
- test('should not fetch when sessionId is empty', () => {
84
- const { getByText } = renderWithClient(queryClient, <Component sessionId="" log={pushState} />);
85
-
86
- getByText('Status: pending');
87
- expect(avStashApi.get).not.toHaveBeenCalled();
88
- });
89
- });
package/tests/util.jsx DELETED
@@ -1,8 +0,0 @@
1
- import React from 'react';
2
- import { render } from '@testing-library/react';
3
- import { QueryClientProvider } from '@tanstack/react-query';
4
-
5
- // Taken from https://github.com/tannerlinsley/react-query/blob/master/src/react/tests/utils.tsx#L6
6
- export default function renderWithClient(client, ui) {
7
- return render(<QueryClientProvider client={client}>{ui}</QueryClientProvider>);
8
- }