Welcome to mirror list, hosted at ThFree Co, Russian Federation.

metric_card_spec.js « components « shared « analytics « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f587d227abcbebfeb62f6f1c319f8556f78065b (plain)
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
import { GlDeprecatedSkeletonLoading as GlSkeletonLoading } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
import MetricCard from '~/analytics/shared/components/metric_card.vue';

const metrics = [
  { key: 'first_metric', value: 10, label: 'First metric', unit: 'days', link: 'some_link' },
  { key: 'second_metric', value: 20, label: 'Yet another metric' },
  { key: 'third_metric', value: null, label: 'Null metric without value', unit: 'parsecs' },
  { key: 'fourth_metric', value: '-', label: 'Metric without value', unit: 'parsecs' },
];

const defaultProps = {
  title: 'My fancy title',
  isLoading: false,
  metrics,
};

describe('MetricCard', () => {
  let wrapper;

  const factory = (props = defaultProps) => {
    wrapper = mount(MetricCard, {
      propsData: {
        ...defaultProps,
        ...props,
      },
      directives: {
        GlTooltip: createMockDirective(),
      },
    });
  };

  afterEach(() => {
    wrapper.destroy();
  });

  const findTitle = () => wrapper.find({ ref: 'title' });
  const findLoadingIndicator = () => wrapper.find(GlSkeletonLoading);
  const findMetricsWrapper = () => wrapper.find({ ref: 'metricsWrapper' });
  const findMetricItem = () => wrapper.findAll({ ref: 'metricItem' });
  const findTooltip = () => wrapper.find('[data-testid="tooltip"]');

  describe('template', () => {
    it('renders the title', () => {
      factory();

      expect(findTitle().text()).toContain('My fancy title');
    });

    describe('when isLoading is true', () => {
      beforeEach(() => {
        factory({ isLoading: true });
      });

      it('displays a loading indicator', () => {
        expect(findLoadingIndicator().exists()).toBe(true);
      });

      it('does not display the metrics container', () => {
        expect(findMetricsWrapper().exists()).toBe(false);
      });
    });

    describe('when isLoading is false', () => {
      beforeEach(() => {
        factory({ isLoading: false });
      });

      it('does not display a loading indicator', () => {
        expect(findLoadingIndicator().exists()).toBe(false);
      });

      it('displays the metrics container', () => {
        expect(findMetricsWrapper().exists()).toBe(true);
      });

      it('renders two metrics', () => {
        expect(findMetricItem()).toHaveLength(metrics.length);
      });

      describe('with tooltip text', () => {
        const tooltipText = 'This is a tooltip';
        const tooltipMetric = {
          key: 'fifth_metric',
          value: '-',
          label: 'Metric with tooltip',
          unit: 'parsecs',
          tooltipText,
        };

        beforeEach(() => {
          factory({
            isLoading: false,
            metrics: [tooltipMetric],
          });
        });

        it('will render a tooltip', () => {
          const tt = getBinding(findTooltip().element, 'gl-tooltip');
          expect(tt.value.title).toEqual(tooltipText);
        });
      });

      describe.each`
        columnIndex | label                          | value  | unit       | link
        ${0}        | ${'First metric'}              | ${10}  | ${' days'} | ${'some_link'}
        ${1}        | ${'Yet another metric'}        | ${20}  | ${''}      | ${null}
        ${2}        | ${'Null metric without value'} | ${'-'} | ${''}      | ${null}
        ${3}        | ${'Metric without value'}      | ${'-'} | ${''}      | ${null}
      `('metric columns', ({ columnIndex, label, value, unit, link }) => {
        it(`renders ${value}${unit} ${label} with URL ${link}`, () => {
          const allMetricItems = findMetricItem();
          const metricItem = allMetricItems.at(columnIndex);
          const text = metricItem.text();

          expect(text).toContain(`${value}${unit}`);
          expect(text).toContain(label);

          if (link) {
            expect(metricItem.find('a').attributes('href')).toBe(link);
          } else {
            expect(metricItem.find('a').exists()).toBe(false);
          }
        });
      });
    });
  });
});