Skip to content

Commit

Permalink
feat: add link to homepage, if present
Browse files Browse the repository at this point in the history
  • Loading branch information
thislooksfun committed Mar 13, 2021
1 parent 852fe7b commit 20d5e1a
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
11 changes: 11 additions & 0 deletions src/Repo.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export let name: string;
export let url: string;
export let isFork: boolean;
export let homepage: string | undefined = undefined;
export let description: string | undefined = undefined;
export let sourceUrl: string | undefined = undefined;
export let sourceName: string | undefined = undefined;
Expand Down Expand Up @@ -56,6 +57,12 @@
&nbsp; <span data-testid="fork-count">{forks}</span>
</div>
{/if}

{#if homepage}
<div class="homepage" data-testid="homepage">
<a href={homepage} data-testid="homepage-link">{homepage}</a>
</div>
{/if}
</div>
</div>

Expand Down Expand Up @@ -88,6 +95,10 @@
}
}
.homepage {
color: var(--svc-text-link, #0366d6);
}
.small {
font-size: 0.85em;
}
Expand Down
3 changes: 3 additions & 0 deletions src/util/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import getColors from "./colors";
export interface RawRepo {
name: string;
html_url: string;
homepage: string;
description?: string;
language?: string;
fork: boolean;
Expand All @@ -19,6 +20,7 @@ export interface Repo {
name: string;
url: string;
isFork: boolean;
homepage?: string;
description?: string;
sourceUrl?: string;
sourceName?: string;
Expand All @@ -39,6 +41,7 @@ async function transform(repo: RawRepo): Promise<Repo> {
forks: repo.forks,
};

if (repo.homepage) out.homepage = repo.homepage;
if (repo.description != null) out.description = repo.description;
if (repo.source != null) {
out.sourceName = repo.source.full_name;
Expand Down
10 changes: 10 additions & 0 deletions test/RepoCard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ it("renders the card correctly", async () => {
} else {
expect(queryByTestId("forks")).not.toBeInTheDocument();
}

if (repo.homepage) {
expect(getByTestId("homepage")).toBeInTheDocument();
const hl = getByTestId("homepage-link");
expect(hl).toBeInTheDocument();
expect(hl.getAttribute("href")).toEqual(repo.homepage);
expect(hl.textContent).toEqual(repo.homepage);
} else {
expect(queryByTestId("homepage")).not.toBeInTheDocument();
}
})
);
});
Expand Down
14 changes: 13 additions & 1 deletion test/helper/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ import fc from "fast-check";
export function buildRaw(
name: string,
html_url: string,
homepage: string,
desc: string | null,
lang: string | null,
fork: boolean,
src: { full_name: string; html_url: string } | null,
stargazers_count: number,
forks: number
): RawRepo {
let raw: RawRepo = { name, html_url, fork, stargazers_count, forks };
let raw: RawRepo = {
name,
html_url,
homepage,
fork,
stargazers_count,
forks,
};
if (desc != null) raw.description = desc;
if (lang != null) raw.language = lang;
if (src != null) raw.source = src;
Expand All @@ -22,6 +30,7 @@ export function buildRaw(
export function buildRepo(
name: string,
url: string,
homepage: string | null,
desc: string | null,
lang: string | null,
isFork: boolean,
Expand All @@ -30,6 +39,7 @@ export function buildRepo(
forks: number
): Repo {
let repo: Repo = { name, url, isFork, stars, forks };
if (homepage != null) repo.homepage = homepage;
if (desc != null) repo.description = desc;
if (lang != null) {
repo.language = lang;
Expand All @@ -48,6 +58,7 @@ function _randRepoOwn(): fc.Arbitrary<Repo> {
{
name: fc.string(),
url: fc.webUrl(),
homepage: fc.webUrl(),
description: fc.string(),
language: fc.string(),
isFork: fc.constant(false),
Expand All @@ -63,6 +74,7 @@ function _randRepoFork(): fc.Arbitrary<Repo> {
{
name: fc.string(),
url: fc.webUrl(),
homepage: fc.webUrl(),
description: fc.string(),
language: fc.string(),
isFork: fc.constant(true),
Expand Down
8 changes: 5 additions & 3 deletions test/util/repo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ describe("getRepo()", () => {
fc.asyncProperty(
fc.string(), // name
fc.string(), // html_url
fc.option(fc.webUrl()), // homepage
fc.option(fc.string()), // description?
fc.option(fc.string()), // language?
fc.boolean(), // fork
Expand All @@ -38,9 +39,10 @@ describe("getRepo()", () => {
),
fc.integer(), // stargazers_count
fc.integer(), // forks
async (name, url, desc, lang, fork, src, sg, forks) => {
const raw = buildRaw(name, url, desc, lang, fork, src, sg, forks);
const repo = buildRepo(name, url, desc, lang, fork, src, sg, forks);
async (name, url, hp, desc, lang, frk, src, sg, frks) => {
const hpr = hp ? hp : "";
const raw = buildRaw(name, url, hpr, desc, lang, frk, src, sg, frks);
const repo = buildRepo(name, url, hp, desc, lang, frk, src, sg, frks);

if (lang != null) {
fetchColors.mockResolvedValue({
Expand Down

0 comments on commit 20d5e1a

Please sign in to comment.