-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
21744c4
commit 88090d3
Showing
5 changed files
with
141 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import type { PageParams } from "./common"; | ||
|
||
export interface AddOnParams extends PageParams { | ||
query?: boolean; | ||
query?: string; | ||
active?: boolean; | ||
default?: boolean; | ||
featured?: boolean; | ||
premium?: boolean; | ||
category?: string; | ||
respository?: string; | ||
repository?: string; | ||
} |
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,95 @@ | ||
import { vi, test, describe, it, expect, beforeEach, afterEach } from "vitest"; | ||
import * as addons from "./addons"; | ||
import { BASE_API_URL } from "@/config/config"; | ||
import { addonsList } from "@/test/fixtures/addons"; | ||
import { emptyList } from "@/test/fixtures/common"; | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
describe("getAddons", () => { | ||
let mockFetch; | ||
beforeEach(() => { | ||
mockFetch = vi | ||
.fn() | ||
.mockResolvedValue({ ok: true, json: async () => addonsList }); | ||
}); | ||
it("calls the addons API endpoint", async () => { | ||
await addons.getAddons({}, mockFetch); | ||
const expectedEndpoint = new URL(`addons`, BASE_API_URL); | ||
expect(mockFetch).toHaveBeenCalledWith( | ||
expectedEndpoint, | ||
expect.any(Object), | ||
); | ||
}); | ||
it("passes parameters through as query arguments", async () => { | ||
await addons.getAddons( | ||
{ active: true, featured: true, query: "foobar" }, | ||
mockFetch, | ||
); | ||
const expectedEndpoint = new URL(`addons`, BASE_API_URL); | ||
expectedEndpoint.searchParams.set("active", "true"); | ||
expectedEndpoint.searchParams.set("featured", "true"); | ||
expectedEndpoint.searchParams.set("query", "foobar"); | ||
expect(mockFetch).toHaveBeenCalledWith( | ||
expectedEndpoint, | ||
expect.any(Object), | ||
); | ||
}); | ||
it("returns the full list", async () => { | ||
const response = await addons.getAddons({}, mockFetch); | ||
expect(response).toBe(addonsList); | ||
}); | ||
it("calls SvelteKit's error fn given a response error", async () => { | ||
mockFetch = vi.fn().mockResolvedValue({ | ||
ok: false, | ||
status: 500, | ||
statusText: "Server Error!", | ||
}); | ||
await expect(addons.getAddons({}, mockFetch)).rejects.toThrowError(); | ||
}); | ||
}); | ||
|
||
test("getPinnedAddons", async () => { | ||
const mockFetch = vi | ||
.fn() | ||
.mockResolvedValue({ ok: true, json: async () => {} }); | ||
await addons.getPinnedAddons(mockFetch); | ||
expect(mockFetch).toHaveBeenCalledWith( | ||
new URL(`addons?active=true`, BASE_API_URL), | ||
expect.any(Object), | ||
); | ||
}); | ||
|
||
describe("getAddon", async () => { | ||
let mockFetch; | ||
beforeEach(() => { | ||
mockFetch = vi.fn().mockResolvedValue({ | ||
ok: true, | ||
json: async () => addonsList, | ||
}); | ||
}); | ||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
it("calls the addons list endpoint with a respository query argument", async () => { | ||
await addons.getAddon("MuckRock", "addon-repo", mockFetch); | ||
expect(mockFetch).toHaveBeenCalledWith( | ||
new URL(`addons?repository=MuckRock%2Faddon-repo`, BASE_API_URL), | ||
expect.any(Object), | ||
); | ||
}); | ||
it("returns the first result in the addon list", async () => { | ||
const response = await addons.getAddon("MuckRock", "addon-repo", mockFetch); | ||
expect(response).toEqual(addonsList.results[0]); | ||
}); | ||
it("returns null if the returned list is empty", async () => { | ||
mockFetch = vi.fn().mockResolvedValue({ | ||
ok: true, | ||
json: async () => emptyList, | ||
}); | ||
const response = await addons.getAddon("MuckRock", "addon-repo", mockFetch); | ||
expect(response).toEqual(null); | ||
}); | ||
}); |
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,18 @@ | ||
import { breadcrumbTrail } from "$lib/utils/navigation"; | ||
import { getAddon } from "@/lib/api/addons.js"; | ||
import { error } from "@sveltejs/kit"; | ||
|
||
export async function load({ url, params, fetch, parent }) { | ||
const { owner, repo } = params; | ||
const addon = await getAddon(owner, repo, fetch); | ||
if (!addon) { | ||
return error(404, "Add-On Not Found"); | ||
} | ||
const breadcrumbs = await breadcrumbTrail(parent, [ | ||
{ href: url.pathname, title: addon.name }, | ||
]); | ||
return { | ||
addon, | ||
breadcrumbs, | ||
}; | ||
} |
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,12 @@ | ||
<script lang="ts"> | ||
import MainLayout from "@/lib/components/MainLayout.svelte"; | ||
export let data; | ||
</script> | ||
|
||
<MainLayout> | ||
<svelte:fragment slot="content"> | ||
<h1>{data.addon.name}</h1> | ||
<p>Placeholder</p> | ||
</svelte:fragment> | ||
</MainLayout> |