|
1 | 1 | import * as React from 'react'; |
2 | | -import { render, screen } from '@testing-library/react'; |
| 2 | +import { render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import { Location } from 'react-router'; |
3 | 4 | import { |
4 | 5 | Basic, |
5 | 6 | CustomLoading, |
@@ -89,6 +90,63 @@ describe('<CanAccess>', () => { |
89 | 90 | resolveCheckAuth(false); |
90 | 91 | await screen.findByText('Not allowed'); |
91 | 92 | }); |
| 93 | + it('redirects to the /authentication-error route by default in case of error', async () => { |
| 94 | + let rejectCheckAuth; |
| 95 | + let location: Location; |
| 96 | + const authProvider: AuthProvider = { |
| 97 | + login: () => Promise.reject('bad method'), |
| 98 | + logout: () => Promise.reject('bad method'), |
| 99 | + checkAuth: () => Promise.reject('bad method'), |
| 100 | + checkError: () => Promise.reject('bad method'), |
| 101 | + getPermissions: () => Promise.reject('bad method'), |
| 102 | + canAccess: () => |
| 103 | + new Promise((_, reject) => { |
| 104 | + rejectCheckAuth = reject; |
| 105 | + }), |
| 106 | + }; |
| 107 | + const { container } = render( |
| 108 | + <Basic |
| 109 | + authProvider={authProvider} |
| 110 | + locationCallback={l => { |
| 111 | + location = l; |
| 112 | + }} |
| 113 | + /> |
| 114 | + ); |
| 115 | + expect(container.textContent).toEqual(''); |
| 116 | + rejectCheckAuth(new Error('failed')); |
| 117 | + await waitFor(() => |
| 118 | + expect(location.pathname).toEqual('/authentication-error') |
| 119 | + ); |
| 120 | + }); |
| 121 | + it('redirects to the /authentication-error route by default in case of error in an Admin with a basename', async () => { |
| 122 | + let rejectCheckAuth; |
| 123 | + let location: Location; |
| 124 | + const authProvider: AuthProvider = { |
| 125 | + login: () => Promise.reject('bad method'), |
| 126 | + logout: () => Promise.reject('bad method'), |
| 127 | + checkAuth: () => Promise.reject('bad method'), |
| 128 | + checkError: () => Promise.reject('bad method'), |
| 129 | + getPermissions: () => Promise.reject('bad method'), |
| 130 | + canAccess: () => |
| 131 | + new Promise((_, reject) => { |
| 132 | + rejectCheckAuth = reject; |
| 133 | + }), |
| 134 | + }; |
| 135 | + const { container } = render( |
| 136 | + <Basic |
| 137 | + authProvider={authProvider} |
| 138 | + basename="/admin" |
| 139 | + locationCallback={l => { |
| 140 | + location = l; |
| 141 | + }} |
| 142 | + /> |
| 143 | + ); |
| 144 | + expect(container.textContent).toEqual(''); |
| 145 | + rejectCheckAuth(new Error('failed')); |
| 146 | + await waitFor(() => |
| 147 | + expect(location.pathname).toEqual('/admin/authentication-error') |
| 148 | + ); |
| 149 | + }); |
92 | 150 | it('shows the protected content when users are authorized', async () => { |
93 | 151 | let resolveCheckAuth; |
94 | 152 | const authProvider: AuthProvider = { |
|
0 commit comments