Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more test cases for anomalous conditions #10

Merged
merged 1 commit into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/Bio/Bio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe("Bio", () => {
expect(result).toContain(`>${props.name}</span>`);
expect(result).toContain(`>${props.description}</span>`);
expect(result).toContain(`<img src="${props.icon}"`);
expect(result).toContain(`Profile picture of ${props.name}`);
});
}
});
39 changes: 32 additions & 7 deletions src/components/Contact/Contact.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,37 @@ import Contact from "./Contact.astro";
import * as stories from "./story.ts";

describe("Contact", () => {
for (const [name, props] of Object.entries(stories)) {
test(name, async () => {
const container: AstroContainer = await AstroContainer.create();
const result: string = await container.renderToString(Contact, { props });
describe("Valid service and ID", () => {
for (const [name, props] of Object.entries(stories)) {
test(name, async () => {
const container: AstroContainer = await AstroContainer.create();
const result: string = await container.renderToString(Contact, {
props,
});

expect(result).toContain(`>${props.id}</span>`);
});
}
expect(result).toContain(`>${props.id}</span>`);
expect(result).toMatch(new RegExp(`href=["'].*${props.id}["']`));
expect(result).toContain('<use href="#ai:');
expect(result).toContain("aria-label");
});
}
});

describe("Invalid service or ID", () => {
const invalidProps: { service: string; id: string }[] = [
{ service: "Mastodon", id: "invalid-id" },
{ service: "Misskey", id: "invalid-id" },
{ service: "invalid-service", id: "id" },
];

for (const [_, props] of Object.entries(invalidProps)) {
test(props.service, async () => {
const container: AstroContainer = await AstroContainer.create();

await expect(
container.renderToString(Contact, { props }),
).rejects.toThrow();
});
}
});
});
41 changes: 28 additions & 13 deletions src/components/Homepage/Homepage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,37 @@ import { experimental_AstroContainer as AstroContainer } from "astro/container";
import { describe, expect, test } from "vitest";

import Homepage from "./Homepage.astro";
import type { Props } from "./Homepage.astro";
import * as stories from "./story.ts";

describe("Homepage", () => {
for (const [name, props] of Object.entries(stories)) {
test(name, async () => {
const container: AstroContainer = await AstroContainer.create();
const result: string = await container.renderToString(Homepage, {
props,
describe("Valid URL", () => {
for (const [name, props] of Object.entries(stories)) {
test(name, async () => {
const container: AstroContainer = await AstroContainer.create();
const result: string = await container.renderToString(Homepage, {
props,
});
const url = new URL(props.url);

expect(result).toContain(
`> ${encodeURI(url.hostname)}${
url.pathname === "/" ? "" : url.pathname
} </span>`,
);
expect(result).toContain('<use href="#ai:');
expect(result).toContain("aria-label");
});
const url = new URL(props.url);
}
});

test("Invalid URL", async () => {
const container: AstroContainer = await AstroContainer.create();

expect(result).toContain(
`> ${encodeURI(url.hostname)}${
url.pathname === "/" ? "" : url.pathname
} </span>`,
);
});
}
await expect(
container.renderToString(Homepage, {
props: { url: "not-valid-url" } satisfies Props,
}),
).rejects.toThrow();
});
});
24 changes: 19 additions & 5 deletions src/libs/contact.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import { type service, services } from "../types/services";

const isFediverse = (service: service): boolean => {
return service === "Mastodon" || service === "Misskey";
};

type contact = { url: string; icon: string };

export const getContact = (service: service, id: string): contact => {
const url: string =
service === "Mastodon" || service === "Misskey"
? `https://${id.split("@")[2]}/${id}`
: services[service].url + id;
return { url, icon: services[service].icon };
const specified: contact = services[service];

if (!specified) throw new Error(`Unsupported service: ${service}`);

if (isFediverse(service)) {
const parts: string[] = id.split("@");
if (parts.length !== 3 || parts[0] !== "") {
throw new Error(`Invalid ${service} ID format`);
}
}

const url: string = isFediverse(service)
? `https://${id.split("@")[2]}/${id}`
: specified.url + id;
return { url, icon: specified.icon };
};