17782: Fixes almost all tests (4 left) mostly by fixing namespace-type imports.
[arvados-workbench2.git] / src / components / tree / tree.test.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4 import React from 'react';
5 import { mount } from 'enzyme';
6 import * as Enzyme from 'enzyme';
7 import Adapter from 'enzyme-adapter-react-16';
8 import ListItem from "@material-ui/core/ListItem/ListItem";
9
10 import { Tree, TreeItem, TreeItemStatus } from './tree';
11 import { ProjectResource } from '../../models/project';
12 import { mockProjectResource } from '../../models/test-utils';
13 import { Checkbox } from '@material-ui/core';
14
15 Enzyme.configure({ adapter: new Adapter() });
16
17 describe("Tree component", () => {
18
19     it("should render ListItem", () => {
20         const project: TreeItem<ProjectResource> = {
21             data: mockProjectResource(),
22             id: "3",
23             open: true,
24             active: true,
25             status: TreeItemStatus.LOADED
26         };
27         const wrapper = mount(<Tree
28             render={project => <div />}
29             toggleItemOpen={jest.fn()}
30             toggleItemActive={jest.fn()}
31             onContextMenu={jest.fn()}
32             items={[project]} />);
33         expect(wrapper.find(ListItem)).toHaveLength(1);
34     });
35
36     it("should render arrow", () => {
37         const project: TreeItem<ProjectResource> = {
38             data: mockProjectResource(),
39             id: "3",
40             open: true,
41             active: true,
42             status: TreeItemStatus.LOADED,
43         };
44         const wrapper = mount(<Tree
45             render={project => <div />}
46             toggleItemOpen={jest.fn()}
47             toggleItemActive={jest.fn()}
48             onContextMenu={jest.fn()}
49             items={[project]} />);
50         expect(wrapper.find('i')).toHaveLength(1);
51     });
52
53     it("should render checkbox", () => {
54         const project: TreeItem<ProjectResource> = {
55             data: mockProjectResource(),
56             id: "3",
57             open: true,
58             active: true,
59             status: TreeItemStatus.LOADED
60         };
61         const wrapper = mount(<Tree
62             showSelection={true}
63             render={() => <div />}
64             toggleItemOpen={jest.fn()}
65             toggleItemActive={jest.fn()}
66             onContextMenu={jest.fn()}
67             items={[project]} />);
68         expect(wrapper.find(Checkbox)).toHaveLength(1);
69     });
70
71     it("call onSelectionChanged with associated item", () => {
72         const project: TreeItem<ProjectResource> = {
73             data: mockProjectResource(),
74             id: "3",
75             open: true,
76             active: true,
77             status: TreeItemStatus.LOADED,
78         };
79         const spy = jest.fn();
80         const onSelectionChanged = (event: any, item: TreeItem<any>) => spy(item);
81         const wrapper = mount(<Tree
82             showSelection={true}
83             render={() => <div />}
84             toggleItemOpen={jest.fn()}
85             toggleItemActive={jest.fn()}
86             onContextMenu={jest.fn()}
87             toggleItemSelection={onSelectionChanged}
88             items={[project]} />);
89         wrapper.find(Checkbox).simulate('click');
90         expect(spy).toHaveBeenLastCalledWith({
91             data: mockProjectResource(),
92             id: "3",
93             open: true,
94             active: true,
95             status: TreeItemStatus.LOADED,
96         });
97     });
98
99 });