bigbuffet-rw/app/soapbox/jest/factory.ts

104 lines
2.4 KiB
TypeScript
Raw Normal View History

import { v4 as uuidv4 } from 'uuid';
import {
accountSchema,
2023-05-02 16:30:21 -07:00
adSchema,
cardSchema,
groupMemberSchema,
groupRelationshipSchema,
groupSchema,
groupTagSchema,
2023-05-02 16:49:13 -07:00
relationshipSchema,
2023-06-20 12:24:39 -07:00
statusSchema,
type Account,
2023-05-02 16:30:21 -07:00
type Ad,
type Card,
type Group,
type GroupMember,
type GroupRelationship,
type GroupTag,
2023-05-02 16:49:13 -07:00
type Relationship,
2023-05-12 18:50:30 -07:00
type Status,
} from 'soapbox/schemas';
import { GroupRoles } from 'soapbox/schemas/group-member';
2023-06-20 12:24:39 -07:00
import type { PartialDeep } from 'type-fest';
// TODO: there's probably a better way to create these factory functions.
// This looks promising but didn't work on my first attempt: https://github.com/anatine/zod-plugins/tree/main/packages/zod-mock
2023-06-20 12:24:39 -07:00
function buildAccount(props: PartialDeep<Account> = {}): Account {
return accountSchema.parse(Object.assign({
id: uuidv4(),
}, props));
}
2023-06-20 12:24:39 -07:00
function buildCard(props: PartialDeep<Card> = {}): Card {
2023-05-02 16:30:21 -07:00
return cardSchema.parse(Object.assign({
url: 'https://soapbox.test',
}, props));
}
2023-06-20 12:24:39 -07:00
function buildGroup(props: PartialDeep<Group> = {}): Group {
return groupSchema.parse(Object.assign({
id: uuidv4(),
2023-05-31 05:57:43 -07:00
owner: {
id: uuidv4(),
},
}, props));
}
2023-06-20 12:24:39 -07:00
function buildGroupRelationship(props: PartialDeep<GroupRelationship> = {}): GroupRelationship {
return groupRelationshipSchema.parse(Object.assign({
id: uuidv4(),
}, props));
}
2023-06-20 12:24:39 -07:00
function buildGroupTag(props: PartialDeep<GroupTag> = {}): GroupTag {
return groupTagSchema.parse(Object.assign({
id: uuidv4(),
name: uuidv4(),
}, props));
}
function buildGroupMember(
2023-06-20 12:24:39 -07:00
props: PartialDeep<GroupMember> = {},
accountProps: PartialDeep<Account> = {},
): GroupMember {
return groupMemberSchema.parse(Object.assign({
id: uuidv4(),
account: buildAccount(accountProps),
role: GroupRoles.USER,
}, props));
}
2023-06-20 12:24:39 -07:00
function buildAd(props: PartialDeep<Ad> = {}): Ad {
2023-05-02 16:30:21 -07:00
return adSchema.parse(Object.assign({
card: buildCard(),
}, props));
}
2023-06-20 12:24:39 -07:00
function buildRelationship(props: PartialDeep<Relationship> = {}): Relationship {
2023-05-02 16:49:13 -07:00
return relationshipSchema.parse(Object.assign({
id: uuidv4(),
}, props));
}
2023-06-20 12:24:39 -07:00
function buildStatus(props: PartialDeep<Status> = {}) {
return statusSchema.parse(Object.assign({
2023-05-08 10:29:11 -07:00
id: uuidv4(),
account: buildAccount(),
2023-05-08 10:29:11 -07:00
}, props));
}
2023-05-02 16:49:13 -07:00
export {
2023-06-20 12:24:39 -07:00
buildAccount,
buildAd,
2023-05-02 16:49:13 -07:00
buildCard,
buildGroup,
buildGroupMember,
2023-05-02 16:49:13 -07:00
buildGroupRelationship,
buildGroupTag,
buildRelationship,
2023-05-08 10:29:11 -07:00
buildStatus,
2023-05-02 16:49:13 -07:00
};