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

feat: support link text #23

Merged
merged 8 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
163 changes: 21 additions & 142 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
"esm-env": "^1.0.0"
},
"devDependencies": {
"@prismicio/client": "^7.4.0",
"@prismicio/mock": "^0.3.1",
"@prismicio/client": "7.9.0-alpha.2",
"@prismicio/mock": "0.3.8-alpha.0",
"@size-limit/preset-small-lib": "^11.0.1",
"@sveltejs/kit": "^2.0.0",
"@sveltejs/package": "^2.2.3",
Expand Down Expand Up @@ -106,5 +106,9 @@
},
"publishConfig": {
"access": "public"
},
"overrides": {
"@prismicio/client": "7.9.0-alpha.2",
"@prismicio/mock": "0.3.8-alpha.0"
}
}
8 changes: 4 additions & 4 deletions src/PrismicLink.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import {
asLinkAttrs,
isFilled,
angeloashmore marked this conversation as resolved.
Show resolved Hide resolved
type AsLinkAttrsConfig,
type LinkField,
type PrismicDocument,
Expand Down Expand Up @@ -41,6 +42,7 @@
});

$: resolvedRel = typeof rel === "string" ? rel : linkAttrs.rel;
$: resolvedText = isFilled.link(field) ? field.text : undefined;
</script>

<!--
Expand All @@ -49,9 +51,7 @@

@example Rendering a Link field:
```svelte
<PrismicLink field={document.data.example_link}>
Example anchor text.
</PrismicLink>
<PrismicLink field={document.data.example_link} />
angeloashmore marked this conversation as resolved.
Show resolved Hide resolved
```
-->

Expand All @@ -62,5 +62,5 @@
on:click
{...$$restProps}
>
<slot />
<slot>{resolvedText}</slot>
</a>
27 changes: 27 additions & 0 deletions test/PrismicLink.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @vitest-environment happy-dom
import { expect, it } from "vitest";

import { render } from "@testing-library/svelte";

import PrismicLinkTestWrapper from "./PrismicLinkTestWrapper.svelte";

it("renders the link's text if no children are provided", (ctx) => {
const model = ctx.mock.model.link({ text: true, allowTargetBlank: false });
const field = ctx.mock.value.link({ type: "Web", model });
const { container } = render(PrismicLinkTestWrapper, { field });

expect(container.innerHTML).toBe(
`<div><a href="${field.url}" rel="noreferrer">${field.text}</a></div>`,
);
});

it("renders the given children, overriding the link's text", (ctx) => {
const model = ctx.mock.model.link({ text: true, allowTargetBlank: false });
const field = ctx.mock.value.link({ type: "Web", model });
const children = ctx.mock.value.keyText();
const { container } = render(PrismicLinkTestWrapper, { field, children });

expect(container.innerHTML).toBe(
`<div><a href="${field.url}" rel="noreferrer">${children}</a></div>`,
);
});
18 changes: 18 additions & 0 deletions test/PrismicLinkTestWrapper.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!--
This file is a wrapper for the PrismicLink component to be used in tests as
passing slot content to the PrismicLink component is not easy in tests.
-->

<script lang="ts">
import { FilledLinkToWebField } from "@prismicio/client";
import { PrismicLink } from "../src";

export let field: FilledLinkToWebField;
export let children: string | undefined = undefined;
</script>

{#if children}
<PrismicLink {field}>{children}</PrismicLink>
{:else}
<PrismicLink {field} />
{/if}
Loading