-
Notifications
You must be signed in to change notification settings - Fork 9
/
Provide Console Feedback
91 lines (81 loc) · 3.61 KB
/
Provide Console Feedback
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const { addScore, getLeaderboard, resetLeaderboard } = require('./leaderboard');
describe('Leaderboard Functions', () => {
beforeEach(() => {
resetLeaderboard(); // Reset the leaderboard before each test
});
describe('Score Management', () => {
test('should add a score correctly for a new user', () => {
addScore('Alice', 100);
const leaderboard = getLeaderboard();
console.log('Leaderboard after adding Alice:', leaderboard);
expect(leaderboard).toEqual([{ name: 'Alice', score: 100 }]);
});
test('should update the score for an existing user correctly', () => {
addScore('Bob', 50);
console.log('Leaderboard after adding Bob with score 50:', getLeaderboard());
addScore('Bob', 75); // Update score
const leaderboard = getLeaderboard();
console.log('Leaderboard after updating Bob with score 75:', leaderboard);
expect(leaderboard).toEqual([{ name: 'Bob', score: 75 }]);
});
test('should only store the latest score for each user', () => {
addScore('Alice', 50);
console.log('Leaderboard after adding Alice with score 50:', getLeaderboard());
addScore('Alice', 150);
const leaderboard = getLeaderboard();
console.log('Leaderboard after updating Alice with score 150:', leaderboard);
expect(leaderboard).toEqual([{ name: 'Alice', score: 150 }]);
});
test('should handle zero and negative scores without errors', () => {
addScore('NegativeUser', -50);
addScore('ZeroUser', 0);
const leaderboard = getLeaderboard();
console.log('Leaderboard after adding NegativeUser and ZeroUser:', leaderboard);
expect(leaderboard).toContainEqual({ name: 'NegativeUser', score: -50 });
expect(leaderboard).toContainEqual({ name: 'ZeroUser', score: 0 });
});
test('should handle large scores correctly and reflect them in the leaderboard', () => {
addScore('BigScoreUser', 1000000);
const leaderboard = getLeaderboard();
console.log('Leaderboard after adding BigScoreUser with large score:', leaderboard);
expect(leaderboard).toEqual([{ name: 'BigScoreUser', score: 1000000 }]);
});
});
describe('Leaderboard Functionality', () => {
test('should return an empty leaderboard if no scores are added', () => {
const leaderboard = getLeaderboard();
console.log('Leaderboard when no scores are added:', leaderboard);
expect(leaderboard).toEqual([]);
});
test('should handle multiple users and return their scores correctly', () => {
addScore('Alice', 100);
addScore('Bob', 200);
const leaderboard = getLeaderboard();
console.log('Leaderboard after adding multiple users:', leaderboard);
expect(leaderboard).toEqual([
{ name: 'Alice', score: 100 },
{ name: 'Bob', score: 200 },
]);
});
test('should maintain leaderboard order when sorted by score', () => {
addScore('Charlie', 150);
addScore('Alice', 100);
addScore('Bob', 200);
const leaderboard = getLeaderboard();
console.log('Leaderboard after adding users in various orders:', leaderboard);
expect(leaderboard).toEqual([
{ name: 'Bob', score: 200 },
{ name: 'Charlie', score: 150 },
{ name: 'Alice', score: 100 },
]);
});
test('should reset the leaderboard correctly after adding scores', () => {
addScore('Alice', 100);
console.log('Leaderboard before reset:', getLeaderboard());
resetLeaderboard();
const leaderboard = getLeaderboard();
console.log('Leaderboard after reset:', leaderboard);
expect(leaderboard).toEqual([]);
});
});
});