diff --git a/packages/convex-helpers/server/customFunctions.test.ts b/packages/convex-helpers/server/customFunctions.test.ts index 7a5e615e..8b2af8f9 100644 --- a/packages/convex-helpers/server/customFunctions.test.ts +++ b/packages/convex-helpers/server/customFunctions.test.ts @@ -358,6 +358,34 @@ export const outerRemoves = outerRemover({ return { ctxInner: ctx["inner"], ctxOuter: ctx["outer"], ...args }; }, }); +// Extend the previous ctx with new args +export const outerExtender = customQuery(inner, { + args: { outer: v.string() }, + input: async (ctx, args) => { + return { ctx: { outer: [args.outer, ctx.inner] }, args: {} }; + }, +}); +export const outerExtends = outerExtender({ + args: { a: v.string() }, + handler: async (ctx, args) => { + return { outer: ctx.outer, inner: ctx.inner, ...args }; + }, +}); +// Deleted ctx is correctly undefined on nested functions +export const dbRemover = customQuery( + query, + customCtx(() => ({ db: undefined })), +); +export const outerDBRemover = customQuery( + dbRemover, + customCtx(() => ({ outer: "outer" })), +); +export const outerRemovesDB = outerDBRemover({ + args: {}, + handler: async (ctx, args) => { + return { db: ctx?.db ? "present" : "missing", outer: ctx.outer, ...args }; + }, +}); /** * Test helpers @@ -388,6 +416,8 @@ const testApi: ApiFromModules<{ create: typeof create; outerAdds: typeof outerAdds; outerRemoves: typeof outerRemoves; + outerExtends: typeof outerExtends; + outerRemovesDB: typeof outerRemovesDB; }; }>["fns"] = anyApi["customFunctions.test"] as any; @@ -568,4 +598,23 @@ describe("nested custom functions", () => { t.query(testApi.outerAdds, { a: 3 as any, outer: "" }), ).rejects.toThrow("Validator error: Expected `string`"); }); + + test("extends prev ctx", async () => { + const t = convexTest(schema, modules); + expect( + await t.query(testApi.outerExtends, { a: "hi", outer: "extended" }), + ).toMatchObject({ + inner: "inner", + outer: ["extended", "inner"], + a: "hi", + }); + }); + + test("keeps deleted ctx", async () => { + const t = convexTest(schema, modules); + expect(await t.query(testApi.outerRemovesDB, {})).toMatchObject({ + db: "missing", + outer: "outer", + }); + }); });