Skip to content

Commit

Permalink
test: update tests with required async functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEdoRan committed Dec 8, 2024
1 parent 7668e06 commit a3ca439
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 28 deletions.
38 changes: 23 additions & 15 deletions packages/next-safe-action/src/__tests__/action-callbacks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ test("action with no input schema and no server errors calls `onSuccess` and `on
return;
},
{
onSuccess: () => {
onSuccess: async () => {
executed++;
},
onError: () => {
onError: async () => {
executed++; // should not be called
},
onSettled: () => {
onSettled: async () => {
executed++;
},
}
Expand All @@ -57,7 +57,15 @@ test("action with input schemas and no errors calls `onSuccess` and `onSettled`
};
},
{
onSuccess: ({ clientInput, bindArgsClientInputs, parsedInput, bindArgsParsedInputs, data, metadata, ctx }) => {
onSuccess: async ({
clientInput,
bindArgsClientInputs,
parsedInput,
bindArgsParsedInputs,
data,
metadata,
ctx,
}) => {
executed++;

assert.deepStrictEqual(
Expand All @@ -75,10 +83,10 @@ test("action with input schemas and no errors calls `onSuccess` and `onSettled`
}
);
},
onError: () => {
onError: async () => {
executed++; // should not be called
},
onSettled: ({ clientInput, bindArgsClientInputs, result, metadata, ctx }) => {
onSettled: async ({ clientInput, bindArgsClientInputs, result, metadata, ctx }) => {
executed++;

assert.deepStrictEqual(
Expand Down Expand Up @@ -115,10 +123,10 @@ test("action with input schemas and server error calls `onError` and `onSettled`
throw new Error("Server error");
},
{
onSuccess: () => {
onSuccess: async () => {
executed++; // should not be called
},
onError({ error, clientInput, bindArgsClientInputs, metadata, ctx }) {
onError: async ({ error, clientInput, bindArgsClientInputs, metadata, ctx }) => {
executed++;

assert.deepStrictEqual(
Expand All @@ -134,7 +142,7 @@ test("action with input schemas and server error calls `onError` and `onSettled`
}
);
},
onSettled({ clientInput, bindArgsClientInputs, result, metadata, ctx }) {
onSettled: async ({ clientInput, bindArgsClientInputs, result, metadata, ctx }) => {
executed++;

assert.deepStrictEqual(
Expand Down Expand Up @@ -171,10 +179,10 @@ test("action with validation errors calls `onError` and `onSettled` callbacks wi
};
},
{
onSuccess: () => {
onSuccess: async () => {
executed++; // should not be called
},
onError({ error, clientInput, bindArgsClientInputs, metadata, ctx }) {
onError: async ({ error, clientInput, bindArgsClientInputs, metadata, ctx }) => {
executed++;

assert.deepStrictEqual(
Expand Down Expand Up @@ -202,7 +210,7 @@ test("action with validation errors calls `onError` and `onSettled` callbacks wi
}
);
},
onSettled({ clientInput, bindArgsClientInputs, result, metadata, ctx }) {
onSettled: async ({ clientInput, bindArgsClientInputs, result, metadata, ctx }) => {
executed++;

assert.deepStrictEqual(
Expand Down Expand Up @@ -252,10 +260,10 @@ test("action with server validation error calls `onError` and `onSettled` callba
});
},
{
onSuccess: () => {
onSuccess: async () => {
executed++; // should not be called
},
onError({ error, clientInput, bindArgsClientInputs, metadata, ctx }) {
onError: async ({ error, clientInput, bindArgsClientInputs, metadata, ctx }) => {
executed++;

assert.deepStrictEqual(
Expand All @@ -275,7 +283,7 @@ test("action with server validation error calls `onError` and `onSettled` callba
}
);
},
onSettled({ clientInput, bindArgsClientInputs, result, metadata, ctx }) {
onSettled: async ({ clientInput, bindArgsClientInputs, result, metadata, ctx }) => {
executed++;

assert.deepStrictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ test("action with invalid bind args input gives back an object with correct `bin
];

const action = dac
.bindArgsSchemas(bindArgsSchemas, { handleBindArgsValidationErrorsShape: flattenBindArgsValidationErrors })
.bindArgsSchemas(bindArgsSchemas, {
handleBindArgsValidationErrorsShape: async (ve) => flattenBindArgsValidationErrors(ve),
})
.action(async () => {
return {
ok: true,
Expand Down Expand Up @@ -138,7 +140,9 @@ test("action with invalid bind args input gives back an object with correct `bin
];

const action = foac
.bindArgsSchemas(bindArgsSchemas, { handleBindArgsValidationErrorsShape: flattenBindArgsValidationErrors })
.bindArgsSchemas(bindArgsSchemas, {
handleBindArgsValidationErrorsShape: async (ve) => flattenBindArgsValidationErrors(ve),
})
.action(async () => {
return {
ok: true,
Expand Down Expand Up @@ -225,7 +229,9 @@ test("action with invalid bind args input gives back an object with correct `bin
];

const action = flac
.bindArgsSchemas(bindArgsSchemas, { handleBindArgsValidationErrorsShape: formatBindArgsValidationErrors })
.bindArgsSchemas(bindArgsSchemas, {
handleBindArgsValidationErrorsShape: async (ve) => formatBindArgsValidationErrors(ve),
})
.action(async () => {
return {
ok: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ test("action with invalid bind args input and invalid main input gives back an o

const action = dac
.schema(schema)
.bindArgsSchemas(bindArgsSchemas, { handleBindArgsValidationErrorsShape: flattenBindArgsValidationErrors })
.bindArgsSchemas(bindArgsSchemas, {
handleBindArgsValidationErrorsShape: async (ve) => flattenBindArgsValidationErrors(ve),
})
.action(async () => {
return {
ok: true,
Expand Down Expand Up @@ -131,7 +133,7 @@ test("action with invalid bind args input and invalid main input gives back an o
];

const action = foac
.schema(schema, { handleValidationErrorsShape: flattenValidationErrors })
.schema(schema, { handleValidationErrorsShape: async (ve) => flattenValidationErrors(ve) })
.bindArgsSchemas(bindArgsSchemas)
.action(async () => {
return {
Expand Down Expand Up @@ -179,7 +181,9 @@ test("action with invalid bind args input and valid main input gives back an obj

const action = foac
.schema(schema)
.bindArgsSchemas(bindArgsSchemas, { handleBindArgsValidationErrorsShape: flattenBindArgsValidationErrors })
.bindArgsSchemas(bindArgsSchemas, {
handleBindArgsValidationErrorsShape: async (ve) => flattenBindArgsValidationErrors(ve),
})
.action(async () => {
return {
ok: true,
Expand Down Expand Up @@ -287,8 +291,10 @@ test("action with invalid bind args input, invalid main input and root level sch
];

const action = flac
.schema(schema, { handleValidationErrorsShape: formatValidationErrors })
.bindArgsSchemas(bindArgsSchemas, { handleBindArgsValidationErrorsShape: formatBindArgsValidationErrors })
.schema(schema, { handleValidationErrorsShape: async (ve) => formatValidationErrors(ve) })
.bindArgsSchemas(bindArgsSchemas, {
handleBindArgsValidationErrorsShape: async (ve) => formatBindArgsValidationErrors(ve),
})
.action(async () => {
return {
ok: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/next-safe-action/src/__tests__/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,10 +357,10 @@ test("overridden formatted validation errors in execution result from middleware
z.object({
username: z.string().max(3),
}),
{ handleValidationErrorsShape: formatValidationErrors }
{ handleValidationErrorsShape: async (ve) => formatValidationErrors(ve) }
)
.bindArgsSchemas([z.object({ age: z.number().positive() })], {
handleBindArgsValidationErrorsShape: formatBindArgsValidationErrors,
handleBindArgsValidationErrorsShape: async (ve) => formatBindArgsValidationErrors(ve),
})
.use(async ({ next }) => {
// Await action execution.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ test("action with invalid input gives back an object with correct `validationErr

const action = dac
.schema(schema, {
handleValidationErrorsShape: flattenValidationErrors,
handleValidationErrorsShape: async (ve) => flattenValidationErrors(ve),
})
.action(async () => {
return {
Expand Down Expand Up @@ -318,7 +318,7 @@ test("action with invalid input gives back an object with correct `validationErr

const action = foac
.schema(schema, {
handleValidationErrorsShape: flattenValidationErrors,
handleValidationErrorsShape: async (ve) => flattenValidationErrors(ve),
})
.action(async () => {
return {
Expand Down Expand Up @@ -452,7 +452,7 @@ test("action with invalid input gives back an object with correct `validationErr

const action = flac
.schema(schema, {
handleValidationErrorsShape: formatValidationErrors,
handleValidationErrorsShape: async (ve) => formatValidationErrors(ve),
})
.action(async () => {
return {
Expand Down

0 comments on commit a3ca439

Please sign in to comment.