Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { mockAppRoot } from '@rocket.chat/mock-providers';
import { renderHook } from '@testing-library/react';

import { useReplyInDMAction } from './useReplyInDMAction';
import { createFakeMessage, createFakeRoom, createFakeSubscription, createFakeUser } from '../../../../tests/mocks/data';
import { roomCoordinator } from '../../../lib/rooms/roomCoordinator';

jest.mock('../../../lib/rooms/roomCoordinator', () => ({
roomCoordinator: {
openRouteLink: jest.fn(),
},
}));

const mockedOpenRouteLink = jest.mocked(roomCoordinator.openRouteLink);

const currentUser = createFakeUser({
_id: 'current-user-id',
username: 'currentuser',
});

const messageAuthor = {
_id: 'author-user-id',
username: 'authoruser',
name: 'Author User',
};

const message = createFakeMessage({
_id: 'reply-message-id',
u: messageAuthor,
});

const room = createFakeRoom({
_id: 'channel-id',
t: 'c',
name: 'general',
});

const subscription = createFakeSubscription({
rid: 'channel-id',
t: 'c',
});

afterEach(() => {
jest.clearAllMocks();
});

describe('useReplyInDMAction', () => {
it('should not carry over the msg search parameter when opening a direct message', () => {
const getSearchParameters = jest.fn().mockReturnValue({
msg: 'stale-message-id',
layout: 'embedded',
});

const { result } = renderHook(() => useReplyInDMAction(message, { room, subscription }), {
wrapper: mockAppRoot().withUser(currentUser).withPermission('create-d').withRouter({ getSearchParameters }).build(),
});

expect(result.current).not.toBeNull();

result.current?.action({ stopPropagation: jest.fn() } as unknown as UIEvent);

expect(getSearchParameters).toHaveBeenCalled();
expect(mockedOpenRouteLink).toHaveBeenCalledWith(
'd',
{ name: messageAuthor.username },
{
layout: 'embedded',
reply: 'reply-message-id',
},
);

const searchParams = mockedOpenRouteLink.mock.calls[0]?.[2];
expect(searchParams).not.toHaveProperty('msg');
});

it('should return null when already in a direct message room', () => {
const dmRoom = createFakeRoom({ t: 'd' });
const dmSubscription = createFakeSubscription({ t: 'd' });

const { result } = renderHook(() => useReplyInDMAction(message, { room: dmRoom, subscription: dmSubscription }), {
wrapper: mockAppRoot().withUser(currentUser).withPermission('create-d').build(),
});

expect(result.current).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ export const useReplyInDMAction = (
context: ['message', 'message-mobile', 'threads', 'federated'],
type: 'communication',
action() {
const { msg: _, ...searchParameters } = router.getSearchParameters();
roomCoordinator.openRouteLink(
'd',
{ name: message.u.username },
{
...router.getSearchParameters(),
...searchParameters,
reply: message._id,
},
);
Expand Down
59 changes: 59 additions & 0 deletions apps/meteor/client/lib/chats/flows/replyBroadcast.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { replyBroadcast } from './replyBroadcast';
import { createFakeMessage } from '../../../../tests/mocks/data';
import { router } from '../../../providers/RouterProvider';
import { roomCoordinator } from '../../rooms/roomCoordinator';
import type { ChatAPI } from '../ChatAPI';

jest.mock('../../rooms/roomCoordinator', () => ({
roomCoordinator: {
openRouteLink: jest.fn(),
},
}));

jest.mock('../../../providers/RouterProvider', () => ({
router: {
getSearchParameters: jest.fn(),
},
}));

const mockedOpenRouteLink = jest.mocked(roomCoordinator.openRouteLink);
const mockedGetSearchParameters = jest.mocked(router.getSearchParameters);

const messageAuthor = {
_id: 'author-user-id',
username: 'authoruser',
name: 'Author User',
};

const message = createFakeMessage({
_id: 'reply-message-id',
u: messageAuthor,
});

afterEach(() => {
jest.clearAllMocks();
});

describe('replyBroadcast', () => {
it('should not carry over the msg search parameter when opening a direct message', async () => {
mockedGetSearchParameters.mockReturnValue({
msg: 'stale-message-id',
layout: 'embedded',
});

await replyBroadcast({} as ChatAPI, message);

expect(mockedGetSearchParameters).toHaveBeenCalled();
expect(mockedOpenRouteLink).toHaveBeenCalledWith(
'd',
{ name: messageAuthor.username },
{
layout: 'embedded',
reply: 'reply-message-id',
},
);

const searchParams = mockedOpenRouteLink.mock.calls[0]?.[2];
expect(searchParams).not.toHaveProperty('msg');
});
});
3 changes: 2 additions & 1 deletion apps/meteor/client/lib/chats/flows/replyBroadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { roomCoordinator } from '../../rooms/roomCoordinator';
import type { ChatAPI } from '../ChatAPI';

export const replyBroadcast = async (_chat: ChatAPI, message: IMessage) => {
const { msg: _, ...searchParameters } = router.getSearchParameters();
roomCoordinator.openRouteLink(
'd',
{ name: message.u.username },
{
...router.getSearchParameters(),
...searchParameters,
reply: message._id,
},
);
Expand Down
Loading