|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | + |
| 4 | +import React, { useState } from 'react'; |
| 5 | + |
| 6 | +import { VerticalTabs } from './VerticalTabs'; |
| 7 | +import { VerticalTabsItem } from './VerticalTabs.types'; |
| 8 | + |
| 9 | +const INITIAL_TAB = 2; |
| 10 | +const DISABLED_TAB = 1; |
| 11 | +const TAB_ITEMS: VerticalTabsItem[] = [ |
| 12 | + { |
| 13 | + label: 'Tab 1', |
| 14 | + infoText: 'Info text 1', |
| 15 | + content: 'Content 1', |
| 16 | + }, |
| 17 | + { |
| 18 | + label: 'Tab 2', |
| 19 | + disabled: true, |
| 20 | + content: 'Content 2', |
| 21 | + }, |
| 22 | + { |
| 23 | + label: 'Tab 3', |
| 24 | + content: 'Content 3', |
| 25 | + }, |
| 26 | +]; |
| 27 | + |
| 28 | +const TestComponent = () => { |
| 29 | + const [index, setIndex] = useState(INITIAL_TAB); |
| 30 | + return ( |
| 31 | + <VerticalTabs |
| 32 | + items={TAB_ITEMS} |
| 33 | + selectedIndex={index} |
| 34 | + onChange={setIndex} |
| 35 | + header={() => <h1>This is header</h1>} |
| 36 | + footer={() => <p>This is footer</p>} |
| 37 | + /> |
| 38 | + ); |
| 39 | +}; |
| 40 | + |
| 41 | +describe('VerticalTabs', () => { |
| 42 | + it('renders all of the tabs', () => { |
| 43 | + const { getByText } = render(<TestComponent />); |
| 44 | + |
| 45 | + TAB_ITEMS.forEach(item => { |
| 46 | + expect(getByText(item.label as string)).toBeInTheDocument(); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('renders initial content', () => { |
| 51 | + const { getByText } = render(<TestComponent />); |
| 52 | + expect( |
| 53 | + getByText(TAB_ITEMS[INITIAL_TAB].content as string), |
| 54 | + ).toBeInTheDocument(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('renders header', () => { |
| 58 | + const { getByText } = render(<TestComponent />); |
| 59 | + expect(getByText('This is header')).toBeInTheDocument(); |
| 60 | + }); |
| 61 | + |
| 62 | + it('renders footer', () => { |
| 63 | + const { getByText } = render(<TestComponent />); |
| 64 | + expect(getByText('This is footer')).toBeInTheDocument(); |
| 65 | + }); |
| 66 | + |
| 67 | + it('does not render non selected tab content', () => { |
| 68 | + const { getByText } = render(<TestComponent />); |
| 69 | + expect(() => getByText(TAB_ITEMS[0].content as string)).toThrow(); |
| 70 | + }); |
| 71 | + |
| 72 | + it('switches tab content when clicked', () => { |
| 73 | + const { getByText } = render(<TestComponent />); |
| 74 | + |
| 75 | + userEvent.click(getByText(TAB_ITEMS[0].label as string)); |
| 76 | + expect(getByText(TAB_ITEMS[0].content as string)).toBeInTheDocument(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('does not switch to content of disabled tab', () => { |
| 80 | + const { getByText } = render(<TestComponent />); |
| 81 | + |
| 82 | + userEvent.click(getByText(TAB_ITEMS[DISABLED_TAB].label as string)); |
| 83 | + expect( |
| 84 | + getByText(TAB_ITEMS[INITIAL_TAB].content as string), |
| 85 | + ).toBeInTheDocument(); |
| 86 | + expect(() => |
| 87 | + getByText(TAB_ITEMS[DISABLED_TAB].content as string), |
| 88 | + ).toThrow(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('triggers onChange callback when tab item is clicked', () => { |
| 92 | + const mockFunction = jest.fn(); |
| 93 | + |
| 94 | + const { getByText } = render( |
| 95 | + <VerticalTabs |
| 96 | + items={TAB_ITEMS} |
| 97 | + selectedIndex={0} |
| 98 | + onChange={mockFunction} |
| 99 | + />, |
| 100 | + ); |
| 101 | + |
| 102 | + userEvent.click(getByText(TAB_ITEMS[0].label as string)); |
| 103 | + expect(mockFunction).toHaveBeenCalledTimes(1); |
| 104 | + }); |
| 105 | + |
| 106 | + it('does not trigger onChange callback when disabled tab item is clicked', () => { |
| 107 | + const mockFunction = jest.fn(); |
| 108 | + |
| 109 | + const { getByText } = render( |
| 110 | + <VerticalTabs |
| 111 | + items={TAB_ITEMS} |
| 112 | + selectedIndex={0} |
| 113 | + onChange={mockFunction} |
| 114 | + />, |
| 115 | + ); |
| 116 | + |
| 117 | + userEvent.click(getByText(TAB_ITEMS[DISABLED_TAB].label as string)); |
| 118 | + expect(mockFunction).toHaveBeenCalledTimes(0); |
| 119 | + }); |
| 120 | + |
| 121 | + it('adds className to vertical tabs root wrapper', () => { |
| 122 | + const { container } = render( |
| 123 | + <VerticalTabs |
| 124 | + items={TAB_ITEMS} |
| 125 | + selectedIndex={0} |
| 126 | + className="test-class" |
| 127 | + />, |
| 128 | + ); |
| 129 | + // adding "container" to make sure correct element is found |
| 130 | + expect(container.firstChild).toHaveClass('container test-class'); |
| 131 | + }); |
| 132 | + |
| 133 | + it('adds className to tab list wrapper', () => { |
| 134 | + const { container } = render( |
| 135 | + <VerticalTabs |
| 136 | + items={TAB_ITEMS} |
| 137 | + selectedIndex={0} |
| 138 | + tabsClassName="test-class" |
| 139 | + />, |
| 140 | + ); |
| 141 | + |
| 142 | + // adding "aside" to make sure correct element is found |
| 143 | + expect(container.firstChild?.childNodes[0]).toHaveClass('aside test-class'); |
| 144 | + }); |
| 145 | + |
| 146 | + it('adds className to tab content wrapper', () => { |
| 147 | + const { container } = render( |
| 148 | + <VerticalTabs |
| 149 | + items={TAB_ITEMS} |
| 150 | + selectedIndex={0} |
| 151 | + contentClassName="test-class" |
| 152 | + />, |
| 153 | + ); |
| 154 | + |
| 155 | + // adding "content" to make sure correct element is found |
| 156 | + expect(container.firstChild?.childNodes[1]).toHaveClass( |
| 157 | + 'content test-class', |
| 158 | + ); |
| 159 | + }); |
| 160 | +}); |
0 commit comments