mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-04 00:30:27 +00:00
refactor: implement factories for testing
This commit is contained in:
parent
a6e3989aee
commit
acd91f9fb0
4 changed files with 41 additions and 37 deletions
32
frontend/lib/api/__test__/factories/index.ts
Normal file
32
frontend/lib/api/__test__/factories/index.ts
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import { faker } from "@faker-js/faker";
|
||||||
|
import { LabelCreate, LocationCreate, UserRegistration } from "../../types/data-contracts";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a random user registration object that can be
|
||||||
|
* used to signup a new user.
|
||||||
|
*/
|
||||||
|
function user(): UserRegistration {
|
||||||
|
return {
|
||||||
|
email: faker.internet.email(),
|
||||||
|
password: faker.internet.password(),
|
||||||
|
name: faker.name.firstName(),
|
||||||
|
token: "",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function location(): LocationCreate {
|
||||||
|
return {
|
||||||
|
name: faker.address.city(),
|
||||||
|
description: faker.lorem.sentence(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function label(): LabelCreate {
|
||||||
|
return {
|
||||||
|
name: faker.lorem.word(),
|
||||||
|
description: faker.lorem.sentence(),
|
||||||
|
color: faker.internet.color(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const factories = { user, location, label };
|
|
@ -1,17 +1,7 @@
|
||||||
import { describe, test, expect } from "vitest";
|
import { describe, test, expect } from "vitest";
|
||||||
import { faker } from "@faker-js/faker";
|
import { factories } from "./factories";
|
||||||
import { UserRegistration } from "../types/data-contracts";
|
|
||||||
import { client, sharedUserClient, userClient } from "./test-utils";
|
import { client, sharedUserClient, userClient } from "./test-utils";
|
||||||
|
|
||||||
function userFactory(): UserRegistration {
|
|
||||||
return {
|
|
||||||
email: faker.internet.email(),
|
|
||||||
password: faker.internet.password(),
|
|
||||||
name: faker.name.firstName(),
|
|
||||||
token: "",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
describe("[GET] /api/v1/status", () => {
|
describe("[GET] /api/v1/status", () => {
|
||||||
test("server should respond", async () => {
|
test("server should respond", async () => {
|
||||||
const api = client();
|
const api = client();
|
||||||
|
@ -23,7 +13,7 @@ describe("[GET] /api/v1/status", () => {
|
||||||
|
|
||||||
describe("first time user workflow (register, login, join group)", () => {
|
describe("first time user workflow (register, login, join group)", () => {
|
||||||
const api = client();
|
const api = client();
|
||||||
const userData = userFactory();
|
const userData = factories.user();
|
||||||
|
|
||||||
test("user should be able to register", async () => {
|
test("user should be able to register", async () => {
|
||||||
const { response } = await api.register(userData);
|
const { response } = await api.register(userData);
|
||||||
|
@ -59,7 +49,7 @@ describe("first time user workflow (register, login, join group)", () => {
|
||||||
|
|
||||||
// Create User 2 with token
|
// Create User 2 with token
|
||||||
|
|
||||||
const duplicateUser = userFactory();
|
const duplicateUser = factories.user();
|
||||||
duplicateUser.token = data.token;
|
duplicateUser.token = data.token;
|
||||||
|
|
||||||
const { response: registerResp } = await api.register(duplicateUser);
|
const { response: registerResp } = await api.register(duplicateUser);
|
||||||
|
|
|
@ -1,23 +1,17 @@
|
||||||
import { describe, expect, test } from "vitest";
|
import { describe, expect, test } from "vitest";
|
||||||
import { LabelOut } from "../../types/data-contracts";
|
import { LabelOut } from "../../types/data-contracts";
|
||||||
import { UserClient } from "../../user";
|
import { UserClient } from "../../user";
|
||||||
|
import { factories } from "../factories";
|
||||||
import { sharedUserClient } from "../test-utils";
|
import { sharedUserClient } from "../test-utils";
|
||||||
|
|
||||||
describe("locations lifecycle (create, update, delete)", () => {
|
describe("locations lifecycle (create, update, delete)", () => {
|
||||||
let increment = 0;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* useLabel sets up a label resource for testing, and returns a function
|
* useLabel sets up a label resource for testing, and returns a function
|
||||||
* that can be used to delete the label from the backend server.
|
* that can be used to delete the label from the backend server.
|
||||||
*/
|
*/
|
||||||
async function useLabel(api: UserClient): Promise<[LabelOut, () => Promise<void>]> {
|
async function useLabel(api: UserClient): Promise<[LabelOut, () => Promise<void>]> {
|
||||||
const { response, data } = await api.labels.create({
|
const { response, data } = await api.labels.create(factories.label());
|
||||||
name: `__test__.label.name_${increment}`,
|
|
||||||
description: `__test__.label.description_${increment}`,
|
|
||||||
color: "",
|
|
||||||
});
|
|
||||||
expect(response.status).toBe(201);
|
expect(response.status).toBe(201);
|
||||||
increment++;
|
|
||||||
|
|
||||||
const cleanup = async () => {
|
const cleanup = async () => {
|
||||||
const { response } = await api.labels.delete(data.id);
|
const { response } = await api.labels.delete(data.id);
|
||||||
|
@ -29,11 +23,7 @@ describe("locations lifecycle (create, update, delete)", () => {
|
||||||
test("user should be able to create a label", async () => {
|
test("user should be able to create a label", async () => {
|
||||||
const api = await sharedUserClient();
|
const api = await sharedUserClient();
|
||||||
|
|
||||||
const labelData = {
|
const labelData = factories.label();
|
||||||
name: "test-label",
|
|
||||||
description: "test-description",
|
|
||||||
color: "",
|
|
||||||
};
|
|
||||||
|
|
||||||
const { response, data } = await api.labels.create(labelData);
|
const { response, data } = await api.labels.create(labelData);
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,17 @@
|
||||||
import { describe, expect, test } from "vitest";
|
import { describe, expect, test } from "vitest";
|
||||||
import { LocationOut } from "../../types/data-contracts";
|
import { LocationOut } from "../../types/data-contracts";
|
||||||
import { UserClient } from "../../user";
|
import { UserClient } from "../../user";
|
||||||
|
import { factories } from "../factories";
|
||||||
import { sharedUserClient } from "../test-utils";
|
import { sharedUserClient } from "../test-utils";
|
||||||
|
|
||||||
describe("locations lifecycle (create, update, delete)", () => {
|
describe("locations lifecycle (create, update, delete)", () => {
|
||||||
let increment = 0;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* useLocatio sets up a location resource for testing, and returns a function
|
* useLocatio sets up a location resource for testing, and returns a function
|
||||||
* that can be used to delete the location from the backend server.
|
* that can be used to delete the location from the backend server.
|
||||||
*/
|
*/
|
||||||
async function useLocation(api: UserClient): Promise<[LocationOut, () => Promise<void>]> {
|
async function useLocation(api: UserClient): Promise<[LocationOut, () => Promise<void>]> {
|
||||||
const { response, data } = await api.locations.create({
|
const { response, data } = await api.locations.create(factories.location());
|
||||||
name: `__test__.location.name_${increment}`,
|
|
||||||
description: `__test__.location.description_${increment}`,
|
|
||||||
});
|
|
||||||
expect(response.status).toBe(201);
|
expect(response.status).toBe(201);
|
||||||
increment++;
|
|
||||||
|
|
||||||
const cleanup = async () => {
|
const cleanup = async () => {
|
||||||
const { response } = await api.locations.delete(data.id);
|
const { response } = await api.locations.delete(data.id);
|
||||||
|
@ -29,10 +24,7 @@ describe("locations lifecycle (create, update, delete)", () => {
|
||||||
test("user should be able to create a location", async () => {
|
test("user should be able to create a location", async () => {
|
||||||
const api = await sharedUserClient();
|
const api = await sharedUserClient();
|
||||||
|
|
||||||
const locationData = {
|
const locationData = factories.location();
|
||||||
name: "test-location",
|
|
||||||
description: "test-description",
|
|
||||||
};
|
|
||||||
|
|
||||||
const { response, data } = await api.locations.create(locationData);
|
const { response, data } = await api.locations.create(locationData);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue