forked from X-DIABLO-X/LEADERBOARD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Enhanced
75 lines (65 loc) · 2.35 KB
/
Enhanced
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
const { addScore, getLeaderboard, resetLeaderboard } = require('./leaderboard');
describe('Leaderboard Functions', () => {
beforeEach(() => {
resetLeaderboard(); // Reset the leaderboard before each test
});
test('should add a score correctly', () => {
addScore('Alice', 100);
const leaderboard = getLeaderboard();
expect(leaderboard).toEqual([{ name: 'Alice', score: 100 }]);
});
test('should update score for existing user', () => {
addScore('Bob', 50);
addScore('Bob', 75); // Update score
const leaderboard = getLeaderboard();
expect(leaderboard).toEqual([{ name: 'Bob', score: 75 }]);
});
test('should return an empty leaderboard if no scores added', () => {
const leaderboard = getLeaderboard();
expect(leaderboard).toEqual([]);
});
test('should handle multiple users correctly', () => {
addScore('Alice', 100);
addScore('Bob', 200);
const leaderboard = getLeaderboard();
expect(leaderboard).toEqual([
{ name: 'Alice', score: 100 },
{ name: 'Bob', score: 200 },
]);
});
test('should maintain leaderboard order if sorted by score', () => {
addScore('Charlie', 150);
addScore('Alice', 100);
addScore('Bob', 200);
const leaderboard = getLeaderboard();
expect(leaderboard).toEqual([
{ name: 'Bob', score: 200 },
{ name: 'Charlie', score: 150 },
{ name: 'Alice', score: 100 },
]);
});
test('should reset the leaderboard correctly', () => {
addScore('Alice', 100);
resetLeaderboard();
const leaderboard = getLeaderboard();
expect(leaderboard).toEqual([]);
});
test('should handle zero and negative scores', () => {
addScore('NegativeUser', -50);
addScore('ZeroUser', 0);
const leaderboard = getLeaderboard();
expect(leaderboard).toContainEqual({ name: 'NegativeUser', score: -50 });
expect(leaderboard).toContainEqual({ name: 'ZeroUser', score: 0 });
});
test('should handle large scores correctly', () => {
addScore('BigScoreUser', 1000000);
const leaderboard = getLeaderboard();
expect(leaderboard).toEqual([{ name: 'BigScoreUser', score: 1000000 }]);
});
test('should only store the latest score for each user', () => {
addScore('Alice', 50);
addScore('Alice', 150);
const leaderboard = getLeaderboard();
expect(leaderboard).toEqual([{ name: 'Alice', score: 150 }]);
});
});