Skip to content

Commit

Permalink
[CLNP-6010] Add more unit tests for existing message search custom ho…
Browse files Browse the repository at this point in the history
…ok (#1278)

#### Before
<img width="705" alt="Screenshot 2024-12-06 at 6 23 24 PM"
src="https://github.com/user-attachments/assets/8269506e-b347-4091-a3d7-254aabb063d9">

#### After
<img width="703" alt="Screenshot 2024-12-06 at 6 36 19 PM"
src="https://github.com/user-attachments/assets/4480241a-4bfd-4096-a13f-27f7c6ac76ba">
  • Loading branch information
AhyoungRyu authored Dec 9, 2024
1 parent b9e7602 commit 766d1d7
Show file tree
Hide file tree
Showing 5 changed files with 893 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MessageSearchQuery } from '@sendbird/chat/message';

import { MessageSearchProvider } from '../MessageSearchProvider';
import useMessageSearch from '../hooks/useMessageSearch';
import useScrollCallback from '../hooks/useScrollCallback';

jest.mock('../../../../lib/Sendbird/context/hooks/useSendbird', () => ({
__esModule: true,
Expand Down Expand Up @@ -46,6 +47,11 @@ jest.mock('../hooks/useSearchStringEffect', () => ({
}));

describe('MessageSearchProvider', () => {
beforeEach(() => {
jest.clearAllMocks();
(useScrollCallback as jest.Mock).mockClear();
});

const initialState = {
allMessages: [],
loading: false,
Expand Down Expand Up @@ -290,4 +296,102 @@ describe('MessageSearchProvider', () => {
});
});
});

it('handles onResultClick callback correctly', async () => {
const onResultClick = jest.fn();
const wrapper = ({ children }) => (
<MessageSearchProvider
channelUrl="test-channel"
onResultClick={onResultClick}
>
{children}
</MessageSearchProvider>
);

const { result } = renderHook(() => useMessageSearch(), { wrapper });

expect(result.current.state.onResultClick).toBe(onResultClick);
});

it('uses provided messageSearchQuery prop correctly', async () => {
const customQuery = {
limit: 20,
reverse: true,
exactMatch: false,
};

const wrapper = ({ children }) => (
<MessageSearchProvider
channelUrl="test-channel"
messageSearchQuery={customQuery}
>
{children}
</MessageSearchProvider>
);

const { result } = renderHook(() => useMessageSearch(), { wrapper });

expect(result.current.state.messageSearchQuery).toEqual(customQuery);
});

it('executes onResultClick callback when clicking a search result', async () => {
const onResultClick = jest.fn();
const mockMessage = { messageId: 1 };

const wrapper = ({ children }) => (
<MessageSearchProvider
channelUrl="test-channel"
onResultClick={onResultClick}
>
{children}
</MessageSearchProvider>
);

const { result } = renderHook(() => useMessageSearch(), { wrapper });

await act(async () => {
expect(result.current.state.onResultClick).toBe(onResultClick);
result.current.state.onResultClick(mockMessage);
await waitFor(() => {
expect(onResultClick).toHaveBeenCalledWith(mockMessage);
});
});
});

it('does not trigger scroll callback when hasMoreResult is false', async () => {
const wrapper = ({ children }) => (
<MessageSearchProvider channelUrl="test-channel">
{children}
</MessageSearchProvider>
);

const { result } = renderHook(() => useMessageSearch(), { wrapper });

await act(async () => {
const mockQuery = { channelUrl: 'test-channel', hasNext: false };
result.current.actions.startGettingSearchedMessages(mockQuery as any);
result.current.actions.getSearchedMessages([{ messageId: 1 }] as any, mockQuery as any);

await waitFor(() => {
expect(result.current.state.hasMoreResult).toBe(false);
});
});

await act(async () => {
const mockEvent = {
target: {
scrollTop: 100,
scrollHeight: 100,
clientHeight: 50,
},
};

const prevLoading = result.current.state.loading;
result.current.state.handleOnScroll(mockEvent);

await waitFor(() => {
expect(result.current.state.loading).toBe(prevLoading);
});
});
});
});
Loading

0 comments on commit 766d1d7

Please sign in to comment.