Skip to content

Commit

Permalink
Added changes to socket testing in Unit tests in test/topics.js
Browse files Browse the repository at this point in the history
  • Loading branch information
MrUrias committed Feb 27, 2025
1 parent dd081ea commit 21d95b8
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion test/topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -2589,6 +2589,12 @@ describe('topicsController.setResolved - Unit Test', () => {
notifications.create = function (...args) {
notifications.createCalls.push(args);
};

// Mock socket.emit
socket.emitCalls = [];
socket.emit = function (...args) {
socket.emitCalls.push(args);
};
});

afterEach(async () => {
Expand All @@ -2606,7 +2612,7 @@ describe('topicsController.setResolved - Unit Test', () => {
});
});

it('should successfully unmark a topic as resolved', async () => {
it('should successfully unmark a topic as resolved (set unresolved)', async () => {
req.body.resolved = false;
await topicsController.setResolved(req, res);

Expand Down Expand Up @@ -2671,6 +2677,26 @@ describe('topicsController.setResolved - Unit Test', () => {
assert.strictEqual(notification.tid, req.params.tid, 'Notification should reference the correct topic ID');
assert.strictEqual(notification.from, req.uid, 'Notification should be from the user who marked it');
});

it('should emit a socket event when a topic is marked as resolved', async () => {
req.body.resolved = true;
await topicsController.setResolved(req, res);

assert.strictEqual(socket.emitCalls.length, 1, 'Expected socket.emit to be called once');
const [event, data] = socket.emitCalls[0];
assert.strictEqual(event, 'event:topicResolvedUpdated', 'Expected event name to match');
assert.deepStrictEqual(data, { tid: testTid, resolved: true }, 'Expected emitted data to match');
});

it('should emit a socket event when a topic is marked as unresolved', async () => {
req.body.resolved = false;
await topicsController.setResolved(req, res);

assert.strictEqual(socket.emitCalls.length, 1, 'Expected socket.emit to be called once');
const [event, data] = socket.emitCalls[0];
assert.strictEqual(event, 'event:topicResolvedUpdated', 'Expected event name to match');
assert.deepStrictEqual(data, { tid: testTid, resolved: false }, 'Expected emitted data to match');
});
});


Expand Down

0 comments on commit 21d95b8

Please sign in to comment.