-
Notifications
You must be signed in to change notification settings - Fork 14
/
ContextualMenu.test.js
44 lines (41 loc) · 1.74 KB
/
ContextualMenu.test.js
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
import React from "react";
import { render, fireEvent } from "@testing-library/react";
import DxcContextualMenu from "./ContextualMenu.tsx";
const items = [{ label: "Item 1" }, { label: "Item 2" }, { label: "Item 3" }, { label: "Item 4" }];
const sections = [
{
title: "Team repositories",
items: [{ label: "Approved locations" }, { label: "Approved locations" }, { label: "Approved locations" }],
},
{
items: [{ label: "Approved locations" }, { label: "Approved locations" }, { label: "Approved locations" }],
},
];
describe("Context menu component tests", () => {
test("Context menu renders with correct aria attributes", () => {
const { getAllByRole, getByRole } = render(<DxcContextualMenu items={items} defaultSelectedItemIndex={0} />);
const renderedItems = getAllByRole("menuitem");
expect(renderedItems.length).toBe(4);
expect(renderedItems[0].getAttribute("aria-selected")).toBeTruthy();
expect(getByRole("menu")).toBeTruthy();
});
test("Context menu (with sections) renders with correct aria attributes", () => {
const { getAllByRole, getByRole } = render(<DxcContextualMenu items={sections} defaultSelectedItemIndex={4} />);
const renderedItems = getAllByRole("menuitem");
expect(renderedItems.length).toBe(6);
expect(renderedItems[4].getAttribute("aria-selected")).toBeTruthy();
expect(getAllByRole("group").length).toBe(2);
});
test("onSelect event from each item is called correctly", () => {
const test = [
{
label: "Tested item",
onSelect: jest.fn(),
},
];
const { getByRole } = render(<DxcContextualMenu items={test} />);
const item = getByRole("menuitem");
fireEvent.click(item);
expect(test[0].onSelect).toHaveBeenCalled();
});
});