1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from "react";
6 import { mount, configure } from "enzyme";
7 import { ConditionalTabs, TabData } from "./conditional-tabs";
8 import Adapter from 'enzyme-adapter-react-16';
9 import { Tab } from "@material-ui/core";
11 configure({ adapter: new Adapter() });
13 describe("<ConditionalTabs />", () => {
14 let tabs: TabData[] = [];
20 content: <div id="content1">Content1</div>,
24 content: <div id="content2">Content2</div>,
28 content: <div id="content3">Content3</div>,
32 it("renders only visible tabs", () => {
34 const tabContainer = mount(<ConditionalTabs
38 // expect 2 visible tabs
39 expect(tabContainer.find(Tab)).toHaveLength(2);
40 expect(tabContainer.find(Tab).at(0).text()).toBe("Tab1");
41 expect(tabContainer.find(Tab).at(1).text()).toBe("Tab3");
42 // expect visible content 1 and tab 3 to be hidden but exist
43 // content 2 stays unrendered since the tab is hidden
44 expect(tabContainer.find('div#content1').text()).toBe("Content1");
45 expect(tabContainer.find('div#content1').prop('hidden')).toBeFalsy();
46 expect(tabContainer.find('div#content2').exists()).toBeFalsy();
47 expect(tabContainer.find('div#content3').prop('hidden')).toBeTruthy();
51 tabContainer.setProps({ tabs: tabs });
52 tabContainer.update();
54 // Expect 3 visible tabs
55 expect(tabContainer.find(Tab)).toHaveLength(3);
56 expect(tabContainer.find(Tab).at(0).text()).toBe("Tab1");
57 expect(tabContainer.find(Tab).at(1).text()).toBe("Tab2");
58 expect(tabContainer.find(Tab).at(2).text()).toBe("Tab3");
59 // Expect visible content 1 and hidden content 2/3
60 expect(tabContainer.find('div#content1').text()).toBe("Content1");
61 expect(tabContainer.find('div#content1').prop('hidden')).toBeFalsy();
62 expect(tabContainer.find('div#content2').prop('hidden')).toBeTruthy();
63 expect(tabContainer.find('div#content3').prop('hidden')).toBeTruthy();
65 // Click on Tab2 (position 1)
66 tabContainer.find(Tab).at(1).simulate('click');
68 // Expect 3 visible tabs
69 expect(tabContainer.find(Tab)).toHaveLength(3);
70 expect(tabContainer.find(Tab).at(0).text()).toBe("Tab1");
71 expect(tabContainer.find(Tab).at(1).text()).toBe("Tab2");
72 expect(tabContainer.find(Tab).at(2).text()).toBe("Tab3");
73 // Expect visible content2 and hidden content 1/3
74 expect(tabContainer.find('div#content2').text()).toBe("Content2");
75 expect(tabContainer.find('div#content1').prop('hidden')).toBeTruthy();
76 expect(tabContainer.find('div#content2').prop('hidden')).toBeFalsy();
77 expect(tabContainer.find('div#content3').prop('hidden')).toBeTruthy();
80 it("resets selected tab on tab visibility change", () => {
82 const tabContainer = mount(<ConditionalTabs
86 // Expect second tab to be Tab3
87 expect(tabContainer.find(Tab).at(1).text()).toBe("Tab3");
88 // Click on Tab3 (position 2)
89 tabContainer.find(Tab).at(1).simulate('click');
90 expect(tabContainer.find('div#content3').text()).toBe("Content3");
91 expect(tabContainer.find('div#content1').prop('hidden')).toBeTruthy();
92 expect(tabContainer.find('div#content2').exists()).toBeFalsy();
93 expect(tabContainer.find('div#content3').prop('hidden')).toBeFalsy();
95 // when Tab2 becomes visible
97 tabContainer.setProps({ tabs: tabs });
98 tabContainer.update(); // Needed or else tab1 content will still be hidden
100 // Selected tab resets to 1, tabs 2/3 are hidden
101 expect(tabContainer.find('div#content1').text()).toBe("Content1");
102 expect(tabContainer.find('div#content1').prop('hidden')).toBeFalsy();
103 expect(tabContainer.find('div#content2').prop('hidden')).toBeTruthy();
104 expect(tabContainer.find('div#content3').prop('hidden')).toBeTruthy();