info-card-view
[arvados.git] / src / views-components / main-app-bar / main-app-bar.test.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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";
14
15 configure({ adapter: new Adapter() });
16
17 describe("<MainAppBar />", () => {
18
19     const user: User = {
20         firstName: "Test",
21         lastName: "User",
22         email: "test.user@example.com",
23         uuid: "",
24         ownerUuid: ""
25     };
26
27     it("renders all components and the menu for authenticated user if user prop has value", () => {
28         const mainAppBar = mount(
29             <MainAppBar
30                 user={user}
31                 onContextMenu={jest.fn()}
32                 onDetailsPanelToggle={jest.fn()}
33                 {...{ searchText: "", breadcrumbs: [], menuItems: { accountMenu: [], helpMenu: [], anonymousMenu: [] }, onSearch: jest.fn(), onBreadcrumbClick: jest.fn(), onMenuItemClick: jest.fn() }}
34             />
35         );
36         expect(mainAppBar.find(SearchBar)).toHaveLength(1);
37         expect(mainAppBar.find(Breadcrumbs)).toHaveLength(1);
38         expect(mainAppBar.find(DropdownMenu)).toHaveLength(2);
39     });
40
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(
44             <MainAppBar
45                 menuItems={menuItems}
46                 onDetailsPanelToggle={jest.fn()}
47                 onContextMenu={jest.fn()}
48                 {...{ searchText: "", breadcrumbs: [], onSearch: jest.fn(), onBreadcrumbClick: jest.fn(), onMenuItemClick: jest.fn() }}
49             />
50         );
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);
55     });
56
57     it("communicates with <SearchBar />", () => {
58         const onSearch = jest.fn();
59         const mainAppBar = mount(
60             <MainAppBar
61                 searchText="search text"
62                 searchDebounce={2000}
63                 onContextMenu={jest.fn()}
64                 onSearch={onSearch}
65                 onDetailsPanelToggle={jest.fn()}
66                 {...{ user, breadcrumbs: [], menuItems: { accountMenu: [], helpMenu: [], anonymousMenu: [] }, onBreadcrumbClick: jest.fn(), onMenuItemClick: jest.fn() }}
67             />
68         );
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");
74     });
75
76     it("communicates with <Breadcrumbs />", () => {
77         const items = [{ label: "breadcrumb 1" }];
78         const onBreadcrumbClick = jest.fn();
79         const mainAppBar = mount(
80             <MainAppBar
81                 breadcrumbs={items}
82                 onContextMenu={jest.fn()}
83                 onBreadcrumbClick={onBreadcrumbClick}
84                 onDetailsPanelToggle={jest.fn()}
85                 {...{ user, searchText: "", menuItems: { accountMenu: [], helpMenu: [], anonymousMenu: [] }, onSearch: jest.fn(), onMenuItemClick: jest.fn() }}
86             />
87         );
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]);
92     });
93
94     it("communicates with menu", () => {
95         const onMenuItemClick = jest.fn();
96         const menuItems = { accountMenu: [{label: "log out"}], helpMenu: [], anonymousMenu: [] };
97         const mainAppBar = mount(
98             <MainAppBar
99                 menuItems={menuItems}
100                 onContextMenu={jest.fn()}
101                 onMenuItemClick={onMenuItemClick}
102                 onDetailsPanelToggle={jest.fn()}
103                 {...{ user, searchText: "", breadcrumbs: [], onSearch: jest.fn(), onBreadcrumbClick: jest.fn() }}
104             />
105         );
106
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]);
110     });
111 });