Skip to content

Commit

Permalink
test(installer): #32 add missing unit test for resolving options
Browse files Browse the repository at this point in the history
  • Loading branch information
sdorra committed Nov 13, 2024
1 parent ff040b7 commit 145d1b2
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/installer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function createMigrator(directory: string) {
return {
name: migrator.name,
createMigration: async (ask: Ask) => {
const options = await resolveOptions(migrator, ask);
const options = await resolveOptions(migrator.options, ask);

return migrator.createMigration(
{
Expand Down
20 changes: 19 additions & 1 deletion packages/installer/src/migration/options.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from "vitest";
import { z } from "zod";
import { type Option, zodToOptions } from "./options.js";
import { type Option, resolveOptions, zodToOptions } from "./options.js";

describe("zodToOptions", () => {
function expectSingleOption(options: Array<Option>) {
Expand Down Expand Up @@ -101,3 +101,21 @@ describe("zodToOptions", () => {
expect(option.choices).toEqual(["none", "markdown", "mdx"]);
});
});

describe("resolveOptions", () => {
it("should resolve options", async () => {
const schema = z.object({
name: z.string(),
});

const ask = async (option: Option) => {
if (option.name === "name") {
return "test";
}
return "unknown";
};

const options = await resolveOptions(schema, ask);
expect(options).toEqual({ name: "test" });
});
});
4 changes: 1 addition & 3 deletions packages/installer/src/migration/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ export function zodToOptions(schema: ZodObject<ZodRawShape>) {

export type Ask = (options: Option) => Promise<string>;

export async function resolveOptions(migrator: Migrator, ask: Ask) {
export async function resolveOptions(schema: ZodObject<ZodRawShape>, ask: Ask) {
const resolvedOptions: any = {};

const schema = migrator.options;

const options = zodToOptions(schema);
for (const option of options) {
const value = await ask(option);
Expand Down

0 comments on commit 145d1b2

Please sign in to comment.