Skip to content

Commit

Permalink
fix: remove the use of deprecated query operator $ne (#2504)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
- own user will not anymore be filtered out of the selection list of users to mention if `mentionAllAppUsers` is enabled on MessageInput
  • Loading branch information
MartinCupela committed Sep 16, 2024
1 parent 8235d45 commit 09614f6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
22 changes: 15 additions & 7 deletions src/components/ChannelSearch/__tests__/ChannelSearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,11 @@ describe('ChannelSearch', () => {
});

it('search is performed by default on users and not channels', async () => {
const limit = 8;
const otherUsers = Array.from({ length: limit }, generateUser);
jest.useFakeTimers('modern');
const client = await getTestClientWithUser(user);
jest.spyOn(client, 'queryUsers').mockResolvedValue({ users: [user] });
jest.spyOn(client, 'queryUsers').mockResolvedValue({ users: [...otherUsers, user] });
jest.spyOn(client, 'queryChannels').mockImplementation();
const { typeText } = await renderSearch({ client });
await act(() => {
Expand All @@ -141,21 +143,26 @@ describe('ChannelSearch', () => {
expect(client.queryUsers).toHaveBeenCalledWith(
expect.objectContaining({
$or: [{ id: { $autocomplete: typedText } }, { name: { $autocomplete: typedText } }],
id: { $ne: 'id' },
}),
{ id: 1 },
{ limit: 8 },
{ limit },
);
expect(client.queryUsers).toHaveBeenCalledTimes(1);
expect(client.queryChannels).not.toHaveBeenCalled();
otherUsers.forEach((user) => {
expect(screen.queryByText(user.name)).toBeInTheDocument();
});
expect(screen.queryByText(user.name)).not.toBeInTheDocument();

jest.useRealTimers();
});

it('search is performed on users and channels if enabled', async () => {
const limit = 8;
const otherUsers = Array.from({ length: limit }, generateUser);
jest.useFakeTimers('modern');
const client = await getTestClientWithUser(user);
jest.spyOn(client, 'queryUsers').mockResolvedValue({ users: [user] });
jest.spyOn(client, 'queryUsers').mockResolvedValue({ users: [...otherUsers, user] });
jest.spyOn(client, 'queryChannels').mockResolvedValue([channelResponseData]);

const { typeText } = await renderSearch({ client, props: { searchForChannels: true } });
Expand All @@ -169,7 +176,10 @@ describe('ChannelSearch', () => {

expect(client.queryUsers).toHaveBeenCalledTimes(1);
expect(client.queryChannels).toHaveBeenCalledTimes(1);

otherUsers.forEach((user) => {
expect(screen.queryByText(user.name)).toBeInTheDocument();
});
expect(screen.queryByText(user.name)).not.toBeInTheDocument();
jest.useRealTimers();
});

Expand Down Expand Up @@ -270,7 +280,6 @@ describe('ChannelSearch', () => {
expect(client.queryUsers).toHaveBeenCalledWith(
expect.objectContaining({
$or: [{ id: { $autocomplete: textToQuery } }, { name: { $autocomplete: textToQuery } }],
id: { $ne: 'id' },
}),
{ id: 1 },
{ limit: 8 },
Expand Down Expand Up @@ -311,7 +320,6 @@ describe('ChannelSearch', () => {
expect(client.queryUsers).toHaveBeenCalledWith(
expect.objectContaining({
$or: [{ id: { $autocomplete: textToQuery } }, { name: { $autocomplete: textToQuery } }],
id: { $ne: 'id' },
}),
{ id: 1 },
{ limit: 8 },
Expand Down
5 changes: 2 additions & 3 deletions src/components/ChannelSearch/hooks/useChannelSearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,6 @@ export const useChannelSearch = <
// @ts-expect-error
{
$or: [{ id: { $autocomplete: text } }, { name: { $autocomplete: text } }],
id: { $ne: client.userID },
...searchQueryParams?.userFilters?.filters,
},
{ id: 1, ...searchQueryParams?.userFilters?.sort },
Expand All @@ -226,7 +225,7 @@ export const useChannelSearch = <
if (!searchForChannels) {
searchQueryPromiseInProgress.current = userQueryPromise;
const { users } = await searchQueryPromiseInProgress.current;
results = users;
results = users.filter((u) => u.id !== client.user?.id);
} else {
const channelQueryPromise = client.queryChannels(
// @ts-expect-error
Expand All @@ -245,7 +244,7 @@ export const useChannelSearch = <

const [channels, { users }] = await searchQueryPromiseInProgress.current;

results = [...channels, ...users];
results = [...channels, ...users.filter((u) => u.id !== client.user?.id)];
}
} catch (error) {
console.error(error);
Expand Down
1 change: 0 additions & 1 deletion src/components/MessageInput/hooks/useUserTrigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ export const useUserTrigger = <
// @ts-expect-error
{
$or: [{ id: { $autocomplete: query } }, { name: { $autocomplete: query } }],
id: { $ne: client.userID },
...(typeof mentionQueryParams.filters === 'function'
? mentionQueryParams.filters(query)
: mentionQueryParams.filters),
Expand Down

0 comments on commit 09614f6

Please sign in to comment.