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, MainAppBarProps } 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";
14 import { MemoryRouter } from 'react-router-dom';
16 configure({ adapter: new Adapter() });
18 describe("<MainAppBar />", () => {
23 email: "test.user@example.com",
28 it("renders all components and the menu for authenticated user if user prop has value", () => {
29 const mainAppBar = mount(
32 {...mockMainAppBarProps({ user })}
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 {...mockMainAppBarProps({ user: undefined, menuItems })}
50 expect(mainAppBar.find(SearchBar)).toHaveLength(0);
51 expect(mainAppBar.find(Breadcrumbs)).toHaveLength(0);
52 expect(mainAppBar.find(DropdownMenu)).toHaveLength(0);
53 expect(mainAppBar.find(Button)).toHaveLength(1);
56 it("communicates with <SearchBar />", () => {
57 const onSearch = jest.fn();
58 const mainAppBar = mount(
61 {...mockMainAppBarProps({ searchText: 'search text', searchDebounce: 2000, onSearch, user })}
65 const searchBar = mainAppBar.find(SearchBar);
66 expect(searchBar.prop("value")).toBe("search text");
67 expect(searchBar.prop("debounce")).toBe(2000);
68 searchBar.prop("onSearch")("new search text");
69 expect(onSearch).toBeCalledWith("new search text");
72 it("communicates with menu", () => {
73 const onMenuItemClick = jest.fn();
74 const menuItems = { accountMenu: [{ label: "log out" }], helpMenu: [], anonymousMenu: [] };
75 const mainAppBar = mount(
78 {...mockMainAppBarProps({ menuItems, onMenuItemClick, user })}
83 mainAppBar.find(DropdownMenu).at(0).find(IconButton).simulate("click");
84 mainAppBar.find(DropdownMenu).at(0).find(MenuItem).at(1).simulate("click");
85 expect(onMenuItemClick).toBeCalledWith(menuItems.accountMenu[0]);
89 const Breadcrumbs = () => <span>Breadcrumbs</span>;
91 const mockMainAppBarProps = (props: Partial<MainAppBarProps>): MainAppBarProps => ({
93 breadcrumbs: Breadcrumbs,
101 onMenuItemClick: jest.fn(),
102 onDetailsPanelToggle: jest.fn(),