winsoroaks
winsoroaks•17mo ago

how to mock two tables?

hi! i currently have the following snippet
it("createUserProfile should create a person", async () => {
const ctx = {
db: {
insert: vi.fn(),
query: vi.fn().mockReturnValue({
withIndex: vi.fn().mockReturnValue({
unique: vi.fn().mockReturnValue(null),
}),
}),
},
auth: {
getUserIdentity: vi.fn().mockReturnValue(
Promise.resolve({
tokenIdentifier: "user id",
})
),
},
}

// @ts-ignore
await createUserProfile(ctx, { clerkOrgId: "fake org id" })
expect(ctx.db.insert.mock.calls.length === 1)
expect(ctx.db.insert.mock.calls[0]).toEqual([
"user",
{
clerkOrgId: "fake org id",
clerkUserId: "user id",
},
])
})
it("createUserProfile should create a person", async () => {
const ctx = {
db: {
insert: vi.fn(),
query: vi.fn().mockReturnValue({
withIndex: vi.fn().mockReturnValue({
unique: vi.fn().mockReturnValue(null),
}),
}),
},
auth: {
getUserIdentity: vi.fn().mockReturnValue(
Promise.resolve({
tokenIdentifier: "user id",
})
),
},
}

// @ts-ignore
await createUserProfile(ctx, { clerkOrgId: "fake org id" })
expect(ctx.db.insert.mock.calls.length === 1)
expect(ctx.db.insert.mock.calls[0]).toEqual([
"user",
{
clerkOrgId: "fake org id",
clerkUserId: "user id",
},
])
})
what happens if createUserProfile so happen to call 2 DBs? can i do something like
const ctx = {
db:
db1: {},
db2: {},
...
const ctx = {
db:
db1: {},
db2: {},
...
getting an error when i try it 😅
4 Replies
winsoroaks
winsoroaksOP•17mo ago
also typescript didnt like my ctx for missing the schedule, storage, and bunch of other funcs in db . i have to ts-ignore it
Michal Srb
Michal Srb•17mo ago
what happens if createUserProfile so happen to call 2 DBs? can i do something like
Hey @winsoroaks, I don't understand this question. What is the concern?
winsoroaks
winsoroaksOP•17mo ago
sorry, lemme try again. is it possible to mock two different docs / dbs? im trying to do
const ctx = {
db: {
insert: vi.fn(),
query: vi.fn().mockReturnValue({
db1: {
filter: vi.fn().mockReturnValue({}),
},
db2: vi.fn().mockReturnValue({}),
}),
},

const ctx = {
db: {
db1: {
insert: vi.fn(),
query: vi.fn().mockReturnValue({}),
},
db2: {
insert: vi.fn(),
query: vi.fn().mockReturnValue({}),
},
const ctx = {
db: {
insert: vi.fn(),
query: vi.fn().mockReturnValue({
db1: {
filter: vi.fn().mockReturnValue({}),
},
db2: vi.fn().mockReturnValue({}),
}),
},

const ctx = {
db: {
db1: {
insert: vi.fn(),
query: vi.fn().mockReturnValue({}),
},
db2: {
insert: vi.fn(),
query: vi.fn().mockReturnValue({}),
},
but i didnt seem to quite get them correct. my function that im trying to test looks like this
const currentUser = await ctx.db
.query('db1')
.filter((q) => q.and(q.eq(q.field("email"), email)))
.unique()

const userAgain = await ctx.db
.query("db2")
.filter((q) => q.eq(q.field("userId"), currentUser._id))
.unique()
const currentUser = await ctx.db
.query('db1')
.filter((q) => q.and(q.eq(q.field("email"), email)))
.unique()

const userAgain = await ctx.db
.query("db2")
.filter((q) => q.eq(q.field("userId"), currentUser._id))
.unique()
im not sure which part of the mocking i've messed up 😦 can u pls help me out? oh looks like i fixed it by doing
const ctx = {
db: {
insert: vi.fn(),
query: vi.fn().mockImplementation((dbName) => {
if (dbName === "db1") {
return {
filter: vi.fn().mockReturnValue({}),
}
} else if (dbName === "db2") {
return vi.fn().mockReturnValue({})
}
}),
},
const ctx = {
db: {
insert: vi.fn(),
query: vi.fn().mockImplementation((dbName) => {
if (dbName === "db1") {
return {
filter: vi.fn().mockReturnValue({}),
}
} else if (dbName === "db2") {
return vi.fn().mockReturnValue({})
}
}),
},
Michal Srb
Michal Srb•17mo ago
Btw we call db1 and db2 "tables" in the Convex lingo.

Did you find this page helpful?