summaryrefslogtreecommitdiff
path: root/spec/frontend/grafana_integration/components/grafana_integration_spec.js
blob: f1a8e6fe2dcaa89d6077486f1a4a60fe08a91624 (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
import { GlButton } from '@gitlab/ui';
import { mount, shallowMount } from '@vue/test-utils';
import { TEST_HOST } from 'helpers/test_constants';
import { deprecatedCreateFlash as createFlash } from '~/flash';
import GrafanaIntegration from '~/grafana_integration/components/grafana_integration.vue';
import { createStore } from '~/grafana_integration/store';
import axios from '~/lib/utils/axios_utils';
import { refreshCurrentPage } from '~/lib/utils/url_utility';

jest.mock('~/lib/utils/url_utility');
jest.mock('~/flash');

describe('grafana integration component', () => {
  let wrapper;
  let store;
  const operationsSettingsEndpoint = `${TEST_HOST}/mock/ops/settings/endpoint`;
  const grafanaIntegrationUrl = `${TEST_HOST}`;
  const grafanaIntegrationToken = 'someToken';

  beforeEach(() => {
    store = createStore({
      operationsSettingsEndpoint,
      grafanaIntegrationUrl,
      grafanaIntegrationToken,
    });
  });

  afterEach(() => {
    if (wrapper.destroy) {
      wrapper.destroy();
      createFlash.mockReset();
      refreshCurrentPage.mockReset();
    }
  });

  describe('default state', () => {
    it('to match the default snapshot', () => {
      wrapper = shallowMount(GrafanaIntegration, { store });

      expect(wrapper.element).toMatchSnapshot();
    });
  });

  it('renders header text', () => {
    wrapper = shallowMount(GrafanaIntegration, { store });

    expect(wrapper.find('.js-section-header').text()).toBe('Grafana authentication');
  });

  describe('expand/collapse button', () => {
    it('renders as an expand button by default', () => {
      wrapper = shallowMount(GrafanaIntegration, { store });

      const button = wrapper.find(GlButton);

      expect(button.text()).toBe('Expand');
    });
  });

  describe('sub-header', () => {
    it('renders descriptive text', () => {
      wrapper = shallowMount(GrafanaIntegration, { store });

      expect(wrapper.find('.js-section-sub-header').text()).toContain(
        'Set up Grafana authentication to embed Grafana panels in GitLab Flavored Markdown.\n      Learn more.',
      );
    });
  });

  describe('form', () => {
    beforeEach(() => {
      jest.spyOn(axios, 'patch').mockImplementation();
    });

    afterEach(() => {
      axios.patch.mockReset();
    });

    describe('submit button', () => {
      const findSubmitButton = () => wrapper.find('.settings-content form').find(GlButton);

      const endpointRequest = [
        operationsSettingsEndpoint,
        {
          project: {
            grafana_integration_attributes: {
              grafana_url: grafanaIntegrationUrl,
              token: grafanaIntegrationToken,
              enabled: false,
            },
          },
        },
      ];

      it('submits form on click', () => {
        wrapper = mount(GrafanaIntegration, { store });
        axios.patch.mockResolvedValue();

        findSubmitButton(wrapper).trigger('click');

        expect(axios.patch).toHaveBeenCalledWith(...endpointRequest);
        return wrapper.vm.$nextTick().then(() => expect(refreshCurrentPage).toHaveBeenCalled());
      });

      it('creates flash banner on error', () => {
        const message = 'mockErrorMessage';
        wrapper = mount(GrafanaIntegration, { store });
        axios.patch.mockRejectedValue({ response: { data: { message } } });

        findSubmitButton().trigger('click');

        expect(axios.patch).toHaveBeenCalledWith(...endpointRequest);
        return wrapper.vm
          .$nextTick()
          .then(jest.runAllTicks)
          .then(() =>
            expect(createFlash).toHaveBeenCalledWith(
              `There was an error saving your changes. ${message}`,
              'alert',
            ),
          );
      });
    });
  });
});