-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
test: MM-59420 unit test for app/actions/remote/integrations.ts #8305
Open
Prathamesh010
wants to merge
1
commit into
mattermost:main
Choose a base branch
from
Prathamesh010:test/MM-59413
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. | ||
// See LICENSE.txt for license information. | ||
|
||
import {getCurrentChannelId, getCurrentTeamId} from '@app/queries/servers/system'; | ||
import {logDebug} from '@app/utils/log'; | ||
import DatabaseManager from '@database/manager'; | ||
import IntegrationsManager from '@managers/integrations_manager'; | ||
import NetworkManager from '@managers/network_manager'; | ||
import {getFullErrorMessage} from '@utils/errors'; | ||
|
||
import * as integrations from './integrations'; | ||
import {forceLogoutIfNecessary} from './session'; | ||
|
||
jest.mock('@database/manager'); | ||
jest.mock('@managers/integrations_manager'); | ||
jest.mock('@queries/servers/system'); | ||
jest.mock('./session'); | ||
jest.mock('@utils/log'); | ||
jest.mock('@utils/errors'); | ||
|
||
describe('integrations', () => { | ||
const serverUrl = 'baseHandler.test.com'; | ||
const postId = 'post_id'; | ||
const actionId = 'action_id'; | ||
const actionCookie = 'action_cookie'; | ||
const selectedOption = 'option1'; | ||
|
||
const mockClient = { | ||
submitInteractiveDialog: jest.fn(), | ||
doPostActionWithCookie: jest.fn(), | ||
}; | ||
|
||
beforeAll(() => { | ||
// eslint-disable-next-line | ||
// @ts-ignore | ||
NetworkManager.getClient = () => mockClient; | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('submitInteractiveDialog', () => { | ||
beforeEach(async () => { | ||
await DatabaseManager.init([serverUrl]); | ||
}); | ||
|
||
afterEach(async () => { | ||
await DatabaseManager.destroyServerDatabase(serverUrl); | ||
}); | ||
|
||
it('should submit the dialog successfully', async () => { | ||
mockClient.submitInteractiveDialog.mockResolvedValue({status: 200}); | ||
(getCurrentChannelId as jest.Mock).mockResolvedValue('channel_id'); | ||
(getCurrentTeamId as jest.Mock).mockResolvedValue('team_id'); | ||
const {data} = await integrations.submitInteractiveDialog(serverUrl, {url: 'url'} as DialogSubmission); | ||
expect(data).toBeDefined(); | ||
expect(mockClient.submitInteractiveDialog).toHaveBeenCalledWith({ | ||
url: 'url', | ||
channel_id: 'channel_id', | ||
team_id: 'team_id', | ||
}); | ||
}); | ||
|
||
it('should handle errors correctly', async () => { | ||
const error = new Error('Test error'); | ||
mockClient.submitInteractiveDialog.mockRejectedValue(error); | ||
(getFullErrorMessage as jest.Mock).mockReturnValue('Full error message'); | ||
|
||
const {error: resultError} = await integrations.submitInteractiveDialog(serverUrl, {url: 'url'} as DialogSubmission); | ||
|
||
expect(resultError).toBeTruthy(); | ||
expect(forceLogoutIfNecessary).toHaveBeenCalledWith(serverUrl, error); | ||
expect(logDebug).toHaveBeenCalledWith('error on submitInteractiveDialog', 'Full error message'); | ||
}); | ||
}); | ||
|
||
describe('postActionWithCookie', () => { | ||
beforeEach(() => { | ||
mockClient.doPostActionWithCookie.mockResolvedValue({status: 200, trigger_id: 'trigger_id'}); | ||
}); | ||
|
||
it('should post action with cookie successfully', async () => { | ||
const mockSetTriggerId = jest.fn(); | ||
IntegrationsManager.getManager = jest.fn().mockReturnValue({ | ||
setTriggerId: mockSetTriggerId, | ||
}); | ||
|
||
const {data} = await integrations.postActionWithCookie(serverUrl, postId, actionId, actionCookie, selectedOption); | ||
|
||
expect(data).toBeDefined(); | ||
expect(mockClient.doPostActionWithCookie).toHaveBeenCalledWith(postId, actionId, actionCookie, selectedOption); | ||
expect(mockSetTriggerId).toHaveBeenCalledWith('trigger_id'); | ||
}); | ||
|
||
it('should handle errors correctly and call forceLogoutIfNecessary, logDebug, and getFullErrorMessage', async () => { | ||
const error = new Error('Error message'); | ||
mockClient.doPostActionWithCookie.mockRejectedValue(error); | ||
(getFullErrorMessage as jest.Mock).mockReturnValue('Full error message'); | ||
|
||
const {error: resultError} = await integrations.postActionWithCookie(serverUrl, postId, actionId, actionCookie, selectedOption); | ||
|
||
expect(resultError).toBeTruthy(); | ||
expect(forceLogoutIfNecessary).toHaveBeenCalledWith(serverUrl, error); | ||
expect(logDebug).toHaveBeenCalledWith('error on postActionWithCookie', 'Full error message'); | ||
}); | ||
}); | ||
|
||
describe('selectAttachmentMenuAction', () => { | ||
it('should call postActionWithCookie with the correct parameters', async () => { | ||
mockClient.doPostActionWithCookie.mockResolvedValue({status: 200, trigger_id: 'trigger_id'}); | ||
IntegrationsManager.getManager = jest.fn().mockReturnValue({setTriggerId: jest.fn()}); | ||
|
||
const data = await integrations.selectAttachmentMenuAction(serverUrl, postId, actionId, selectedOption); | ||
|
||
expect(data).toBeDefined(); | ||
expect(mockClient.doPostActionWithCookie).toHaveBeenCalledWith(postId, actionId, '', selectedOption); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know part of the codebase do it this way, but I feel it is a better test if we follow what is done in
app/managers/draft_upload_manager
with something like this:Among other things, this helps verify that we are getting the correct client. Not a huge deal, because there would be little chance we are doing that wrong, but makes the test more correct with not much more effort (and less ts-ignore comments :P).