Skip to content

Commit e4fff56

Browse files
Essential Randomnessessential-randomness
authored andcommitted
mock endpoints for series tests
1 parent 4f81425 commit e4fff56

File tree

10 files changed

+2306
-25
lines changed

10 files changed

+2306
-25
lines changed

src/series/getters.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Author, Series, SeriesWorkSummary } from "types/entities";
2-
import { SeriesPage, WorkPage } from "../page-loaders";
32
import { CheerioAPI, load } from "cheerio";
4-
import { getWorkDetailsFromUrl, getWorkUrl } from "src/urls";
3+
import { SeriesPage, WorkPage } from "../page-loaders";
54
import {
65
getWorkBookmarkCount,
76
getWorkHits,
@@ -11,6 +10,7 @@ import {
1110
getWorkTotalChapters,
1211
getWorkWordCount,
1312
} from "src/works/work-getters";
13+
import { getWorkDetailsFromUrl, getWorkUrl } from "src/urls";
1414

1515
const monthMap: { [month: string]: string } = {
1616
Jan: "01",
@@ -65,7 +65,7 @@ export const getSeriesDescription = (
6565
export const getSeriesNotes = ($seriesPage: SeriesPage): string | null => {
6666
const notes = $seriesPage("dl.series dd:nth-of-type(5)");
6767
if (notes.prevAll().first().text().trim() === "Notes:") {
68-
return notes.html().trim();
68+
return notes.html()!.trim();
6969
} else {
7070
return null;
7171
}
@@ -136,7 +136,7 @@ const getSeriesWork = (workHtml: string): SeriesWorkSummary => {
136136
const publishedChapters = getWorkPublishedChapters($$work);
137137

138138
const url = $work("a[href*='/works/']").attr("href") as string;
139-
const id = getWorkDetailsFromUrl({ url }).workId
139+
const id = getWorkDetailsFromUrl({ url }).workId;
140140

141141
return {
142142
id,

src/works/work-getters.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ export const getWorkSeries = ($workPage: WorkPage): BasicSeries[] => {
152152
const link = seriesHtml.find("a:not(.next, .previous)");
153153

154154
series[index] = {
155-
id: link.attr("href").replace("/series/", ""),
155+
id: link.attr("href")!.replace("/series/", ""),
156156
name: link.text().trim(),
157-
index: matches.length > 0 ? parseInt(matches[0]) : -1,
157+
index: matches!.length > 0 ? parseInt(matches![0]) : -1,
158158
};
159159
});
160160

tests/mocks/data/series/1728802/index.html

Lines changed: 487 additions & 0 deletions
Large diffs are not rendered by default.

tests/mocks/data/series/2270465/index.html

Lines changed: 665 additions & 0 deletions
Large diffs are not rendered by default.

tests/mocks/data/series/2817877/index.html

Lines changed: 688 additions & 0 deletions
Large diffs are not rendered by default.

tests/mocks/data/series/2946579/index.html

Lines changed: 424 additions & 0 deletions
Large diffs are not rendered by default.

tests/mocks/handlers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import allHandlers from "./handlers/all";
33
import feedHandlers from "./handlers/tags/feed";
44
import nameHandlers from "./handlers/tags/name";
55
import profileHandlers from "./handlers/users/profile";
6+
import seriesHandlers from "./handlers/series";
67
import tagWorksHandlers from "./handlers/tags/works";
78
import workPageHandlers from "./handlers/works";
89
import worksHandlers from "./handlers/works";
@@ -17,5 +18,6 @@ export default [
1718
nameHandlers,
1819
workPageHandlers,
1920
worksNavigateHandlers,
21+
seriesHandlers,
2022
allHandlers,
2123
];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { fileURLToPath } from "url";
2+
import filenamify from "filenamify";
3+
import fs from "fs";
4+
import path from "path";
5+
import { rest } from "msw";
6+
7+
const SERIES_DATA_DIR = path.resolve(
8+
fileURLToPath(import.meta.url),
9+
"../../../data/series"
10+
);
11+
12+
export default rest.all(
13+
"https://archiveofourown.org/series/:series_id",
14+
(req, res, ctx) => {
15+
const html = fs.readFileSync(
16+
path.resolve(
17+
SERIES_DATA_DIR,
18+
filenamify(req.params.series_id as string),
19+
"index.html"
20+
)
21+
);
22+
23+
return res(ctx.set("Content-Type", "text/html"), ctx.body(html));
24+
}
25+
);

tests/series.test.ts

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ describe("Fetches series information", () => {
99
expect(series).toMatchObject({
1010
id: "2270465",
1111
name: "OG Titan",
12-
begunAt: "2021-04-11",
12+
startedAt: "2021-04-11",
1313
updatedAt: "2023-02-13",
14-
creators: [{ username: "MyHero", pseud: "MyHero" }],
14+
authors: [{ username: "MyHero", pseud: "MyHero" }],
1515
description:
1616
"<p>My potentially related stories about the relationship between the OG Titans. Probably focused on Dick Grayson.</p>",
1717
notes: null,
@@ -354,22 +354,6 @@ describe("Fetches series information", () => {
354354
stats: { bookmarks: 28, kudos: 242, hits: 1 },
355355
},
356356
],
357-
workTitles: [
358-
"Away from all of Reality",
359-
"Code B",
360-
"Donna Troy Loves You",
361-
"Rockin Robin",
362-
"Realistic Exit Strategy",
363-
"You starting down the road leaving me again",
364-
],
365-
workUrls: [
366-
"https://archiveofourown.org/works/30604247",
367-
"https://archiveofourown.org/works/30794750",
368-
"https://archiveofourown.org/works/30914645",
369-
"https://archiveofourown.org/works/31221131",
370-
"https://archiveofourown.org/works/35757790",
371-
"https://archiveofourown.org/works/44149795",
372-
],
373357
});
374358
});
375359

types/entities.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ export interface User {
4141
export interface SeriesWorkSummary
4242
extends Omit<
4343
WorkSummary,
44-
"category" | "publishedAt" | "rating" | "tags" | "stats" | "locked"
44+
| "category"
45+
| "publishedAt"
46+
| "rating"
47+
| "tags"
48+
| "stats"
49+
| "locked"
50+
| "series"
4551
> {
4652
url: string;
4753
tags: Omit<WorkSummary["tags"], "warnings">;

0 commit comments

Comments
 (0)