-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtargeted-content.test.js
More file actions
57 lines (47 loc) · 1.7 KB
/
Copy pathtargeted-content.test.js
File metadata and controls
57 lines (47 loc) · 1.7 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
/**
* @vitest-environment jsdom
*/
import { screen, getByRole } from "@testing-library/dom";
import "@testing-library/jest-dom";
import initTargetedContent from "./targeted-content";
const componentHtml = `<div
class="cads-targeted-content js-cads-targeted-content"
data-descriptive-label-hide="close this section"
data-descriptive-label-show="show this section"
data-close-label="Close"
data-title-text="Targeted content title"
id="targeted-content-123">
<h2 class="cads-targeted-content__title js-cads-targeted-content__title">
<div class="cads-targeted-content__title-text">Targeted content title</div>
</h2>
<div
class="cads-targeted-content__content cads-prose js-cads-targeted-content__content"
id="targeted-content-123-content">
<p>Targeted content body</p>
</div>
</div>`;
test("allow toggling targeted content", () => {
document.body.innerHTML = componentHtml;
initTargetedContent();
const headingEl = screen.getByRole("heading");
const buttonEl = getByRole(headingEl, "button");
const parentEl = headingEl.parentElement;
expect(parentEl).toHaveClass("cads-targeted-content--toggleable");
function expectOpen() {
expect(parentEl).toHaveClass("cads-targeted-content--open");
expect(buttonEl).toHaveAttribute("aria-expanded", "true");
}
function expectClosed() {
expect(parentEl).not.toHaveClass("cads-targeted-content--open");
expect(buttonEl).toHaveAttribute("aria-expanded", "false");
}
expect(headingEl.innerHTML).toMatchSnapshot("closed");
expectClosed();
buttonEl.click();
expect(headingEl.innerHTML).toMatchSnapshot("open");
expectOpen();
buttonEl.click();
expectClosed();
screen.getByText("Close").click();
expectClosed();
});