Skip to content

Commit 6a7ac49

Browse files
committed
Added addon jest tests
1 parent a234ea3 commit 6a7ac49

24 files changed

+343
-63
lines changed

examples/typescript/src/example1.ts

+18-15
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import {
2-
ApiItemRequest,
3-
ApiSourceRequest,
4-
ApiSubtitleRequest,
52
createWorkerAddon,
6-
Item,
3+
ItemRequest,
4+
MainItem,
5+
PlayableItem,
76
Source,
8-
Subtitle
9-
} from "../../../dist";
7+
SourceRequest,
8+
Subtitle,
9+
SubtitleRequest
10+
} from "../../../src";
1011

11-
const EXAMPLE_ITEMS: Item[] = [
12+
// Export needed for tests
13+
export const EXAMPLE_ITEMS: PlayableItem[] = [
1214
{
1315
type: "movie",
1416
ids: {
@@ -38,7 +40,8 @@ type ExampleSources = {
3840
[k: string]: Source[];
3941
};
4042

41-
const EXAMPLE_SOURCES: ExampleSources = {
43+
// Export needed for tests
44+
export const EXAMPLE_SOURCES: ExampleSources = {
4245
id1235: [
4346
{
4447
type: "url",
@@ -68,7 +71,8 @@ type ExampleSubtitle = {
6871
[k: string]: Subtitle[];
6972
};
7073

71-
const EXAMPLE_SUBTITLES: ExampleSubtitle = {
74+
// Export needed for tests
75+
export const EXAMPLE_SUBTITLES: ExampleSubtitle = {
7276
elephant: [
7377
{
7478
id: "vtt",
@@ -89,7 +93,7 @@ const EXAMPLE_SUBTITLES: ExampleSubtitle = {
8993
]
9094
};
9195

92-
const addon = createWorkerAddon({
96+
export const addon = createWorkerAddon({
9397
id: "example1",
9498
name: "Typescript Example Addon",
9599
version: "1.0.0",
@@ -101,22 +105,21 @@ const addon = createWorkerAddon({
101105
hasMore: false
102106
};
103107
})
104-
.registerActionHandler("item", async (args: ApiItemRequest, ctx) => {
108+
.registerActionHandler("item", async (args: ItemRequest, ctx) => {
105109
const id = args.ids["example1"];
106110
const item = EXAMPLE_ITEMS.find(item => item.ids["example1"] === id);
107111
if (!item) throw new Error("Not found");
108112
return item;
109113
})
110-
.registerActionHandler("source", async (args: ApiSourceRequest, ctx) => {
114+
.registerActionHandler("source", async (args: SourceRequest, ctx) => {
111115
const id = args.ids["example1"];
112116
const sources = EXAMPLE_SOURCES[id];
113117
return sources ?? [];
114118
})
115-
.registerActionHandler("subtitle", async (args: ApiSubtitleRequest, ctx) => {
119+
.registerActionHandler("subtitle", async (args: SubtitleRequest, ctx) => {
116120
// ids.id is an alias for ids["addon-id"]
117121
const id = args.ids.id;
122+
await ctx.requestCache(id);
118123
const subtitles = EXAMPLE_SUBTITLES[id];
119124
return subtitles ?? [];
120125
});
121-
122-
export default addon;
+3-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { createRepositoryAddon, serveAddons } from "../../../dist";
1+
import { createRepositoryAddon } from "../../../src";
2+
import { addon as exampleAddon } from "./example1";
23

3-
import exampleAddon from "./example1";
4-
5-
const addon = createRepositoryAddon({
4+
export const addon = createRepositoryAddon({
65
id: "example-repo",
76
name: "Example Repository",
87
version: "1.0.0"
@@ -11,5 +10,3 @@ const addon = createRepositoryAddon({
1110
addon.addUrl("https://addons.watched.com/js/tmdb");
1211
addon.addUrl("https://addons.watched.com/js/archive.org");
1312
addon.addAddon(exampleAddon);
14-
15-
export default addon;

examples/typescript/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import example1 from "./example1";
2-
import exampleRepo from "./exampleRepo";
1+
import { addon as example1 } from "./example1";
2+
import { addon as exampleRepo } from "./exampleRepo";
33

44
export { example1, exampleRepo };

jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
preset: "ts-jest",
3+
testEnvironment: "node",
4+
setupFilesAfterEnv: ["./jest.setup.ts"]
5+
};

jest.setup.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
jest.mock("./src/utils/signature", () => ({
2+
validateSignature(sig: string) {
3+
return {};
4+
}
5+
}));

package-lock.json

+178
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)