|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import React from 'react'; |
| 18 | +import { fireEvent, render } from "@testing-library/react"; |
| 19 | +import { act } from 'react-dom/test-utils'; |
| 20 | + |
| 21 | +import TabbedView, { Tab, TabLocation } from "../../../src/components/structures/TabbedView"; |
| 22 | + |
| 23 | +describe('<TabbedView />', () => { |
| 24 | + const generalTab = new Tab( |
| 25 | + 'GENERAL', |
| 26 | + 'General', |
| 27 | + 'general', |
| 28 | + <div>general</div>, |
| 29 | + ); |
| 30 | + const labsTab = new Tab( |
| 31 | + 'LABS', |
| 32 | + 'Labs', |
| 33 | + 'labs', |
| 34 | + <div>labs</div>, |
| 35 | + ); |
| 36 | + const securityTab = new Tab( |
| 37 | + 'SECURITY', |
| 38 | + 'Security', |
| 39 | + 'security', |
| 40 | + <div>security</div>, |
| 41 | + ); |
| 42 | + const defaultProps = { |
| 43 | + tabLocation: TabLocation.LEFT, |
| 44 | + tabs: [generalTab, labsTab, securityTab], |
| 45 | + }; |
| 46 | + const getComponent = (props = {}): React.ReactElement => <TabbedView {...defaultProps} {...props} />; |
| 47 | + |
| 48 | + const getTabTestId = (tab: Tab): string => `settings-tab-${tab.id}`; |
| 49 | + const getActiveTab = (container: HTMLElement): Element | undefined => |
| 50 | + container.getElementsByClassName('mx_TabbedView_tabLabel_active')[0]; |
| 51 | + const getActiveTabBody = (container: HTMLElement): Element | undefined => |
| 52 | + container.getElementsByClassName('mx_TabbedView_tabPanel')[0]; |
| 53 | + |
| 54 | + it('renders tabs', () => { |
| 55 | + const { container } = render(getComponent()); |
| 56 | + expect(container).toMatchSnapshot(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('renders first tab as active tab when no initialTabId', () => { |
| 60 | + const { container } = render(getComponent()); |
| 61 | + expect(getActiveTab(container).textContent).toEqual(generalTab.label); |
| 62 | + expect(getActiveTabBody(container).textContent).toEqual('general'); |
| 63 | + }); |
| 64 | + |
| 65 | + it('renders first tab as active tab when initialTabId is not valid', () => { |
| 66 | + const { container } = render(getComponent({ initialTabId: 'bad-tab-id' })); |
| 67 | + expect(getActiveTab(container).textContent).toEqual(generalTab.label); |
| 68 | + expect(getActiveTabBody(container).textContent).toEqual('general'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('renders initialTabId tab as active when valid', () => { |
| 72 | + const { container } = render(getComponent({ initialTabId: securityTab.id })); |
| 73 | + expect(getActiveTab(container).textContent).toEqual(securityTab.label); |
| 74 | + expect(getActiveTabBody(container).textContent).toEqual('security'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('renders without error when there are no tabs', () => { |
| 78 | + const { container } = render(getComponent({ tabs: [] })); |
| 79 | + expect(container).toMatchSnapshot(); |
| 80 | + }); |
| 81 | + |
| 82 | + it('sets active tab on tab click', () => { |
| 83 | + const { container, getByTestId } = render(getComponent()); |
| 84 | + |
| 85 | + act(() => { |
| 86 | + fireEvent.click(getByTestId(getTabTestId(securityTab))); |
| 87 | + }); |
| 88 | + |
| 89 | + expect(getActiveTab(container).textContent).toEqual(securityTab.label); |
| 90 | + expect(getActiveTabBody(container).textContent).toEqual('security'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('calls onchange on on tab click', () => { |
| 94 | + const onChange = jest.fn(); |
| 95 | + const { getByTestId } = render(getComponent({ onChange })); |
| 96 | + |
| 97 | + act(() => { |
| 98 | + fireEvent.click(getByTestId(getTabTestId(securityTab))); |
| 99 | + }); |
| 100 | + |
| 101 | + expect(onChange).toHaveBeenCalledWith(securityTab.id); |
| 102 | + }); |
| 103 | + |
| 104 | + it('keeps same tab active when order of tabs changes', () => { |
| 105 | + // start with middle tab active |
| 106 | + const { container, rerender } = render(getComponent({ initialTabId: labsTab.id })); |
| 107 | + |
| 108 | + expect(getActiveTab(container).textContent).toEqual(labsTab.label); |
| 109 | + |
| 110 | + rerender(getComponent({ tabs: [labsTab, generalTab, securityTab] })); |
| 111 | + |
| 112 | + // labs tab still active |
| 113 | + expect(getActiveTab(container).textContent).toEqual(labsTab.label); |
| 114 | + }); |
| 115 | + |
| 116 | + it('does not reactivate inititalTabId on rerender', () => { |
| 117 | + const { container, getByTestId, rerender } = render(getComponent()); |
| 118 | + |
| 119 | + expect(getActiveTab(container).textContent).toEqual(generalTab.label); |
| 120 | + |
| 121 | + // make security tab active |
| 122 | + act(() => { |
| 123 | + fireEvent.click(getByTestId(getTabTestId(securityTab))); |
| 124 | + }); |
| 125 | + expect(getActiveTab(container).textContent).toEqual(securityTab.label); |
| 126 | + |
| 127 | + // rerender with new tab location |
| 128 | + rerender(getComponent({ tabLocation: TabLocation.TOP })); |
| 129 | + |
| 130 | + // still security tab |
| 131 | + expect(getActiveTab(container).textContent).toEqual(securityTab.label); |
| 132 | + }); |
| 133 | +}); |
0 commit comments