2022-09-09 22:46:53 +00:00
|
|
|
import { describe, expect, test } from "vitest";
|
2024-03-01 17:21:45 +00:00
|
|
|
import type { LabelOut } from "../../types/data-contracts";
|
|
|
|
import type { UserClient } from "../../user";
|
2022-10-09 17:23:21 +00:00
|
|
|
import { factories } from "../factories";
|
2022-09-09 22:46:53 +00:00
|
|
|
import { sharedUserClient } from "../test-utils";
|
2022-09-05 00:37:37 +00:00
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
describe("locations lifecycle (create, update, delete)", () => {
|
2022-09-05 00:37:37 +00:00
|
|
|
/**
|
|
|
|
* useLabel sets up a label resource for testing, and returns a function
|
|
|
|
* that can be used to delete the label from the backend server.
|
|
|
|
*/
|
2022-10-07 02:54:09 +00:00
|
|
|
async function useLabel(api: UserClient): Promise<[LabelOut, () => Promise<void>]> {
|
2022-10-09 17:23:21 +00:00
|
|
|
const { response, data } = await api.labels.create(factories.label());
|
2022-09-05 00:37:37 +00:00
|
|
|
expect(response.status).toBe(201);
|
|
|
|
|
|
|
|
const cleanup = async () => {
|
|
|
|
const { response } = await api.labels.delete(data.id);
|
|
|
|
expect(response.status).toBe(204);
|
|
|
|
};
|
|
|
|
return [data, cleanup];
|
|
|
|
}
|
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
test("user should be able to create a label", async () => {
|
2022-09-05 00:37:37 +00:00
|
|
|
const api = await sharedUserClient();
|
|
|
|
|
2022-10-09 17:23:21 +00:00
|
|
|
const labelData = factories.label();
|
2022-09-05 00:37:37 +00:00
|
|
|
|
|
|
|
const { response, data } = await api.labels.create(labelData);
|
|
|
|
|
|
|
|
expect(response.status).toBe(201);
|
|
|
|
expect(data.id).toBeTruthy();
|
|
|
|
|
|
|
|
// Ensure we can get the label
|
|
|
|
const { response: getResponse, data: getData } = await api.labels.get(data.id);
|
|
|
|
|
|
|
|
expect(getResponse.status).toBe(200);
|
|
|
|
expect(getData.id).toBe(data.id);
|
|
|
|
expect(getData.name).toBe(labelData.name);
|
|
|
|
expect(getData.description).toBe(labelData.description);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
const { response: deleteResponse } = await api.labels.delete(data.id);
|
|
|
|
expect(deleteResponse.status).toBe(204);
|
|
|
|
});
|
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
test("user should be able to update a label", async () => {
|
2022-09-05 00:37:37 +00:00
|
|
|
const api = await sharedUserClient();
|
|
|
|
const [label, cleanup] = await useLabel(api);
|
|
|
|
|
|
|
|
const labelData = {
|
2022-09-09 22:46:53 +00:00
|
|
|
name: "test-label",
|
|
|
|
description: "test-description",
|
|
|
|
color: "",
|
2022-09-05 00:37:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const { response, data } = await api.labels.update(label.id, labelData);
|
|
|
|
expect(response.status).toBe(200);
|
|
|
|
expect(data.id).toBe(label.id);
|
|
|
|
|
|
|
|
// Ensure we can get the label
|
|
|
|
const { response: getResponse, data: getData } = await api.labels.get(data.id);
|
|
|
|
expect(getResponse.status).toBe(200);
|
|
|
|
expect(getData.id).toBe(data.id);
|
|
|
|
expect(getData.name).toBe(labelData.name);
|
|
|
|
expect(getData.description).toBe(labelData.description);
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
await cleanup();
|
|
|
|
});
|
|
|
|
|
2022-09-09 22:46:53 +00:00
|
|
|
test("user should be able to delete a label", async () => {
|
2022-09-05 00:37:37 +00:00
|
|
|
const api = await sharedUserClient();
|
|
|
|
const [label, _] = await useLabel(api);
|
|
|
|
|
|
|
|
const { response } = await api.labels.delete(label.id);
|
|
|
|
expect(response.status).toBe(204);
|
|
|
|
|
|
|
|
// Ensure we can't get the label
|
|
|
|
const { response: getResponse } = await api.labels.get(label.id);
|
|
|
|
expect(getResponse.status).toBe(404);
|
|
|
|
});
|
|
|
|
});
|