Skip to content

Commit

Permalink
fix: avoid fetching favorite status for anonymous user (#15590)
Browse files Browse the repository at this point in the history
* avoid fetching favorite status for anonymous user

* add test + fix types

* fix lint errors
  • Loading branch information
aspedrosa authored Jul 9, 2021
1 parent 2ebba51 commit 7ec6bdf
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ const createProps = () => ({
dash_edit_perm: false,
dash_save_perm: false,
dash_share_perm: false,
userId: 1,
userId: '1',
metadata: {},
common: {
conf: {},
},
},
userId: 1,
dashboardTitle: 'Dashboard Title',
charts: {},
layout: {},
Expand Down Expand Up @@ -248,6 +249,22 @@ test('should render the selected fave icon', () => {
).toBeInTheDocument();
});

test('should NOT render the fave icon on anonymous user', () => {
const mockedProps = createProps();
const anonymousUserProps = {
...mockedProps,
userId: undefined,
};
render(setup(anonymousUserProps));
expect(mockedProps.fetchFaveStar).not.toHaveBeenCalled();
expect(() =>
screen.getByRole('img', { name: 'favorite-unselected' }),
).toThrowError('Unable to find');
expect(() =>
screen.getByRole('img', { name: 'favorite-selected' }),
).toThrowError('Unable to find');
});

test('should fave', async () => {
const mockedProps = createProps();
render(setup(mockedProps));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const createProps = () => ({
id: 1,
dash_edit_perm: true,
dash_save_perm: true,
userId: 1,
userId: '1',
metadata: {},
common: {
conf: {},
Expand Down
4 changes: 3 additions & 1 deletion superset-frontend/src/dashboard/components/Header/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const propTypes = {
addSuccessToast: PropTypes.func.isRequired,
addDangerToast: PropTypes.func.isRequired,
addWarningToast: PropTypes.func.isRequired,
userId: PropTypes.number,
dashboardInfo: PropTypes.object.isRequired,
dashboardTitle: PropTypes.string.isRequired,
dataMask: PropTypes.object.isRequired,
Expand Down Expand Up @@ -366,6 +367,7 @@ class Header extends React.PureComponent {
updateCss,
editMode,
isPublished,
userId,
dashboardInfo,
hasUnsavedChanges,
isLoading,
Expand Down Expand Up @@ -404,7 +406,7 @@ class Header extends React.PureComponent {
canEdit={userCanEdit}
canSave={userCanSaveAs}
/>
{dashboardInfo.userId && (
{userId && (
<FaveStar
itemId={dashboardInfo.id}
fetchFaveStar={this.props.fetchFaveStar}
Expand Down
3 changes: 2 additions & 1 deletion superset-frontend/src/dashboard/components/Header/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { ChartState } from 'src/explore/types';

interface DashboardInfo {
id: number;
userId: number;
userId: string | undefined;
dash_edit_perm: boolean;
dash_save_perm: boolean;
metadata?: Record<string, any>;
Expand Down Expand Up @@ -65,6 +65,7 @@ export interface HeaderProps {
charts: ChartState | {};
colorScheme?: string;
customCss: string;
userId: number | undefined;
dashboardInfo: DashboardInfo;
dashboardTitle: string;
setColorSchemeAndUnsavedChanges: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function mapStateToProps({
dashboardInfo,
charts,
dataMask,
user,
}) {
return {
dashboardInfo,
Expand All @@ -80,7 +81,7 @@ function mapStateToProps({
colorScheme: dashboardState.colorScheme,
charts,
dataMask,
userId: dashboardInfo.userId,
userId: user.userId,
isStarred: !!dashboardState.isStarred,
isPublished: !!dashboardState.isPublished,
isLoading: isDashboardLoading(charts),
Expand Down

0 comments on commit 7ec6bdf

Please sign in to comment.