[MM-55326] Convert ./components/post_view/commented_on_files_message/commented_on_files_message.tsx from Class Component to Function Component (#25407)

* covert to func component

* update snap

* minor fix
This commit is contained in:
Sudhanva-Nadiger 2023-11-14 02:03:40 +05:30 committed by GitHub
parent 5a14575dee
commit 9156770b25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 53 additions and 66 deletions

View File

@ -93,7 +93,7 @@ exports[`components/post_view/CommentedOn should match snapshot 3`] = `
className="theme" className="theme"
onClick={[MockFunction]} onClick={[MockFunction]}
> >
<Connect(CommentedOnFilesMessage) <Connect(Component)
parentPostId="post_id" parentPostId="post_id"
/> />
</a> </a>

View File

@ -4,44 +4,33 @@
import {shallow} from 'enzyme'; import {shallow} from 'enzyme';
import React from 'react'; import React from 'react';
import type {Post} from '@mattermost/types/posts';
import CommentedOn from 'components/post_view/commented_on/commented_on'; import CommentedOn from 'components/post_view/commented_on/commented_on';
import CommentedOnFilesMessage from 'components/post_view/commented_on_files_message'; import CommentedOnFilesMessage from 'components/post_view/commented_on_files_message';
import {TestHelper} from 'utils/test_helper';
describe('components/post_view/CommentedOn', () => { describe('components/post_view/CommentedOn', () => {
const baseProps = { const baseProps = {
displayName: 'user_displayName', displayName: 'user_displayName',
enablePostUsernameOverride: false, enablePostUsernameOverride: false,
onCommentClick: jest.fn(), onCommentClick: jest.fn(),
post: { post: TestHelper.getPostMock({
id: 'post_id', id: 'post_id',
message: 'text message', message: 'text message',
props: { props: {
from_webhook: 'true', from_webhook: 'true',
override_username: 'override_username', override_username: 'override_username',
}, },
create_at: 0,
update_at: 10, update_at: 10,
edit_at: 20, edit_at: 20,
delete_at: 30, delete_at: 30,
is_pinned: false,
user_id: 'user_id',
channel_id: 'channel_id', channel_id: 'channel_id',
root_id: 'root_id', root_id: 'root_id',
original_id: 'original_id', original_id: 'original_id',
type: 'system_add_remove',
hashtags: 'hashtags', hashtags: 'hashtags',
pending_post_id: 'pending_post_id', pending_post_id: 'pending_post_id',
reply_count: 1, reply_count: 1,
metadata: { }),
embeds: [],
emojis: [],
files: [],
images: {},
reactions: [],
},
} as Post,
}; };
test('should match snapshot', () => { test('should match snapshot', () => {

View File

@ -4,17 +4,14 @@
import React from 'react'; import React from 'react';
import {render, renderWithContext, screen} from 'tests/react_testing_utils'; import {render, renderWithContext, screen} from 'tests/react_testing_utils';
import {TestHelper} from 'utils/test_helper';
import CommentedOnFilesMessage from './commented_on_files_message'; import CommentedOnFilesMessage from './commented_on_files_message';
describe('components/CommentedOnFilesMessage', () => { describe('components/CommentedOnFilesMessage', () => {
const baseProps = {
parentPostId: 'parentPostId',
};
test('component state when no files', () => { test('component state when no files', () => {
render( render(
<CommentedOnFilesMessage {...baseProps}/>, <CommentedOnFilesMessage/>,
); );
//no file is given in props //no file is given in props
@ -22,31 +19,25 @@ describe('components/CommentedOnFilesMessage', () => {
}); });
test('should match component state for single file', () => { test('should match component state for single file', () => {
const props = { const fileInfos = [
...baseProps, TestHelper.getFileInfoMock({id: 'file_id_1', name: 'image_1.png', extension: 'png', create_at: 1}),
fileInfos: [{id: 'file_id_1', name: 'image_1.png', extension: 'png', create_at: 1}], ];
};
render( render(
<CommentedOnFilesMessage {...props}/>, <CommentedOnFilesMessage fileInfos={fileInfos}/>,
); );
expect(screen.getByTestId('fileInfo')).toHaveTextContent('image_1.png'); expect(screen.getByTestId('fileInfo')).toHaveTextContent('image_1.png');
}); });
test('should match component state for multiple files', () => { test('should match component state for multiple files', () => {
const fileInfos = [ const fileInfos = [
{id: 'file_id_3', name: 'image_3.png', extension: 'png', create_at: 3}, TestHelper.getFileInfoMock({id: 'file_id_3', name: 'image_3.png', extension: 'png', create_at: 3}),
{id: 'file_id_2', name: 'image_2.png', extension: 'png', create_at: 2}, TestHelper.getFileInfoMock({id: 'file_id_2', name: 'image_2.png', extension: 'png', create_at: 2}),
{id: 'file_id_1', name: 'image_1.png', extension: 'png', create_at: 1}, TestHelper.getFileInfoMock({id: 'file_id_1', name: 'image_1.png', extension: 'png', create_at: 1}),
]; ];
const props = {
...baseProps,
fileInfos,
};
renderWithContext( renderWithContext(
<CommentedOnFilesMessage {...props}/>, <CommentedOnFilesMessage fileInfos={fileInfos}/>,
); );
// total files = 3 // total files = 3

View File

@ -4,43 +4,42 @@
import React from 'react'; import React from 'react';
import {FormattedMessage} from 'react-intl'; import {FormattedMessage} from 'react-intl';
interface Props { import type {FileInfo} from '@mattermost/types/files';
/* interface Props {
* The id of the post that was commented on
*/
parentPostId: string;
/* /*
* An array of file metadata for the parent post * An array of file metadata for the parent post
*/ */
fileInfos?: Array<{name: string}>; fileInfos?: FileInfo[];
} }
export default class CommentedOnFilesMessage extends React.PureComponent<Props> { const CommentedOnFilesMessage = ({
render() { fileInfos,
if (!this.props.fileInfos || this.props.fileInfos.length === 0) { }: Props) => {
return null; if (!fileInfos?.length) {
} return null;
}
let plusMore = null; let plusMore = null;
if (this.props.fileInfos.length > 1) { if (fileInfos.length > 1) {
plusMore = ( plusMore = (
<FormattedMessage <FormattedMessage
id='post_body.plusMore' id='post_body.plusMore'
defaultMessage=' plus {count, number} other {count, plural, one {file} other {files}}' defaultMessage=' plus {count, number} other {count, plural, one {file} other {files}}'
values={{ values={{
count: this.props.fileInfos.length - 1, count: fileInfos.length - 1,
}} }}
/> />
);
}
return (
<span data-testid='fileInfo'>
{this.props.fileInfos[0].name}
{plusMore}
</span>
); );
} }
}
return (
<span data-testid='fileInfo'>
{fileInfos[0].name}
{plusMore}
</span>
);
};
export default React.memo(CommentedOnFilesMessage);

View File

@ -9,10 +9,18 @@ import {makeGetFilesForPost} from 'mattermost-redux/selectors/entities/files';
import CommentedOnFilesMessage from './commented_on_files_message'; import CommentedOnFilesMessage from './commented_on_files_message';
type OwnProps = {
/*
* The id of the post that was commented on
*/
parentPostId: string;
}
function makeMapStateToProps() { function makeMapStateToProps() {
const selectFileInfosForPost = makeGetFilesForPost(); const selectFileInfosForPost = makeGetFilesForPost();
return function mapStateToProps(state: GlobalState, ownProps: {parentPostId: string}) { return function mapStateToProps(state: GlobalState, ownProps: OwnProps) {
let fileInfos; let fileInfos;
if (ownProps.parentPostId) { if (ownProps.parentPostId) {
fileInfos = selectFileInfosForPost(state, ownProps.parentPostId); fileInfos = selectFileInfosForPost(state, ownProps.parentPostId);