1 | import React from 'react';
|
2 | import { render, waitForElement, cleanup } from '@testing-library/react';
|
3 | import { avRegionsApi } from '@availity/api-axios';
|
4 | import { useCurrentRegion } from '..';
|
5 |
|
6 | jest.mock('@availity/api-axios');
|
7 |
|
8 | const mockRegionApi = type => {
|
9 | let body = {};
|
10 | if (type === 'valid') {
|
11 | body = {
|
12 | config: { polling: false },
|
13 | data: {
|
14 | regions: [
|
15 | {
|
16 | id: 'FL',
|
17 | value: 'Florida',
|
18 | },
|
19 | ],
|
20 | },
|
21 | status: 200,
|
22 | statusText: 'Ok',
|
23 | };
|
24 | } else if (type === 'invalid') {
|
25 | body = {
|
26 | config: { polling: false },
|
27 | status: 200,
|
28 | statusText: 'Ok',
|
29 | };
|
30 | }
|
31 | avRegionsApi.getCurrentRegion.mockResolvedValue(body);
|
32 | };
|
33 |
|
34 | afterEach(() => {
|
35 | jest.clearAllMocks();
|
36 | cleanup();
|
37 | });
|
38 |
|
39 | const Component = () => {
|
40 | const [region, loading, error] = useCurrentRegion();
|
41 |
|
42 | if (error) return <span data-testid="error">An error occurred.</span>;
|
43 |
|
44 | return loading ? <span data-testid="loading" /> : JSON.stringify(region);
|
45 | };
|
46 |
|
47 | describe('useCurrentRegion', () => {
|
48 | test('should return loading', () => {
|
49 | mockRegionApi('valid');
|
50 | const { getByTestId } = render(<Component />);
|
51 |
|
52 | getByTestId('loading');
|
53 | });
|
54 |
|
55 | test('should return region', async () => {
|
56 | mockRegionApi('valid');
|
57 | const { getByText } = render(<Component />);
|
58 |
|
59 | await waitForElement(() =>
|
60 | getByText(
|
61 | JSON.stringify({
|
62 | code: 'FL',
|
63 | value: 'Florida',
|
64 | })
|
65 | )
|
66 | );
|
67 | });
|
68 |
|
69 | test('handles error', async () => {
|
70 | mockRegionApi('invalid');
|
71 | const { getByTestId } = render(<Component />);
|
72 |
|
73 | await waitForElement(() => getByTestId('error'));
|
74 | });
|
75 | });
|
76 |
|
\ | No newline at end of file |