2020-05-24 23:13:21 +05:30
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
|
|
|
import * as urlUtility from '~/lib/utils/url_utility';
|
2022-11-25 23:54:43 +05:30
|
|
|
import SidebarService from '~/sidebar/services/sidebar_service';
|
2020-05-24 23:13:21 +05:30
|
|
|
import SidebarMediator from '~/sidebar/sidebar_mediator';
|
|
|
|
import SidebarStore from '~/sidebar/stores/sidebar_store';
|
|
|
|
import Mock from './mock_data';
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
jest.mock('~/flash');
|
2021-12-11 22:18:48 +05:30
|
|
|
jest.mock('~/vue_shared/plugins/global_toast');
|
2022-05-07 20:08:51 +05:30
|
|
|
jest.mock('~/commons/nav/user_merge_requests');
|
2021-12-11 22:18:48 +05:30
|
|
|
|
2020-05-24 23:13:21 +05:30
|
|
|
describe('Sidebar mediator', () => {
|
|
|
|
const { mediator: mediatorMockData } = Mock;
|
|
|
|
let mock;
|
|
|
|
let mediator;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
mediator = new SidebarMediator(mediatorMockData);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
SidebarService.singleton = null;
|
|
|
|
SidebarStore.singleton = null;
|
|
|
|
SidebarMediator.singleton = null;
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
jest.clearAllMocks();
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
it('assigns yourself', () => {
|
2020-05-24 23:13:21 +05:30
|
|
|
mediator.assignYourself();
|
|
|
|
|
|
|
|
expect(mediator.store.currentUser).toEqual(mediatorMockData.currentUser);
|
|
|
|
expect(mediator.store.assignees[0]).toEqual(mediatorMockData.currentUser);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('saves assignees', () => {
|
|
|
|
mock.onPut(mediatorMockData.endpoint).reply(200, {});
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
return mediator.saveAssignees('issue[assignee_ids]').then((resp) => {
|
2020-05-24 23:13:21 +05:30
|
|
|
expect(resp.status).toEqual(200);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
it('assigns yourself as a reviewer', () => {
|
|
|
|
mediator.addSelfReview();
|
|
|
|
|
|
|
|
expect(mediator.store.currentUser).toEqual(mediatorMockData.currentUser);
|
|
|
|
expect(mediator.store.reviewers[0]).toEqual(mediatorMockData.currentUser);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('saves reviewers', () => {
|
|
|
|
const mockUpdateResponseData = {
|
|
|
|
reviewers: [1, 2],
|
|
|
|
assignees: [3, 4],
|
|
|
|
};
|
|
|
|
const field = 'merge_request[reviewers_ids]';
|
|
|
|
const reviewers = [
|
|
|
|
{ id: 1, suggested: true },
|
|
|
|
{ id: 2, suggested: false },
|
|
|
|
];
|
|
|
|
|
|
|
|
let serviceSpy;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mediator.store.reviewers = reviewers;
|
|
|
|
serviceSpy = jest
|
|
|
|
.spyOn(mediator.service, 'update')
|
|
|
|
.mockReturnValue(Promise.resolve({ data: mockUpdateResponseData }));
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sends correct data to service', () => {
|
|
|
|
const data = {
|
|
|
|
reviewer_ids: [1, 2],
|
|
|
|
suggested_reviewer_ids: [1],
|
|
|
|
};
|
|
|
|
|
|
|
|
mediator.saveReviewers(field);
|
|
|
|
|
|
|
|
expect(serviceSpy).toHaveBeenCalledWith(field, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('saves reviewers', () => {
|
|
|
|
return mediator.saveReviewers(field).then(() => {
|
|
|
|
expect(mediator.store.assignees).toEqual(mockUpdateResponseData.assignees);
|
|
|
|
expect(mediator.store.reviewers).toEqual(mockUpdateResponseData.reviewers);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
it('fetches the data', async () => {
|
2020-05-24 23:13:21 +05:30
|
|
|
const mockData = Mock.responseMap.GET[mediatorMockData.endpoint];
|
|
|
|
mock.onGet(mediatorMockData.endpoint).reply(200, mockData);
|
|
|
|
const spy = jest.spyOn(mediator, 'processFetchedData').mockReturnValue(Promise.resolve());
|
2022-11-25 23:54:43 +05:30
|
|
|
await mediator.fetch();
|
2020-05-24 23:13:21 +05:30
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(spy).toHaveBeenCalledWith(mockData);
|
2020-05-24 23:13:21 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
it('processes fetched data', () => {
|
|
|
|
const mockData = Mock.responseMap.GET[mediatorMockData.endpoint];
|
|
|
|
mediator.processFetchedData(mockData);
|
|
|
|
|
|
|
|
expect(mediator.store.assignees).toEqual(mockData.assignees);
|
|
|
|
expect(mediator.store.humanTimeEstimate).toEqual(mockData.human_time_estimate);
|
|
|
|
expect(mediator.store.humanTotalTimeSpent).toEqual(mockData.human_total_time_spent);
|
|
|
|
expect(mediator.store.timeEstimate).toEqual(mockData.time_estimate);
|
|
|
|
expect(mediator.store.totalTimeSpent).toEqual(mockData.total_time_spent);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets moveToProjectId', () => {
|
|
|
|
const projectId = 7;
|
|
|
|
const spy = jest.spyOn(mediator.store, 'setMoveToProjectId').mockReturnValue(Promise.resolve());
|
|
|
|
|
|
|
|
mediator.setMoveToProjectId(projectId);
|
|
|
|
|
|
|
|
expect(spy).toHaveBeenCalledWith(projectId);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetches autocomplete projects', () => {
|
|
|
|
const searchTerm = 'foo';
|
|
|
|
mock.onGet(mediatorMockData.projectsAutocompleteEndpoint).reply(200, {});
|
|
|
|
const getterSpy = jest
|
|
|
|
.spyOn(mediator.service, 'getProjectsAutocomplete')
|
|
|
|
.mockReturnValue(Promise.resolve({ data: {} }));
|
|
|
|
const setterSpy = jest
|
|
|
|
.spyOn(mediator.store, 'setAutocompleteProjects')
|
|
|
|
.mockReturnValue(Promise.resolve());
|
|
|
|
|
|
|
|
return mediator.fetchAutocompleteProjects(searchTerm).then(() => {
|
|
|
|
expect(getterSpy).toHaveBeenCalledWith(searchTerm);
|
|
|
|
expect(setterSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('moves issue', () => {
|
|
|
|
const mockData = Mock.responseMap.POST[mediatorMockData.moveIssueEndpoint];
|
|
|
|
const moveToProjectId = 7;
|
|
|
|
mock.onPost(mediatorMockData.moveIssueEndpoint).reply(200, mockData);
|
|
|
|
mediator.store.setMoveToProjectId(moveToProjectId);
|
|
|
|
const moveIssueSpy = jest
|
|
|
|
.spyOn(mediator.service, 'moveIssue')
|
|
|
|
.mockReturnValue(Promise.resolve({ data: { web_url: mockData.web_url } }));
|
|
|
|
const urlSpy = jest.spyOn(urlUtility, 'visitUrl').mockReturnValue({});
|
|
|
|
|
|
|
|
return mediator.moveIssue().then(() => {
|
|
|
|
expect(moveIssueSpy).toHaveBeenCalledWith(moveToProjectId);
|
|
|
|
expect(urlSpy).toHaveBeenCalledWith(mockData.web_url);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|