Skip to content

Commit 9060ddb

Browse files
committed
chore: add tests for queryReactions
1 parent 37b1bb0 commit 9060ddb

File tree

1 file changed

+143
-2
lines changed

1 file changed

+143
-2
lines changed

test/unit/client.test.js

Lines changed: 143 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@ import { StableWSConnection } from '../../src/connection';
99
import { mockChannelQueryResponse } from './test-utils/mockChannelQueryResponse';
1010
import { DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE } from '../../src/constants';
1111

12-
import { describe, beforeEach, it, expect, beforeAll, afterAll } from 'vitest';
12+
import {
13+
describe,
14+
beforeEach,
15+
it,
16+
expect,
17+
beforeAll,
18+
afterEach,
19+
afterAll,
20+
vi,
21+
} from 'vitest';
1322
import { Channel } from '../../src';
14-
import { ChannelAPIResponse } from '../../src';
23+
import { normalizeQuerySort } from '../../src/utils';
24+
import { MockOfflineDB } from './offline-support/MockOfflineDB';
1525

1626
describe('StreamChat getInstance', () => {
1727
beforeEach(() => {
@@ -750,6 +760,137 @@ describe('StreamChat.queryChannels', async () => {
750760
});
751761
});
752762

763+
describe('StreamChat.queryReactions', () => {
764+
let client;
765+
let dispatchSpy;
766+
let postStub;
767+
const messageId = 'msg-1';
768+
const filter = { type: { $in: ['like', 'love'] } };
769+
const sort = [{ created_at: -1 }];
770+
const options = { limit: 50 };
771+
772+
const offlineReactions = [
773+
{ type: 'like', user_id: 'user-1', message_id: messageId },
774+
{ type: 'love', user_id: 'user-2', message_id: messageId },
775+
];
776+
777+
const postResponse = {
778+
reactions: [
779+
{ type: 'like', user_id: 'user-1', message_id: messageId },
780+
{ type: 'love', user_id: 'user-2', message_id: messageId },
781+
],
782+
};
783+
784+
beforeEach(async () => {
785+
client = await getClientWithUser();
786+
const offlineDb = new MockOfflineDB({ client });
787+
788+
client.setOfflineDBApi(offlineDb);
789+
await client.offlineDb.init(client.userID);
790+
791+
dispatchSpy = vi.spyOn(client, 'dispatchEvent');
792+
postStub = vi.spyOn(client, 'post').mockResolvedValueOnce(postResponse);
793+
client.offlineDb.getReactions.mockResolvedValue(offlineReactions);
794+
});
795+
796+
afterEach(() => {
797+
vi.resetAllMocks();
798+
});
799+
800+
it('should query reactions from offlineDb and dispatch offline_reactions.queried event', async () => {
801+
const result = await client.queryReactions(messageId, filter, sort, options);
802+
803+
expect(client.offlineDb.getReactions).toHaveBeenCalledWith({
804+
messageId,
805+
filters: filter,
806+
sort,
807+
limit: options.limit,
808+
});
809+
810+
expect(dispatchSpy).toHaveBeenCalledTimes(1);
811+
// dispatchEvent enriches the event with some extra data which
812+
// makes testing inconvenient.
813+
const dispatchSpyCallArguments = dispatchSpy.mock.calls[0];
814+
delete dispatchSpyCallArguments[0].received_at;
815+
expect(dispatchSpyCallArguments).toStrictEqual([
816+
{
817+
type: 'offline_reactions.queried',
818+
offlineReactions,
819+
},
820+
]);
821+
822+
expect(postStub).toHaveBeenCalledTimes(1);
823+
expect(postStub).toHaveBeenCalledWith(
824+
`${client.baseURL}/messages/${encodeURIComponent(messageId)}/reactions`,
825+
{
826+
filter,
827+
sort: normalizeQuerySort(sort),
828+
limit: 50,
829+
},
830+
);
831+
832+
expect(result).to.eql(postResponse);
833+
});
834+
835+
it('should skip querying offlineDb if options.next is true', async () => {
836+
await client.queryReactions(messageId, filter, sort, { next: true, limit: 20 });
837+
838+
expect(client.offlineDb.getReactions).not.toHaveBeenCalled();
839+
840+
expect(postStub).toHaveBeenCalledWith(
841+
`${client.baseURL}/messages/${encodeURIComponent(messageId)}/reactions`,
842+
{
843+
filter,
844+
sort: normalizeQuerySort(sort),
845+
next: true,
846+
limit: 20,
847+
},
848+
);
849+
});
850+
851+
it('should not dispatch event if offlineDb returns null', async () => {
852+
client.offlineDb.getReactions.mockResolvedValue(null);
853+
854+
await client.queryReactions(messageId, filter, sort, options);
855+
856+
expect(client.offlineDb.getReactions).toHaveBeenCalledTimes(1);
857+
expect(dispatchSpy).not.toHaveBeenCalled();
858+
expect(postStub).toHaveBeenCalledWith(
859+
`${client.baseURL}/messages/${encodeURIComponent(messageId)}/reactions`,
860+
{
861+
filter,
862+
sort: normalizeQuerySort(sort),
863+
limit: 50,
864+
},
865+
);
866+
});
867+
868+
it('should log a warning if offlineDb.getReactions throws', async () => {
869+
client.offlineDb.getReactions.mockRejectedValue(new Error('DB error'));
870+
const loggerSpy = vi.fn();
871+
client.logger = loggerSpy;
872+
873+
await client.queryReactions(messageId, filter, sort, options);
874+
875+
expect(loggerSpy).toHaveBeenCalledWith(
876+
'warn',
877+
'An error has occurred while querying offline reactions',
878+
expect.objectContaining({
879+
error: expect.any(Error),
880+
}),
881+
);
882+
expect(dispatchSpy).not.toHaveBeenCalled();
883+
expect(postStub).toHaveBeenCalledWith(
884+
`${client.baseURL}/messages/${encodeURIComponent(messageId)}/reactions`,
885+
{
886+
filter,
887+
sort: normalizeQuerySort(sort),
888+
limit: 50,
889+
},
890+
);
891+
});
892+
});
893+
753894
describe('X-Stream-Client header', () => {
754895
let client;
755896

0 commit comments

Comments
 (0)