', () => {
const Foo = () => Foo
;
it('should not render its child while loading', async () => {
const authProvider = {
checkAuth: new Promise(() => {}),
} as any;
render(
Loading}>
);
await screen.findByText('Loading');
});
it('should not render its child when checkAuth raises an error', async () => {
const NeverDisplayedComponent = jest.fn(() => (
It should not be called
));
const authProvider = {
checkAuth: jest.fn().mockRejectedValue(undefined),
logout: jest.fn().mockResolvedValue(undefined),
} as any;
render(
);
// Ensure that the NeverDisplayedComponent is not called
await waitFor(() => {
// Ensure that checkAuth and logout were called as expected
expect(authProvider.checkAuth).toHaveBeenCalled();
expect(authProvider.logout).toHaveBeenCalled();
expect(NeverDisplayedComponent).toHaveBeenCalledTimes(0);
});
});
it('should render its child when authenticated', async () => {
const authProvider = {
login: () => Promise.reject('bad method'),
logout: () => Promise.reject('bad method'),
checkAuth: jest.fn().mockResolvedValueOnce(''),
checkError: () => Promise.reject('bad method'),
getPermissions: () => Promise.reject('bad method'),
};
const store = memoryStore();
const reset = jest.spyOn(store, 'reset');
render(
);
await screen.findByText('Foo');
expect(reset).toHaveBeenCalledTimes(0);
});
it('should logout, redirect to login and show a notification if the auth fails', async () => {
const authProvider = {
login: jest.fn().mockResolvedValue(''),
logout: jest.fn().mockResolvedValue(''),
checkAuth: jest.fn().mockRejectedValue(undefined),
checkError: jest.fn().mockResolvedValue(''),
getPermissions: jest.fn().mockResolvedValue(''),
};
const store = memoryStore();
const reset = jest.spyOn(store, 'reset');
const Login = () => {
const location = useLocation();
return (
{(location.state as any).nextPathname}
);
};
let notificationsSpy;
const Notification = () => {
const { notifications } = useNotificationContext();
React.useEffect(() => {
notificationsSpy = notifications;
}, [notifications]);
return null;
};
render(
}
/>