@prismatic-io/embedded
Version:
Embed Prismatic's integration marketplace and workflow designer within your existing application.
229 lines (219 loc) • 7.07 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vitest_1 = require("vitest");
const generateTypes_1 = require("./generateTypes");
const HEADER = `// This file was auto-generated by @prismatic-io/embedded generate-types.
// Do not edit this file manually.`;
const formatExpected = (body) => (0, generateTypes_1.biomeFormat)(`${HEADER}\n\nexport {}\n\n${body}`);
(0, vitest_1.describe)("generateTypes", () => {
(0, vitest_1.it)("produces an empty WorkflowContexts interface when given no contexts", async () => {
const result = await (0, generateTypes_1.generateTypes)([]);
const expected = await formatExpected(`
declare module "@prismatic-io/embedded" {
interface WorkflowContexts {}
}
`);
(0, vitest_1.expect)(result).toEqual(expected);
});
(0, vitest_1.it)("generates a typed context from a simple schema", async () => {
const contexts = [
{
stableKey: "my-workflow",
contextSchema: {
type: "object",
properties: {
name: { type: "string" },
},
required: ["name"],
},
},
];
const result = await (0, generateTypes_1.generateTypes)(contexts);
const expected = await formatExpected(`
declare module "@prismatic-io/embedded" {
interface MyWorkflowContext {
name: string;
}
interface WorkflowContexts {
"my-workflow": MyWorkflowContext;
}
}
`);
(0, vitest_1.expect)(result).toEqual(expected);
});
(0, vitest_1.it)("generates multiple contexts", async () => {
const contexts = [
{
stableKey: "first",
contextSchema: {
type: "object",
properties: { a: { type: "string" } },
},
},
{
stableKey: "second",
contextSchema: {
type: "object",
properties: { b: { type: "number" } },
},
},
];
const result = await (0, generateTypes_1.generateTypes)(contexts);
const expected = await formatExpected(`
declare module "@prismatic-io/embedded" {
interface FirstContext {
a?: string;
}
interface SecondContext {
b?: number;
}
interface WorkflowContexts {
first: FirstContext;
second: SecondContext;
}
}
`);
(0, vitest_1.expect)(result).toEqual(expected);
});
vitest_1.it.each([
["kebab-case", "KebabCaseContext"],
["snake_case", "SnakeCaseContext"],
["dot.delimited", "DotDelimitedContext"],
["alreadyPascal", "AlreadyPascalContext"],
["multi--separator", "MultiSeparatorContext"],
])("converts stableKey %s to type name %s", (stableKey, expected) => {
(0, vitest_1.expect)((0, generateTypes_1.stableKeyToTypeName)(stableKey)).toEqual(expected);
});
(0, vitest_1.it)("handles nested object schemas", async () => {
const contexts = [
{
stableKey: "nested",
contextSchema: {
type: "object",
properties: {
address: {
type: "object",
properties: {
street: { type: "string" },
zip: { type: "string" },
},
required: ["street"],
},
},
required: ["address"],
},
},
];
const result = await (0, generateTypes_1.generateTypes)(contexts);
const expected = await formatExpected(`
declare module "@prismatic-io/embedded" {
interface NestedContext {
address: {
street: string;
zip?: string;
};
}
interface WorkflowContexts {
nested: NestedContext;
}
}
`);
(0, vitest_1.expect)(result).toEqual(expected);
});
(0, vitest_1.it)("marks properties without required as optional", async () => {
const contexts = [
{
stableKey: "optional-fields",
contextSchema: {
type: "object",
properties: {
required_field: { type: "string" },
optional_field: { type: "number" },
},
required: ["required_field"],
},
},
];
const result = await (0, generateTypes_1.generateTypes)(contexts);
const expected = await formatExpected(`
declare module "@prismatic-io/embedded" {
interface OptionalFieldsContext {
required_field: string;
optional_field?: number;
}
interface WorkflowContexts {
"optional-fields": OptionalFieldsContext;
}
}
`);
(0, vitest_1.expect)(result).toEqual(expected);
});
(0, vitest_1.it)("disambiguates colliding type names with numeric suffixes", async () => {
const contexts = [
{
stableKey: "my-workflow",
contextSchema: {
type: "object",
properties: { a: { type: "string" } },
},
},
{
stableKey: "myWorkflow",
contextSchema: {
type: "object",
properties: { b: { type: "string" } },
},
},
];
const result = await (0, generateTypes_1.generateTypes)(contexts);
const expected = await formatExpected(`
declare module "@prismatic-io/embedded" {
interface MyWorkflowContext1 {
a?: string;
}
interface MyWorkflowContext2 {
b?: string;
}
interface WorkflowContexts {
"my-workflow": MyWorkflowContext1;
myWorkflow: MyWorkflowContext2;
}
}
`);
(0, vitest_1.expect)(result).toEqual(expected);
});
(0, vitest_1.it)("does not suffix when there is no collision", async () => {
const contexts = [
{
stableKey: "my-workflow",
contextSchema: {
type: "object",
properties: { a: { type: "string" } },
},
},
{
stableKey: "something-else",
contextSchema: {
type: "object",
properties: { b: { type: "string" } },
},
},
];
const result = await (0, generateTypes_1.generateTypes)(contexts);
const expected = await formatExpected(`
declare module "@prismatic-io/embedded" {
interface MyWorkflowContext {
a?: string;
}
interface SomethingElseContext {
b?: string;
}
interface WorkflowContexts {
"my-workflow": MyWorkflowContext;
"something-else": SomethingElseContext;
}
}
`);
(0, vitest_1.expect)(result).toEqual(expected);
});
});