Skip to content

Commit

Permalink
Added tests and credits
Browse files Browse the repository at this point in the history
  • Loading branch information
tu2463 committed Oct 11, 2024
1 parent 83f5a00 commit b1b38bf
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/topics/favorite.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const db = require('../database');

// Credit: partially written by ChatGPT
module.exports = function (Topics) {
Topics.toggleFavorite = async function (tid, uid) {
const exists = await Topics.exists(tid);
Expand Down
83 changes: 83 additions & 0 deletions test/topics/favorite.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
'use strict';

const assert = require('assert');
const db = require('../database');

const Topics = {};
require('../../src/topics/favorite')(Topics); // Corrected path

describe('Topics', function () {

Check failure on line 9 in test/topics/favorite.js

View workflow job for this annotation

GitHub Actions / test

Unexpected function expression
let dbCalls;

beforeEach(function () {

Check failure on line 12 in test/topics/favorite.js

View workflow job for this annotation

GitHub Actions / test

Unexpected function expression
dbCalls = {
setAdd: [],
sortedSetAdd: [],
setRemove: [],
sortedSetRemove: [],
isSortedSetMember: []

Check failure on line 18 in test/topics/favorite.js

View workflow job for this annotation

GitHub Actions / test

Missing trailing comma
};

db.setAdd = async (...args) => { dbCalls.setAdd.push(args); };
db.sortedSetAdd = async (...args) => { dbCalls.sortedSetAdd.push(args); };
db.setRemove = async (...args) => { dbCalls.setRemove.push(args); };
db.sortedSetRemove = async (...args) => { dbCalls.sortedSetRemove.push(args); };
db.isSortedSetMember = async (...args) => { dbCalls.isSortedSetMember.push(args); return false; };
Topics.exists = async () => true;
});

describe('.toggleFavorite()', function () {

Check failure on line 29 in test/topics/favorite.js

View workflow job for this annotation

GitHub Actions / test

Unexpected function expression
it('should throw an error if topic does not exist', async function () {

Check failure on line 30 in test/topics/favorite.js

View workflow job for this annotation

GitHub Actions / test

Unexpected function expression
Topics.exists = async () => false;
try {
await Topics.toggleFavorite(1, 1);
} catch (err) {
assert.strictEqual(err.message, '[[error:no-topic]]');

Check failure on line 35 in test/topics/favorite.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 4 tabs but found 16 spaces
}

Check failure on line 36 in test/topics/favorite.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 3 tabs but found 12 spaces
});

Check failure on line 37 in test/topics/favorite.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces

it('should unfavorite a topic if already favorited', async function () {

Check failure on line 39 in test/topics/favorite.js

View workflow job for this annotation

GitHub Actions / test

Expected indentation of 2 tabs but found 8 spaces

Check failure on line 39 in test/topics/favorite.js

View workflow job for this annotation

GitHub Actions / test

Unexpected function expression
db.isSortedSetMember = async () => true;
const result = await Topics.toggleFavorite(1, 1);
assert.strictEqual(result, false);
assert.deepStrictEqual(dbCalls.setRemove, [['tid:1:favoriters', 1]]);
assert.deepStrictEqual(dbCalls.sortedSetRemove, [['uid:1:favorite_tids', 1]]);
});

it('should favorite a topic if not already favorited', async function () {
db.isSortedSetMember = async () => false;
const result = await Topics.toggleFavorite(1, 1);
assert.strictEqual(result, true);
assert.deepStrictEqual(dbCalls.setAdd, [['tid:1:favoriters', 1]]);
assert.strictEqual(dbCalls.sortedSetAdd.length, 1);
assert.strictEqual(dbCalls.sortedSetAdd[0][0], 'uid:1:favorite_tids');
assert.strictEqual(dbCalls.sortedSetAdd[0][2], 1);
});
});

describe('.favorite()', function () {
it('should add a topic to favorites', async function () {
await Topics.favorite(1, 1);
assert.deepStrictEqual(dbCalls.setAdd, [['tid:1:favoriters', 1]]);
assert.strictEqual(dbCalls.sortedSetAdd.length, 1);
assert.strictEqual(dbCalls.sortedSetAdd[0][0], 'uid:1:favorite_tids');
assert.strictEqual(dbCalls.sortedSetAdd[0][2], 1);
});
});

describe('.unfavorite()', function () {
it('should remove a topic from favorites', async function () {
await Topics.unfavorite(1, 1);
assert.deepStrictEqual(dbCalls.setRemove, [['tid:1:favoriters', 1]]);
assert.deepStrictEqual(dbCalls.sortedSetRemove, [['uid:1:favorite_tids', 1]]);
});
});

describe('.isFavorited()', function () {
it('should check if a topic is favorited', async function () {
const result = await Topics.isFavorited(1, 1);
assert.strictEqual(result, false);
assert.deepStrictEqual(dbCalls.isSortedSetMember, [['uid:1:favorite_tids', 1]]);
});
});
});

0 comments on commit b1b38bf

Please sign in to comment.