2023-07-15 18:55:03 +02:00
|
|
|
import { Unit } from "../../src/Models/Unit"
|
|
|
|
import { Denomination } from "../../src/Models/Denomination"
|
2023-02-03 04:48:32 +01:00
|
|
|
import { describe, expect, it } from "vitest"
|
2024-04-28 23:04:09 +02:00
|
|
|
import Validators from "../../src/UI/InputElement/Validators"
|
2022-03-15 01:53:08 +01:00
|
|
|
|
|
|
|
describe("Unit", () => {
|
|
|
|
it("should convert a value back and forth", () => {
|
2023-12-12 03:46:51 +01:00
|
|
|
const denomintion = Denomination.fromJson(
|
2022-08-18 19:17:15 +02:00
|
|
|
{
|
2022-03-15 01:53:08 +01:00
|
|
|
canonicalDenomination: "MW",
|
|
|
|
alternativeDenomination: ["megawatts", "megawatt"],
|
|
|
|
human: {
|
2023-12-12 03:46:51 +01:00
|
|
|
en: "{quantity} megawatts",
|
|
|
|
nl: "{quantity} megawatt",
|
2022-03-15 01:53:08 +01:00
|
|
|
},
|
|
|
|
},
|
2024-04-28 23:04:09 +02:00
|
|
|
Validators.get("float"),
|
2022-03-15 01:53:08 +01:00
|
|
|
"test"
|
2022-09-08 21:40:48 +02:00
|
|
|
)
|
2022-03-15 01:53:08 +01:00
|
|
|
|
2024-04-28 23:04:09 +02:00
|
|
|
const canonical = denomintion.canonicalValue("5", true, false)
|
2023-02-03 04:48:32 +01:00
|
|
|
expect(canonical).toBe("5 MW")
|
2024-04-28 23:04:09 +02:00
|
|
|
const units = new Unit("quantity", ["key"], [denomintion], false, Validators.get("float"))
|
2022-08-18 19:17:15 +02:00
|
|
|
const [detected, detectedDenom] = units.findDenomination("5 MW", () => "be")
|
2023-02-03 04:48:32 +01:00
|
|
|
expect(detected).toBe("5")
|
|
|
|
expect(detectedDenom).toBe(denomintion)
|
2022-03-15 01:53:08 +01:00
|
|
|
})
|
2024-04-28 23:04:09 +02:00
|
|
|
|
|
|
|
it("should convert an inverted value back and forth", () => {
|
|
|
|
const denomintion = Denomination.fromJson(
|
|
|
|
{
|
|
|
|
canonicalDenomination: "year",
|
|
|
|
human: {
|
|
|
|
en: "{quantity} year",
|
|
|
|
nl: "{quantity} year",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Validators.get("float"),
|
|
|
|
"test"
|
|
|
|
)
|
|
|
|
|
|
|
|
const canonical = denomintion.canonicalValue("5", true, true)
|
|
|
|
expect(canonical).toBe("5/year")
|
2024-06-16 16:06:26 +02:00
|
|
|
const unit = new Unit(
|
|
|
|
"quantity",
|
|
|
|
["key"],
|
|
|
|
[denomintion],
|
|
|
|
false,
|
|
|
|
Validators.get("float"),
|
|
|
|
true
|
|
|
|
)
|
2024-04-28 23:04:09 +02:00
|
|
|
const [detected, detectedDenom] = unit.findDenomination("5/year", () => "be")
|
|
|
|
expect(detected).toBe("5")
|
|
|
|
expect(detectedDenom).toBe(denomintion)
|
|
|
|
})
|
2022-03-15 01:53:08 +01:00
|
|
|
})
|