Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/app/Ranking.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ export class Ranking {
}

getScores() {
return JSON.parse(localStorage.getItem(this.mode)) ?? [];
const results = JSON.parse(localStorage.getItem(this.mode));
return (results ?? []).sort(
(a, b) => b.score / b.maxScore - a.score / a.maxScore,
);
}
}
22 changes: 20 additions & 2 deletions test/ranking.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ describe("Ranking's logic", () => {
},
];

const sortedResults = [
{
user: 'user3',
score: 15,
maxScore: 25, // 60%
},
{
user: 'user1',
score: 15,
maxScore: 30, // 50%
},
{
user: 'user2',
score: 18,
maxScore: 40, // 45%
},
];

const expectedResultsWhenOnlyOne = [
{
user: 'user',
Expand Down Expand Up @@ -115,11 +133,11 @@ describe("Ranking's logic", () => {
expect(JSON.parse(localStorage.getItem('starships'))).toEqual(results);
});

it('Ranking saved in local storage should be returned', () => {
it('Ranking saved in local storage should be returned in order', () => {
const starshipsRanking = new Ranking('starships');
localStorage.setItem('starships', JSON.stringify(results));

expect(starshipsRanking.getScores()).toEqual(results);
expect(starshipsRanking.getScores()).toEqual(sortedResults);
});

it('When no score is saved in local storage then return an empty array', () => {
Expand Down