Skip to content

Commit

Permalink
fix: remove usage of current user from appstate (#13765)
Browse files Browse the repository at this point in the history
  • Loading branch information
karanh37 authored Oct 30, 2023
1 parent d75e2e8 commit 4fbb538
Show file tree
Hide file tree
Showing 71 changed files with 258 additions and 446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import { Popover, Space } from 'antd';
import classNames from 'classnames';
import { compare, Operation } from 'fast-json-patch';
import { isUndefined } from 'lodash';
import { observer } from 'mobx-react';
import React, { FC, useEffect, useRef, useState } from 'react';
import AppState from '../../../AppState';
import { ReactionOperation } from '../../../enums/reactions.enum';
import { AnnouncementDetails } from '../../../generated/api/feed/createThread';
import { Post } from '../../../generated/entity/feed/thread';
Expand All @@ -27,6 +25,7 @@ import {
getEntityFQN,
getEntityType,
} from '../../../utils/FeedUtils';
import { useAuthContext } from '../../authentication/auth-provider/AuthProvider';
import UserPopOverCard from '../../common/PopOverCard/UserPopOverCard';
import ProfilePicture from '../../common/ProfilePicture/ProfilePicture';
import EditAnnouncementModal from '../../Modals/AnnouncementModal/EditAnnouncementModal';
Expand Down Expand Up @@ -60,8 +59,7 @@ const ActivityFeedCard: FC<ActivityFeedCardProp> = ({
const entityType = getEntityType(entityLink ?? '');
const entityFQN = getEntityFQN(entityLink ?? '');
const entityField = getEntityField(entityLink ?? '');

const currentUser = AppState.getCurrentUserDetails();
const { currentUser } = useAuthContext();
const containerRef = useRef<HTMLDivElement>(null);
const [feedDetail, setFeedDetail] = useState<Post>(feed);

Expand Down Expand Up @@ -255,4 +253,4 @@ const ActivityFeedCard: FC<ActivityFeedCardProp> = ({
);
};

export default observer(ActivityFeedCard);
export default ActivityFeedCard;
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import { User } from '../../../generated/entity/teams/user';
import PopoverContent from './PopoverContent';

const onConfirmation = jest.fn();
Expand All @@ -35,26 +36,18 @@ const mockProps = {
reactions: [],
threadId: '3d6bb831-fbe4-484e-ba54-1bd7568ddc59',
};
const mockUserData: User = {
name: 'aaron_johnson0',
email: 'testUser1@email.com',
id: '011bdb24-90a7-4a97-ba66-24002adb2b12',
isAdmin: true,
};

jest.mock('../../../AppState', () => {
const mockUser = {
id: '011bdb24-90a7-4a97-ba66-24002adb2b12',
type: 'user',
name: 'aaron_johnson0',
fullyQualifiedName: 'aaron_johnson0',
displayName: 'Aaron Johnson',
deleted: false,
isAdmin: true,
href: 'http://localhost:8585/api/v1/users/011bdb24-90a7-4a97-ba66-24002adb2b12',
teams: [{ id: '8754b53f-15cd-4d9a-af52-bdb3a2abffss' }],
};

return {
getCurrentUserDetails: jest.fn().mockReturnValue(mockUser),
userDetails: undefined,
nonSecureUserDetails: mockUser,
};
});
jest.mock('../../authentication/auth-provider/AuthProvider', () => ({
useAuthContext: jest.fn(() => ({
currentUser: mockUserData,
})),
}));

jest.mock('../../../constants/reactions.constant', () => ({
REACTION_LIST: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import Icon from '@ant-design/icons';
import { Popover, Space } from 'antd';
import { isNil, isUndefined, uniqueId } from 'lodash';
import React, { FC, useMemo, useState } from 'react';
import AppState from '../../../AppState';
import { ReactComponent as IconFeedDelete } from '../../../assets/svg/ic-delete.svg';
import { ReactComponent as IconEdit } from '../../../assets/svg/ic-edit.svg';
import { ReactComponent as IconReaction } from '../../../assets/svg/ic-reaction.svg';
Expand All @@ -24,6 +23,7 @@ import { REACTION_LIST } from '../../../constants/reactions.constant';
import { ReactionOperation } from '../../../enums/reactions.enum';
import { Post } from '../../../generated/entity/feed/thread';
import { ReactionType } from '../../../generated/type/reaction';
import { useAuthContext } from '../../authentication/auth-provider/AuthProvider';
import Reaction from '../../Reactions/Reaction';
import { ConfirmState } from './ActivityFeedCard.interface';

Expand Down Expand Up @@ -59,12 +59,7 @@ const PopoverContent: FC<Props> = ({
isAnnouncement,
editAnnouncementPermission,
}) => {
// get current user details
const currentUser = useMemo(
() => AppState.getCurrentUserDetails(),
[AppState.userDetails, AppState.nonSecureUserDetails]
);

const { currentUser } = useAuthContext();
const [visible, setVisible] = useState<boolean>(false);

const hide = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
} from 'react';
import { useTranslation } from 'react-i18next';
import { useHistory, useParams } from 'react-router-dom';
import AppState from '../../../AppState';
import { ReactComponent as AllActivityIcon } from '../../../assets/svg/all-activity-v2.svg';
import { ReactComponent as CheckIcon } from '../../../assets/svg/ic-check.svg';
import { ReactComponent as MentionIcon } from '../../../assets/svg/ic-mentions.svg';
Expand All @@ -48,6 +47,7 @@ import {
ENTITY_LINK_SEPARATOR,
getEntityFeedLink,
} from '../../../utils/EntityUtils';
import { useAuthContext } from '../../authentication/auth-provider/AuthProvider';
import Loader from '../../Loader/Loader';
import { TaskTab } from '../../Task/TaskTab/TaskTab.component';
import '../../Widgets/FeedsWidget/FeedsWidget.less';
Expand All @@ -72,10 +72,7 @@ export const ActivityFeedTab = ({
}: ActivityFeedTabProps) => {
const history = useHistory();
const { t } = useTranslation();
const currentUser = useMemo(
() => AppState.getCurrentUserDetails(),
[AppState.userDetails, AppState.nonSecureUserDetails]
);
const { currentUser } = useAuthContext();
const [elementRef, isInView] = useElementInView({
...observerOptions,
root: document.querySelector('#center-container'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { Operation } from 'fast-json-patch';
import { isEqual, isUndefined } from 'lodash';
import React, { FC, Fragment, RefObject, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import AppState from '../../../AppState';
import { confirmStateInitialValue } from '../../../constants/Feeds.constants';
import { observerOptions } from '../../../constants/Mydata.constants';
import { ERROR_PLACEHOLDER_TYPE } from '../../../enums/common.enum';
Expand All @@ -32,6 +31,7 @@ import { Paging } from '../../../generated/type/paging';
import { useElementInView } from '../../../hooks/useElementInView';
import { getAllFeeds } from '../../../rest/feedsAPI';
import { showErrorToast } from '../../../utils/ToastUtils';
import { useAuthContext } from '../../authentication/auth-provider/AuthProvider';
import ErrorPlaceHolder from '../../common/error-with-placeholder/ErrorPlaceHolder';
import Loader from '../../Loader/Loader';
import ConfirmationModal from '../../Modals/ConfirmationModal/ConfirmationModal';
Expand All @@ -56,6 +56,7 @@ const ActivityThreadPanelBody: FC<ActivityThreadPanelBodyProp> = ({
threadType,
}) => {
const { t } = useTranslation();
const { currentUser } = useAuthContext();
const [threads, setThreads] = useState<Thread[]>([]);
const [selectedThread, setSelectedThread] = useState<Thread>();
const [selectedThreadId, setSelectedThreadId] = useState<string>('');
Expand Down Expand Up @@ -163,10 +164,9 @@ const ActivityThreadPanelBody: FC<ActivityThreadPanelBodyProp> = ({
};

const onPostThread = (value: string) => {
const currentUser = AppState.userDetails?.name ?? AppState.users[0]?.name;
const data = {
message: value,
from: currentUser,
from: currentUser?.name ?? '',
about: threadLink,
};
createThread(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { Popover, Space } from 'antd';
import { uniqueId } from 'lodash';
import React, { useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import AppState from '../../../AppState';
import { ReactComponent as DeleteIcon } from '../../../assets/svg/ic-delete.svg';
import { ReactComponent as IconEdit } from '../../../assets/svg/ic-edit.svg';
import { ReactComponent as IconReaction } from '../../../assets/svg/ic-reaction.svg';
Expand All @@ -30,6 +29,7 @@ import {
Thread,
ThreadType,
} from '../../../generated/entity/feed/thread';
import { useAuthContext } from '../../authentication/auth-provider/AuthProvider';
import { useActivityFeedProvider } from '../ActivityFeedProvider/ActivityFeedProvider';
import './activity-feed-actions.less';

Expand All @@ -47,10 +47,7 @@ const ActivityFeedActions = ({
onEditPost,
}: ActivityFeedActionsProps) => {
const { t } = useTranslation();
const currentUser = useMemo(
() => AppState.getCurrentUserDetails(),
[AppState.userDetails, AppState.nonSecureUserDetails]
);
const { currentUser } = useAuthContext();
const isAuthor = post.from === currentUser?.name;
const [visible, setVisible] = useState<boolean>(false);
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ import { CreateTestCase } from '../../generated/api/tests/createTestCase';
import { TestCase } from '../../generated/tests/testCase';
import { TestSuite } from '../../generated/tests/testSuite';
import { createExecutableTestSuite, createTestCase } from '../../rest/testAPI';
import { getCurrentUserId } from '../../utils/CommonUtils';
import { getEntityBreadcrumbs, getEntityName } from '../../utils/EntityUtils';
import { getEncodedFqn } from '../../utils/StringsUtils';
import { showErrorToast } from '../../utils/ToastUtils';
import { useAuthContext } from '../authentication/auth-provider/AuthProvider';
import SuccessScreen from '../common/success-screen/SuccessScreen';
import TitleBreadcrumb from '../common/title-breadcrumb/title-breadcrumb.component';
import { TitleBreadcrumbProps } from '../common/title-breadcrumb/title-breadcrumb.interface';
Expand All @@ -70,6 +70,7 @@ const AddDataQualityTestV1: React.FC<AddDataQualityTestProps> = ({
const [testSuiteData, setTestSuiteData] = useState<TestSuite>();
const [testCaseRes, setTestCaseRes] = useState<TestCase>();
const [addIngestion, setAddIngestion] = useState(false);
const { currentUser } = useAuthContext();

const breadcrumb = useMemo(() => {
const data: TitleBreadcrumbProps['titleLinks'] = [
Expand All @@ -92,10 +93,10 @@ const AddDataQualityTestV1: React.FC<AddDataQualityTestProps> = ({

const owner = useMemo(
() => ({
id: getCurrentUserId(),
id: currentUser?.id ?? '',
type: OwnerType.USER,
}),
[getCurrentUserId]
[currentUser]
);

const handleRedirection = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ import {
import { IngestionPipeline } from '../../generated/entity/services/ingestionPipelines/ingestionPipeline';

import { IngestionWorkflowData } from '../../interface/service.interface';
import {
getCurrentUserId,
getIngestionFrequency,
} from '../../utils/CommonUtils';
import { getIngestionFrequency } from '../../utils/CommonUtils';
import { getIngestionName } from '../../utils/ServiceUtils';
import { useAuthContext } from '../authentication/auth-provider/AuthProvider';
import SuccessScreen from '../common/success-screen/SuccessScreen';
import IngestionStepper from '../IngestionStepper/IngestionStepper.component';
import DeployIngestionLoaderModal from '../Modals/DeployIngestionLoaderModal/DeployIngestionLoaderModal';
Expand Down Expand Up @@ -65,6 +63,7 @@ const AddIngestion = ({
onFocus,
}: AddIngestionProps) => {
const { t } = useTranslation();
const { currentUser } = useAuthContext();

// lazy initialization to initialize the data only once
const [workflowData, setWorkflowData] = useState<IngestionWorkflowData>(
Expand Down Expand Up @@ -151,7 +150,7 @@ const AddIngestion = ({
name: ingestionName,
displayName: ingestionName,
owner: {
id: getCurrentUserId(),
id: currentUser?.id ?? '',
type: 'user',
},
pipelineType: pipelineType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { ServiceCategory } from '../../enums/service.enum';
import { PipelineType } from '../../generated/entity/services/ingestionPipelines/ingestionPipeline';
import { useAirflowStatus } from '../../hooks/useAirflowStatus';
import { ConfigData } from '../../interface/service.interface';
import { getCurrentUserId, getServiceLogo } from '../../utils/CommonUtils';
import { getServiceLogo } from '../../utils/CommonUtils';
import { getAddServicePath, getSettingPath } from '../../utils/RouterUtils';
import {
getServiceCreatedLabel,
Expand All @@ -41,6 +41,7 @@ import {
import { getEncodedFqn } from '../../utils/StringsUtils';
import { showErrorToast } from '../../utils/ToastUtils';
import AddIngestion from '../AddIngestion/AddIngestion.component';
import { useAuthContext } from '../authentication/auth-provider/AuthProvider';
import ServiceDocPanel from '../common/ServiceDocPanel/ServiceDocPanel';
import SuccessScreen from '../common/success-screen/SuccessScreen';
import TitleBreadcrumb from '../common/title-breadcrumb/title-breadcrumb.component';
Expand All @@ -66,6 +67,7 @@ const AddService = ({
handleAddIngestion,
}: AddServiceProps) => {
const history = useHistory();
const { currentUser } = useAuthContext();
const { fetchAirflowStatus } = useAirflowStatus();

const [showErrorMessage, setShowErrorMessage] = useState(
Expand Down Expand Up @@ -131,7 +133,7 @@ const AddService = ({
serviceType: selectServiceType,
description: serviceConfig.description,
owner: {
id: getCurrentUserId(),
id: currentUser?.id ?? '',
type: 'user',
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import {
Space,
Typography,
} from 'antd';
import React, { useMemo } from 'react';
import React from 'react';
import { useTranslation } from 'react-i18next';
import AppState from '../../../AppState';
import { Transi18next } from '../../../utils/CommonUtils';
import { getRelativeTime } from '../../../utils/date-time/DateTimeUtils';
import { getEntityName } from '../../../utils/EntityUtils';
import { useAuthContext } from '../../authentication/auth-provider/AuthProvider';
import BrandImage from '../../common/BrandImage/BrandImage';
import UserPopOverCard from '../../common/PopOverCard/UserPopOverCard';
import ProfilePicture from '../../common/ProfilePicture/ProfilePicture';
Expand All @@ -44,10 +44,7 @@ const AppInstallVerifyCard = ({
onSave,
}: AppInstallVerifyCardProps) => {
const { t } = useTranslation();
const currentUser = useMemo(
() => AppState.getCurrentUserDetails(),
[AppState.userDetails, AppState.nonSecureUserDetails]
);
const { currentUser } = useAuthContext();

return (
<div className="flex-center flex-col">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2023 Collate.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { fireEvent, render, screen } from '@testing-library/react';
import React from 'react';
import ApplicationCard from './ApplicationCard.component';

const props = {
title: 'Search Index',
description: 'Hello World',
linkTitle: 'Show More',
onClick: jest.fn(),
appName: 'Search Index',
showDescription: true,
};

describe('ApplicationCard', () => {
it('renders the title correctly', () => {
render(<ApplicationCard {...props} />);

expect(screen.getByText('Search Index')).toBeInTheDocument();
expect(screen.getByText('Hello World')).toBeInTheDocument();
});

it('does not render the description when showDescription is false', () => {
render(<ApplicationCard {...props} showDescription={false} />);

expect(screen.queryByText('Hello World')).toBeNull();
});

it('calls onClick when the link button is clicked', () => {
const onClick = jest.fn();
render(<ApplicationCard {...props} onClick={onClick} />);
fireEvent.click(screen.getByTestId('config-btn'));

expect(onClick).toHaveBeenCalledTimes(1);
});
});
Loading

0 comments on commit 4fbb538

Please sign in to comment.