2022-10-09 17:23:21 +00:00
|
|
|
import { faker } from "@faker-js/faker";
|
|
|
|
import { expect } from "vitest";
|
|
|
|
import { overrideParts } from "../../base/urls";
|
|
|
|
import { PublicApi } from "../../public";
|
2022-10-16 05:41:27 +00:00
|
|
|
import { ItemField, LabelCreate, LocationCreate, UserRegistration } from "../../types/data-contracts";
|
2022-10-09 17:23:21 +00:00
|
|
|
import * as config from "../../../../test/config";
|
|
|
|
import { UserClient } from "../../user";
|
|
|
|
import { Requests } from "../../../requests";
|
|
|
|
|
2022-10-16 05:41:27 +00:00
|
|
|
function itemField(id = null): ItemField {
|
|
|
|
return {
|
2023-02-18 06:41:01 +00:00
|
|
|
// @ts-expect-error
|
2022-10-16 05:41:27 +00:00
|
|
|
id,
|
|
|
|
name: faker.lorem.word(),
|
|
|
|
type: "text",
|
|
|
|
textValue: faker.lorem.sentence(),
|
|
|
|
booleanValue: false,
|
|
|
|
numberValue: faker.datatype.number(),
|
2023-02-18 06:41:01 +00:00
|
|
|
timeValue: "",
|
2022-10-16 05:41:27 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-10-09 17:23:21 +00:00
|
|
|
/**
|
|
|
|
* 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: "",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-18 06:41:01 +00:00
|
|
|
function location(parentId: string | null = null): LocationCreate {
|
2022-10-09 17:23:21 +00:00
|
|
|
return {
|
2023-02-18 06:41:01 +00:00
|
|
|
parentId,
|
2022-10-09 17:23:21 +00:00
|
|
|
name: faker.address.city(),
|
|
|
|
description: faker.lorem.sentence(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function label(): LabelCreate {
|
|
|
|
return {
|
|
|
|
name: faker.lorem.word(),
|
|
|
|
description: faker.lorem.sentence(),
|
|
|
|
color: faker.internet.color(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function publicClient(): PublicApi {
|
|
|
|
overrideParts(config.BASE_URL, "/api/v1");
|
|
|
|
const requests = new Requests("");
|
|
|
|
return new PublicApi(requests);
|
|
|
|
}
|
|
|
|
|
|
|
|
function userClient(token: string): UserClient {
|
|
|
|
overrideParts(config.BASE_URL, "/api/v1");
|
|
|
|
const requests = new Requests("", token);
|
2023-02-18 06:41:01 +00:00
|
|
|
return new UserClient(requests, "");
|
2022-10-09 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TestUser = {
|
|
|
|
client: UserClient;
|
|
|
|
user: UserRegistration;
|
|
|
|
};
|
|
|
|
|
|
|
|
async function userSingleUse(): Promise<TestUser> {
|
|
|
|
const usr = user();
|
|
|
|
|
|
|
|
const pub = publicClient();
|
|
|
|
await pub.register(usr);
|
|
|
|
const result = await pub.login(usr.email, usr.password);
|
|
|
|
|
|
|
|
expect(result.error).toBeFalsy();
|
|
|
|
expect(result.status).toBe(200);
|
|
|
|
|
|
|
|
return {
|
2023-02-18 06:41:01 +00:00
|
|
|
client: new UserClient(new Requests("", result.data.token), result.data.attachmentToken),
|
2022-10-09 17:23:21 +00:00
|
|
|
user: usr,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export const factories = {
|
|
|
|
user,
|
|
|
|
location,
|
|
|
|
label,
|
2022-10-16 05:41:27 +00:00
|
|
|
itemField,
|
2022-10-09 17:23:21 +00:00
|
|
|
client: {
|
|
|
|
public: publicClient,
|
|
|
|
user: userClient,
|
|
|
|
singleUse: userSingleUse,
|
|
|
|
},
|
|
|
|
};
|