Skip to content

Commit

Permalink
[MM-57726] Convert AddIncomingWebhook from Class Component to Functio…
Browse files Browse the repository at this point in the history
…n Component (#26992)

* Convert AddIncomingWebhook from Class Component to Function Component

* Review fix

* Review fix

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
  • Loading branch information
MeHow25 and mattermost-build committed May 24, 2024
1 parent 6f3327c commit 3cac94a
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,47 @@
import {shallow} from 'enzyme';
import React from 'react';

import type {ChannelType} from '@mattermost/types/channels';
import type {DeepPartial} from '@mattermost/types/utilities';

import AddIncomingWebhook from 'components/integrations/add_incoming_webhook/add_incoming_webhook';

import {renderWithContext, userEvent} from 'tests/react_testing_utils';
import {TestHelper} from 'utils/test_helper';

import type {GlobalState} from 'types/store';

const initialState = {
entities: {
channels: {
currentChannelId: 'current_channel_id',
channels: {
current_channel_id: TestHelper.getChannelMock({
id: 'current_channel_id',
team_id: 'current_team_id',
type: 'O' as ChannelType,
name: 'current_channel_id',
}),
},
myMembers: {
current_channel_id: TestHelper.getChannelMembershipMock({channel_id: 'current_channel_id'}),
},
channelsInTeam: {
current_team_id: new Set(['current_channel_id']),
},
},
teams: {
currentTeamId: 'current_team_id',
teams: {
current_team_id: TestHelper.getTeamMock({id: 'current_team_id'}),
},
myMembers: {
current_team_id: TestHelper.getTeamMembershipMock({roles: 'team_roles'}),
},
},
},
} as DeepPartial<GlobalState>;

describe('components/integrations/AddIncomingWebhook', () => {
const createIncomingHook = jest.fn().mockResolvedValue({data: true});
const props = {
Expand All @@ -27,15 +64,28 @@ describe('components/integrations/AddIncomingWebhook', () => {

test('should have called createIncomingHook', () => {
const hook = TestHelper.getIncomingWebhookMock({
channel_id: 'channel_id',
channel_id: 'current_channel_id',
display_name: 'display_name',
description: 'description',
username: 'username',
icon_url: 'icon_url',
create_at: 0,
delete_at: 0,
update_at: 0,
id: '',
});
const wrapper = shallow<AddIncomingWebhook>(<AddIncomingWebhook {...props}/>);
wrapper.instance().addIncomingHook(hook);
const wrapper = renderWithContext(<AddIncomingWebhook {...props}/>, initialState as GlobalState);

userEvent.selectOptions(wrapper.getByRole('combobox'), [hook.channel_id]);
userEvent.type(wrapper.getByLabelText('Title'), hook.display_name);
userEvent.type(wrapper.getByLabelText('Description'), hook.description);
userEvent.type(wrapper.getByLabelText('Username'), hook.username);
userEvent.type(wrapper.getByLabelText('Profile Picture'), hook.icon_url);

userEvent.click(wrapper.getByText('Save'));

expect(createIncomingHook).toHaveBeenCalledTimes(1);
expect(createIncomingHook).toBeCalledWith(hook);
const calledWith = createIncomingHook.mock.calls[0][0];
expect(calledWith).toEqual(hook);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import React from 'react';
import React, {memo, useCallback, useState} from 'react';

import type {IncomingWebhook} from '@mattermost/types/integrations';
import type {Team} from '@mattermost/types/teams';
Expand Down Expand Up @@ -43,45 +43,38 @@ type Props = {
};
};

type State = {
serverError: string;
};

export default class AddIncomingWebhook extends React.PureComponent<Props, State> {
constructor(props: Props) {
super(props);
const AddIncomingWebhook = ({
team,
enablePostUsernameOverride,
enablePostIconOverride,
actions,
}: Props) => {
const [serverError, setServerError] = useState('');

this.state = {
serverError: '',
};
}
const addIncomingHook = useCallback(async (hook: IncomingWebhook) => {
setServerError('');

addIncomingHook = async (hook: IncomingWebhook) => {
this.setState({serverError: ''});

const {data, error} = await this.props.actions.createIncomingHook(hook);
const {data, error} = await actions.createIncomingHook(hook);
if (data) {
getHistory().push(`/${this.props.team.name}/integrations/confirm?type=incoming_webhooks&id=${data.id}`);
getHistory().push(`/${team.name}/integrations/confirm?type=incoming_webhooks&id=${data.id}`);
return;
}

if (error) {
this.setState({serverError: error.message});
setServerError(error.message);
}
};

render() {
return (
<AbstractIncomingWebhook
team={this.props.team}
header={HEADER}
footer={FOOTER}
loading={LOADING}
enablePostUsernameOverride={this.props.enablePostUsernameOverride}
enablePostIconOverride={this.props.enablePostIconOverride}
action={this.addIncomingHook}
serverError={this.state.serverError}
/>
);
}
}
}, [actions, team.name]);

return (
<AbstractIncomingWebhook
team={team}
header={HEADER}
footer={FOOTER}
loading={LOADING}
enablePostUsernameOverride={enablePostUsernameOverride}
enablePostIconOverride={enablePostIconOverride}
action={addIncomingHook}
serverError={serverError}
/>
);
};
export default memo(AddIncomingWebhook);

0 comments on commit 3cac94a

Please sign in to comment.