- Introduced `.prettierrc.json` to enforce consistent code formatting across the project. - Updated unit and e2e test files to address formatting issues, improve readability, and ensure alignment with the new Prettier configuration.
165 lines
5.0 KiB
JavaScript
165 lines
5.0 KiB
JavaScript
import { describe, expect, it } from "vitest";
|
|
import {
|
|
aggregateDepartmentVisits,
|
|
aggregateOrderFrequency,
|
|
aggregateOrderItemFrequency,
|
|
buildOverviewStats,
|
|
buildUsageLogQuery,
|
|
buildVehiclesQuery,
|
|
normalizeListResponse,
|
|
normalizePaginationMeta,
|
|
normalizeRegistrationNumber,
|
|
normalizeRelatedOrders,
|
|
toIsoDateTimeWithoutTimezone,
|
|
uniqueCustomerIdsFromUsageLogs,
|
|
} from "@/views/dashboards/superUserDashboard/vehicle/imports/vehicleViewUtils.js";
|
|
|
|
describe("vehicle view utility query builders", () => {
|
|
it("builds usage log query with OpenAPI pagination params and optional filters", () => {
|
|
const date = new Date(2026, 1, 14, 9, 30, 15, 123);
|
|
const result = buildUsageLogQuery({
|
|
dateFrom: date,
|
|
regNr: "AB12345",
|
|
customerId: "5001",
|
|
vehicleId: "vehicle-guid",
|
|
page: 3,
|
|
perPage: 55,
|
|
});
|
|
|
|
expect(result.page).toBe(3);
|
|
expect(result.per_page).toBe(55);
|
|
expect(result.perPage).toBe(55);
|
|
expect(result.regNr).toBe("AB12345");
|
|
expect(result.customerId).toBe("5001");
|
|
expect(result.vehicleId).toBe("vehicle-guid");
|
|
expect(result.dateFrom).toBe("2026-02-14T09:30:15.123");
|
|
});
|
|
|
|
it("builds vehicles query with reg/customer/id and OpenAPI pagination params", () => {
|
|
const result = buildVehiclesQuery({
|
|
id: 7,
|
|
reg: "CD67890",
|
|
customerId: 9999,
|
|
page: 2,
|
|
perPage: 40,
|
|
});
|
|
|
|
expect(result).toEqual({
|
|
id: 7,
|
|
reg: "CD67890",
|
|
customer_id: 9999,
|
|
page: 2,
|
|
per_page: 40,
|
|
perPage: 40,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("vehicle view utility normalization", () => {
|
|
it("normalizes registration number safely and uppercases", () => {
|
|
expect(normalizeRegistrationNumber("ab 123")).toBe("AB 123");
|
|
expect(normalizeRegistrationNumber("AB%20123")).toBe("AB 123");
|
|
expect(normalizeRegistrationNumber(null)).toBe("");
|
|
});
|
|
|
|
it("formats date without timezone in expected precision", () => {
|
|
const value = toIsoDateTimeWithoutTimezone(new Date(2026, 0, 5, 6, 7, 8, 9));
|
|
expect(value).toBe("2026-01-05T06:07:08.009");
|
|
});
|
|
|
|
it("normalizes API list shapes", () => {
|
|
expect(normalizeListResponse({ data: { data: [{ id: 1 }] } })).toEqual([{ id: 1 }]);
|
|
expect(normalizeListResponse({ data: [{ id: 2 }] })).toEqual([{ id: 2 }]);
|
|
expect(normalizeListResponse({ data: { data: { id: 3 } } })).toEqual([{ id: 3 }]);
|
|
expect(normalizeListResponse(null)).toEqual([]);
|
|
});
|
|
|
|
it("normalizes related order maps", () => {
|
|
expect(normalizeRelatedOrders({ data: { data: { washA: [1, 2] } } })).toEqual({ washA: [1, 2] });
|
|
expect(normalizeRelatedOrders({ data: { data: [] } })).toEqual({});
|
|
});
|
|
|
|
it("normalizes pagination metadata used by all-page exports", () => {
|
|
expect(
|
|
normalizePaginationMeta({
|
|
data: { meta: { pagination: { page: 2, per_page: 25, total: 60 } } },
|
|
})
|
|
).toEqual({
|
|
page: 2,
|
|
perPage: 25,
|
|
total: 60,
|
|
totalPages: 3,
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("vehicle view analytics aggregations", () => {
|
|
const usageLogs = [
|
|
{
|
|
WashId: "wash-1",
|
|
StartTime: "2026-03-01T08:00:00.000Z",
|
|
CustomerId: "1001",
|
|
Location: "Hvidovre",
|
|
WashItems: [
|
|
{ OriginalProductName: "Top Wash", Count: 1 },
|
|
{ OriginalProductName: "Spot Free", Count: 2 },
|
|
],
|
|
},
|
|
{
|
|
WashId: "wash-2",
|
|
StartTime: "2026-03-01T09:30:00.000Z",
|
|
CustomerId: "1002",
|
|
Location: "Hvidovre",
|
|
WashItems: [{ OriginalProductName: "Top Wash", Count: 1 }],
|
|
},
|
|
{
|
|
WashId: "wash-3",
|
|
StartTime: "2026-03-02T12:30:00.000Z",
|
|
CustomerId: "1001",
|
|
Location: "Taastrup",
|
|
WashItems: [{ OriginalProductName: "Chassis", Count: 3 }],
|
|
},
|
|
];
|
|
|
|
const relatedOrders = {
|
|
"wash-1": [5001],
|
|
"wash-2": [5002, 5003],
|
|
"wash-3": [],
|
|
};
|
|
|
|
it("aggregates order frequency by day", () => {
|
|
const result = aggregateOrderFrequency(usageLogs, relatedOrders);
|
|
expect(result).toEqual([{ label: "2026-03-01", value: 3 }]);
|
|
});
|
|
|
|
it("aggregates order item frequency with ranking", () => {
|
|
const result = aggregateOrderItemFrequency(usageLogs, 5);
|
|
expect(result).toEqual([
|
|
{ label: "Chassis", value: 3 },
|
|
{ label: "Spot Free", value: 2 },
|
|
{ label: "Top Wash", value: 2 },
|
|
]);
|
|
});
|
|
|
|
it("aggregates department visits", () => {
|
|
const result = aggregateDepartmentVisits(usageLogs);
|
|
expect(result).toEqual([
|
|
{ label: "Hvidovre", value: 2 },
|
|
{ label: "Taastrup", value: 1 },
|
|
]);
|
|
});
|
|
|
|
it("extracts related unique customer ids", () => {
|
|
expect(uniqueCustomerIdsFromUsageLogs(usageLogs).sort()).toEqual(["1001", "1002"]);
|
|
});
|
|
|
|
it("builds overview statistics from usage logs and related orders", () => {
|
|
const result = buildOverviewStats(usageLogs, relatedOrders);
|
|
expect(result.totalWashes).toBe(3);
|
|
expect(result.uniqueCustomers).toBe(2);
|
|
expect(result.linkedOrders).toBe(3);
|
|
expect(result.uniqueDepartments).toBe(2);
|
|
expect(result.latestWashAt).toContain("2026-03-02");
|
|
});
|
|
});
|