-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Elysia from "elysia"; | ||
import { buildUrl } from "tests/utils"; | ||
import { base } from "~/base"; | ||
import { movies } from "~/controllers/movies"; | ||
import { seed } from "~/controllers/seed"; | ||
import type { SeedMovie } from "~/models/movie"; | ||
|
||
export const movieApp = new Elysia().use(base).use(movies).use(seed); | ||
|
||
export const getMovie = async (id: string, langs?: string) => { | ||
const resp = await movieApp.handle( | ||
new Request(`http://localhost/movies/${id}`, { | ||
method: "GET", | ||
headers: langs | ||
? { | ||
"Accept-Language": langs, | ||
} | ||
: {}, | ||
}), | ||
); | ||
const body = await resp.json(); | ||
return [resp, body] as const; | ||
}; | ||
|
||
export const getMovies = async ({ | ||
langs, | ||
...query | ||
}: { | ||
filter?: string; | ||
limit?: number; | ||
after?: string; | ||
sort?: string | string[]; | ||
langs?: string; | ||
}) => { | ||
const resp = await movieApp.handle( | ||
new Request(buildUrl("movies", query), { | ||
method: "GET", | ||
headers: langs | ||
? { | ||
"Accept-Language": langs, | ||
} | ||
: {}, | ||
}), | ||
); | ||
const body = await resp.json(); | ||
return [resp, body] as const; | ||
}; | ||
|
||
export const createMovie = async (movie: SeedMovie) => { | ||
const resp = await movieApp.handle( | ||
new Request("http://localhost/movies", { | ||
method: "POST", | ||
body: JSON.stringify(movie), | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}), | ||
); | ||
const body = await resp.json(); | ||
return [resp, body] as const; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { expect } from "bun:test"; | ||
import Elysia from "elysia"; | ||
|
||
export function expectStatus(resp: Response, body: object) { | ||
const matcher = expect({ ...body, status: resp.status }); | ||
return { | ||
toBe: (status: number) => { | ||
matcher.toMatchObject({ status: status }); | ||
}, | ||
}; | ||
} | ||
|
||
export const buildUrl = (route: string, query: Record<string, any>) => { | ||
const params = new URLSearchParams(); | ||
for (const [key, value] of Object.entries(query)) { | ||
if (!Array.isArray(value)) { | ||
params.append(key, value.toString()); | ||
continue; | ||
} | ||
for (const v of value) params.append(key, v.toString()); | ||
} | ||
return `http://localhost/${route}?${params}`; | ||
}; |