All files / packages/content/src/testing factories.ts

100% Statements 22/22
100% Branches 4/4
100% Functions 7/7
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66        5x   5x 5x   5x   22x   12x   5x 8x   8x                 5x                 5x 7x   7x     5x 6x 6x   6x             24x                   6x    
import { fakerES } from "@faker-js/faker";
import { countWords } from "#core/adapters/providers/wordpress/WordpressText";
import type { Asset, Author, Entry, Term } from "#core/domain/content";
 
export const faker = fakerES;
 
faker.seed(192);
faker.setDefaultRefDate("2026-01-01T00:00:00Z");
 
const PIXEL = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7";
 
const id = (): string => String(faker.number.int({ min: 1, max: 99999 }));
 
const timestamp = (date: Date): string => date.toISOString().replace(/\.\d{3}Z$/, "Z");
 
export const fakeTerm = (overrides: Partial<Term> = {}): Term => {
    const name = faker.lorem.words({ min: 1, max: 3 });
 
    return {
        id: id(),
        resource: "categories",
        slug: faker.helpers.slugify(name).toLowerCase(),
        name,
        ...overrides,
    };
};
 
export const fakeAsset = (overrides: Partial<Asset> = {}): Asset => ({
    id: id(),
    url: PIXEL,
    alt: faker.lorem.sentence(),
    width: 1200,
    height: 630,
    ...overrides,
});
 
export const fakeAuthor = (overrides: Partial<Author> = {}): Author => {
    const name = faker.person.fullName();
 
    return { id: id(), slug: faker.helpers.slugify(name).toLowerCase(), name, ...overrides };
};
 
export const fakeEntry = (overrides: Partial<Entry> = {}): Entry => {
    const title = faker.lorem.sentence({ min: 3, max: 8 }).replace(/\.$/, "");
    const publishedAt = faker.date.past({ years: 2 });
 
    const entry: Entry = {
        id: id(),
        slug: faker.helpers.slugify(title).toLowerCase(),
        title,
        excerpt: { format: "html", value: `<p>${faker.lorem.sentence()}</p>` },
        body: {
            format: "html",
            value: Array.from({ length: 4 }, () => `<p>${faker.lorem.paragraph()}</p>`).join(""),
        },
        publishedAt: timestamp(publishedAt),
        updatedAt: timestamp(faker.date.soon({ days: 30, refDate: publishedAt })),
        terms: [fakeTerm()],
        authors: [fakeAuthor()],
        ...overrides,
    };
 
    // Counted from whichever body won, so an overridden body never carries the default's count
    return { wordCount: countWords(entry.body.value), ...entry };
};