diff options
Diffstat (limited to 'spec/frontend/incidents_settings/components')
6 files changed, 233 insertions, 35 deletions
diff --git a/spec/frontend/incidents_settings/components/__snapshots__/incidents_settings_tabs_spec.js.snap b/spec/frontend/incidents_settings/components/__snapshots__/incidents_settings_tabs_spec.js.snap index d5d927eb63e..5f355ee8261 100644 --- a/spec/frontend/incidents_settings/components/__snapshots__/incidents_settings_tabs_spec.js.snap +++ b/spec/frontend/incidents_settings/components/__snapshots__/incidents_settings_tabs_spec.js.snap @@ -48,7 +48,14 @@ exports[`IncidentsSettingTabs should render the component 1`] = ` data-testid="AlertsSettingsForm-tab" /> </gl-tab-stub> - <!----> + <gl-tab-stub + title="PagerDuty integration" + > + <pagerdutysettingsform-stub + class="gl-pt-3" + data-testid="PagerDutySettingsForm-tab" + /> + </gl-tab-stub> <!----> </gl-tabs-stub> </div> diff --git a/spec/frontend/incidents_settings/components/__snapshots__/pagerduty_form_spec.js.snap b/spec/frontend/incidents_settings/components/__snapshots__/pagerduty_form_spec.js.snap new file mode 100644 index 00000000000..17ada722034 --- /dev/null +++ b/spec/frontend/incidents_settings/components/__snapshots__/pagerduty_form_spec.js.snap @@ -0,0 +1,89 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Alert integration settings form should match the default snapshot 1`] = ` +<div> + <!----> + + <p> + Setting up a webhook with PagerDuty will automatically create a GitLab issue for each PagerDuty incident. + </p> + + <form> + <gl-form-group-stub + class="col-8 col-md-9 gl-p-0" + > + <gl-toggle-stub + id="active" + label="Active" + labelposition="top" + value="true" + /> + </gl-form-group-stub> + + <gl-form-group-stub + class="col-8 col-md-9 gl-p-0" + label="Webhook URL" + label-class="label-bold" + label-for="url" + > + <gl-form-input-group-stub + data-testid="webhook-url" + id="url" + predefinedoptions="[object Object]" + readonly="" + value="pagerduty.webhook.com" + /> + + <div + class="gl-text-gray-400 gl-pt-2" + > + <gl-sprintf-stub + message="Create a GitLab issue for each PagerDuty incident by %{docsLink}" + /> + </div> + + <gl-button-stub + category="tertiary" + class="gl-mt-3" + data-testid="webhook-reset-btn" + icon="" + role="button" + size="medium" + tabindex="0" + variant="default" + > + + Reset webhook URL + + </gl-button-stub> + + <gl-modal-stub + modalclass="" + modalid="resetWebhookModal" + ok-title="Reset webhook URL" + ok-variant="danger" + size="md" + title="Reset webhook URL" + titletag="h4" + > + + Resetting the webhook URL for this project will require updating this integration's settings in PagerDuty. + + </gl-modal-stub> + </gl-form-group-stub> + + <gl-button-stub + category="tertiary" + class="js-no-auto-disable" + icon="" + size="medium" + type="submit" + variant="success" + > + + Save changes + + </gl-button-stub> + </form> +</div> +`; diff --git a/spec/frontend/incidents_settings/components/alerts_form_spec.js b/spec/frontend/incidents_settings/components/alerts_form_spec.js index 2a27347e40e..04832f31e58 100644 --- a/spec/frontend/incidents_settings/components/alerts_form_spec.js +++ b/spec/frontend/incidents_settings/components/alerts_form_spec.js @@ -1,24 +1,16 @@ import { shallowMount } from '@vue/test-utils'; -import MockAdapter from 'axios-mock-adapter'; -import axios from '~/lib/utils/axios_utils'; import AlertsSettingsForm from '~/incidents_settings/components/alerts_form.vue'; -import { ERROR_MSG } from '~/incidents_settings/constants'; -import createFlash from '~/flash'; -import { refreshCurrentPage } from '~/lib/utils/url_utility'; -import waitForPromises from 'helpers/wait_for_promises'; - -jest.mock('~/flash'); -jest.mock('~/lib/utils/url_utility'); describe('Alert integration settings form', () => { let wrapper; + const service = { updateSettings: jest.fn().mockResolvedValue() }; const findForm = () => wrapper.find({ ref: 'settingsForm' }); beforeEach(() => { wrapper = shallowMount(AlertsSettingsForm, { provide: { - operationsSettingsEndpoint: 'operations/endpoint', + service, alertSettings: { issueTemplateKey: 'selecte_tmpl', createIssue: true, @@ -32,6 +24,7 @@ describe('Alert integration settings form', () => { afterEach(() => { if (wrapper) { wrapper.destroy(); + wrapper = null; } }); @@ -42,30 +35,15 @@ describe('Alert integration settings form', () => { }); describe('form', () => { - let mock; - - beforeEach(() => { - mock = new MockAdapter(axios); - }); - - afterEach(() => { - mock.restore(); - }); - - it('should refresh the page on successful submit', () => { - mock.onPatch().reply(200); - findForm().trigger('submit'); - return waitForPromises().then(() => { - expect(refreshCurrentPage).toHaveBeenCalled(); - }); - }); - - it('should display a flah message on unsuccessful submit', () => { - mock.onPatch().reply(400); + it('should call service `updateSettings` on submit', () => { findForm().trigger('submit'); - return waitForPromises().then(() => { - expect(createFlash).toHaveBeenCalledWith(expect.stringContaining(ERROR_MSG), 'alert'); - }); + expect(service.updateSettings).toHaveBeenCalledWith( + expect.objectContaining({ + create_issue: wrapper.vm.createIssueEnabled, + issue_template_key: wrapper.vm.issueTemplate, + send_email: wrapper.vm.sendEmailEnabled, + }), + ); }); }); }); diff --git a/spec/frontend/incidents_settings/components/incidents_settings_service_spec.js b/spec/frontend/incidents_settings/components/incidents_settings_service_spec.js new file mode 100644 index 00000000000..58f9a318808 --- /dev/null +++ b/spec/frontend/incidents_settings/components/incidents_settings_service_spec.js @@ -0,0 +1,55 @@ +import axios from '~/lib/utils/axios_utils'; +import AxiosMockAdapter from 'axios-mock-adapter'; +import httpStatusCodes from '~/lib/utils/http_status'; +import IncidentsSettingsService from '~/incidents_settings/incidents_settings_service'; +import { ERROR_MSG } from '~/incidents_settings/constants'; +import createFlash from '~/flash'; +import { refreshCurrentPage } from '~/lib/utils/url_utility'; + +jest.mock('~/flash'); +jest.mock('~/lib/utils/url_utility'); + +describe('IncidentsSettingsService', () => { + const settingsEndpoint = 'operations/settings'; + const webhookUpdateEndpoint = 'webhook/update'; + let mock; + let service; + + beforeEach(() => { + mock = new AxiosMockAdapter(axios); + service = new IncidentsSettingsService(settingsEndpoint, webhookUpdateEndpoint); + }); + + afterEach(() => { + mock.restore(); + }); + + describe('updateSettings', () => { + it('should refresh the page on successful update', () => { + mock.onPatch().reply(httpStatusCodes.OK); + + return service.updateSettings({}).then(() => { + expect(refreshCurrentPage).toHaveBeenCalled(); + }); + }); + + it('should display a flash message on update error', () => { + mock.onPatch().reply(httpStatusCodes.BAD_REQUEST); + + return service.updateSettings({}).then(() => { + expect(createFlash).toHaveBeenCalledWith(expect.stringContaining(ERROR_MSG), 'alert'); + }); + }); + }); + + describe('resetWebhookUrl', () => { + it('should make a call for webhook update', () => { + jest.spyOn(axios, 'post'); + mock.onPost().reply(httpStatusCodes.OK); + + return service.resetWebhookUrl().then(() => { + expect(axios.post).toHaveBeenCalledWith(webhookUpdateEndpoint); + }); + }); + }); +}); diff --git a/spec/frontend/incidents_settings/components/incidents_settings_tabs_spec.js b/spec/frontend/incidents_settings/components/incidents_settings_tabs_spec.js index c56b9ed2a69..47e2aecc108 100644 --- a/spec/frontend/incidents_settings/components/incidents_settings_tabs_spec.js +++ b/spec/frontend/incidents_settings/components/incidents_settings_tabs_spec.js @@ -6,7 +6,9 @@ describe('IncidentsSettingTabs', () => { let wrapper; beforeEach(() => { - wrapper = shallowMount(IncidentsSettingTabs); + wrapper = shallowMount(IncidentsSettingTabs, { + provide: { glFeatures: { pagerdutyWebhook: true } }, + }); }); afterEach(() => { diff --git a/spec/frontend/incidents_settings/components/pagerduty_form_spec.js b/spec/frontend/incidents_settings/components/pagerduty_form_spec.js new file mode 100644 index 00000000000..521094ad54c --- /dev/null +++ b/spec/frontend/incidents_settings/components/pagerduty_form_spec.js @@ -0,0 +1,67 @@ +import { shallowMount } from '@vue/test-utils'; +import waitForPromises from 'helpers/wait_for_promises'; +import PagerDutySettingsForm from '~/incidents_settings/components/pagerduty_form.vue'; +import { GlAlert, GlModal } from '@gitlab/ui'; + +describe('Alert integration settings form', () => { + let wrapper; + const resetWebhookUrl = jest.fn(); + const service = { updateSettings: jest.fn().mockResolvedValue(), resetWebhookUrl }; + + const findForm = () => wrapper.find({ ref: 'settingsForm' }); + const findWebhookInput = () => wrapper.find('[data-testid="webhook-url"]'); + const findModal = () => wrapper.find(GlModal); + const findAlert = () => wrapper.find(GlAlert); + + beforeEach(() => { + wrapper = shallowMount(PagerDutySettingsForm, { + provide: { + service, + pagerDutySettings: { + active: true, + webhookUrl: 'pagerduty.webhook.com', + webhookUpdateEndpoint: 'webhook/update', + }, + }, + }); + }); + + afterEach(() => { + if (wrapper) { + wrapper.destroy(); + wrapper = null; + } + }); + + it('should match the default snapshot', () => { + expect(wrapper.element).toMatchSnapshot(); + }); + + it('should call service `updateSettings` on form submit', () => { + findForm().trigger('submit'); + expect(service.updateSettings).toHaveBeenCalledWith( + expect.objectContaining({ pagerduty_active: wrapper.vm.active }), + ); + }); + + describe('Webhook reset', () => { + it('should make a call for webhook reset and reset form values', async () => { + const newWebhookUrl = 'new.webhook.url?token=token'; + resetWebhookUrl.mockResolvedValueOnce({ + data: { pagerduty_webhook_url: newWebhookUrl }, + }); + findModal().vm.$emit('ok'); + await waitForPromises(); + expect(resetWebhookUrl).toHaveBeenCalled(); + expect(findWebhookInput().attributes('value')).toBe(newWebhookUrl); + expect(findAlert().attributes('variant')).toBe('success'); + }); + + it('should show error message and NOT reset webhook url', async () => { + resetWebhookUrl.mockRejectedValueOnce(); + findModal().vm.$emit('ok'); + await waitForPromises(); + expect(findAlert().attributes('variant')).toBe('danger'); + }); + }); +}); |