1
import * as React from 'react';
3
import { shallow, mount } from 'enzyme';
4
import Graph from './Graph';
5
import ReactResizeDetector from 'react-resize-detector';
6
import { Legend } from './Legend';
7
import { GraphDisplayMode } from './Panel';
9
describe('Graph', () => {
11
jest.spyOn(window, 'requestAnimationFrame').mockImplementation((cb: any) => cb());
14
// Source: https://github.com/maslianok/react-resize-detector#testing-with-enzyme-and-jest
16
window.ResizeObserver = jest.fn().mockImplementation(() => ({
19
disconnect: jest.fn(),
24
window.ResizeObserver = ResizeObserver;
27
describe('data is returned', () => {
30
startTime: 1572128592,
34
displayMode: GraphDisplayMode.Stacked,
42
instance: 'localhost:9090',
77
instance: 'localhost:9090',
85
timestamp: 1572130580,
94
it('renders a graph with props', () => {
95
const graph = shallow(<Graph {...props} />);
96
const div = graph.find('div').filterWhere((elem) => elem.prop('className') === 'graph-test');
97
const resize = div.find(ReactResizeDetector);
98
const innerdiv = div.find('div').filterWhere((elem) => elem.prop('className') === 'graph-chart');
99
expect(resize.prop('handleWidth')).toBe(true);
100
expect(div).toHaveLength(1);
101
expect(innerdiv).toHaveLength(1);
103
describe('Legend', () => {
104
it('renders a legend', () => {
105
const graph = shallow(<Graph {...props} />);
106
expect(graph.find(Legend)).toHaveLength(1);
110
describe('on component update', () => {
115
mockPlot = jest.spyOn($, 'plot').mockReturnValue({ setData: jest.fn(), draw: jest.fn(), destroy: jest.fn() } as any);
119
displayMode: GraphDisplayMode.Stacked,
121
startTime: 1572128592,
125
data: { result: [{ values: [], metric: {} }] },
129
spyState = jest.spyOn(graph.instance(), 'setState');
132
spyState.mockReset();
133
mockPlot.mockReset();
135
it('should trigger state update when new data is received', () => {
136
graph.setProps({ data: { result: [{ values: [{}], metric: {} }] } });
137
expect(spyState).toHaveBeenCalledWith(
144
data: [[1572128592000, null]],
155
it('should trigger state update when stacked prop is changed', () => {
156
graph.setProps({ displayMode: GraphDisplayMode.Lines });
157
expect(spyState).toHaveBeenCalledWith(
164
data: [[1572128592000, null]],
176
describe('on unmount', () => {
177
it('should call destroy plot', () => {
181
displayMode: GraphDisplayMode.Stacked,
183
startTime: 1572128592,
187
data: { result: [{ values: [], metric: {} }] },
191
const spyPlotDestroy = jest.spyOn(graph.instance(), 'componentWillUnmount');
193
expect(spyPlotDestroy).toHaveBeenCalledTimes(1);
194
spyPlotDestroy.mockReset();
198
describe('plot', () => {
199
it('should not call jquery.plot if chartRef not exist', () => {
200
const mockSetData = jest.fn();
201
jest.spyOn($, 'plot').mockReturnValue({ setData: mockSetData, draw: jest.fn(), destroy: jest.fn() } as any);
202
const graph = shallow(
205
displayMode: GraphDisplayMode.Stacked,
207
startTime: 1572128592,
211
data: { result: [{ values: [], metric: {} }] },
215
(graph.instance() as any).plot();
216
expect(mockSetData).not.toBeCalled();
218
it('should call jquery.plot if chartRef exist', () => {
219
const mockPlot = jest
221
.mockReturnValue({ setData: jest.fn(), draw: jest.fn(), destroy: jest.fn() } as any);
225
displayMode: GraphDisplayMode.Stacked,
227
startTime: 1572128592,
231
data: { result: [{ values: [], metric: {} }] },
235
(graph.instance() as any).plot();
236
expect(mockPlot).toBeCalled();
238
it('should destroy plot', () => {
239
const mockDestroy = jest.fn();
240
jest.spyOn($, 'plot').mockReturnValue({ setData: jest.fn(), draw: jest.fn(), destroy: mockDestroy } as any);
244
displayMode: GraphDisplayMode.Stacked,
246
startTime: 1572128592,
250
data: { result: [{ values: [], metric: {} }] },
254
(graph.instance() as any).plot();
255
(graph.instance() as any).destroyPlot();
256
expect(mockDestroy).toHaveBeenCalledTimes(2);
259
describe('plotSetAndDraw', () => {
260
it('should call spyPlotSetAndDraw on legend hover', () => {
261
jest.spyOn($, 'plot').mockReturnValue({ setData: jest.fn(), draw: jest.fn(), destroy: jest.fn() } as any);
265
displayMode: GraphDisplayMode.Stacked,
267
startTime: 1572128592,
273
{ values: [], metric: {} },
274
{ values: [], metric: {} },
280
(graph.instance() as any).plot(); // create chart
281
const spyPlotSetAndDraw = jest.spyOn(graph.instance() as any, 'plotSetAndDraw');
282
graph.find('.legend-item').at(0).simulate('mouseover');
283
expect(spyPlotSetAndDraw).toHaveBeenCalledTimes(1);
285
it('should call spyPlotSetAndDraw with chartDate from state as default value', () => {
286
const mockSetData = jest.fn();
289
.mockReturnValue({ setData: mockSetData, draw: jest.fn(), destroy: jest.fn() } as any);
290
const graph: any = mount(
293
displayMode: GraphDisplayMode.Stacked,
295
startTime: 1572128592,
301
{ values: [], metric: {} },
302
{ values: [], metric: {} },
308
(graph.instance() as any).plot(); // create chart
309
graph.find('.graph-legend').simulate('mouseout');
310
expect(mockSetData).toHaveBeenCalledWith(graph.state().chartData.series);