UNPKG

1.58 kBJavaScriptView Raw
1import React from 'react';
2import { render, waitForElement, cleanup } from '@testing-library/react';
3import { avUserApi } from '@availity/api-axios';
4import { useCurrentUser } from '..';
5
6jest.mock('@availity/api-axios');
7
8afterEach(() => {
9 jest.clearAllMocks();
10 cleanup();
11});
12
13const Component = () => {
14 const [user, loading, error] = useCurrentUser();
15
16 return loading ? (
17 <span data-testid="loading" />
18 ) : (
19 JSON.stringify(user || error)
20 );
21};
22
23describe('useCurrentUser', () => {
24 test('should return loading', () => {
25 avUserApi.me.mockResolvedValueOnce({
26 id: 'aka12345',
27 userId: 'testExample',
28 akaname: 'aka12345',
29 lastName: 'Last',
30 firstName: 'First',
31 });
32
33 const { getByTestId } = render(<Component />);
34
35 getByTestId('loading');
36 });
37
38 test('should return user', async () => {
39 avUserApi.me.mockResolvedValueOnce({
40 id: 'aka12345',
41 userId: 'testExample',
42 akaname: 'aka12345',
43 lastName: 'Last',
44 firstName: 'First',
45 });
46
47 const { getByText } = render(<Component />);
48
49 await waitForElement(() =>
50 getByText(
51 JSON.stringify({
52 id: 'aka12345',
53 userId: 'testExample',
54 akaname: 'aka12345',
55 lastName: 'Last',
56 firstName: 'First',
57 })
58 )
59 );
60 });
61
62 test('should set error on rejected promise', async () => {
63 avUserApi.me.mockRejectedValueOnce('An error occured');
64
65 const { getByText } = render(<Component />);
66
67 await waitForElement(() => getByText('"An error occured"'));
68 });
69});
70
\No newline at end of file