external_dashboard_spec.js 4.8 KB
Newer Older
1
import { mount, shallowMount, createLocalVue } from '@vue/test-utils';
A
Adriel Santiago 已提交
2 3
import { GlButton, GlLink, GlFormGroup, GlFormInput } from '@gitlab/ui';
import ExternalDashboard from '~/operation_settings/components/external_dashboard.vue';
4 5 6 7
import store from '~/operation_settings/store';
import axios from '~/lib/utils/axios_utils';
import { refreshCurrentPage } from '~/lib/utils/url_utility';
import createFlash from '~/flash';
A
Adriel Santiago 已提交
8 9
import { TEST_HOST } from 'helpers/test_constants';

10 11 12 13
jest.mock('~/lib/utils/axios_utils');
jest.mock('~/lib/utils/url_utility');
jest.mock('~/flash');

A
Adriel Santiago 已提交
14 15
describe('operation settings external dashboard component', () => {
  let wrapper;
16 17
  const operationsSettingsEndpoint = `${TEST_HOST}/mock/ops/settings/endpoint`;
  const externalDashboardUrl = `http://mock-external-domain.com/external/dashboard/url`;
A
Adriel Santiago 已提交
18
  const externalDashboardHelpPagePath = `${TEST_HOST}/help/page/path`;
19 20 21 22 23 24 25 26 27 28 29
  const localVue = createLocalVue();
  const mountComponent = (shallow = true) => {
    const config = [
      ExternalDashboard,
      {
        localVue,
        store: store({
          operationsSettingsEndpoint,
          externalDashboardUrl,
          externalDashboardHelpPagePath,
        }),
A
Adriel Santiago 已提交
30
      },
31 32 33 34 35 36 37 38 39 40 41
    ];
    wrapper = shallow ? shallowMount(...config) : mount(...config);
  };

  afterEach(() => {
    if (wrapper.destroy) {
      wrapper.destroy();
    }
    axios.patch.mockReset();
    refreshCurrentPage.mockReset();
    createFlash.mockReset();
A
Adriel Santiago 已提交
42 43 44
  });

  it('renders header text', () => {
45
    mountComponent();
A
Adriel Santiago 已提交
46 47 48
    expect(wrapper.find('.js-section-header').text()).toBe('External Dashboard');
  });

A
Adriel Santiago 已提交
49 50 51 52 53 54 55 56
  describe('expand/collapse button', () => {
    it('renders as an expand button by default', () => {
      const button = wrapper.find(GlButton);

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

A
Adriel Santiago 已提交
57 58 59 60
  describe('sub-header', () => {
    let subHeader;

    beforeEach(() => {
61
      mountComponent();
A
Adriel Santiago 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
      subHeader = wrapper.find('.js-section-sub-header');
    });

    it('renders descriptive text', () => {
      expect(subHeader.text()).toContain(
        'Add a button to the metrics dashboard linking directly to your existing external dashboards.',
      );
    });

    it('renders help page link', () => {
      const link = subHeader.find(GlLink);

      expect(link.text()).toBe('Learn more');
      expect(link.attributes().href).toBe(externalDashboardHelpPagePath);
    });
  });

  describe('form', () => {
80 81
    describe('input label', () => {
      let formGroup;
A
Adriel Santiago 已提交
82

83 84 85 86
      beforeEach(() => {
        mountComponent();
        formGroup = wrapper.find(GlFormGroup);
      });
A
Adriel Santiago 已提交
87

88 89 90
      it('uses label text', () => {
        expect(formGroup.attributes().label).toBe('Full dashboard URL');
      });
A
Adriel Santiago 已提交
91

92 93 94 95 96 97
      it('uses description text', () => {
        expect(formGroup.attributes().description).toBe(
          'Enter the URL of the dashboard you want to link to',
        );
      });
    });
A
Adriel Santiago 已提交
98

99 100
    describe('input field', () => {
      let input;
A
Adriel Santiago 已提交
101

102 103 104
      beforeEach(() => {
        mountComponent();
        input = wrapper.find(GlFormInput);
A
Adriel Santiago 已提交
105 106
      });

107 108 109
      it('defaults to externalDashboardUrl', () => {
        expect(input.attributes().value).toBe(externalDashboardUrl);
      });
A
Adriel Santiago 已提交
110

111 112 113 114
      it('uses a placeholder', () => {
        expect(input.attributes().placeholder).toBe('https://my-org.gitlab.io/my-dashboards');
      });
    });
A
Adriel Santiago 已提交
115

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

119 120 121 122 123 124 125 126 127 128 129 130 131
      const endpointRequest = [
        operationsSettingsEndpoint,
        {
          project: {
            metrics_setting_attributes: {
              external_dashboard_url: externalDashboardUrl,
            },
          },
        },
      ];

      it('renders button label', () => {
        mountComponent();
132
        const submit = findSubmitButton();
133
        expect(submit.text()).toBe('Save Changes');
A
Adriel Santiago 已提交
134 135
      });

136 137 138
      it('submits form on click', () => {
        mountComponent(false);
        axios.patch.mockResolvedValue();
139
        findSubmitButton().trigger('click');
140 141

        expect(axios.patch).toHaveBeenCalledWith(...endpointRequest);
A
Adriel Santiago 已提交
142

143 144
        return wrapper.vm.$nextTick().then(() => expect(refreshCurrentPage).toHaveBeenCalled());
      });
A
Adriel Santiago 已提交
145

146 147 148 149
      it('creates flash banner on error', () => {
        mountComponent(false);
        const message = 'mockErrorMessage';
        axios.patch.mockRejectedValue({ response: { data: { message } } });
150
        findSubmitButton().trigger('click');
151 152 153 154 155 156 157 158 159 160 161 162

        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',
            ),
          );
A
Adriel Santiago 已提交
163 164 165 166
      });
    });
  });
});