|
| 1 | +import { createOverflowManager } from './overflowManager'; |
| 2 | +import type { ObserveOptions, OverflowEventPayload } from './types'; |
| 3 | + |
| 4 | +describe('overflowManager', () => { |
| 5 | + beforeAll(() => { |
| 6 | + global.ResizeObserver = class ResizeObserver { |
| 7 | + public observe() { |
| 8 | + // do nothing |
| 9 | + } |
| 10 | + |
| 11 | + public unobserve() { |
| 12 | + // do nothing |
| 13 | + } |
| 14 | + |
| 15 | + public disconnect() { |
| 16 | + // do nothing |
| 17 | + } |
| 18 | + } as unknown as typeof ResizeObserver; |
| 19 | + }); |
| 20 | + |
| 21 | + const createElementWithSize = (tagName: string, width: number) => { |
| 22 | + const element = document.createElement(tagName); |
| 23 | + Object.defineProperty(element, 'offsetWidth', { configurable: true, value: width }); |
| 24 | + Object.defineProperty(element, 'offsetHeight', { configurable: true, value: width }); |
| 25 | + |
| 26 | + return element; |
| 27 | + }; |
| 28 | + |
| 29 | + const createContainer = (width: number) => { |
| 30 | + const container = document.createElement('div'); |
| 31 | + Object.defineProperty(container, 'clientWidth', { configurable: true, value: width }); |
| 32 | + Object.defineProperty(container, 'clientHeight', { configurable: true, value: width }); |
| 33 | + |
| 34 | + return container; |
| 35 | + }; |
| 36 | + |
| 37 | + const createObserveOptions = (options: Partial<ObserveOptions> = {}): ObserveOptions => ({ |
| 38 | + overflowAxis: 'horizontal', |
| 39 | + overflowDirection: 'end', |
| 40 | + padding: 10, |
| 41 | + minimumVisible: 0, |
| 42 | + hasHiddenItems: false, |
| 43 | + onUpdateItemVisibility: jest.fn(), |
| 44 | + onUpdateOverflow: jest.fn(), |
| 45 | + ...options, |
| 46 | + }); |
| 47 | + |
| 48 | + const lastDispatch = (onUpdateOverflow: jest.Mock): OverflowEventPayload => |
| 49 | + onUpdateOverflow.mock.calls[onUpdateOverflow.mock.calls.length - 1][0]; |
| 50 | + |
| 51 | + it('should dispatch overflow update after forceUpdate', () => { |
| 52 | + const onUpdateOverflow = jest.fn(); |
| 53 | + const options = createObserveOptions({ onUpdateOverflow }); |
| 54 | + const manager = createOverflowManager(options); |
| 55 | + const container = createContainer(100); |
| 56 | + const itemA = createElementWithSize('button', 40); |
| 57 | + const itemB = createElementWithSize('button', 40); |
| 58 | + const menu = createElementWithSize('button', 20); |
| 59 | + |
| 60 | + manager.addItem({ element: itemA, id: 'a', priority: 1 }); |
| 61 | + manager.addItem({ element: itemB, id: 'b', priority: 0 }); |
| 62 | + manager.addOverflowMenu(menu); |
| 63 | + manager.observe(container); |
| 64 | + manager.forceUpdate(); |
| 65 | + |
| 66 | + const dispatch = lastDispatch(onUpdateOverflow); |
| 67 | + expect(dispatch.visibleItems.map(item => item.id).sort()).toEqual(['a', 'b']); |
| 68 | + expect(dispatch.invisibleItems).toEqual([]); |
| 69 | + expect(dispatch.groupVisibility).toEqual({}); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should re-dispatch when setOptions changes a relevant option', () => { |
| 73 | + const onUpdateOverflow = jest.fn(); |
| 74 | + const options = createObserveOptions({ onUpdateOverflow }); |
| 75 | + const manager = createOverflowManager(options); |
| 76 | + const container = createContainer(100); |
| 77 | + const itemA = createElementWithSize('button', 40); |
| 78 | + const itemB = createElementWithSize('button', 40); |
| 79 | + const menu = createElementWithSize('button', 20); |
| 80 | + |
| 81 | + manager.addItem({ element: itemA, id: 'a', priority: 1 }); |
| 82 | + manager.addItem({ element: itemB, id: 'b', priority: 0 }); |
| 83 | + manager.addOverflowMenu(menu); |
| 84 | + manager.observe(container); |
| 85 | + manager.forceUpdate(); |
| 86 | + |
| 87 | + onUpdateOverflow.mockClear(); |
| 88 | + manager.setOptions({ padding: 30 }); |
| 89 | + |
| 90 | + expect(onUpdateOverflow).toHaveBeenCalled(); |
| 91 | + const dispatch = lastDispatch(onUpdateOverflow); |
| 92 | + expect(dispatch.visibleItems.map(item => item.id)).toEqual(['a']); |
| 93 | + expect(dispatch.invisibleItems.map(item => item.id)).toEqual(['b']); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should not re-dispatch when setOptions is called with a partial that does not change anything', () => { |
| 97 | + const onUpdateOverflow = jest.fn(); |
| 98 | + const options = createObserveOptions({ onUpdateOverflow }); |
| 99 | + const manager = createOverflowManager(options); |
| 100 | + const container = createContainer(100); |
| 101 | + const itemA = createElementWithSize('button', 40); |
| 102 | + |
| 103 | + manager.addItem({ element: itemA, id: 'a', priority: 1 }); |
| 104 | + manager.observe(container, options); |
| 105 | + manager.forceUpdate(); |
| 106 | + |
| 107 | + onUpdateOverflow.mockClear(); |
| 108 | + manager.setOptions({ padding: 10 }); // padding is already 10; no real change |
| 109 | + |
| 110 | + expect(onUpdateOverflow).not.toHaveBeenCalled(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('disconnect stops observation and re-observe restarts dispatching', () => { |
| 114 | + const onUpdateOverflow = jest.fn(); |
| 115 | + const options = createObserveOptions({ onUpdateOverflow }); |
| 116 | + const manager = createOverflowManager(options); |
| 117 | + const container = createContainer(100); |
| 118 | + const item = createElementWithSize('button', 40); |
| 119 | + |
| 120 | + manager.addItem({ element: item, id: 'a', priority: 1 }); |
| 121 | + manager.observe(container); |
| 122 | + manager.forceUpdate(); |
| 123 | + expect(lastDispatch(onUpdateOverflow).visibleItems.map(i => i.id)).toEqual(['a']); |
| 124 | + |
| 125 | + manager.disconnect(); |
| 126 | + onUpdateOverflow.mockClear(); |
| 127 | + |
| 128 | + manager.addItem({ element: item, id: 'a', priority: 1 }); |
| 129 | + manager.observe(container); |
| 130 | + manager.forceUpdate(); |
| 131 | + expect(onUpdateOverflow).toHaveBeenCalled(); |
| 132 | + expect(lastDispatch(onUpdateOverflow).visibleItems.map(i => i.id)).toEqual(['a']); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should remove items through removeItem', () => { |
| 136 | + const onUpdateOverflow = jest.fn(); |
| 137 | + const options = createObserveOptions({ onUpdateOverflow }); |
| 138 | + const manager = createOverflowManager(options); |
| 139 | + const container = createContainer(100); |
| 140 | + const item = createElementWithSize('button', 40); |
| 141 | + |
| 142 | + manager.addItem({ element: item, id: 'a', priority: 1 }); |
| 143 | + manager.observe(container); |
| 144 | + manager.forceUpdate(); |
| 145 | + |
| 146 | + expect(lastDispatch(onUpdateOverflow).visibleItems.map(i => i.id)).toEqual(['a']); |
| 147 | + |
| 148 | + manager.removeItem('a'); |
| 149 | + manager.forceUpdate(); |
| 150 | + |
| 151 | + const dispatch = lastDispatch(onUpdateOverflow); |
| 152 | + expect(dispatch.visibleItems).toEqual([]); |
| 153 | + expect(dispatch.invisibleItems).toEqual([]); |
| 154 | + }); |
| 155 | +}); |
0 commit comments