2022-10-16 05:41:27 +00:00
|
|
|
import { faker } from "@faker-js/faker";
|
2022-09-24 19:33:38 +00:00
|
|
|
import { describe, test, expect } from "vitest";
|
2024-03-01 17:21:45 +00:00
|
|
|
import type { ItemField, ItemUpdate, LocationOut } from "../../types/data-contracts";
|
2022-09-24 19:33:38 +00:00
|
|
|
import { AttachmentTypes } from "../../types/non-generated";
|
2024-03-01 17:21:45 +00:00
|
|
|
import type { UserClient } from "../../user";
|
2022-10-16 05:41:27 +00:00
|
|
|
import { factories } from "../factories";
|
2022-09-24 19:33:38 +00:00
|
|
|
import { sharedUserClient } from "../test-utils";
|
|
|
|
|
|
|
|
describe("user should be able to create an item and add an attachment", () => {
|
|
|
|
let increment = 0;
|
|
|
|
/**
|
2024-01-18 19:45:42 +00:00
|
|
|
* useLocation sets up a location resource for testing, and returns a function
|
2022-09-24 19:33:38 +00:00
|
|
|
* that can be used to delete the location from the backend server.
|
|
|
|
*/
|
2022-10-07 02:54:09 +00:00
|
|
|
async function useLocation(api: UserClient): Promise<[LocationOut, () => Promise<void>]> {
|
2022-09-24 19:33:38 +00:00
|
|
|
const { response, data } = await api.locations.create({
|
2023-02-18 06:41:01 +00:00
|
|
|
parentId: null,
|
2022-09-24 19:33:38 +00:00
|
|
|
name: `__test__.location.name_${increment}`,
|
|
|
|
description: `__test__.location.description_${increment}`,
|
|
|
|
});
|
|
|
|
expect(response.status).toBe(201);
|
|
|
|
increment++;
|
|
|
|
|
|
|
|
const cleanup = async () => {
|
|
|
|
const { response } = await api.locations.delete(data.id);
|
|
|
|
expect(response.status).toBe(204);
|
|
|
|
};
|
|
|
|
|
|
|
|
return [data, cleanup];
|
|
|
|
}
|
|
|
|
|
|
|
|
test("user should be able to create an item and add an attachment", async () => {
|
|
|
|
const api = await sharedUserClient();
|
|
|
|
const [location, cleanup] = await useLocation(api);
|
|
|
|
|
|
|
|
const { response, data: item } = await api.items.create({
|
2022-12-10 05:57:57 +00:00
|
|
|
parentId: null,
|
2022-09-24 19:33:38 +00:00
|
|
|
name: "test-item",
|
|
|
|
labelIds: [],
|
|
|
|
description: "test-description",
|
|
|
|
locationId: location.id,
|
|
|
|
});
|
|
|
|
expect(response.status).toBe(201);
|
|
|
|
|
|
|
|
// Add attachment
|
|
|
|
{
|
|
|
|
const testFile = new Blob(["test"], { type: "text/plain" });
|
2022-12-10 05:57:57 +00:00
|
|
|
const { response } = await api.items.attachments.add(item.id, testFile, "test.txt", AttachmentTypes.Attachment);
|
2022-09-24 19:33:38 +00:00
|
|
|
expect(response.status).toBe(201);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Attachment
|
|
|
|
const { response: itmResp, data } = await api.items.get(item.id);
|
|
|
|
expect(itmResp.status).toBe(200);
|
|
|
|
|
|
|
|
expect(data.attachments).toHaveLength(1);
|
|
|
|
expect(data.attachments[0].document.title).toBe("test.txt");
|
|
|
|
|
2022-12-10 05:57:57 +00:00
|
|
|
const resp = await api.items.attachments.delete(data.id, data.attachments[0].id);
|
2022-09-24 19:33:38 +00:00
|
|
|
expect(resp.response.status).toBe(204);
|
|
|
|
|
|
|
|
api.items.delete(item.id);
|
|
|
|
await cleanup();
|
|
|
|
});
|
2022-10-16 05:41:27 +00:00
|
|
|
|
|
|
|
test("user should be able to create and delete fields on an item", async () => {
|
|
|
|
const api = await sharedUserClient();
|
|
|
|
const [location, cleanup] = await useLocation(api);
|
|
|
|
|
|
|
|
const { response, data: item } = await api.items.create({
|
2022-12-10 05:57:57 +00:00
|
|
|
parentId: null,
|
2022-10-16 05:41:27 +00:00
|
|
|
name: faker.vehicle.model(),
|
|
|
|
labelIds: [],
|
|
|
|
description: faker.lorem.paragraph(1),
|
|
|
|
locationId: location.id,
|
|
|
|
});
|
|
|
|
expect(response.status).toBe(201);
|
|
|
|
|
|
|
|
const fields: ItemField[] = [
|
|
|
|
factories.itemField(),
|
|
|
|
factories.itemField(),
|
|
|
|
factories.itemField(),
|
|
|
|
factories.itemField(),
|
|
|
|
];
|
|
|
|
|
|
|
|
// Add fields
|
|
|
|
const itemUpdate = {
|
2022-12-10 05:57:57 +00:00
|
|
|
parentId: null,
|
2022-10-16 05:41:27 +00:00
|
|
|
...item,
|
2023-02-18 06:41:01 +00:00
|
|
|
locationId: item.location?.id || null,
|
2022-10-16 05:41:27 +00:00
|
|
|
labelIds: item.labels.map(l => l.id),
|
|
|
|
fields,
|
|
|
|
};
|
|
|
|
|
2023-02-18 06:41:01 +00:00
|
|
|
const { response: updateResponse, data: item2 } = await api.items.update(item.id, itemUpdate as ItemUpdate);
|
2022-10-16 05:41:27 +00:00
|
|
|
expect(updateResponse.status).toBe(200);
|
|
|
|
|
|
|
|
expect(item2.fields).toHaveLength(fields.length);
|
|
|
|
|
|
|
|
for (let i = 0; i < fields.length; i++) {
|
|
|
|
expect(item2.fields[i].name).toBe(fields[i].name);
|
|
|
|
expect(item2.fields[i].textValue).toBe(fields[i].textValue);
|
|
|
|
expect(item2.fields[i].numberValue).toBe(fields[i].numberValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
itemUpdate.fields = [fields[0], fields[1]];
|
|
|
|
|
2023-02-18 06:41:01 +00:00
|
|
|
const { response: updateResponse2, data: item3 } = await api.items.update(item.id, itemUpdate as ItemUpdate);
|
2022-10-16 05:41:27 +00:00
|
|
|
expect(updateResponse2.status).toBe(200);
|
|
|
|
|
|
|
|
expect(item3.fields).toHaveLength(2);
|
|
|
|
for (let i = 0; i < item3.fields.length; i++) {
|
|
|
|
expect(item3.fields[i].name).toBe(itemUpdate.fields[i].name);
|
|
|
|
expect(item3.fields[i].textValue).toBe(itemUpdate.fields[i].textValue);
|
|
|
|
expect(item3.fields[i].numberValue).toBe(itemUpdate.fields[i].numberValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
});
|
2022-12-10 05:57:57 +00:00
|
|
|
|
|
|
|
test("users should be able to create and few maintenance logs for an item", async () => {
|
|
|
|
const api = await sharedUserClient();
|
|
|
|
const [location, cleanup] = await useLocation(api);
|
|
|
|
const { response, data: item } = await api.items.create({
|
|
|
|
parentId: null,
|
|
|
|
name: faker.vehicle.model(),
|
|
|
|
labelIds: [],
|
|
|
|
description: faker.lorem.paragraph(1),
|
|
|
|
locationId: location.id,
|
|
|
|
});
|
|
|
|
expect(response.status).toBe(201);
|
|
|
|
|
|
|
|
const maintenanceEntries = [];
|
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
const { response, data } = await api.items.maintenance.create(item.id, {
|
|
|
|
name: faker.vehicle.model(),
|
|
|
|
description: faker.lorem.paragraph(1),
|
2023-11-16 02:19:51 +00:00
|
|
|
completedDate: faker.date.past(),
|
2023-02-27 03:42:23 +00:00
|
|
|
scheduledDate: "null",
|
2023-11-16 02:19:51 +00:00
|
|
|
cost: faker.number.int(100).toString(),
|
2022-12-10 05:57:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(response.status).toBe(201);
|
|
|
|
maintenanceEntries.push(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log
|
|
|
|
{
|
|
|
|
const { response, data } = await api.items.maintenance.getLog(item.id);
|
|
|
|
expect(response.status).toBe(200);
|
|
|
|
expect(data.entries).toHaveLength(maintenanceEntries.length);
|
|
|
|
expect(data.costAverage).toBeGreaterThan(0);
|
|
|
|
expect(data.costTotal).toBeGreaterThan(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
});
|
2024-02-25 22:04:24 +00:00
|
|
|
|
|
|
|
test("full path of item should be retrievable", async () => {
|
|
|
|
const api = await sharedUserClient();
|
|
|
|
const [location, cleanup] = await useLocation(api);
|
|
|
|
|
|
|
|
const locations = [location.name, faker.animal.dog(), faker.animal.cat(), faker.animal.cow(), faker.animal.bear()];
|
|
|
|
|
|
|
|
let lastLocationId = location.id;
|
|
|
|
for (let i = 1; i < locations.length; i++) {
|
|
|
|
// Skip first one
|
|
|
|
const { response, data: loc } = await api.locations.create({
|
|
|
|
parentId: lastLocationId,
|
|
|
|
name: locations[i],
|
|
|
|
description: "",
|
|
|
|
});
|
|
|
|
expect(response.status).toBe(201);
|
|
|
|
|
|
|
|
lastLocationId = loc.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { response, data: item } = await api.items.create({
|
|
|
|
name: faker.vehicle.model(),
|
|
|
|
labelIds: [],
|
|
|
|
description: faker.lorem.paragraph(1),
|
|
|
|
locationId: lastLocationId,
|
|
|
|
});
|
|
|
|
expect(response.status).toBe(201);
|
|
|
|
|
|
|
|
const { response: pathResponse, data: fullpath } = await api.items.fullpath(item.id);
|
|
|
|
expect(pathResponse.status).toBe(200);
|
|
|
|
|
|
|
|
const names = fullpath.map(p => p.name);
|
|
|
|
|
|
|
|
expect(names).toHaveLength(locations.length + 1);
|
|
|
|
expect(names).toEqual([...locations, item.name]);
|
|
|
|
|
|
|
|
cleanup();
|
|
|
|
});
|
2022-09-24 19:33:38 +00:00
|
|
|
});
|