import { DocumentRenderer, SharedSliceRenderer } from "@prismicio/api-renderer" import type { CustomTypeDef } from "@prismicio/api-renderer/lib/models" import type { WidgetKey } from "@prismicio/types-internal/lib/common" import type { SharedSliceContent } from "@prismicio/types-internal/lib/content" import type { WidgetContent } from "@prismicio/types-internal/lib/content" import type { SharedSlice } from "@prismicio/types-internal/lib/customtypes" import { type CustomType, flattenSections, toStatic as toStaticCustomType, } from "@prismicio/types-internal/lib/customtypes" import { v4 as uuid } from "uuid" import mockCtx from "./MockContext" export function renderDocumentMock( customType: CustomType, sharedSlices: Partial>, mocks: Partial>, ) { const filteredSharedSlices = Object.entries(sharedSlices).filter( (entry): entry is [string, SharedSlice] => Boolean(entry[1]), ) const filteredMocks = Object.entries(mocks).filter( (entry): entry is [WidgetKey, WidgetContent] => Boolean(entry[1]), ) const staticCustomType = toStaticCustomType( customType, new Map(filteredSharedSlices), ) const customTypeDef: CustomTypeDef = { customTypeId: customType.id, fields: Object.fromEntries(flattenSections(staticCustomType)), } return DocumentRenderer(mockCtx).renderMocks(customTypeDef, filteredMocks) } export function renderSliceMock( sliceModel: SharedSlice, mock: SharedSliceContent, ): unknown { return { ...(SharedSliceRenderer(mockCtx).renderMocks(sliceModel, mock) as object), id: `${sliceModel.id}$${uuid()}`, slice_type: sliceModel.id, } }