1
0
Fork 0
fahrPool/web/lib/auftraege/validate-fahrt.test.ts

86 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { describe, expect, it } from "vitest";
import { validateFahrtBeginn, validateFahrtEnde, validateStartFin } from "./validate-fahrt";
describe("validateFahrtBeginn", () => {
it("akzeptiert einen gueltigen KM-Stand", () => {
const result = validateFahrtBeginn({ startKm: "12345" });
expect(result).toEqual({ valid: true, startKm: 12345 });
});
it("lehnt einen fehlenden KM-Stand ab", () => {
const result = validateFahrtBeginn({ startKm: "" });
expect(result).toEqual({ valid: false, error: "Bitte einen KM-Stand eingeben." });
});
it("lehnt einen negativen KM-Stand ab", () => {
const result = validateFahrtBeginn({ startKm: "-5" });
expect(result).toEqual({ valid: false, error: "Der KM-Stand darf nicht negativ sein." });
});
});
describe("validateFahrtEnde", () => {
it("akzeptiert einen End-KM-Stand groesser als der Start-KM-Stand", () => {
const result = validateFahrtEnde({ endKm: "12500", startKm: 12345 });
expect(result).toEqual({ valid: true, endKm: 12500 });
});
it("lehnt einen fehlenden End-KM-Stand ab", () => {
const result = validateFahrtEnde({ endKm: "", startKm: 12345 });
expect(result).toEqual({ valid: false, error: "Bitte einen End-KM-Stand eingeben." });
});
it("lehnt einen End-KM-Stand kleiner als der Start-KM-Stand ab", () => {
const result = validateFahrtEnde({ endKm: "100", startKm: 12345 });
expect(result).toEqual({
valid: false,
error: "Der End-KM-Stand darf nicht kleiner als der Start-KM-Stand (12345) sein.",
});
});
});
describe("validateStartFin", () => {
const hinterlegt = "WDB9066331S123456";
it("ohne Eingabe: kein Abgleich", () => {
expect(validateStartFin({ startFin: "", hinterlegteFin: hinterlegt, abweichungBestaetigt: null })).toEqual({
valid: true,
fin: null,
});
});
it("normalisiert und akzeptiert gleiche FIN", () => {
expect(
validateStartFin({ startFin: " wdb9066331s123456 ", hinterlegteFin: hinterlegt, abweichungBestaetigt: null })
).toEqual({ valid: true, fin: "WDB9066331S123456" });
});
it("akzeptiert FIN, wenn keine hinterlegt ist", () => {
expect(validateStartFin({ startFin: hinterlegt, hinterlegteFin: null, abweichungBestaetigt: null })).toEqual({
valid: true,
fin: hinterlegt,
});
});
it("lehnt ungueltige FIN ab", () => {
expect(validateStartFin({ startFin: "ABC", hinterlegteFin: null, abweichungBestaetigt: null })).toEqual({
valid: false,
error: "Die FIN muss 17 Zeichen lang sein (A–Z, 0–9, ohne I, O, Q).",
});
});
it("verlangt Bestaetigung bei Abweichung", () => {
expect(
validateStartFin({ startFin: "WVWZZZ1KZAW999999", hinterlegteFin: hinterlegt, abweichungBestaetigt: null })
).toEqual({
valid: false,
error: "Die FIN weicht von der hinterlegten FIN ab – bitte die Abweichung bestätigen.",
});
});
it("akzeptiert bestaetigte Abweichung", () => {
expect(
validateStartFin({ startFin: "WVWZZZ1KZAW999999", hinterlegteFin: hinterlegt, abweichungBestaetigt: "on" })
).toEqual({ valid: true, fin: "WVWZZZ1KZAW999999" });
});
});