Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MM-57726] Convert AddIncomingWebhook from Class Component to Function Component #26992

Merged
merged 4 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -26,16 +63,29 @@ describe('components/integrations/AddIncomingWebhook', () => {
});

test('should have called createIncomingHook', () => {
const hook = TestHelper.getIncomingWebhookMock({
channel_id: 'channel_id',
const hook = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we remove the call to the TestHelper?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call was returning an object with some additional properties like create_at or delete_at that I assumed they are not relevant in this test. Do you think this call should stay?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I am not mistaken, it should stay, yes. The idea of this call is to mock the default values so it looks like the object you would naturally receive out in the wild. If those extra values are creating any issue in terms of types or anything, we can look a bit deeper why that is, but it is better if we use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I get it :) I've added these additional properties. Now the test is comparing whole objects, and it seems to work 👍.

channel_id: 'current_channel_id',
display_name: 'display_name',
description: 'description',
username: 'username',
icon_url: 'icon_url',
});
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.channel_id).toEqual(hook.channel_id);
expect(calledWith.display_name).toEqual(hook.display_name);
expect(calledWith.description).toEqual(hook.description);
expect(calledWith.username).toEqual(hook.username);
expect(calledWith.icon_url).toEqual(hook.icon_url);
});
});
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, 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);

this.state = {
serverError: '',
};
}
const AddIncomingWebhook = ({
team,
enablePostUsernameOverride,
enablePostIconOverride,
actions,
}: Props) => {
const [serverError, setServerError] = useState('');

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a useCallback so we don't pass a new function on every render.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

setServerError('');

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