2021-03-11 19:13:27 +05:30
|
|
|
import { GlSprintf, GlModal, GlFormGroup, GlFormCheckbox, GlLoadingIcon } from '@gitlab/ui';
|
|
|
|
import { shallowMount } from '@vue/test-utils';
|
|
|
|
import axios from 'axios';
|
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import { extendedWrapper } from 'helpers/vue_test_utils_helper';
|
|
|
|
import waitForPromises from 'helpers/wait_for_promises';
|
2023-03-17 16:20:25 +05:30
|
|
|
import { HTTP_STATUS_NOT_FOUND, HTTP_STATUS_OK } from '~/lib/utils/http_status';
|
2021-03-11 19:13:27 +05:30
|
|
|
import CustomNotificationsModal from '~/notifications/components/custom_notifications_modal.vue';
|
|
|
|
import { i18n } from '~/notifications/constants';
|
|
|
|
|
|
|
|
const mockNotificationSettingsResponses = {
|
|
|
|
default: {
|
|
|
|
level: 'custom',
|
|
|
|
events: {
|
|
|
|
new_release: true,
|
|
|
|
new_note: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
updated: {
|
|
|
|
level: 'custom',
|
|
|
|
events: {
|
|
|
|
new_release: true,
|
|
|
|
new_note: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockToastShow = jest.fn();
|
|
|
|
|
|
|
|
describe('CustomNotificationsModal', () => {
|
|
|
|
let wrapper;
|
|
|
|
let mockAxios;
|
|
|
|
|
|
|
|
function createComponent(options = {}) {
|
|
|
|
const { injectedProperties = {}, props = {} } = options;
|
|
|
|
return extendedWrapper(
|
|
|
|
shallowMount(CustomNotificationsModal, {
|
|
|
|
props: {
|
|
|
|
...props,
|
|
|
|
},
|
|
|
|
provide: {
|
|
|
|
...injectedProperties,
|
|
|
|
},
|
|
|
|
mocks: {
|
|
|
|
$toast: {
|
|
|
|
show: mockToastShow,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
stubs: {
|
|
|
|
GlModal,
|
|
|
|
GlFormGroup,
|
|
|
|
GlFormCheckbox,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
const findModalBodyDescription = () => wrapper.findComponent(GlSprintf);
|
|
|
|
const findAllCheckboxes = () => wrapper.findAllComponents(GlFormCheckbox);
|
2021-03-11 19:13:27 +05:30
|
|
|
const findCheckboxAt = (index) => findAllCheckboxes().at(index);
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
gon.api_version = 'v4';
|
|
|
|
mockAxios = new MockAdapter(axios);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
mockAxios.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('template', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
wrapper = createComponent();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the body title and the body message', () => {
|
|
|
|
expect(wrapper.findByTestId('modalBodyTitle').text()).toBe(
|
|
|
|
i18n.customNotificationsModal.bodyTitle,
|
|
|
|
);
|
|
|
|
expect(findModalBodyDescription().attributes('message')).toContain(
|
|
|
|
i18n.customNotificationsModal.bodyMessage,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('checkbox items', () => {
|
|
|
|
beforeEach(async () => {
|
2023-05-27 22:25:52 +05:30
|
|
|
const endpointUrl = '/api/v4/notification_settings';
|
|
|
|
|
|
|
|
mockAxios
|
|
|
|
.onGet(endpointUrl)
|
|
|
|
.reply(HTTP_STATUS_OK, mockNotificationSettingsResponses.default);
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
wrapper = createComponent();
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
wrapper.findComponent(GlModal).vm.$emit('show');
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
await waitForPromises();
|
2021-03-11 19:13:27 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
index | eventId | eventName | enabled | loading
|
|
|
|
${0} | ${'new_release'} | ${'New release'} | ${true} | ${false}
|
2023-05-27 22:25:52 +05:30
|
|
|
${1} | ${'new_note'} | ${'New note'} | ${false} | ${false}
|
2021-03-11 19:13:27 +05:30
|
|
|
`(
|
|
|
|
'renders a checkbox for "$eventName" with checked=$enabled',
|
2023-06-20 00:43:36 +05:30
|
|
|
({ index, eventName, enabled, loading }) => {
|
2021-03-11 19:13:27 +05:30
|
|
|
const checkbox = findCheckboxAt(index);
|
|
|
|
expect(checkbox.text()).toContain(eventName);
|
|
|
|
expect(checkbox.vm.$attrs.checked).toBe(enabled);
|
2022-10-11 01:57:18 +05:30
|
|
|
expect(checkbox.findComponent(GlLoadingIcon).exists()).toBe(loading);
|
2021-03-11 19:13:27 +05:30
|
|
|
},
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('API calls', () => {
|
|
|
|
describe('load notification settings', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(axios, 'get');
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
projectId | groupId | endpointUrl | notificationType | condition
|
|
|
|
${1} | ${null} | ${'/api/v4/projects/1/notification_settings'} | ${'project'} | ${'a projectId is given'}
|
|
|
|
${null} | ${1} | ${'/api/v4/groups/1/notification_settings'} | ${'group'} | ${'a groupId is given'}
|
|
|
|
${null} | ${null} | ${'/api/v4/notification_settings'} | ${'global'} | ${'neither projectId nor groupId are given'}
|
|
|
|
`(
|
|
|
|
'requests $notificationType notification settings when $condition',
|
|
|
|
async ({ projectId, groupId, endpointUrl }) => {
|
|
|
|
const injectedProperties = {
|
|
|
|
projectId,
|
|
|
|
groupId,
|
|
|
|
};
|
|
|
|
|
|
|
|
mockAxios
|
|
|
|
.onGet(endpointUrl)
|
2023-03-17 16:20:25 +05:30
|
|
|
.reply(HTTP_STATUS_OK, mockNotificationSettingsResponses.default);
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
wrapper = createComponent({ injectedProperties });
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
wrapper.findComponent(GlModal).vm.$emit('show');
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(axios.get).toHaveBeenCalledWith(endpointUrl);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
it('updates the loading state and the events property', async () => {
|
|
|
|
const endpointUrl = '/api/v4/notification_settings';
|
|
|
|
|
|
|
|
mockAxios
|
|
|
|
.onGet(endpointUrl)
|
2023-03-17 16:20:25 +05:30
|
|
|
.reply(HTTP_STATUS_OK, mockNotificationSettingsResponses.default);
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
wrapper = createComponent();
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
wrapper.findComponent(GlModal).vm.$emit('show');
|
2021-03-11 19:13:27 +05:30
|
|
|
expect(wrapper.vm.isLoading).toBe(true);
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(axios.get).toHaveBeenCalledWith(endpointUrl);
|
|
|
|
expect(wrapper.vm.isLoading).toBe(false);
|
|
|
|
expect(wrapper.vm.events).toEqual([
|
|
|
|
{ id: 'new_release', enabled: true, name: 'New release', loading: false },
|
|
|
|
{ id: 'new_note', enabled: false, name: 'New note', loading: false },
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('shows a toast message when the request fails', async () => {
|
2023-03-17 16:20:25 +05:30
|
|
|
mockAxios.onGet('/api/v4/notification_settings').reply(HTTP_STATUS_NOT_FOUND, {});
|
2021-03-11 19:13:27 +05:30
|
|
|
wrapper = createComponent();
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
wrapper.findComponent(GlModal).vm.$emit('show');
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
expect(mockToastShow).toHaveBeenCalledWith(
|
2021-04-17 20:07:23 +05:30
|
|
|
'An error occurred while loading the notification settings. Please try again.',
|
2021-03-11 19:13:27 +05:30
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('update notification settings', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(axios, 'put');
|
|
|
|
});
|
|
|
|
|
|
|
|
it.each`
|
|
|
|
projectId | groupId | endpointUrl | notificationType | condition
|
|
|
|
${1} | ${null} | ${'/api/v4/projects/1/notification_settings'} | ${'project'} | ${'a projectId is given'}
|
|
|
|
${null} | ${1} | ${'/api/v4/groups/1/notification_settings'} | ${'group'} | ${'a groupId is given'}
|
|
|
|
${null} | ${null} | ${'/api/v4/notification_settings'} | ${'global'} | ${'neither projectId nor groupId are given'}
|
|
|
|
`(
|
2022-10-11 01:57:18 +05:30
|
|
|
'updates the $notificationType notification settings when $condition and the user clicks the checkbox',
|
2021-03-11 19:13:27 +05:30
|
|
|
async ({ projectId, groupId, endpointUrl }) => {
|
|
|
|
mockAxios
|
|
|
|
.onGet(endpointUrl)
|
2023-03-17 16:20:25 +05:30
|
|
|
.reply(HTTP_STATUS_OK, mockNotificationSettingsResponses.default);
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
mockAxios
|
|
|
|
.onPut(endpointUrl)
|
2023-03-17 16:20:25 +05:30
|
|
|
.reply(HTTP_STATUS_OK, mockNotificationSettingsResponses.updated);
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
const injectedProperties = {
|
|
|
|
projectId,
|
|
|
|
groupId,
|
|
|
|
};
|
|
|
|
|
|
|
|
wrapper = createComponent({ injectedProperties });
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
wrapper.findComponent(GlModal).vm.$emit('show');
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
await waitForPromises();
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
findCheckboxAt(1).vm.$emit('change', true);
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
|
|
|
expect(axios.put).toHaveBeenCalledWith(endpointUrl, {
|
|
|
|
new_note: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(wrapper.vm.events).toEqual([
|
|
|
|
{ id: 'new_release', enabled: true, name: 'New release', loading: false },
|
|
|
|
{ id: 'new_note', enabled: true, name: 'New note', loading: false },
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
it('shows a toast message when the request fails', async () => {
|
2023-05-27 22:25:52 +05:30
|
|
|
const endpointUrl = '/api/v4/notification_settings';
|
|
|
|
|
|
|
|
mockAxios
|
|
|
|
.onGet(endpointUrl)
|
|
|
|
.reply(HTTP_STATUS_OK, mockNotificationSettingsResponses.default);
|
|
|
|
|
|
|
|
mockAxios.onPut(endpointUrl).reply(HTTP_STATUS_NOT_FOUND, {});
|
2021-03-11 19:13:27 +05:30
|
|
|
wrapper = createComponent();
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
wrapper.findComponent(GlModal).vm.$emit('show');
|
2021-03-11 19:13:27 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
await waitForPromises();
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
findCheckboxAt(1).vm.$emit('change', true);
|
|
|
|
|
|
|
|
await waitForPromises();
|
|
|
|
|
2021-09-30 23:02:18 +05:30
|
|
|
expect(mockToastShow).toHaveBeenCalledWith(
|
2021-04-17 20:07:23 +05:30
|
|
|
'An error occurred while updating the notification settings. Please try again.',
|
2021-03-11 19:13:27 +05:30
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|