Skip to content

Commit c5d6fc6

Browse files
committed
chore: add tests for handleMessage invocation on dispatchEvent
1 parent 846dabb commit c5d6fc6

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

test/unit/client.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,6 +1019,55 @@ describe('message deletion', () => {
10191019
});
10201020
});
10211021

1022+
describe('dispatchEvent: offlineDb.executeQuerySafely', () => {
1023+
let client;
1024+
let executeQuerySafelySpy;
1025+
1026+
beforeEach(async () => {
1027+
client = await getClientWithUser({ id: 'user-abc' });
1028+
const offlineDb = new MockOfflineDB({ client });
1029+
await offlineDb.init(client.userID);
1030+
client.setOfflineDBApi(offlineDb);
1031+
1032+
executeQuerySafelySpy = vi.spyOn(offlineDb, 'executeQuerySafely');
1033+
});
1034+
1035+
afterEach(() => {
1036+
vi.restoreAllMocks();
1037+
});
1038+
1039+
it('should call executeQuerySafely with correct event', () => {
1040+
const testEvent = {
1041+
type: 'message.new',
1042+
cid: 'messaging:test',
1043+
};
1044+
1045+
vi.spyOn(client.offlineDb, 'handleEvent').mockResolvedValue({});
1046+
1047+
client.dispatchEvent(testEvent);
1048+
1049+
expect(executeQuerySafelySpy).toHaveBeenCalledTimes(1);
1050+
expect(executeQuerySafelySpy).toHaveBeenCalledWith(expect.any(Function), {
1051+
method: 'handleEvent;message.new',
1052+
});
1053+
1054+
// Verify the inner function calls db.handleEvent correctly
1055+
const fn = executeQuerySafelySpy.mock.calls[0][0];
1056+
fn(client.offlineDb);
1057+
1058+
expect(client.offlineDb.handleEvent).toHaveBeenCalledWith({ event: testEvent });
1059+
});
1060+
1061+
it('should work normally if client.offlineDb is not set', () => {
1062+
client.offlineDb = undefined;
1063+
1064+
const event = { type: 'user.updated' };
1065+
1066+
expect(() => client.dispatchEvent(event)).not.toThrow();
1067+
expect(executeQuerySafelySpy).not.toHaveBeenCalled();
1068+
});
1069+
});
1070+
10221071
describe('X-Stream-Client header', () => {
10231072
let client;
10241073

0 commit comments

Comments
 (0)