Skip to content
This repository has been archived by the owner on Mar 13, 2024. It is now read-only.

Commit

Permalink
[MM-20489] Migrate failed_post_options and its tests to typescript (#…
Browse files Browse the repository at this point in the history
…6717)

* Migrate failed_post_options and its tests to typescript

* Move mocked post props to test_helper

* Use getPostMock in rhs_thread tests

* Use getChannelMock in rhs_thread tests

Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
  • Loading branch information
2 people authored and calebroseland committed Oct 27, 2020
1 parent 60538ee commit 04ceb42
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import React from 'react';
import {shallow} from 'enzyme';

import FailedPostOptions from 'components/post_view/failed_post_options/failed_post_options.jsx';
import {TestHelper} from 'utils/test_helper';

import FailedPostOptions from 'components/post_view/failed_post_options/failed_post_options';

describe('components/post_view/FailedPostOptions', () => {
const baseProps = {
post: {
id: 'post_id',
},
post: TestHelper.getPostMock(),
actions: {
createPost: jest.fn(),
removePost: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,43 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import PropTypes from 'prop-types';
import React from 'react';
import React, {MouseEvent} from 'react';
import {FormattedMessage} from 'react-intl';

export default class FailedPostOptions extends React.PureComponent {
static propTypes = {
post: PropTypes.object.isRequired,
actions: PropTypes.shape({
createPost: PropTypes.func.isRequired,
removePost: PropTypes.func.isRequired,
}).isRequired,
}
import {Post} from 'mattermost-redux/types/posts';
import {FileInfo} from 'mattermost-redux/types/files';
import {DispatchFunc, GetStateFunc} from 'mattermost-redux/types/actions';
import {ExtendedPost} from 'mattermost-redux/actions/posts';

type CreatePostAction =
(post: Post, files?: FileInfo[]) => (dispatch: DispatchFunc) => Promise<{data: boolean}>;
type RemovePostAction =
(post: ExtendedPost) => (dispatch: DispatchFunc, getState: GetStateFunc) => void;

type Props = {
post: Post;
actions: {
createPost: CreatePostAction;
removePost: RemovePostAction;
};
};

retryPost = (e) => {
export default class FailedPostOptions extends React.PureComponent<Props> {
retryPost = (e: MouseEvent) : void => {
e.preventDefault();

const post = {...this.props.post};
Reflect.deleteProperty(post, 'id');
this.props.actions.createPost(post);
}

cancelPost = (e) => {
cancelPost = (e: MouseEvent) : void => {
e.preventDefault();

this.props.actions.removePost(this.props.post);
}

render() {
render(): JSX.Element {
return (
<span className='pending-post-actions'>
<a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
// See LICENSE.txt for license information.

import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {bindActionCreators, Dispatch} from 'redux';

import {removePost} from 'mattermost-redux/actions/posts';
import {GenericAction} from 'mattermost-redux/types/actions';

import {createPost} from 'actions/post_actions.jsx';
import {createPost} from 'actions/post_actions';

import FailedPostOptions from './failed_post_options.jsx';
import FailedPostOptions from './failed_post_options';

function mapDispatchToProps(dispatch) {
function mapDispatchToProps(dispatch: Dispatch<GenericAction>) {
return {
actions: bindActionCreators({
createPost,
Expand Down
39 changes: 4 additions & 35 deletions components/rhs_thread/rhs_thread.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,55 +13,24 @@ import {TestHelper} from 'utils/test_helper';
import RhsThread from './rhs_thread';

describe('components/RhsThread', () => {
const post: Post = {
edit_at: 0,
original_id: '',
hashtags: '',
pending_post_id: '',
reply_count: 0,
metadata: {
embeds: [],
emojis: [],
files: [],
images: {},
reactions: [],
},
const post: Post = TestHelper.getPostMock({
channel_id: 'channel_id',
create_at: 1502715365009,
delete_at: 0,
id: 'id',
is_pinned: false,
message: 'post message',
parent_id: '',
props: {},
root_id: '',
type: 'system_add_remove',
update_at: 1502715372443,
user_id: 'user_id',
};
});

const channel: Channel = {
id: 'channel_id',
create_at: 0,
update_at: 0,
team_id: 'team_id',
delete_at: 0,
type: 'O',
const channel: Channel = TestHelper.getChannelMock({
display_name: '',
name: '',
header: '',
purpose: '',
last_post_at: 0,
total_msg_count: 0,
extra_update_at: 0,
creator_id: '',
scheme_id: '',
isCurrent: false,
teammate_id: '',
status: '',
fake: false,
group_constrained: false,
};
});

const actions = {
removePost: jest.fn(),
Expand Down
31 changes: 31 additions & 0 deletions utils/test_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Role} from 'mattermost-redux/types/roles';
import {UserProfile} from 'mattermost-redux/types/users';
import {Team, TeamMembership} from 'mattermost-redux/types/teams';
import {Group} from 'mattermost-redux/types/groups';
import {Post} from 'mattermost-redux/types/posts';

export class TestHelper {
public static getUserMock(override: Partial<UserProfile> = {}): UserProfile {
Expand Down Expand Up @@ -185,4 +186,34 @@ export class TestHelper {
};
return Object.assign({}, defaultGroup, override);
}

public static getPostMock(override: Partial<Post> = {}): Post {
const defaultPost: Post = {
edit_at: 0,
original_id: '',
hashtags: '',
pending_post_id: '',
reply_count: 0,
metadata: {
embeds: [],
emojis: [],
files: [],
images: {},
reactions: [],
},
channel_id: '',
create_at: 0,
delete_at: 0,
id: 'id',
is_pinned: false,
message: 'post message',
parent_id: '',
props: {},
root_id: '',
type: 'system_add_remove',
update_at: 0,
user_id: 'user_id',
};
return Object.assign({}, defaultPost, override);
}
}

0 comments on commit 04ceb42

Please sign in to comment.