forked from deephaven/web-client-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPanel.test.tsx
More file actions
124 lines (110 loc) · 3.32 KB
/
Panel.test.tsx
File metadata and controls
124 lines (110 loc) · 3.32 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
/* eslint-disable no-unused-vars */
/* eslint-disable react/prefer-stateless-function */
/* eslint func-names: "off" */
import React, { Component } from 'react';
import { render } from '@testing-library/react';
import type { Container } from '@deephaven/golden-layout';
import Panel from './Panel';
class TestComponentPanel extends Component {}
function makeChildren() {
return <div>Test Children</div>;
}
function makeComponentPanel() {
return new TestComponentPanel({});
}
function makeGlComponent({
on = jest.fn(),
off = jest.fn(),
emit = jest.fn(),
unbind = jest.fn(),
trigger = jest.fn(),
} = {}) {
return { on, off, emit, unbind, trigger };
}
function renderPanel({
children = makeChildren(),
componentPanel = makeComponentPanel(),
glContainer = makeGlComponent(),
glEventHub = makeGlComponent(),
onResize = jest.fn(),
onShow = jest.fn(),
onBeforeShow = jest.fn(),
onHide = jest.fn(),
onTab = jest.fn(),
onTabClicked = jest.fn(),
} = {}) {
return render(
<Panel
componentPanel={componentPanel}
glContainer={glContainer as unknown as Container}
glEventHub={glEventHub}
onResize={onResize}
onShow={onShow}
onBeforeShow={onBeforeShow}
onHide={onHide}
onTab={onTab}
onTabClicked={onTabClicked}
>
{children}
</Panel>
);
}
it('renders without crashing', () => {
renderPanel();
});
describe('adds and emits events correctly', () => {
it('emits a signal when the tab is clicked', () => {
const on = jest.fn();
const off = jest.fn();
const onResize = jest.fn();
const onShow = jest.fn();
const onBeforeShow = jest.fn();
const onHide = jest.fn();
const onTab = jest.fn();
const onTabClicked = jest.fn();
const glContainer = makeGlComponent({ on, off });
const { unmount } = renderPanel({
glContainer,
onResize,
onShow,
onBeforeShow,
onHide,
onTab,
onTabClicked,
});
// Map of event names to callbacks
const propCallbacks = new Map();
propCallbacks.set('resize', onResize);
propCallbacks.set('show', onBeforeShow);
propCallbacks.set('shown', onShow);
propCallbacks.set('hide', onHide);
propCallbacks.set('tab', onTab);
propCallbacks.set('tabClicked', onTabClicked);
const eventCallbacks = new Map();
for (let i = 0; i < on.mock.calls.length; i += 1) {
const call = on.mock.calls[i];
eventCallbacks.set(call[0], call[1]);
}
// If we start listening to something else in Panel, we want to add it to the tests as well, so check the size
expect(on).toHaveBeenCalledTimes(propCallbacks.size);
expect(off).not.toHaveBeenCalled();
const eventNames = [...propCallbacks.keys()];
for (let i = 0; i < eventNames.length; i += 1) {
const eventName = eventNames[i];
const testArg = { eventName };
const propCallback = propCallbacks.get(eventName);
expect(propCallback).not.toHaveBeenCalled();
eventCallbacks.get(eventName)(testArg);
expect(propCallback).toHaveBeenCalledWith(testArg);
}
unmount();
// Make sure we call `off` with the correct event names on unmount
for (let i = 0; i < eventNames.length; i += 1) {
const eventName = eventNames[i];
expect(off).toHaveBeenCalledWith(
eventName,
eventCallbacks.get(eventName)
);
}
});
});