Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,20 @@ describe('UserPopOverCard Component', () => {
expect(screen.getByTestId('testUser')).toHaveTextContent('Test User');
});

it('should not fetch the user when a display name is passed', () => {
render(
<UserPopOverCard
showUserName
displayName="Test User"
userName="testUser"
/>
);

expect(mockUseUserProfile).toHaveBeenCalledWith(
expect.objectContaining({ permission: false })
);
});

it('should not fetch the user when it only renders an avatar', () => {
render(<UserPopOverCard userName="testUser" />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@

import { fireEvent, render, screen, waitFor } from '@testing-library/react';
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { User } from '../../../../../../generated/entity/teams/user';
import { EntityReference } from '../../../../../../generated/entity/type';
import { useApplicationStore } from '../../../../../../hooks/useApplicationStore';
import { searchQuery } from '../../../../../../rest/searchAPI';
import { getUserByName } from '../../../../../../rest/userAPI';
import { showErrorToast } from '../../../../../../utils/ToastUtils';
import PersonaUsersTab from './PersonaUsersTab';

Expand Down Expand Up @@ -65,6 +69,10 @@ jest.mock(
})
);

jest.mock('../../../../../../rest/userAPI', () => ({
getUserByName: jest.fn(),
}));

type Column = { id: string; label: string };

jest.mock('@openmetadata/ui-core-components', () => {
Expand Down Expand Up @@ -175,6 +183,7 @@ const USER_DETAILS = {
id: 'u1',
name: 'alice',
displayName: 'Alice',
email: 'alice@example.com',
teams: [{ id: 't1', name: 'Data', type: 'team' }],
roles: [
{ id: 'r1', name: 'DataSteward', type: 'role' },
Expand Down Expand Up @@ -208,12 +217,14 @@ const renderTab = (
users={USERS}
onUsersChange={mockOnUsersChange}
{...props}
/>
/>,
{ wrapper: MemoryRouter }
);

describe('PersonaUsersTab', () => {
beforeEach(() => {
jest.clearAllMocks();
useApplicationStore.setState({ userProfilePics: {} });
mockSearchUsers(Object.keys(USER_DETAILS));
});

Expand All @@ -224,14 +235,42 @@ describe('PersonaUsersTab', () => {

const names = await screen.findAllByTestId('persona-user-name');

expect(names.map((n) => n.textContent)).toEqual(['Alice', 'bob']);
expect(names).toHaveLength(2);
expect(names[0]).toHaveTextContent('Alice');
expect(names[1]).toHaveTextContent('bob');
expect(searchQuery).toHaveBeenCalledTimes(1);
expect(searchQuery).toHaveBeenCalledWith(
expect.objectContaining({ pageSize: 2, searchIndex: 'user' })
);
expect(screen.getByText('Data')).toBeInTheDocument();
expect(screen.getByText('DataSteward, DataConsumer')).toBeInTheDocument();
expect(screen.getAllByText('--')).toHaveLength(2);
expect(screen.getByTestId('alice')).toHaveTextContent('Alice');
expect(screen.getByTestId('alice')).toHaveAttribute(
'href',
expect.stringContaining('alice')
);
expect(screen.getByText('alice@example.com')).toBeInTheDocument();
expect(screen.getByTestId('bob')).toHaveTextContent('bob');
expect(screen.getAllByText('bob')).toHaveLength(2);
});

it('keeps a cached profile photo when the search document has none', async () => {
const cachedAlice = {
id: 'u1',
name: 'alice',
email: 'alice@example.com',
profile: { images: { image: 'https://example.com/alice.png' } },
} as User;
useApplicationStore.setState({ userProfilePics: { alice: cachedAlice } });
(getUserByName as jest.Mock).mockRejectedValue(new Error('offline'));

renderTab();

expect(await screen.findByTestId('alice')).toHaveTextContent('Alice');
expect(useApplicationStore.getState().userProfilePics.alice).toBe(
cachedAlice
);
});

it('skips users missing from the search index', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ import { searchQuery } from '../../../../../../rest/searchAPI';
import { getEntityName } from '../../../../../../utils/EntityNameUtils';
import { getTermQuery } from '../../../../../../utils/SearchPureUtils';
import { showErrorToast } from '../../../../../../utils/ToastUtils';
import { getUserIdentifier } from '../../../../../../utils/UsersPureUtils';
import NoDataPlaceholder from '../../../../../common/EmptyPlaceholder/NoDataPlaceholder';
import UserPopOverCard from '../../../../../common/PopOverCard/UserPopOverCard';
import { UserTeamSelectableList } from '../../../../../common/UserTeamSelectableList/UserTeamSelectableList.component';

type UserColumnId = 'name' | 'teams' | 'roles' | 'actions';
type UserColumnId = 'name' | 'email' | 'teams' | 'roles' | 'actions';
type UserColumn = { id: UserColumnId; label: string; className?: string };

interface PersonaUsersTabProps {
Expand Down Expand Up @@ -84,6 +86,7 @@ const PersonaUsersTab = ({
const columns = useMemo<UserColumn[]>(
() => [
{ id: 'name', label: t('label.name'), className: 'tw:w-72' },
{ id: 'email', label: t('label.email') },
{ id: 'teams', label: t('label.team-plural') },
{ id: 'roles', label: t('label.role-plural') },
{ id: 'actions', label: t('label.action-plural'), className: 'tw:w-20' },
Expand Down Expand Up @@ -113,9 +116,9 @@ const PersonaUsersTab = ({
: undefined;

if (active) {
setUserDetails(
response?.hits.hits.map((hit) => hit._source as User) ?? []
);
const fetched =
response?.hits.hits.map((hit) => hit._source as User) ?? [];
setUserDetails(fetched);
}
} catch (error) {
if (active) {
Expand All @@ -140,13 +143,20 @@ const PersonaUsersTab = ({
switch (colId) {
case 'name':
return (
<Typography
className="tw:truncate tw:block"
data-testid="persona-user-name"
size="text-sm"
tooltip={getEntityName(user)}
weight="medium">
{getEntityName(user)}
<span className="tw:block tw:min-w-0" data-testid="persona-user-name">
<UserPopOverCard
showUserName
className="tw:min-w-0 tw:overflow-hidden tw:text-sm tw:font-medium"
displayName={getEntityName(user)}
profileWidth={16}
userName={user.name}
Comment thread
shah-harshit marked this conversation as resolved.
Resolved
/>
</span>
);
case 'email':
return (
<Typography className="tw:truncate tw:block" size="text-sm">
{getUserIdentifier(user)}
</Typography>
);
case 'teams':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ export const useUserLabel = ({
displayName?: ReactNode;
enabled: boolean;
}): ReactNode => {
// A passed displayName is the label, so there is nothing to look up.
const [, , storedUser] = useUserProfile({
permission: enabled,
permission: enabled && !displayName,
name: userName,
});

Expand Down
Loading