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-59652 Add unit tests to actions/websocket/channel #8417

Merged
merged 2 commits into from
Dec 18, 2024

Conversation

jwilander
Copy link
Member

Summary

Add unit tests to actions/websocket/channel increasing coverage to 94.33%.

Ticket Link

https://mattermost.atlassian.net/browse/MM-59652

Checklist

  • Added or updated unit tests (required for all new features)
  • Has UI changes
  • Includes text changes and localization file updates
  • Have tested against the 5 core themes to ensure consistency between them.
  • Have run E2E tests by adding label E2E iOS tests for PR.

Device Information

This PR was tested on: n/a

Screenshots

n/a

Release Note

NONE

@jwilander jwilander added the 2: Dev Review Requires review by a core commiter label Dec 16, 2024
@@ -328,7 +328,7 @@ export async function handleUserAddedToChannelEvent(serverUrl: string, msg: any)

loadCallForChannel(serverUrl, channelId);
} else {
const addedUser = getUserById(database, userId);
Copy link
Member Author

Choose a reason for hiding this comment

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

@larkox I noticed this was checking against the existence of the returned promise instead of the user. Could this potentially be the cause of some issues we've seen with user added to channel events?

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice catch!!

Copy link
Contributor

Choose a reason for hiding this comment

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

Great catch indeed. About what it might solve, I don't think it is any "terrible" issue. It is just the logic to get a missing user that has been added to the channel. And that is probably "autofixed" by the at mention in the "added to channel" post.

@jwilander jwilander requested review from larkox and enahum December 16, 2024 21:45
Copy link
Contributor

@larkox larkox left a comment

Choose a reason for hiding this comment

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

The tests are technically correct, but I am not a big fan of them. I added a few comments, but I haven't reviewed all the tests (I trust they will be technically correct in the same way).

Let me know if you want to keep the tests like this and I will finish my review taking that into consideration, or if you want to improve the tests.

app/actions/websocket/channel.test.ts Outdated Show resolved Hide resolved
app/actions/websocket/channel.test.ts Show resolved Hide resolved
app/actions/websocket/channel.test.ts Outdated Show resolved Hide resolved
Comment on lines 78 to 81
(getChannelById as jest.Mock).mockResolvedValue(null);
(fetchMyChannel as jest.Mock).mockResolvedValue({channels: [{}], memberships: [{}]});
(prepareMyChannelsForTeam as jest.Mock).mockResolvedValue([[]]);
(addChannelToDefaultCategory as jest.Mock).mockResolvedValue({models: []});
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not a great fan of such heavy mocking for two main reasons:

  • The data is not real
  • Changes beyond this file may not make the test break (integration testing vs pure unit testing)

It is a more philosophical discussion, so I am 0/5 on whether this require any change.

Copy link
Member Author

Choose a reason for hiding this comment

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

The data is not real

The data isn't real in any test. Even if it's more of an integration test somewhere there needs to be mock/fake data. We could make the data more realistic for these tests but it's not required by the implementation of the function so I don't see the point.

Changes beyond this file may not make the test break (integration testing vs pure unit testing)

These should be unit tests imo and only focused on testing the functionality in the related functions for the most part. The unit tests of other files should cover their functionality. Then we have E2E tests to cover integration. I feel that having every test be an integration test would be overkill and make the tests too fragile but I'm open to having my mind changed.

Thoughts @enahum ?

Copy link
Member Author

Choose a reason for hiding this comment

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

app/actions/websocket/channel.test.ts Show resolved Hide resolved
app/actions/websocket/channel.test.ts Outdated Show resolved Hide resolved
app/actions/websocket/channel.test.ts Show resolved Hide resolved

await handleChannelCreatedEvent(serverUrl, msg);

expect(getChannelById).not.toHaveBeenCalled();
Copy link
Contributor

Choose a reason for hiding this comment

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

We are checking here for a implementation detail. We should be checking the effect of the function instead (whether the channel gets added to the database, for example).

app/actions/websocket/channel.test.ts Outdated Show resolved Hide resolved
@jwilander
Copy link
Member Author

@larkox I seemed to have kicked a hornet's nest in regards to pure vs integration style unit tests. While that's getting figured out I'd rather not hold up this PR as it helps us move towards our coverage goals. I made some updates based on your feedback - can you take another look? Unless there's major blockers I'd like to get this merged and then we can revisit after the discussion completes

@jwilander jwilander requested a review from larkox December 17, 2024 21:42
@jwilander jwilander added 4: Reviews Complete All reviewers have approved the pull request and removed 2: Dev Review Requires review by a core commiter labels Dec 18, 2024
@jwilander jwilander merged commit 663cda2 into main Dec 18, 2024
6 checks passed
@jwilander jwilander deleted the mm-59652_actions-websocket-channel-tests branch December 18, 2024 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4: Reviews Complete All reviewers have approved the pull request release-note-none
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants