-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppAnchorButton.spec.js
More file actions
64 lines (49 loc) · 1.99 KB
/
Copy pathAppAnchorButton.spec.js
File metadata and controls
64 lines (49 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { afterEach, describe, expect, it, vi } from "vitest";
import { cleanup, fireEvent, render } from "@testing-library/svelte";
import { base } from "$app/paths";
import { AppAnchorButton } from "..";
describe("AppAnchorButton", () => {
const baseProps = {
className: "foo bar",
href: "/setup",
id: "some-id",
};
afterEach(cleanup);
it("should render an `AnchorButton` with the base path prepended to the `href` attribute, if the `href` represents an absolute URL", async () => {
const { container, getByRole, rerender } = render(
AppAnchorButton,
baseProps
);
const anchorA = getByRole("link");
expect(container.firstElementChild).toMatchSnapshot();
expect(anchorA).toHaveAttribute("href", `${base}${baseProps.href}`);
expect(anchorA).toHaveClass("foo bar");
expect(anchorA).toHaveAttribute("id", baseProps.id);
await rerender({ ...baseProps, href: "/" });
const anchorB = getByRole("link");
expect(anchorB).toHaveAttribute("href", `${base}/`);
expect(anchorB).toHaveClass("foo bar");
expect(anchorB).toHaveAttribute("id", baseProps.id);
});
it("should leave the `AnchorButton` as it is if the `href` points to a relative path", () => {
const { getByRole } = render(AppAnchorButton, {
...baseProps,
href: "foo/bar",
});
expect(getByRole("link")).toHaveAttribute("href", "foo/bar");
});
it("should leave the `AnchorButton` as it is if the `href` points to an external URL", () => {
const href = "http://example.com";
const { getByRole } = render(AppAnchorButton, { ...baseProps, href });
expect(getByRole("link")).toHaveAttribute("href", href);
});
it("should forward the `onclick` event to the `AnchorButton`", async () => {
const handler = vi.fn();
const { getByRole } = render(AppAnchorButton, {
events: { click: handler },
props: { ...baseProps, href: "#" },
});
await fireEvent.click(getByRole("link"));
expect(handler).toHaveBeenCalledTimes(1);
});
});