Skip to content

Commit

Permalink
Add tests for countryUpdate
Browse files Browse the repository at this point in the history
  • Loading branch information
dcramer committed Aug 24, 2024
1 parent 199d464 commit ac7ab5d
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions apps/server/src/trpc/routes/countryUpdate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { db } from "@peated/server/db";
import { countries } from "@peated/server/db/schema";
import { omit } from "@peated/server/lib/filter";
import waitError from "@peated/server/lib/test/waitError";
import { eq } from "drizzle-orm";
import { createCaller } from "../router";

test("requires authentication", async () => {
const caller = createCaller({ user: null });
const err = await waitError(
caller.countryUpdate({
slug: "test-country",
}),
);
expect(err).toMatchInlineSnapshot(`[TRPCError: UNAUTHORIZED]`);
});

test("requires mod", async ({ defaults }) => {
const caller = createCaller({ user: defaults.user });
const err = await waitError(
caller.countryUpdate({
slug: "test-country",
}),
);
expect(err).toMatchInlineSnapshot(`[TRPCError: UNAUTHORIZED]`);
});

test("no changes", async ({ fixtures }) => {
const country = await fixtures.Country();

const caller = createCaller({
user: await fixtures.User({ mod: true }),
});
const data = await caller.countryUpdate({
slug: country.slug,
});

expect(data.id).toBeDefined();

const [newCountry] = await db
.select()
.from(countries)
.where(eq(countries.id, data.id));

expect(country).toEqual(newCountry);
});

test("can change description", async ({ fixtures }) => {
const country = await fixtures.Country();

const caller = createCaller({
user: await fixtures.User({ mod: true }),
});
const data = await caller.countryUpdate({
slug: country.slug,
description: "New description",
});

expect(data.id).toBeDefined();

const [newCountry] = await db
.select()
.from(countries)
.where(eq(countries.id, data.id));

expect(omit(country, "description", "descriptionSrc", "updatedAt")).toEqual(
omit(newCountry, "description", "descriptionSrc", "updatedAt"),
);
expect(newCountry.description).toBe("New description");
expect(newCountry.descriptionSrc).toBe("user");
});

test("can change summary", async ({ fixtures }) => {
const country = await fixtures.Country();

const caller = createCaller({
user: await fixtures.User({ mod: true }),
});
const data = await caller.countryUpdate({
slug: country.slug,
summary: "New summary",
});

expect(data.id).toBeDefined();

const [newCountry] = await db
.select()
.from(countries)
.where(eq(countries.id, data.id));

expect(omit(country, "summary", "updatedAt")).toEqual(
omit(newCountry, "summary", "updatedAt"),
);
expect(newCountry.summary).toBe("New summary");
});

test("throws error for invalid country slug", async ({ fixtures }) => {
const caller = createCaller({
user: await fixtures.User({ mod: true }),
});
const err = await waitError(
caller.countryUpdate({
slug: "nonexistent-country",
description: "New description",
}),
);
expect(err).toMatchInlineSnapshot(`[TRPCError: NOT_FOUND]`);
});

0 comments on commit ac7ab5d

Please sign in to comment.