Skip to content

Commit 2d7a0ba

Browse files
committed
resolved ESLint issues and applied fixes
1 parent a66cd94 commit 2d7a0ba

File tree

1 file changed

+72
-82
lines changed

1 file changed

+72
-82
lines changed

test/graphql/types/Organization/creator.test.ts

Lines changed: 72 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import type { FastifyInstance, FastifyReply } from "fastify";
22
import type { MercuriusContext } from "mercurius";
3-
import { beforeEach, describe, expect, it, vi } from "vitest";
4-
import type { GraphQLContext } from "~/src/graphql/context";
5-
import type { User } from "~/src/graphql/types/User/User";
6-
import { TalawaGraphQLError } from "~/src/utilities/TalawaGraphQLError";
3+
import {
4+
beforeEach,
5+
describe,
6+
expect,
7+
it,
8+
vi,
9+
MockedFunction as viMockedFunction,
10+
} from "vitest";
11+
import type { GraphQLContext } from "../../../../src/graphql/context";
12+
import type { User } from "../../../../src/graphql/types/User/User";
13+
import { TalawaGraphQLError } from "../../../../src/utilities/TalawaGraphQLError";
714

815
type ResolverContext = GraphQLContext & MercuriusContext;
916

@@ -20,7 +27,7 @@ interface TestContext extends Pick<MercuriusContext, "reply"> {
2027
drizzleClient: {
2128
query: {
2229
usersTable: {
23-
findFirst: vi.MockedFunction<
30+
findFirst: viMockedFunction<
2431
(params: {
2532
columns?: Record<string, boolean>;
2633
with?: Record<string, unknown>;
@@ -94,17 +101,7 @@ const resolveCreator = async (
94101
parent: OrganizationParent,
95102
_args: Record<string, never>,
96103
ctx: ResolverContext,
97-
): Promise<typeof User | null> => {
98-
if (!ctx.currentClient.isAuthenticated || !ctx.currentClient.user?.id) {
99-
throw new TalawaGraphQLError({
100-
extensions: {
101-
code: "unauthenticated",
102-
},
103-
});
104-
}
105-
106-
const currentUserId = ctx.currentClient.user.id;
107-
104+
): Promise<User | null> => {
108105
const currentUser = await ctx.drizzleClient.query.usersTable.findFirst({
109106
columns: {
110107
role: true,
@@ -114,16 +111,11 @@ const resolveCreator = async (
114111
columns: {
115112
role: true,
116113
},
117-
where: (
118-
fields: { organizationId: string },
119-
operators: { eq: (field: string, value: string) => boolean },
120-
) => operators.eq(fields.organizationId, parent.id),
114+
where: (fields, operators) =>
115+
operators.eq(fields.organizationId, parent.id),
121116
},
122117
},
123-
where: (
124-
fields: { id: string },
125-
operators: { eq: (field: string, value: string) => boolean },
126-
) => operators.eq(fields.id, currentUserId),
118+
where: (fields, operators) => operators.eq(fields.id, currentUser),
127119
});
128120

129121
if (!currentUser) {
@@ -399,71 +391,69 @@ describe("Organization Resolver - Creator Field", () => {
399391
expect(result).toEqual(mockCreator);
400392
});
401393

402-
describe("Edge Cases", () => {
403-
it("should verify columns are correctly specified in the user query", async () => {
404-
const mockUser = createMockUser("administrator");
394+
it("should verify columns are correctly specified in the user query", async () => {
395+
const mockUser = createMockUser("administrator");
405396

406-
let columnsVerified = false;
407-
ctx.drizzleClient.query.usersTable.findFirst.mockImplementationOnce(
408-
async (params: { columns: Record<string, boolean> }) => {
409-
expect(params.columns).toEqual({ role: true });
410-
columnsVerified = true;
411-
return mockUser;
412-
},
413-
);
397+
let columnsVerified = false;
398+
ctx.drizzleClient.query.usersTable.findFirst.mockImplementationOnce(
399+
async (params: { columns: Record<string, boolean> }) => {
400+
expect(params.columns).toEqual({ role: true });
401+
columnsVerified = true;
402+
return mockUser;
403+
},
404+
);
414405

415-
const mockCreator = {
416-
id: "creator-456",
417-
role: "member",
418-
};
419-
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValueOnce(
420-
mockCreator,
421-
);
406+
const mockCreator = {
407+
id: "creator-456",
408+
role: "member",
409+
};
410+
ctx.drizzleClient.query.usersTable.findFirst.mockResolvedValueOnce(
411+
mockCreator,
412+
);
422413

423-
await resolveCreator(
424-
mockOrganization,
425-
{},
426-
ctx as unknown as ResolverContext,
427-
);
414+
await resolveCreator(
415+
mockOrganization,
416+
{},
417+
ctx as unknown as ResolverContext,
418+
);
428419

429-
expect(columnsVerified).toBe(true);
430-
});
420+
expect(columnsVerified).toBe(true);
421+
});
431422

432-
it("should handle concurrent access to creator information", async () => {
433-
const mockUser = createMockUser("administrator");
434-
const mockCreator = {
435-
id: "creator-456",
436-
role: "member",
437-
};
438-
439-
// Set up mock to handle multiple calls
440-
ctx.drizzleClient.query.usersTable.findFirst.mockImplementation(
441-
async (params) => {
442-
// Return mockUser for authentication checks
443-
if (params.columns?.role) {
444-
return mockUser;
445-
}
446-
// Return mockCreator for creator lookups
447-
return mockCreator;
448-
},
423+
it("should handle concurrent access to creator information", async () => {
424+
const mockUser = createMockUser("administrator");
425+
const mockCreator = {
426+
id: "creator-456",
427+
role: "member",
428+
};
429+
430+
// Set up mock to handle multiple calls
431+
ctx.drizzleClient.query.usersTable.findFirst.mockImplementation(
432+
async (params) => {
433+
// Return mockUser for authentication checks
434+
if (params.columns?.role) {
435+
return mockUser;
436+
}
437+
// Return mockCreator for creator lookups
438+
return mockCreator;
439+
},
440+
);
441+
442+
// Simulate concurrent requests
443+
const requests = Array(5)
444+
.fill(null)
445+
.map(() =>
446+
resolveCreator(
447+
mockOrganization,
448+
{},
449+
ctx as unknown as ResolverContext,
450+
),
449451
);
450452

451-
// Simulate concurrent requests
452-
const requests = Array(5)
453-
.fill(null)
454-
.map(() =>
455-
resolveCreator(
456-
mockOrganization,
457-
{},
458-
ctx as unknown as ResolverContext,
459-
),
460-
);
461-
462-
const results = await Promise.all(requests);
463-
results.forEach((result) => {
464-
expect(result).toEqual(mockCreator);
465-
});
466-
});
453+
const results = await Promise.all(requests);
454+
for (const result of results) {
455+
expect(result).toEqual(mockCreator);
456+
}
467457
});
468458
});
469459
});

0 commit comments

Comments
 (0)