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,113 @@
import { mockAppRoot } from '@rocket.chat/mock-providers';
import type { SubscriptionWithRoom } from '@rocket.chat/ui-contexts';
import { renderHook } from '@testing-library/react';

import { createFakeSubscription } from '../../../../../tests/mocks/data';
import { emptyUnread, createMockGlobalStore } from '../../../../../tests/mocks/utils';

const fakeTeam = { ...createFakeSubscription({ alert: true }) };
const fakeChannel = { ...createFakeSubscription({ t: 'c', alert: true }) };

const teamChildChannel = {
...createFakeSubscription({ t: 'c', ...emptyUnread, prid: fakeTeam.rid }),
};
const unreadTeamChildChannel = {
...createFakeSubscription({ t: 'c', alert: true, prid: fakeTeam.rid }),
};
const childChannel = {
...createFakeSubscription({ t: 'c', ...emptyUnread, prid: fakeChannel.rid }),
};
const userMentionsChildChannel = {
...createFakeSubscription({ t: 'c', userMentions: 1, prid: fakeChannel.rid }),
};
const groupMentionsChildChannel = {
...createFakeSubscription({ t: 'c', groupMentions: 1, prid: fakeChannel.rid }),
};
const unreadChildChannel = {
...createFakeSubscription({ t: 'c', unread: 1, prid: fakeChannel.rid }),
};
const tUnreadChildChannel = {
...createFakeSubscription({ t: 'c', tunread: ['1'], prid: fakeChannel.rid }),
};
const tUnreadUserChildChannel = {
...createFakeSubscription({ t: 'c', tunreadUser: ['1'], prid: fakeChannel.rid }),
};
const tUnreadGroupChildChannel = {
...createFakeSubscription({ t: 'c', tunreadGroup: ['1'], prid: fakeChannel.rid }),
};

const teamsSubscriptions = [fakeTeam, teamChildChannel, unreadTeamChildChannel] as unknown as SubscriptionWithRoom[];
const channelsSubscriptions = [
fakeChannel,
childChannel,
userMentionsChildChannel,
groupMentionsChildChannel,
unreadChildChannel,
tUnreadChildChannel,
tUnreadUserChildChannel,
tUnreadGroupChildChannel,
] as unknown as SubscriptionWithRoom[];

const subscriptionsMock = [...teamsSubscriptions, ...channelsSubscriptions] as unknown as SubscriptionWithRoom[];

jest.mock('../../../../stores/Subscriptions', () => {
return {
Subscriptions: createMockGlobalStore(subscriptionsMock),
};
});

it('should return all subscriptions parent of fakeChannel', async () => {
const { useChannelsChildrenList } = await import('./useChannelsChildrenList');

const { result } = renderHook(() => useChannelsChildrenList(fakeChannel.rid, false), {
wrapper: mockAppRoot().withSubscriptions(subscriptionsMock).build(),
});

expect(result.current).toHaveLength(channelsSubscriptions.length);
expect(result.current[0].rid).toBe(fakeChannel.rid);

if (result.current[0].rid === fakeChannel.rid) return;
expect(result.current[0].prid).toBe(fakeChannel.rid);
});

it('should return only unread subscriptions parent of fakeChannel', async () => {
const { useChannelsChildrenList, isUnreadSubscription } = await import('./useChannelsChildrenList');

const { result } = renderHook(() => useChannelsChildrenList(fakeChannel.rid, true), {
wrapper: mockAppRoot().withSubscriptions(subscriptionsMock).build(),
});

expect(result.current).toHaveLength(channelsSubscriptions.filter((sub) => isUnreadSubscription(sub)).length);
expect(result.current[0].rid).toBe(fakeChannel.rid);

if (result.current[0].rid === fakeChannel.rid) return;
expect(result.current[0].prid).toBe(fakeChannel.rid);
});

it('should return subscriptions parent of fakeTeam', async () => {
const { useChannelsChildrenList } = await import('./useChannelsChildrenList');

const { result } = renderHook(() => useChannelsChildrenList(fakeTeam.rid, false), {
wrapper: mockAppRoot().withSubscriptions(subscriptionsMock).build(),
});

expect(result.current).toHaveLength(teamsSubscriptions.length);
expect(result.current[0].rid).toBe(fakeTeam.rid);

if (result.current[0].rid === fakeTeam.rid) return;
expect(result.current[0].prid).toBe(fakeTeam.rid);
});

it('should return only unread subscriptions parent of fakeTeam', async () => {
const { useChannelsChildrenList, isUnreadSubscription } = await import('./useChannelsChildrenList');

const { result } = renderHook(() => useChannelsChildrenList(fakeTeam.rid, true), {
wrapper: mockAppRoot().withSubscriptions(subscriptionsMock).build(),
});

expect(result.current).toHaveLength(teamsSubscriptions.filter((sub) => isUnreadSubscription(sub)).length);
expect(result.current[0].rid).toBe(fakeTeam.rid);

if (result.current[0].rid === fakeTeam.rid) return;
expect(result.current[0].prid).toBe(fakeTeam.rid);
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@ import { useShallow } from 'zustand/shallow';
import { pipe } from '../../../../lib/cachedStores';
import { Subscriptions } from '../../../../stores';

const filterUnread = (subscription: ISubscription, unreadOnly: boolean) => !unreadOnly || subscription.unread > 0;
export const isUnreadSubscription = (subscription: ISubscription) => {
return (
subscription.userMentions > 0 ||
subscription.groupMentions > 0 ||
!!(subscription.tunread && subscription.tunread?.length > 0) ||
!!(subscription.tunreadUser && subscription.tunreadUser?.length > 0) ||
!!(!subscription.unread && !subscription.tunread?.length && subscription.alert)
);
};

const filterUnread = (subscription: ISubscription, unreadOnly: boolean) => !unreadOnly || isUnreadSubscription(subscription);

const sortByLmPipe = pipe<SubscriptionWithRoom>().sortByField('lm', -1);

Expand All @@ -14,8 +24,14 @@ const sortByLmPipe = pipe<SubscriptionWithRoom>().sortByField('lm', -1);
* is always at the top of the list.
*/
const getMainRoomAndSort = (records: SubscriptionWithRoom[]) => {
const [mainRoom, ...rest] = records;
return [mainRoom, ...sortByLmPipe.apply(rest)];
const mainRoom = records.find((record) => !record.prid);
const rest = sortByLmPipe.apply(records.filter((record) => mainRoom?.rid !== record.rid));

if (mainRoom) {
rest.unshift(mainRoom);
}

return rest;
};

export const useChannelsChildrenList = (parentRid: string, unreadOnly: boolean, teamId?: string) => {
Expand Down
8 changes: 8 additions & 0 deletions apps/meteor/tests/mocks/utils/createMockGlobalStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function createMockGlobalStore(records: any[]) {
return {
use: (selector: any) => selector(records),
get state() {
return records;
},
};
}
11 changes: 11 additions & 0 deletions apps/meteor/tests/mocks/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export { createMockGlobalStore } from './createMockGlobalStore';

export const emptyUnread = {
userMentions: 0,
groupMentions: 0,
unread: 0,
tunread: undefined,
tunreadUser: undefined,
tunreadGroup: undefined,
alert: false,
};
Loading