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

[MM-20534] migrate 'components/create team/components/team url' module and associated tests to type script #5983

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
MM-20534 fix type errors
  • Loading branch information
jakubnovak998 committed Jul 31, 2020
commit eea21c7778a2170046747c0845b32729737ebed5
4 changes: 2 additions & 2 deletions components/create_team/components/team_url/team_url.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ describe('/components/create_team/components/display_name', () => {
<TeamUrl {...props}/>,
);

await (wrapper.instance() as unknown as TeamUrl).submitNext({preventDefault: jest.fn()} as unknown as React.MouseEvent<HTMLElement>);
await (wrapper.instance() as unknown as TeamUrl).submitNext({preventDefault: jest.fn()} as unknown as React.MouseEvent<HTMLElement, MouseEvent>);
expect(actions.checkIfTeamExists).toHaveBeenCalledTimes(1);
expect(actions.createTeam).not.toHaveBeenCalled();

await (wrapper.instance() as unknown as TeamUrl).submitNext({preventDefault: jest.fn()} as unknown as React.MouseEvent<HTMLElement>);
await (wrapper.instance() as unknown as TeamUrl).submitNext({preventDefault: jest.fn()} as unknown as React.MouseEvent<HTMLElement, MouseEvent>);
expect(actions.checkIfTeamExists).toHaveBeenCalledTimes(2);
expect(actions.createTeam).toHaveBeenCalledTimes(1);
expect(actions.createTeam).toBeCalledWith({display_name: 'test-team', name: 'test-team', type: 'O'});
Expand Down
22 changes: 15 additions & 7 deletions components/create_team/components/team_url/team_url.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ import logoImage from 'images/logo.png';
import FormattedMarkdownMessage from 'components/formatted_markdown_message.jsx';
import OverlayTrigger from 'components/overlay_trigger';

type State = {
isLoading: boolean;
nameError: string | JSX.Element;
}

type Props = {

/*
* Object containing team's display_name and name
*/
state: {team: object; wizard: string};
state: {team: any; wizard: string};

/*
* Function that updates parent component with state props
Expand All @@ -46,9 +51,12 @@ type Props = {
*/
createTeam: (team: Team) => ActionFunc;
};
history: {
push(path: string): void;
};
}

export default class TeamUrl extends React.PureComponent<Props> {
export default class TeamUrl extends React.PureComponent<Props, State> {
public constructor(props: Props) {
Copy link
Member

Choose a reason for hiding this comment

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

Is there a specific reason this is public? Usually we're not creating new instances of this component in JS/TS.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, there isn't any specific reason. I'll remove it.

super(props);

Expand All @@ -74,7 +82,7 @@ export default class TeamUrl extends React.PureComponent<Props> {
e.preventDefault();
trackEvent('signup', 'click_finish');

const name = ReactDOM.findDOMNode<>(this.refs.name).value.trim();
const name = (ReactDOM.findDOMNode(this.refs.name) as HTMLInputElement)?.value.trim();
const cleanedName = URL.cleanUpUrlable(name);
const urlRegex = /^[a-z]+([a-z\-0-9]+|(__)?)[a-z0-9]+$/g;
const {actions: {checkIfTeamExists, createTeam}} = this.props;
Expand Down Expand Up @@ -130,7 +138,7 @@ export default class TeamUrl extends React.PureComponent<Props> {
teamSignup.team.type = 'O';
teamSignup.team.name = name;

const {exists} = await checkIfTeamExists(name);
const {exists}: any = await checkIfTeamExists(name);
Copy link
Member

Choose a reason for hiding this comment

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

This should be {exists: boolean} instead of any.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It causes the problem that exists is not visible in if statement on 143 line and it does not exist on type ActionFunc.

Copy link
Member

Choose a reason for hiding this comment

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

I think you might want to change the return type of checkIfTeamExists in your type declaration. It could just look something like: Promise<{exists: boolean}>.


if (exists) {
this.setState({nameError: (
Expand All @@ -143,7 +151,7 @@ export default class TeamUrl extends React.PureComponent<Props> {
return;
}

const {data, error} = await createTeam(teamSignup.team);
const {data, error}: any = await createTeam(teamSignup.team);
Copy link
Member

Choose a reason for hiding this comment

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

This should be {data: Team, error: Client4Error} instead of any

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It causes the same problems as exists change

Copy link
Member

Choose a reason for hiding this comment

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

You can do the same thing here as above.


if (data) {
this.props.history.push('/' + data.name + '/channels/' + Constants.DEFAULT_CHANNEL);
Expand Down Expand Up @@ -221,7 +229,7 @@ export default class TeamUrl extends React.PureComponent<Props> {
ref='name'
className='form-control'
placeholder=''
maxLength='128'
maxLength={128}
defaultValue={this.props.state.team.name}
autoFocus={true}
onFocus={this.handleFocus}
Expand Down Expand Up @@ -264,7 +272,7 @@ export default class TeamUrl extends React.PureComponent<Props> {
type='submit'
bsStyle='primary'
disabled={this.state.isLoading}
onClick={this.submitNext}
onClick={this.submitNext as any}
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't need to be declared as any.

>
{finishMessage}
</Button>
Expand Down