1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as React from "react";
6 import { mount, configure } from "enzyme";
7 import * as Adapter from "enzyme-adapter-react-16";
8 import { MainAppBar } from "./main-app-bar";
9 import { SearchBar } from "~/components/search-bar/search-bar";
10 import { Breadcrumbs } from "~/components/breadcrumbs/breadcrumbs";
11 import { DropdownMenu } from "~/components/dropdown-menu/dropdown-menu";
12 import { Button, MenuItem, IconButton } from "@material-ui/core";
13 import { User } from "~/models/user";
15 configure({ adapter: new Adapter() });
17 describe("<MainAppBar />", () => {
22 email: "test.user@example.com",
27 it("renders all components and the menu for authenticated user if user prop has value", () => {
28 const mainAppBar = mount(
31 onContextMenu={jest.fn()}
32 onDetailsPanelToggle={jest.fn()}
33 {...{ searchText: "", breadcrumbs: [], menuItems: { accountMenu: [], helpMenu: [], anonymousMenu: [] }, onSearch: jest.fn(), onBreadcrumbClick: jest.fn(), onMenuItemClick: jest.fn() }}
36 expect(mainAppBar.find(SearchBar)).toHaveLength(1);
37 expect(mainAppBar.find(Breadcrumbs)).toHaveLength(1);
38 expect(mainAppBar.find(DropdownMenu)).toHaveLength(2);
41 it("renders only the menu for anonymous user if user prop is undefined", () => {
42 const menuItems = { accountMenu: [], helpMenu: [], anonymousMenu: [{ label: 'Sign in' }] };
43 const mainAppBar = mount(
46 onDetailsPanelToggle={jest.fn()}
47 onContextMenu={jest.fn()}
48 {...{ searchText: "", breadcrumbs: [], onSearch: jest.fn(), onBreadcrumbClick: jest.fn(), onMenuItemClick: jest.fn() }}
51 expect(mainAppBar.find(SearchBar)).toHaveLength(0);
52 expect(mainAppBar.find(Breadcrumbs)).toHaveLength(0);
53 expect(mainAppBar.find(DropdownMenu)).toHaveLength(0);
54 expect(mainAppBar.find(Button)).toHaveLength(1);
57 it("communicates with <SearchBar />", () => {
58 const onSearch = jest.fn();
59 const mainAppBar = mount(
61 searchText="search text"
63 onContextMenu={jest.fn()}
65 onDetailsPanelToggle={jest.fn()}
66 {...{ user, breadcrumbs: [], menuItems: { accountMenu: [], helpMenu: [], anonymousMenu: [] }, onBreadcrumbClick: jest.fn(), onMenuItemClick: jest.fn() }}
69 const searchBar = mainAppBar.find(SearchBar);
70 expect(searchBar.prop("value")).toBe("search text");
71 expect(searchBar.prop("debounce")).toBe(2000);
72 searchBar.prop("onSearch")("new search text");
73 expect(onSearch).toBeCalledWith("new search text");
76 it("communicates with <Breadcrumbs />", () => {
77 const items = [{ label: "breadcrumb 1" }];
78 const onBreadcrumbClick = jest.fn();
79 const mainAppBar = mount(
82 onContextMenu={jest.fn()}
83 onBreadcrumbClick={onBreadcrumbClick}
84 onDetailsPanelToggle={jest.fn()}
85 {...{ user, searchText: "", menuItems: { accountMenu: [], helpMenu: [], anonymousMenu: [] }, onSearch: jest.fn(), onMenuItemClick: jest.fn() }}
88 const breadcrumbs = mainAppBar.find(Breadcrumbs);
89 expect(breadcrumbs.prop("items")).toBe(items);
90 breadcrumbs.prop("onClick")(items[0]);
91 expect(onBreadcrumbClick).toBeCalledWith(items[0]);
94 it("communicates with menu", () => {
95 const onMenuItemClick = jest.fn();
96 const menuItems = { accountMenu: [{label: "log out"}], helpMenu: [], anonymousMenu: [] };
97 const mainAppBar = mount(
100 onContextMenu={jest.fn()}
101 onMenuItemClick={onMenuItemClick}
102 onDetailsPanelToggle={jest.fn()}
103 {...{ user, searchText: "", breadcrumbs: [], onSearch: jest.fn(), onBreadcrumbClick: jest.fn() }}
107 mainAppBar.find(DropdownMenu).at(0).find(IconButton).simulate("click");
108 mainAppBar.find(DropdownMenu).at(0).find(MenuItem).at(1).simulate("click");
109 expect(onMenuItemClick).toBeCalledWith(menuItems.accountMenu[0]);