-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
144 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { WidgetMapData } from "@shared/dto/widgets/base-widget-data.interface"; | ||
|
||
type MapScale = 1 | 2 | 3 | 4 | 5; | ||
|
||
interface MapData { | ||
[key: string]: MapScale; | ||
} | ||
|
||
const FILL_MAP = { | ||
1: "fill-map-1", | ||
2: "fill-map-2", | ||
3: "fill-map-3", | ||
4: "fill-map-4", | ||
5: "fill-map-5", | ||
} as const; | ||
|
||
const BG_MAP = { | ||
1: "bg-map-1", | ||
2: "bg-map-2", | ||
3: "bg-map-3", | ||
4: "bg-map-4", | ||
5: "bg-map-5", | ||
} as const; | ||
|
||
const getScaleFromPercentage = (percentage: number): MapScale => { | ||
if (percentage <= 20) return 1; | ||
if (percentage <= 40) return 2; | ||
if (percentage <= 60) return 3; | ||
if (percentage <= 80) return 4; | ||
return 5; | ||
}; | ||
|
||
const transformMapData = (data: WidgetMapData): MapData => { | ||
return data.reduce( | ||
(acc, { country, value }) => ({ | ||
...acc, | ||
[country]: getScaleFromPercentage(value), | ||
}), | ||
{} as MapData, | ||
); | ||
}; | ||
|
||
export { FILL_MAP, BG_MAP, getScaleFromPercentage, transformMapData }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
type Scale = 1 | 2 | 3 | 4 | 5; | ||
// export type MapScale = 1 | 2 | 3 | 4 | 5; | ||
|
||
export interface MapData { | ||
[key: string]: Scale; | ||
} | ||
// export interface MapData { | ||
// [key: string]: Scale; | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
getScaleFromPercentage, | ||
transformMapData, | ||
} from "@/containers/widget/map/map.utils"; | ||
import { describe, it, expect } from "vitest"; | ||
|
||
describe("getScaleFromPercentage", () => { | ||
it("should return 1 for percentages <= 20", () => { | ||
expect(getScaleFromPercentage(0)).toBe(1); | ||
expect(getScaleFromPercentage(10)).toBe(1); | ||
expect(getScaleFromPercentage(20)).toBe(1); | ||
}); | ||
|
||
it("should return 2 for percentages between 21 and 40", () => { | ||
expect(getScaleFromPercentage(21)).toBe(2); | ||
expect(getScaleFromPercentage(30)).toBe(2); | ||
expect(getScaleFromPercentage(40)).toBe(2); | ||
}); | ||
|
||
it("should return 3 for percentages between 41 and 60", () => { | ||
expect(getScaleFromPercentage(41)).toBe(3); | ||
expect(getScaleFromPercentage(50)).toBe(3); | ||
expect(getScaleFromPercentage(60)).toBe(3); | ||
}); | ||
|
||
it("should return 4 for percentages between 61 and 80", () => { | ||
expect(getScaleFromPercentage(61)).toBe(4); | ||
expect(getScaleFromPercentage(70)).toBe(4); | ||
expect(getScaleFromPercentage(80)).toBe(4); | ||
}); | ||
|
||
it("should return 5 for percentages > 80", () => { | ||
expect(getScaleFromPercentage(81)).toBe(5); | ||
expect(getScaleFromPercentage(90)).toBe(5); | ||
expect(getScaleFromPercentage(100)).toBe(5); | ||
}); | ||
}); | ||
|
||
describe("transformMapData", () => { | ||
it("should transform empty array to empty object", () => { | ||
expect(transformMapData([])).toEqual({}); | ||
}); | ||
|
||
it("should transform single country data correctly", () => { | ||
const input = [{ country: "USA", value: 75 }]; | ||
expect(transformMapData(input)).toEqual({ USA: 4 }); | ||
}); | ||
|
||
it("should transform multiple countries data correctly", () => { | ||
const input = [ | ||
{ country: "BEL", value: 85 }, | ||
{ country: "NED", value: 45 }, | ||
{ country: "ESP", value: 15 }, | ||
]; | ||
expect(transformMapData(input)).toEqual({ | ||
BEL: 5, | ||
NED: 3, | ||
ESP: 1, | ||
}); | ||
}); | ||
|
||
it("should handle boundary values correctly", () => { | ||
const input = [ | ||
{ country: "A", value: 0 }, | ||
{ country: "B", value: 20 }, | ||
{ country: "C", value: 40 }, | ||
{ country: "D", value: 60 }, | ||
{ country: "E", value: 80 }, | ||
{ country: "F", value: 100 }, | ||
]; | ||
expect(transformMapData(input)).toEqual({ | ||
A: 1, | ||
B: 1, | ||
C: 2, | ||
D: 3, | ||
E: 4, | ||
F: 5, | ||
}); | ||
}); | ||
}); |