|
| 1 | +import React from 'react'; |
| 2 | +import { render, act } from '@testing-library/react'; |
| 3 | +import { spyElementPrototypes } from 'rc-util/lib/test/domHook'; |
| 4 | +import Overflow from '../src'; |
| 5 | + |
| 6 | +import { _rs as onResize } from 'rc-resize-observer/lib/utils/observerUtil'; |
| 7 | + |
| 8 | +interface ItemType { |
| 9 | + label: React.ReactNode; |
| 10 | + key: React.Key; |
| 11 | +} |
| 12 | + |
| 13 | +function renderItem(item: ItemType) { |
| 14 | + return item.label; |
| 15 | +} |
| 16 | + |
| 17 | +function renderRest(items: ItemType[]) { |
| 18 | + return `+${items.length}...`; |
| 19 | +} |
| 20 | + |
| 21 | +describe('Overflow.github', () => { |
| 22 | + function getData(count: number) { |
| 23 | + return new Array(count).fill(undefined).map((_, index) => ({ |
| 24 | + label: `Label ${index}`, |
| 25 | + key: `k-${index}`, |
| 26 | + })); |
| 27 | + } |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + jest.useFakeTimers(); |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + jest.useRealTimers(); |
| 35 | + }); |
| 36 | + |
| 37 | + const widths = { |
| 38 | + 'rc-overflow': 100, |
| 39 | + 'rc-overflow-item': 90, |
| 40 | + }; |
| 41 | + |
| 42 | + const propDef = { |
| 43 | + get() { |
| 44 | + let targetWidth = 0; |
| 45 | + Object.keys(widths).forEach(key => { |
| 46 | + if (this.className.includes(key)) { |
| 47 | + targetWidth = widths[key]; |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + return targetWidth; |
| 52 | + }, |
| 53 | + }; |
| 54 | + |
| 55 | + beforeAll(() => { |
| 56 | + spyElementPrototypes(HTMLDivElement, { |
| 57 | + clientWidth: propDef, |
| 58 | + offsetWidth: propDef, |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + async function triggerResize(target: HTMLElement) { |
| 63 | + await act(async () => { |
| 64 | + onResize([{ target } as any]); |
| 65 | + for (let i = 0; i < 10; i += 1) { |
| 66 | + await Promise.resolve(); |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + it('only one', async () => { |
| 72 | + const { container } = render( |
| 73 | + <Overflow<ItemType> |
| 74 | + data={getData(2)} |
| 75 | + itemKey="key" |
| 76 | + renderItem={renderItem} |
| 77 | + renderRest={renderRest} |
| 78 | + maxCount="responsive" |
| 79 | + />, |
| 80 | + ); |
| 81 | + |
| 82 | + // width & rest resize |
| 83 | + await triggerResize(container.querySelector('.rc-overflow')); |
| 84 | + await triggerResize(container.querySelector('.rc-overflow-item-rest')); |
| 85 | + |
| 86 | + act(() => { |
| 87 | + jest.runAllTimers(); |
| 88 | + }); |
| 89 | + |
| 90 | + const items = Array.from( |
| 91 | + container.querySelectorAll<HTMLDivElement>( |
| 92 | + '.rc-overflow-item:not(.rc-overflow-item-rest)', |
| 93 | + ), |
| 94 | + ); |
| 95 | + |
| 96 | + for (let i = 0; i < items.length; i += 1) { |
| 97 | + await triggerResize(items[i]); |
| 98 | + } |
| 99 | + act(() => { |
| 100 | + jest.runAllTimers(); |
| 101 | + }); |
| 102 | + |
| 103 | + expect(container.querySelector('.rc-overflow-item-rest')).toHaveStyle({ |
| 104 | + opacity: 1, |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments