|
1 | 1 | import React from 'react';
|
2 | 2 | import type { ReactWrapper } from 'enzyme';
|
3 |
| -import { render } from '@testing-library/react'; |
| 3 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 4 | +import '@testing-library/dom'; |
4 | 5 | import { mount } from 'enzyme';
|
| 6 | +import { spyElementPrototypes } from 'rc-util/lib/test/domHook'; |
5 | 7 | import KeyCode from 'rc-util/lib/KeyCode';
|
6 | 8 | import Tabs from '../src';
|
7 | 9 | import type { TabsProps } from '../src/Tabs';
|
| 10 | +import type { HackInfo } from './common/util'; |
| 11 | +import { getOffsetSizeFunc } from './common/util'; |
8 | 12 |
|
9 | 13 | describe('Tabs.Basic', () => {
|
| 14 | + let domSpy: ReturnType<typeof spyElementPrototypes>; |
| 15 | + let holder: HTMLDivElement; |
| 16 | + |
| 17 | + const hackOffsetInfo: HackInfo = {}; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + Object.keys(hackOffsetInfo).forEach(key => { |
| 21 | + delete hackOffsetInfo[key]; |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + beforeAll(() => { |
| 26 | + holder = document.createElement('div'); |
| 27 | + document.body.appendChild(holder); |
| 28 | + |
| 29 | + domSpy = spyElementPrototypes(HTMLElement, { |
| 30 | + scrollIntoView: () => {}, |
| 31 | + offsetWidth: { |
| 32 | + get: getOffsetSizeFunc(hackOffsetInfo), |
| 33 | + }, |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + afterAll(() => { |
| 38 | + domSpy.mockRestore(); |
| 39 | + document.body.removeChild(holder); |
| 40 | + }); |
| 41 | + |
10 | 42 | function getTabs(props: TabsProps = null) {
|
11 | 43 | const mergedProps = {
|
12 | 44 | items: [
|
@@ -86,6 +118,60 @@ describe('Tabs.Basic', () => {
|
86 | 118 | mount(getTabs({ items: null }));
|
87 | 119 | });
|
88 | 120 |
|
| 121 | + it('same width in windows call resize', async () => { |
| 122 | + const App = () => { |
| 123 | + const [list, setList] = React.useState([ |
| 124 | + { |
| 125 | + label: `Tab 1`, |
| 126 | + key: '1', |
| 127 | + children: `Content of Tab Pane 1`, |
| 128 | + }, |
| 129 | + { |
| 130 | + label: `Tab 2`, |
| 131 | + key: '2', |
| 132 | + children: `Content of Tab Pane 2`, |
| 133 | + }, |
| 134 | + { |
| 135 | + label: `Tab 3`, |
| 136 | + key: '3', |
| 137 | + children: `Content of Tab Pane 3`, |
| 138 | + }, |
| 139 | + ]); |
| 140 | + const changeItems = () => { |
| 141 | + const listCp = [ |
| 142 | + { |
| 143 | + label: `Tab 4`, |
| 144 | + key: '4', |
| 145 | + children: `Content of Tab Pane 4`, |
| 146 | + }, |
| 147 | + { |
| 148 | + label: `Tab 5`, |
| 149 | + key: '5', |
| 150 | + children: `Content of Tab Pane 5`, |
| 151 | + }, |
| 152 | + { |
| 153 | + label: `Tab 6`, |
| 154 | + key: '6', |
| 155 | + children: `Content of Tab Pane 6`, |
| 156 | + }, |
| 157 | + ]; |
| 158 | + setList(listCp); |
| 159 | + }; |
| 160 | + return ( |
| 161 | + <div className="App"> |
| 162 | + <button id="changeItems" onClick={changeItems}> |
| 163 | + change |
| 164 | + </button> |
| 165 | + <Tabs items={list} /> |
| 166 | + </div> |
| 167 | + ); |
| 168 | + }; |
| 169 | + |
| 170 | + const { container } = render(<App />); |
| 171 | + fireEvent.click(screen.getByRole('button', { name: 'change' })); |
| 172 | + expect(container.querySelector<HTMLElement>('.rc-tabs-ink-bar').offsetWidth).toBe(50); |
| 173 | + }); |
| 174 | + |
89 | 175 | describe('onChange and onTabClick should work', () => {
|
90 | 176 | const list: { name: string; trigger: (wrapper: ReactWrapper) => void }[] = [
|
91 | 177 | {
|
|
0 commit comments