-
Notifications
You must be signed in to change notification settings - Fork 251
/
use-auth.test.tsx
51 lines (47 loc) · 1.68 KB
/
use-auth.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React from 'react';
import useAuth0 from '../src/use-auth0';
import { act, renderHook } from '@testing-library/react-hooks';
import { createWrapper } from './helpers';
import { Auth0ContextInterface, initialContext } from '../src/auth0-context';
describe('useAuth0', () => {
it('should provide the auth context', async () => {
const wrapper = createWrapper();
const {
result: { current },
waitForNextUpdate,
} = renderHook(() => useAuth0(), { wrapper });
await waitForNextUpdate();
expect(current).toBeDefined();
});
it('should throw with no provider', () => {
const {
result: { current },
} = renderHook(() => useAuth0());
expect(current.loginWithRedirect).toThrowError(
'You forgot to wrap your component in <Auth0Provider>.'
);
});
it('should throw when context is not associated with provider', async () => {
const context = React.createContext<Auth0ContextInterface>(initialContext);
const wrapper = createWrapper({ context });
const {
result: { current },
} = renderHook(() => useAuth0(), { wrapper });
await act(async () => {
expect(current.loginWithRedirect).toThrowError(
'You forgot to wrap your component in <Auth0Provider>.'
);
});
});
it('should accept custom auth context', async () => {
const context = React.createContext<Auth0ContextInterface>(initialContext);
const wrapper = createWrapper({ context });
const {
result: { current },
waitForNextUpdate,
} = renderHook(() => useAuth0(context), { wrapper });
await waitForNextUpdate();
expect(current).toBeDefined();
expect(current.loginWithRedirect).not.toThrowError();
});
});