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
jest.mock('~/lib/utils/url_utility');
jest.mock('~/flash');

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

34 35 36 37
  beforeEach(() => {
    jest.spyOn(axios, 'patch').mockImplementation();
  });

38 39 40 41 42 43 44
  afterEach(() => {
    if (wrapper.destroy) {
      wrapper.destroy();
    }
    axios.patch.mockReset();
    refreshCurrentPage.mockReset();
    createFlash.mockReset();
A
Adriel Santiago 已提交
45 46 47
  });

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

A
Adriel Santiago 已提交
52 53 54 55 56 57 58 59
  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 已提交
60 61 62 63
  describe('sub-header', () => {
    let subHeader;

    beforeEach(() => {
64
      mountComponent();
A
Adriel Santiago 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
      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', () => {
83 84
    describe('input label', () => {
      let formGroup;
A
Adriel Santiago 已提交
85

86 87 88 89
      beforeEach(() => {
        mountComponent();
        formGroup = wrapper.find(GlFormGroup);
      });
A
Adriel Santiago 已提交
90

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

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

102 103
    describe('input field', () => {
      let input;
A
Adriel Santiago 已提交
104

105 106 107
      beforeEach(() => {
        mountComponent();
        input = wrapper.find(GlFormInput);
A
Adriel Santiago 已提交
108 109
      });

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

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

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

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

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

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

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

146 147
        return wrapper.vm.$nextTick().then(() => expect(refreshCurrentPage).toHaveBeenCalled());
      });
A
Adriel Santiago 已提交
148

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

        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 已提交
166 167 168 169
      });
    });
  });
});