monitoring_legends_spec.js 3.19 KB
Newer Older
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
import Vue from 'vue';
import MonitoringLegends from '~/monitoring/components/monitoring_legends.vue';
import measurements from '~/monitoring/utils/measurements';

const createComponent = (propsData) => {
  const Component = Vue.extend(MonitoringLegends);

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

function getTextFromNode(component, selector) {
  return component.$el.querySelector(selector).firstChild.nodeValue.trim();
}

describe('MonitoringLegends', () => {
  describe('Computed props', () => {
    it('textTransform', () => {
      const component = createComponent({
        graphWidth: 500,
        graphHeight: 300,
        margin: measurements.large.margin,
        measurements: measurements.large,
        areaColorRgb: '#f0f0f0',
        legendTitle: 'Title',
        yAxisLabel: 'Values',
        metricUsage: 'Value',
      });

      expect(component.textTransform).toContain('translate(15, 120) rotate(-90)');
    });

    it('xPosition', () => {
      const component = createComponent({
        graphWidth: 500,
        graphHeight: 300,
        margin: measurements.large.margin,
        measurements: measurements.large,
        areaColorRgb: '#f0f0f0',
        legendTitle: 'Title',
        yAxisLabel: 'Values',
        metricUsage: 'Value',
      });

      expect(component.xPosition).toEqual(180);
    });

    it('yPosition', () => {
      const component = createComponent({
        graphWidth: 500,
        graphHeight: 300,
        margin: measurements.large.margin,
        measurements: measurements.large,
        areaColorRgb: '#f0f0f0',
        legendTitle: 'Title',
        yAxisLabel: 'Values',
        metricUsage: 'Value',
      });

      expect(component.yPosition).toEqual(240);
    });

    it('rectTransform', () => {
      const component = createComponent({
        graphWidth: 500,
        graphHeight: 300,
        margin: measurements.large.margin,
        measurements: measurements.large,
        areaColorRgb: '#f0f0f0',
        legendTitle: 'Title',
        yAxisLabel: 'Values',
        metricUsage: 'Value',
      });

      expect(component.rectTransform).toContain('translate(0, 120) rotate(-90)');
    });
  });

  it('has 2 rect-axis-text rect svg elements', () => {
    const component = createComponent({
      graphWidth: 500,
      graphHeight: 300,
      margin: measurements.large.margin,
      measurements: measurements.large,
      areaColorRgb: '#f0f0f0',
      legendTitle: 'Title',
      yAxisLabel: 'Values',
      metricUsage: 'Value',
    });

    expect(component.$el.querySelectorAll('.rect-axis-text').length).toEqual(2);
  });

  it('contains text to signal the usage, title and time', () => {
    const component = createComponent({
      graphWidth: 500,
      graphHeight: 300,
      margin: measurements.large.margin,
      measurements: measurements.large,
      areaColorRgb: '#f0f0f0',
      legendTitle: 'Title',
      yAxisLabel: 'Values',
      metricUsage: 'Value',
    });

    expect(getTextFromNode(component, '.text-metric-title')).toEqual(component.legendTitle);
    expect(getTextFromNode(component, '.text-metric-usage')).toEqual(component.metricUsage);
    expect(getTextFromNode(component, '.label-axis-text')).toEqual(component.yAxisLabel);
  });
});