graph_spec.js 3.29 KB
Newer Older
1 2
import Vue from 'vue';
import _ from 'underscore';
3
import Graph from '~/monitoring/components/graph.vue';
4 5 6 7 8
import MonitoringMixins from '~/monitoring/mixins/monitoring_mixins';
import eventHub from '~/monitoring/event_hub';
import { deploymentData, singleRowMetrics } from './mock_data';

const createComponent = (propsData) => {
9
  const Component = Vue.extend(Graph);
10 11 12 13 14 15

  return new Component({
    propsData,
  }).$mount();
};

16
describe('Graph', () => {
17
  beforeEach(() => {
18
    spyOn(MonitoringMixins.methods, 'formatDeployments').and.returnValue({});
19 20 21 22
  });

  it('has a title', () => {
    const component = createComponent({
23
      graphData: singleRowMetrics[0],
24 25 26 27 28
      classType: 'col-md-6',
      updateAspectRatio: false,
      deploymentData,
    });

29
    expect(component.$el.querySelector('.text-center').innerText.trim()).toBe(component.graphData.title);
30 31 32 33
  });

  it('creates a path for the line and area of the graph', (done) => {
    const component = createComponent({
34
      graphData: singleRowMetrics[0],
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
      classType: 'col-md-6',
      updateAspectRatio: false,
      deploymentData,
    });

    Vue.nextTick(() => {
      expect(component.area).toBeDefined();
      expect(component.line).toBeDefined();
      expect(typeof component.area).toEqual('string');
      expect(typeof component.line).toEqual('string');
      expect(_.isFunction(component.xScale)).toBe(true);
      expect(_.isFunction(component.yScale)).toBe(true);
      done();
    });
  });

  describe('Computed props', () => {
    it('axisTransform translates an element Y position depending of its height', () => {
      const component = createComponent({
54
        graphData: singleRowMetrics[0],
55 56 57 58 59 60 61 62 63 64 65 66
        classType: 'col-md-6',
        updateAspectRatio: false,
        deploymentData,
      });

      const transformedHeight = `${component.graphHeight - 100}`;
      expect(component.axisTransform.indexOf(transformedHeight))
        .not.toEqual(-1);
    });

    it('outterViewBox gets a width and height property based on the DOM size of the element', () => {
      const component = createComponent({
67
        graphData: singleRowMetrics[0],
68 69 70 71 72 73 74 75 76 77 78 79 80 81
        classType: 'col-md-6',
        updateAspectRatio: false,
        deploymentData,
      });

      const viewBoxArray = component.outterViewBox.split(' ');
      expect(typeof component.outterViewBox).toEqual('string');
      expect(viewBoxArray[2]).toEqual(component.graphWidth.toString());
      expect(viewBoxArray[3]).toEqual(component.graphHeight.toString());
    });
  });

  it('sends an event to the eventhub when it has finished resizing', (done) => {
    const component = createComponent({
82
      graphData: singleRowMetrics[0],
83 84 85 86 87 88 89 90 91 92 93 94
      classType: 'col-md-6',
      updateAspectRatio: false,
      deploymentData,
    });
    spyOn(eventHub, '$emit');

    component.updateAspectRatio = true;
    Vue.nextTick(() => {
      expect(eventHub.$emit).toHaveBeenCalled();
      done();
    });
  });
95

96
  it('has a title for the y-axis and the chart legend that comes from the backend', () => {
97
    const component = createComponent({
98
      graphData: singleRowMetrics[0],
99 100 101 102 103
      classType: 'col-md-6',
      updateAspectRatio: false,
      deploymentData,
    });

104 105
    expect(component.yAxisLabel).toEqual(component.graphData.y_label);
    expect(component.legendTitle).toEqual(component.graphData.queries[0].label);
106
  });
107
});