-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathoverflowManager.test.ts
More file actions
159 lines (127 loc) · 5.06 KB
/
Copy pathoverflowManager.test.ts
File metadata and controls
159 lines (127 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { createOverflowManager } from './overflowManager';
import type { ObserveOptions } from './types';
describe('overflowManager', () => {
beforeAll(() => {
global.ResizeObserver = class ResizeObserver {
public observe() {
// do nothing
}
public unobserve() {
// do nothing
}
public disconnect() {
// do nothing
}
} as unknown as typeof ResizeObserver;
});
const createElementWithSize = (tagName: string, width: number) => {
const element = document.createElement(tagName);
Object.defineProperty(element, 'offsetWidth', { configurable: true, value: width });
Object.defineProperty(element, 'offsetHeight', { configurable: true, value: width });
return element;
};
const createContainer = (width: number) => {
const container = document.createElement('div');
Object.defineProperty(container, 'clientWidth', { configurable: true, value: width });
Object.defineProperty(container, 'clientHeight', { configurable: true, value: width });
return container;
};
const createObserveOptions = (options: Partial<ObserveOptions> = {}): ObserveOptions => ({
overflowAxis: 'horizontal',
overflowDirection: 'end',
padding: 10,
minimumVisible: 0,
hasHiddenItems: false,
onUpdateItemVisibility: jest.fn(),
onUpdateOverflow: jest.fn(),
...options,
});
const getVisibleIds = (manager: ReturnType<typeof createOverflowManager>) =>
manager
.getSnapshot()
.visibleItems.map(item => item.id)
.sort();
const getInvisibleIds = (manager: ReturnType<typeof createOverflowManager>) =>
manager
.getSnapshot()
.invisibleItems.map(item => item.id)
.sort();
it('should expose a stable snapshot after forceUpdate', () => {
const manager = createOverflowManager(createObserveOptions());
const container = createContainer(100);
const itemA = createElementWithSize('button', 40);
const itemB = createElementWithSize('button', 40);
const menu = createElementWithSize('button', 20);
manager.addItem({ element: itemA, id: 'a', priority: 1 });
manager.addItem({ element: itemB, id: 'b', priority: 0 });
manager.addOverflowMenu(menu);
manager.observe(container);
manager.forceUpdate();
expect(getVisibleIds(manager)).toEqual(['a', 'b']);
expect(getInvisibleIds(manager)).toEqual([]);
expect(manager.getSnapshot().groupVisibility).toEqual({});
});
it('should update snapshot and notify subscribers when options change', () => {
const manager = createOverflowManager(createObserveOptions());
const container = createContainer(100);
const itemA = createElementWithSize('button', 40);
const itemB = createElementWithSize('button', 40);
const menu = createElementWithSize('button', 20);
const listener = jest.fn();
manager.addItem({ element: itemA, id: 'a', priority: 1 });
manager.addItem({ element: itemB, id: 'b', priority: 0 });
manager.addOverflowMenu(menu);
manager.observe(container);
manager.forceUpdate();
const unsubscribe = manager.subscribe(listener);
manager.setOptions({ padding: 30 });
expect(listener).toHaveBeenCalled();
expect(getVisibleIds(manager)).toEqual(['a']);
expect(getInvisibleIds(manager)).toEqual(['b']);
expect(manager.getSnapshot().groupVisibility).toEqual({});
unsubscribe();
});
it('should not notify subscribers when setOptions is called with a partial that does not change anything', () => {
const manager = createOverflowManager(createObserveOptions());
const container = createContainer(100);
const itemA = createElementWithSize('button', 40);
manager.addItem({ element: itemA, id: 'a', priority: 1 });
manager.observe(container);
manager.forceUpdate();
const listener = jest.fn();
manager.subscribe(listener);
manager.setOptions({ padding: 10 }); // padding is already 10; no real change
expect(listener).not.toHaveBeenCalled();
});
it('should reset snapshot state when disconnect runs', () => {
const manager = createOverflowManager(createObserveOptions());
const container = createContainer(100);
const item = createElementWithSize('button', 40);
manager.addItem({ element: item, id: 'a', priority: 1 });
manager.observe(container);
manager.forceUpdate();
expect(getVisibleIds(manager)).toEqual(['a']);
manager.disconnect();
expect(manager.getSnapshot()).toEqual({
visibleItems: [],
invisibleItems: [],
groupVisibility: {},
});
});
it('should remove items through removeItem', () => {
const manager = createOverflowManager(createObserveOptions());
const container = createContainer(100);
const item = createElementWithSize('button', 40);
manager.addItem({ element: item, id: 'a', priority: 1 });
manager.observe(container);
manager.forceUpdate();
expect(getVisibleIds(manager)).toEqual(['a']);
manager.removeItem('a');
manager.forceUpdate();
expect(manager.getSnapshot()).toEqual({
visibleItems: [],
invisibleItems: [],
groupVisibility: {},
});
});
});