From f60abe9aad631d9e69b677ca47106daa22342b40 Mon Sep 17 00:00:00 2001 From: Souma <101255979+5ouma@users.noreply.github.com> Date: Fri, 8 Nov 2024 23:15:38 +0900 Subject: [PATCH] test(component): Add more test cases for anomalous conditions (#10) Some conditions must cause errors. --- src/components/Bio/Bio.test.ts | 1 + src/components/Contact/Contact.test.ts | 39 ++++++++++++++++++---- src/components/Homepage/Homepage.test.ts | 41 ++++++++++++++++-------- src/libs/contact.ts | 24 +++++++++++--- 4 files changed, 80 insertions(+), 25 deletions(-) diff --git a/src/components/Bio/Bio.test.ts b/src/components/Bio/Bio.test.ts index 48aac20..341df0b 100644 --- a/src/components/Bio/Bio.test.ts +++ b/src/components/Bio/Bio.test.ts @@ -13,6 +13,7 @@ describe("Bio", () => { expect(result).toContain(`>${props.name}`); expect(result).toContain(`>${props.description}`); expect(result).toContain(` { - 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}`); - }); - } + expect(result).toContain(`>${props.id}`); + expect(result).toMatch(new RegExp(`href=["'].*${props.id}["']`)); + expect(result).toContain(' { + 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(); + }); + } + }); }); diff --git a/src/components/Homepage/Homepage.test.ts b/src/components/Homepage/Homepage.test.ts index 613f3bf..5691659 100644 --- a/src/components/Homepage/Homepage.test.ts +++ b/src/components/Homepage/Homepage.test.ts @@ -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 + } `, + ); + expect(result).toContain(' { + const container: AstroContainer = await AstroContainer.create(); - expect(result).toContain( - `> ${encodeURI(url.hostname)}${ - url.pathname === "/" ? "" : url.pathname - } `, - ); - }); - } + await expect( + container.renderToString(Homepage, { + props: { url: "not-valid-url" } satisfies Props, + }), + ).rejects.toThrow(); + }); }); diff --git a/src/libs/contact.ts b/src/libs/contact.ts index 2d8d1bc..d1f26d1 100644 --- a/src/libs/contact.ts +++ b/src/libs/contact.ts @@ -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 }; };