You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
portainer/app/react/test-utils/withUserProvider.tsx

44 lines
1.1 KiB

import { ComponentType, useMemo } from 'react';
import { UserContext } from '@/react/hooks/useUser';
import { User } from '@/portainer/users/types';
const mockUser: User = {
EndpointAuthorizations: [],
Id: 1,
Role: 1,
Username: 'mock',
ThemeSettings: {
color: 'auto',
},
};
/**
* A helper function to wrap a component with a UserContext.Provider.
*
* should only be used in tests
*/
export function withUserProvider<T>(
WrappedComponent: ComponentType<T>,
user = mockUser
): ComponentType<T> {
// Try to create a nice displayName for React Dev Tools.
const displayName =
WrappedComponent.displayName || WrappedComponent.name || 'Component';
function WrapperComponent(props: T & JSX.IntrinsicAttributes) {
const state = useMemo(() => ({ user }), []);
return (
<UserContext.Provider value={state}>
{/* eslint-disable-next-line react/jsx-props-no-spreading */}
<WrappedComponent {...props} />
</UserContext.Provider>
);
}
WrapperComponent.displayName = `withUserProvider(${displayName})`;
return WrapperComponent;
}