Skip to content

Commit

Permalink
fix(MM-60645): missing bookmark emoji (#8518)
Browse files Browse the repository at this point in the history
* fix(MM-60645): missing bookmark emoji
* add test
  • Loading branch information
rahimrahman authored Jan 27, 2025
1 parent 167d8a3 commit 1102232
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import React from 'react';

import {act, renderWithIntl} from '@test/intl-test-helper';

import BookmarkIcon from './bookmark_icon';

jest.mock('@context/theme', () => ({
useTheme: () => ({
centerChannelColor: '#000',
}),
}));
jest.mock('@components/emoji', () => 'Emoji');

describe('components/channel_bookmarks/channel_bookmark/BookmarkIcon', () => {
const baseProps = {
emojiSize: 24,
iconSize: 22,
genericStyle: {},
};

it('renders default bookmark icon when no specific content provided', () => {
const {getByTestId} = renderWithIntl(
<BookmarkIcon {...baseProps}/>,
);

expect(getByTestId('bookmark-generic-icon')).toBeOnTheScreen();
});

it('renders FileIcon when file prop is provided', () => {
const props = {
...baseProps,
file: {
id: 'file1',
name: 'test.pdf',
extension: 'pdf',
} as unknown as FileInfo,
};

const {getByTestId} = renderWithIntl(
<BookmarkIcon {...props}/>,
);

expect(getByTestId('bookmark-file-icon')).toBeOnTheScreen();
});

it('renders Image when imageUrl is provided', () => {
const props = {
...baseProps,
imageUrl: 'https://example.com/image.jpg',
imageStyle: {width: 40, height: 40},
};

const {getByTestId} = renderWithIntl(
<BookmarkIcon {...props}/>,
);

expect(getByTestId('bookmark-image')).toBeOnTheScreen();
});

it('renders Emoji when emoji prop is provided', () => {
const props = {
...baseProps,
emoji: 'smile',
emojiStyle: {fontSize: 20},
};

const {getByTestId} = renderWithIntl(
<BookmarkIcon {...props}/>,
);

const emojiComponent = getByTestId('bookmark-emoji');
expect(emojiComponent).toBeOnTheScreen();
expect(emojiComponent.props.emojiName).toBe('smile');
});

it('renders Emoji when emoji prop with `:` is provided', () => {
const props = {
...baseProps,
emoji: ':computer-rage:',
emojiStyle: {fontSize: 20},
};

const {getByTestId} = renderWithIntl(
<BookmarkIcon {...props}/>,
);

const emojiComponent = getByTestId('bookmark-emoji');
expect(emojiComponent).toBeOnTheScreen();
expect(emojiComponent.props.emojiName).toBe('computer-rage');
});

it('falls back to default icon when image fails to load', () => {
const props = {
...baseProps,
imageUrl: 'https://example.com/invalid.jpg',
};

const {getByTestId} = renderWithIntl(
<BookmarkIcon {...props}/>,
);

const image = getByTestId('bookmark-image');
const mockErrorEvent = {
nativeEvent: {
error: new Error('Image failed to load'),
},
};
act(() => {
image.props.onError(mockErrorEvent);
});

expect(getByTestId('bookmark-generic-icon')).toBeOnTheScreen();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const BookmarkIcon = ({emoji, emojiSize, emojiStyle, file, genericStyle, iconSiz
if (file && !emoji && !hasImageError) {
return (
<FileIcon
testID='bookmark-file-icon'
file={file}
iconSize={iconSize}
smallImage={true}
Expand All @@ -40,15 +41,18 @@ const BookmarkIcon = ({emoji, emojiSize, emojiStyle, file, genericStyle, iconSiz
} else if (imageUrl && !emoji && !hasImageError) {
return (
<Image
testID='bookmark-image'
source={{uri: imageUrl}}
style={imageStyle}
onError={handleImageError}
/>
);
} else if (emoji) {
const sanitizedEmoji = emoji.replace(/:/g, '');
return (
<Emoji
emojiName={emoji!}
testID='bookmark-emoji'
emojiName={sanitizedEmoji}
size={emojiSize}
textStyle={emojiStyle}
/>
Expand All @@ -61,6 +65,7 @@ const BookmarkIcon = ({emoji, emojiSize, emojiStyle, file, genericStyle, iconSiz
size={22}
color={theme.centerChannelColor}
style={genericStyle}
testID='bookmark-generic-icon'
/>
);
};
Expand Down
8 changes: 6 additions & 2 deletions app/components/files/file_icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type FileIconProps = {
iconColor?: string;
iconSize?: number;
smallImage?: boolean;
testID?: string;
}

const BLUE_ICON = '#338AFF';
Expand Down Expand Up @@ -49,7 +50,7 @@ const styles = StyleSheet.create({

const FileIcon = ({
backgroundColor, defaultImage = false, failed = false, file,
iconColor, iconSize = 48, smallImage = false,
iconColor, iconSize = 48, smallImage = false, testID = 'file-icon',
}: FileIconProps) => {
const theme = useTheme();
const getFileIconNameAndColor = () => {
Expand Down Expand Up @@ -78,7 +79,10 @@ const FileIcon = ({
const bgColor = backgroundColor || theme?.centerChannelBg || 'transparent';

return (
<View style={[styles.fileIconWrapper, {backgroundColor: bgColor}]}>
<View
style={[styles.fileIconWrapper, {backgroundColor: bgColor}]}
testID={testID}
>
<CompassIcon
name={iconName}
size={iconSize}
Expand Down

0 comments on commit 1102232

Please sign in to comment.