Create simple watchlist tests

This commit is contained in:
Zoe Roux 2025-04-07 09:55:12 +02:00
parent bf361a79d1
commit b3edf31afc
No known key found for this signature in database
5 changed files with 193 additions and 0 deletions

View File

@ -1,5 +1,6 @@
export * from "./movies-helper";
export * from "./series-helper";
export * from "./shows-helper";
export * from "./studio-helper";
export * from "./staff-helper";
export * from "./videos-helper";

View File

@ -1,6 +1,7 @@
import { buildUrl } from "tests/utils";
import { app } from "~/base";
import type { SeedMovie } from "~/models/movie";
import type { MovieWatchStatus } from "~/models/watchlist";
import { getJwtHeaders } from "./jwt";
export const getMovie = async (
@ -66,3 +67,21 @@ export const createMovie = async (movie: SeedMovie) => {
const body = await resp.json();
return [resp, body] as const;
};
export const setMovieStatus = async (
id: string,
status: Omit<MovieWatchStatus, "percent">,
) => {
const resp = await app.handle(
new Request(buildUrl(`movies/${id}/watchstatus`), {
method: "POST",
body: JSON.stringify(status),
headers: {
"Content-Type": "application/json",
...(await getJwtHeaders()),
},
}),
);
const body = await resp.json();
return [resp, body] as const;
};

View File

@ -2,6 +2,7 @@ import { buildUrl } from "tests/utils";
import { app } from "~/base";
import type { SeedSerie } from "~/models/serie";
import { getJwtHeaders } from "./jwt";
import { SerieWatchStatus } from "~/models/watchlist";
export const createSerie = async (serie: SeedSerie) => {
const resp = await app.handle(
@ -162,3 +163,21 @@ export const getNews = async ({
const body = await resp.json();
return [resp, body] as const;
};
export const setSerieStatus = async (
id: string,
status: SerieWatchStatus
) => {
const resp = await app.handle(
new Request(buildUrl(`movies/${id}/watchstatus`), {
method: "POST",
body: JSON.stringify(status),
headers: {
"Content-Type": "application/json",
...(await getJwtHeaders()),
},
}),
);
const body = await resp.json();
return [resp, body] as const;
};

View File

@ -0,0 +1,60 @@
import { buildUrl } from "tests/utils";
import { app } from "~/base";
import { getJwtHeaders } from "./jwt";
export const getShows = async ({
langs,
...query
}: {
filter?: string;
limit?: number;
after?: string;
sort?: string | string[];
query?: string;
langs?: string;
preferOriginal?: boolean;
}) => {
const resp = await app.handle(
new Request(buildUrl("shows", query), {
method: "GET",
headers: langs
? {
"Accept-Language": langs,
...(await getJwtHeaders()),
}
: await getJwtHeaders(),
}),
);
const body = await resp.json();
return [resp, body] as const;
};
export const getWatchlist = async (
id: string,
{
langs,
...query
}: {
filter?: string;
limit?: number;
after?: string;
sort?: string | string[];
query?: string;
langs?: string;
preferOriginal?: boolean;
},
) => {
const resp = await app.handle(
new Request(buildUrl(`profiles/${id}/watchlist`, query), {
method: "GET",
headers: langs
? {
"Accept-Language": langs,
...(await getJwtHeaders()),
}
: await getJwtHeaders(),
}),
);
const body = await resp.json();
return [resp, body] as const;
};

View File

@ -0,0 +1,94 @@
import { beforeAll, describe, expect, it } from "bun:test";
import {
createMovie,
getMovie,
getShows,
getWatchlist,
setMovieStatus,
} from "tests/helpers";
import { expectStatus } from "tests/utils";
import { db } from "~/db";
import { shows } from "~/db/schema";
import { bubble } from "~/models/examples";
beforeAll(async () => {
await db.delete(shows);
const [ret, body] = await createMovie(bubble);
expectStatus(ret, body).toBe(201);
});
describe("Set & get watch status", () => {
it("Creates watchlist entry", async () => {
let [resp, body] = await getWatchlist("me", {});
expectStatus(resp, body).toBe(200);
expect(body.items).toBeArrayOfSize(0);
const [r, b] = await setMovieStatus(bubble.slug, {
status: "completed",
completedAt: "2024-12-21",
score: 85,
});
expectStatus(r, b).toBe(201);
[resp, body] = await getWatchlist("me", {});
expectStatus(resp, body).toBe(200);
expect(body.items).toBeArrayOfSize(1);
expect(body.items[0].slug).toBe(bubble.slug);
expect(body.items[0].watchStatus).toMatchObject({
status: "completed",
completedAt: "2024-12-21",
score: 85,
percent: 100,
});
});
it("Edit watchlist entry", async () => {
let [resp, body] = await getWatchlist("me", {});
expectStatus(resp, body).toBe(200);
expect(body.items).toBeArrayOfSize(1);
const [r, b] = await setMovieStatus(bubble.slug, {
status: "rewatching",
// we still need to specify all values
completedAt: "2024-12-21",
score: 85,
});
expectStatus(r, b).toBe(201);
[resp, body] = await getWatchlist("me", {});
expectStatus(resp, body).toBe(200);
expect(body.items).toBeArrayOfSize(1);
expect(body.items[0].slug).toBe(bubble.slug);
expect(body.items[0].watchStatus).toMatchObject({
status: "completed",
completedAt: "2024-12-21",
score: 85,
percent: 0,
});
});
it("Return watchstatus in /shows", async () => {
const [resp, body] = await getShows({});
expectStatus(resp, body).toBe(200);
expect(body.items).toBeArrayOfSize(1);
expect(body.items[0].slug).toBe(bubble.slug);
expect(body.items[0].watchStatus).toMatchObject({
status: "completed",
completedAt: "2024-12-21",
score: 85,
percent: 0,
});
});
it("Return watchstatus in /movies/:id", async () => {
const [resp, body] = await getMovie(bubble.slug, {});
expectStatus(resp, body).toBe(200);
expect(body.slug).toBe(bubble.slug);
expect(body.watchStatus).toMatchObject({
status: "completed",
completedAt: "2024-12-21",
score: 85,
percent: 0,
});
});
});