Skip to content

Commit 53029d3

Browse files
committed
fix: rewrite tests for current version of the widget
1 parent c6924ba commit 53029d3

File tree

5 files changed

+143
-24
lines changed

5 files changed

+143
-24
lines changed

packages/pluggableWidgets/skiplink-web/e2e/SkipLink.spec.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/pluggableWidgets/skiplink-web/e2e/package.json

Whitespace-only changes.

packages/pluggableWidgets/skiplink-web/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"@mendix/prettier-config-web-widgets": "workspace:*",
5151
"@mendix/run-e2e": "workspace:*",
5252
"@mendix/widget-plugin-hooks": "workspace:*",
53-
"@mendix/widget-plugin-platform": "workspace:*"
53+
"@mendix/widget-plugin-platform": "workspace:*",
54+
"@mendix/widget-plugin-test-utils": "workspace:*"
5455
}
5556
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import "@testing-library/jest-dom";
2+
import { render } from "@testing-library/react";
3+
import { SkipLinkContainerProps } from "../../typings/SkipLinkProps";
4+
import { SkipLink } from "../SkipLink";
5+
6+
describe("SkipLink", () => {
7+
let defaultProps: SkipLinkContainerProps;
8+
let rootElement: HTMLElement;
9+
10+
beforeEach(() => {
11+
// Set up the DOM structure that SkipLink expects
12+
document.body.innerHTML = "";
13+
rootElement = document.createElement("div");
14+
rootElement.id = "root";
15+
document.body.appendChild(rootElement);
16+
17+
defaultProps = {
18+
name: "SkipLink1",
19+
class: "mx-skiplink",
20+
style: {},
21+
linkText: "Skip to main content",
22+
mainContentId: "main-content"
23+
};
24+
});
25+
26+
afterEach(() => {
27+
document.body.innerHTML = "";
28+
});
29+
30+
it("renders skiplink widget and adds skip link to DOM", () => {
31+
render(<SkipLink {...defaultProps} />);
32+
33+
// Check that the skip link was added to the root element
34+
const skipLink = rootElement.querySelector(".skip-link") as HTMLAnchorElement;
35+
expect(skipLink).toBeInTheDocument();
36+
expect(skipLink.textContent).toBe("Skip to main content");
37+
expect(skipLink.href).toBe(`${window.location.origin}/#main-content`);
38+
expect(skipLink.tabIndex).toBe(0);
39+
40+
// Snapshot the actual root element that contains the skip link
41+
expect(rootElement).toMatchSnapshot();
42+
});
43+
44+
it("renders with custom link text", () => {
45+
render(<SkipLink {...defaultProps} linkText="Jump to content" />);
46+
47+
const skipLink = rootElement.querySelector(".skip-link") as HTMLAnchorElement;
48+
expect(skipLink).toBeInTheDocument();
49+
expect(skipLink.textContent).toBe("Jump to content");
50+
51+
expect(rootElement).toMatchSnapshot();
52+
});
53+
54+
it("renders with custom main content id", () => {
55+
render(<SkipLink {...defaultProps} mainContentId="content-area" />);
56+
57+
const skipLink = rootElement.querySelector(".skip-link") as HTMLAnchorElement;
58+
expect(skipLink).toBeInTheDocument();
59+
expect(skipLink.href).toBe(`${window.location.origin}/#content-area`);
60+
61+
expect(rootElement).toMatchSnapshot();
62+
});
63+
64+
it("renders with empty main content id", () => {
65+
render(<SkipLink {...defaultProps} mainContentId="" />);
66+
67+
const skipLink = rootElement.querySelector(".skip-link") as HTMLAnchorElement;
68+
expect(skipLink).toBeInTheDocument();
69+
expect(skipLink.href).toBe(`${window.location.origin}/#`);
70+
71+
expect(rootElement).toMatchSnapshot();
72+
});
73+
74+
it("cleans up skip link when component unmounts", () => {
75+
const { unmount } = render(<SkipLink {...defaultProps} />);
76+
77+
// Verify skip link is present
78+
expect(rootElement.querySelector(".skip-link")).toBeInTheDocument();
79+
80+
// Unmount and verify cleanup
81+
unmount();
82+
expect(rootElement.querySelector(".skip-link")).not.toBeInTheDocument();
83+
});
84+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`SkipLink renders skiplink widget and adds skip link to DOM 1`] = `
4+
<div
5+
id="root"
6+
>
7+
<a
8+
class="skip-link"
9+
href="#main-content"
10+
tabindex="0"
11+
>
12+
Skip to main content
13+
</a>
14+
</div>
15+
`;
16+
17+
exports[`SkipLink renders with custom link text 1`] = `
18+
<div
19+
id="root"
20+
>
21+
<a
22+
class="skip-link"
23+
href="#main-content"
24+
tabindex="0"
25+
>
26+
Jump to content
27+
</a>
28+
</div>
29+
`;
30+
31+
exports[`SkipLink renders with custom main content id 1`] = `
32+
<div
33+
id="root"
34+
>
35+
<a
36+
class="skip-link"
37+
href="#content-area"
38+
tabindex="0"
39+
>
40+
Skip to main content
41+
</a>
42+
</div>
43+
`;
44+
45+
exports[`SkipLink renders with empty main content id 1`] = `
46+
<div
47+
id="root"
48+
>
49+
<a
50+
class="skip-link"
51+
href="#"
52+
tabindex="0"
53+
>
54+
Skip to main content
55+
</a>
56+
</div>
57+
`;

0 commit comments

Comments
 (0)