Mirfayz
Mirfayz
PPrisma
Created by Mirfayz on 7/30/2024 in #help-and-questions
How to test a prisma call that uses `connectOrCreate`? (using vitest ideally)
Hey all! I'm trying to write a unit test for the following prisma call in my Next.js app:
export async function createInterview(data: {
participantIdentifier?: string;
protocolId: string;
}) {
const { participantIdentifier, protocolId } = data;

const createdInterview = await prisma.interview.create({
select: {
participant: true,
id: true,
},
data: {
network: Prisma.JsonNull,
participant: {
connectOrCreate: {
create: {
identifier: participantIdentifier!,
},
where: {
identifier: participantIdentifier,
},
},
},
protocol: {
connect: {
id: protocolId,
},
},
},
});

return {
error: null,
createdInterviewId: createdInterview.id,
errorType: null,
};
}
export async function createInterview(data: {
participantIdentifier?: string;
protocolId: string;
}) {
const { participantIdentifier, protocolId } = data;

const createdInterview = await prisma.interview.create({
select: {
participant: true,
id: true,
},
data: {
network: Prisma.JsonNull,
participant: {
connectOrCreate: {
create: {
identifier: participantIdentifier!,
},
where: {
identifier: participantIdentifier,
},
},
},
protocol: {
connect: {
id: protocolId,
},
},
},
});

return {
error: null,
createdInterviewId: createdInterview.id,
errorType: null,
};
}
Basically, I want to test this case: "It should connect a participant if an existing identifier is provided or create a participant if an identifier doesn't exist in the db'" Is there any way of testing this? Maybe, mocking a DB state and using it to provide state or smt? Would appreciate your help, thank you in advance!
1 replies