Something went wrong. Try again.
Tea journaling on ATProto (alpha) forked from arabica.social/arabica
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697import { describe, expect, it } from "vitest";import { detectTemperatureUnit, formatTemperature, normalizeTemperatureUnit, temperatureInputValue, temperatureStorageValue,} from "../../lib/format";
describe("formatTemperature", () => { it("labels a recorded number by its magnitude, as the Go helpers do", () => { expect(formatTemperature(85)).toBe("85°C"); expect(formatTemperature(93.5)).toBe("94°C"); expect(formatTemperature(100)).toBe("100°C"); expect(formatTemperature(200)).toBe("200°F"); expect(formatTemperature(200, "recorded")).toBe("200°F"); expect(formatTemperature(200, null)).toBe("200°F"); expect(formatTemperature(200, "")).toBe("200°F"); expect(formatTemperature(200, "kelvin")).toBe("200°F"); });
it("keeps a missing temperature empty", () => { expect(formatTemperature(0)).toBe(""); expect(formatTemperature(undefined)).toBe(""); expect(formatTemperature(null, "fahrenheit")).toBe(""); });
it("converts recorded values into the preferred unit", () => { expect(formatTemperature(200, "fahrenheit")).toBe("200°F"); expect(formatTemperature(200, "celsius")).toBe("93°C"); expect(formatTemperature(85, "fahrenheit")).toBe("185°F"); expect(formatTemperature(85, "celsius")).toBe("85°C"); expect(formatTemperature(93.5, "fahrenheit")).toBe("200°F"); });});
describe("temperature units", () => { it("detects the recorded unit from the magnitude", () => { expect(detectTemperatureUnit(0)).toBe("celsius"); expect(detectTemperatureUnit(100)).toBe("celsius"); expect(detectTemperatureUnit(100.5)).toBe("fahrenheit"); expect(detectTemperatureUnit(200)).toBe("fahrenheit"); });
it("normalizes unknown preferences to recorded", () => { expect(normalizeTemperatureUnit("celsius")).toBe("celsius"); expect(normalizeTemperatureUnit("fahrenheit")).toBe("fahrenheit"); expect(normalizeTemperatureUnit("recorded")).toBe("recorded"); expect(normalizeTemperatureUnit(undefined)).toBe("recorded"); expect(normalizeTemperatureUnit(null)).toBe("recorded"); expect(normalizeTemperatureUnit("kelvin")).toBe("recorded"); });
it("shows a stored value in the input unit", () => { expect(temperatureInputValue(200, "recorded")).toBe(200); expect(temperatureInputValue(200, "fahrenheit")).toBe(200); expect(temperatureInputValue(200, "celsius")).toBe(93.3); expect(temperatureInputValue(85, "celsius")).toBe(85); expect(temperatureInputValue(85, "fahrenheit")).toBe(185); expect(temperatureInputValue(0, "fahrenheit")).toBe(0); expect(temperatureInputValue(undefined, "fahrenheit")).toBe(0); });
it("stores a typed value without changing its recorded meaning", () => { expect(temperatureStorageValue(200, "recorded")).toBe(200); expect(temperatureStorageValue(90, "recorded")).toBe(90); expect(temperatureStorageValue(200, "fahrenheit")).toBe(200); expect(temperatureStorageValue(85, "celsius")).toBe(85); expect(temperatureStorageValue(120, "celsius")).toBe(248); expect(temperatureStorageValue(90, "fahrenheit")).toBe(32.2); expect(temperatureStorageValue(0, "celsius")).toBe(0); expect(temperatureStorageValue(Number(""), "celsius")).toBe(0); expect(temperatureStorageValue(Number("warm"), "celsius")).toBe(0); });
it("round trips a typed value through storage and back into the input", () => { for (const value of [200, 212, 140]) { const stored = temperatureStorageValue(value, "fahrenheit"); expect(temperatureInputValue(stored, "fahrenheit")).toBe(value); expect(formatTemperature(stored, "fahrenheit")).toBe(value + "°F"); } for (const value of [85, 93.5, 100]) { const stored = temperatureStorageValue(value, "celsius"); expect(temperatureInputValue(stored, "celsius")).toBe(value); expect(formatTemperature(stored, "celsius")).toBe( Math.round(value) + "°C", ); } expect( temperatureInputValue( temperatureStorageValue(90, "fahrenheit"), "fahrenheit", ), ).toBe(90); });});