Skip to content

Commit

Permalink
Resolve conflict after rebase from main
Browse files Browse the repository at this point in the history
  • Loading branch information
AhyoungRyu authored and HoonBaek committed Dec 3, 2024
1 parent a618b44 commit ca70cca
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 156 deletions.
154 changes: 0 additions & 154 deletions src/modules/GroupChannel/context/__tests__/useMessageActions.spec.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
import { renderHook } from '@testing-library/react-hooks';
import { UserMessageCreateParams, FileMessageCreateParams } from '@sendbird/chat/message';

import { useMessageActions } from '../hooks/useMessageActions';

const mockEventHandlers = {
message: {
onSendMessageFailed: jest.fn(),
onUpdateMessageFailed: jest.fn(),
onFileUploadFailed: jest.fn(),
},
};
const mockChannel = {
url: 'test-channel',
members: [{ userId: '1', nickname: 'user1' }],
};
const mockGetChannel = jest.fn().mockResolvedValue(mockChannel);
const mockMessageCollection = {
dispose: jest.fn(),
setMessageCollectionHandler: jest.fn(),
initialize: jest.fn().mockResolvedValue(null),
loadPrevious: jest.fn(),
loadNext: jest.fn(),
};
jest.mock('../../../../lib/Sendbird/context/hooks/useSendbird', () => ({
__esModule: true,
default: jest.fn(() => ({
state: {
eventHandlers: mockEventHandlers,
stores: {
sdkStore: {
sdk: {
groupChannel: {
getChannel: mockGetChannel,
addGroupChannelHandler: jest.fn(),
removeGroupChannelHandler: jest.fn(),
},
createMessageCollection: jest.fn().mockReturnValue(mockMessageCollection),
},
initialized: true,
},
},
config: {
markAsReadScheduler: {
push: jest.fn(),
},
groupChannel: {
replyType: 'NONE',
threadReplySelectType: 'PARENT',
},
groupChannelSettings: {
enableMessageSearch: true,
},
isOnline: true,
pubSub: {
subscribe: () => ({ remove: jest.fn() }),
},
},
},
})),
}));

describe('useMessageActions', () => {
// Setup common mocks
const mockSendUserMessage = jest.fn();
Expand Down Expand Up @@ -183,4 +242,133 @@ describe('useMessageActions', () => {
);
});
});

describe('processParams', () => {
const mockParams = {
sendUserMessage: jest.fn(),
sendFileMessage: jest.fn(),
sendMultipleFilesMessage: jest.fn(),
updateUserMessage: jest.fn(),
scrollToBottom: jest.fn(),
replyType: 'NONE',
};
it('should handle successful user message', async () => {
const { result } = renderHook(() => useMessageActions(mockParams));
const params: UserMessageCreateParams = { message: 'test' };

await result.current.sendUserMessage(params);

expect(mockParams.sendUserMessage).toHaveBeenCalledWith(
expect.objectContaining({ message: 'test' }),
expect.any(Function),
);
});

it('should handle void return from onBeforeSendFileMessage', async () => {
const onBeforeSendFileMessage = jest.fn();
const { result } = renderHook(() => useMessageActions({
...mockParams,
onBeforeSendFileMessage,
}),
);

const fileParams: FileMessageCreateParams = {
file: new File([], 'test.txt'),
};

await result.current.sendFileMessage(fileParams);

expect(onBeforeSendFileMessage).toHaveBeenCalled();
expect(mockParams.sendFileMessage).toHaveBeenCalledWith(
expect.objectContaining(fileParams),
expect.any(Function),
);
});

it('should handle file upload error', async () => {
// Arrange
const error = new Error('Upload failed');
const onBeforeSendFileMessage = jest.fn().mockRejectedValue(error);
const fileParams: FileMessageCreateParams = {
file: new File([], 'test.txt'),
fileName: 'test.txt',
};

const { result } = renderHook(() => useMessageActions({
...mockParams,
onBeforeSendFileMessage,
}),
);

await expect(async () => {
await result.current.sendFileMessage(fileParams);
}).rejects.toThrow('Upload failed');

// Wait for next tick to ensure all promises are resolved
await new Promise(process.nextTick);

expect(onBeforeSendFileMessage).toHaveBeenCalled();
expect(mockEventHandlers.message.onFileUploadFailed).toHaveBeenCalledWith(error);
expect(mockEventHandlers.message.onSendMessageFailed).toHaveBeenCalledWith(
expect.objectContaining({
file: fileParams.file,
fileName: fileParams.fileName,
}),
error,
);
});

it('should handle message update error', async () => {
// Arrange
const error = new Error('Update failed');
const onBeforeUpdateUserMessage = jest.fn().mockRejectedValue(error);
const messageParams = {
messageId: 1,
message: 'update message',
};

const { result } = renderHook(() => useMessageActions({
...mockParams,
onBeforeUpdateUserMessage,
}),
);

await expect(async () => {
await result.current.updateUserMessage(messageParams.messageId, {
message: messageParams.message,
});
}).rejects.toThrow('Update failed');

// Wait for next tick to ensure all promises are resolved
await new Promise(process.nextTick);

expect(onBeforeUpdateUserMessage).toHaveBeenCalled();
expect(mockEventHandlers.message.onUpdateMessageFailed).toHaveBeenCalledWith(
expect.objectContaining({
message: messageParams.message,
}),
error,
);
});

it('should preserve modified params from onBefore handlers', async () => {
const onBeforeSendUserMessage = jest.fn().mockImplementation((params) => ({
...params,
message: 'modified',
}));

const { result } = renderHook(() => useMessageActions({
...mockParams,
onBeforeSendUserMessage,
}),
);

await result.current.sendUserMessage({ message: 'original' });

expect(mockParams.sendUserMessage).toHaveBeenCalledWith(
expect.objectContaining({ message: 'modified' }),
expect.any(Function),
);
});
});
});
4 changes: 2 additions & 2 deletions src/modules/GroupChannel/context/hooks/useMessageActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
VOICE_MESSAGE_MIME_TYPE,
} from '../../../../utils/consts';
import type { CoreMessageType } from '../../../../utils';
import useSendbirdStateContext from '../../../../hooks/useSendbirdStateContext';
import useSendbird from '../../../../lib/Sendbird/context/hooks/useSendbird';
import type { GroupChannelState, OnBeforeHandler } from '../types';

type MessageListDataSource = ReturnType<typeof useGroupChannelMessages>;
Expand Down Expand Up @@ -70,7 +70,7 @@ export function useMessageActions(params: Params): MessageActions {
quoteMessage,
replyType,
} = params;
const { eventHandlers } = useSendbirdStateContext();
const { state: { eventHandlers } } = useSendbird();
const buildInternalMessageParams = useCallback(
<T extends BaseMessageCreateParams>(basicParams: T): T => {
const messageParams = { ...basicParams } as T;
Expand Down

0 comments on commit ca70cca

Please sign in to comment.