import type { A } from 'ts-toolbelt' /** * Use this function to define your scenario. * * @example * export const standard = defineScenario({ * user: { * dom: { * name: 'Dom Saadi', * email: 'dom@cedarjs.com', * }, * }, * }) * * @example * export const standard = defineScenario({ * user: { * dom: { * name: 'Dom Saadi', * email: 'dom@cedarjs.com', * }, * }, * }) */ export const defineScenario: DefineScenario = (data) => { return data } /** * @example * { * dannyUser: { * data: { * id: 1, * name: 'Danny', * }, * }, * } * * @example * krisUser: (scenario) => { * return { * data: { * id: 2, * name: 'Kris', * }, * } * } */ type ScenarioDefinitionMap< PrismaCreateType extends { data: any }, ModelName extends string | number | symbol = string | number | symbol, TKeys extends string | number | symbol = string | number | symbol, > = Record< TKeys, | A.Compute | (( scenario: Record>, ) => A.Compute) > // ----- // The types below are used to provide global types for scenario and defineScenario, used in testing // --- // Note that the generic is **inside** the interface // This is so we can assign it to a const when we generate scenarios.d.ts // defineScenario({ // modelName: ScenarioDefinitionMap // }) export interface DefineScenario { < PrismaCreateType extends { data: any }, ModelName extends string | number | symbol = string | number | symbol, TKeys extends string | number | symbol = string | number | symbol, >( scenarioMap: Record< ModelName, ScenarioDefinitionMap >, ): Record> } /** * Type of data seeded by the scenario. Use this type to get the 'strict' return type of defineScenario * * @example * import { Product } from '@prisma/client' * * // If you scenario looks like this: * export const standard = defineScenario({ * product: { * shirt: { * id: 55, * price: 10, * }, * }, * }) * * // Export the StandardScenario type as * export StandardScenario = ScenarioData * * // You can also define each of the keys in your scenario, so you get stricter type checking * export StandardScenario = ScenarioData */ export declare type ScenarioData< TModel, // the prisma model, imported from @prisma/client e.g. "Product" TName extends string | number = string | number, // (optional) name of the prisma model e.g. "product" TKeys extends string | number = string | number, // (optional) name of each of the seeded scenarios e.g. "shirt" > = Record> interface TestFunctionWithScenario { (scenario?: TData): Promise } interface DescribeBlockWithGetScenario { (getScenario: () => TData): void } export interface Scenario { (title: string, testFunction: TestFunctionWithScenario): void } // Overload scenario function for namedScenario export interface Scenario { ( namedScenario: string, title: string, testFunction: TestFunctionWithScenario, ): void } export interface Scenario { only: Scenario } export interface DescribeScenario { ( title: string, describeBlock: DescribeBlockWithGetScenario, ): void } export interface DescribeScenario { ( title: string, describeBlock: DescribeBlockWithGetScenario, ): void } // Overload for namedScenario export interface DescribeScenario { ( namedScenario: string, title: string, describeBlock: DescribeBlockWithGetScenario, ): void } export interface DescribeScenario { only: DescribeScenario }