debian-mirror-gitlab/spec/javascripts/sidebar/sidebar_mediator_spec.js

129 lines
4.4 KiB
JavaScript
Raw Normal View History

2019-12-04 20:38:33 +05:30
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
2017-08-17 22:00:37 +05:30
import SidebarMediator from '~/sidebar/sidebar_mediator';
import SidebarStore from '~/sidebar/stores/sidebar_store';
import SidebarService from '~/sidebar/services/sidebar_service';
import Mock from './mock_data';
2019-12-04 20:38:33 +05:30
const { mediator: mediatorMockData } = Mock;
2018-10-15 14:42:47 +05:30
describe('Sidebar mediator', function() {
2019-12-04 20:38:33 +05:30
let mock;
2017-08-17 22:00:37 +05:30
beforeEach(() => {
2019-12-04 20:38:33 +05:30
mock = new MockAdapter(axios);
this.mediator = new SidebarMediator(mediatorMockData);
2017-08-17 22:00:37 +05:30
});
afterEach(() => {
SidebarService.singleton = null;
SidebarStore.singleton = null;
SidebarMediator.singleton = null;
2019-12-04 20:38:33 +05:30
mock.restore();
2017-08-17 22:00:37 +05:30
});
it('assigns yourself ', () => {
this.mediator.assignYourself();
2019-12-04 20:38:33 +05:30
expect(this.mediator.store.currentUser).toEqual(mediatorMockData.currentUser);
expect(this.mediator.store.assignees[0]).toEqual(mediatorMockData.currentUser);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
it('saves assignees', done => {
2019-12-04 20:38:33 +05:30
mock.onPut(mediatorMockData.endpoint).reply(200, {});
2018-12-13 13:39:08 +05:30
this.mediator
.saveAssignees('issue[assignee_ids]')
.then(resp => {
2017-08-17 22:00:37 +05:30
expect(resp.status).toEqual(200);
done();
})
2018-03-17 18:26:18 +05:30
.catch(done.fail);
2017-08-17 22:00:37 +05:30
});
2018-12-13 13:39:08 +05:30
it('fetches the data', done => {
2019-12-04 20:38:33 +05:30
const mockData = Mock.responseMap.GET[mediatorMockData.endpoint];
mock.onGet(mediatorMockData.endpoint).reply(200, mockData);
2018-03-17 18:26:18 +05:30
spyOn(this.mediator, 'processFetchedData').and.callThrough();
2018-12-13 13:39:08 +05:30
this.mediator
.fetch()
2018-03-17 18:26:18 +05:30
.then(() => {
expect(this.mediator.processFetchedData).toHaveBeenCalledWith(mockData);
})
.then(done)
.catch(done.fail);
});
it('processes fetched data', () => {
2019-12-04 20:38:33 +05:30
const mockData = Mock.responseMap.GET[mediatorMockData.endpoint];
2018-03-17 18:26:18 +05:30
this.mediator.processFetchedData(mockData);
expect(this.mediator.store.assignees).toEqual(mockData.assignees);
expect(this.mediator.store.humanTimeEstimate).toEqual(mockData.human_time_estimate);
expect(this.mediator.store.humanTotalTimeSpent).toEqual(mockData.human_total_time_spent);
expect(this.mediator.store.participants).toEqual(mockData.participants);
expect(this.mediator.store.subscribed).toEqual(mockData.subscribed);
expect(this.mediator.store.timeEstimate).toEqual(mockData.time_estimate);
expect(this.mediator.store.totalTimeSpent).toEqual(mockData.total_time_spent);
});
it('sets moveToProjectId', () => {
const projectId = 7;
spyOn(this.mediator.store, 'setMoveToProjectId').and.callThrough();
this.mediator.setMoveToProjectId(projectId);
expect(this.mediator.store.setMoveToProjectId).toHaveBeenCalledWith(projectId);
});
2018-12-13 13:39:08 +05:30
it('fetches autocomplete projects', done => {
2018-03-17 18:26:18 +05:30
const searchTerm = 'foo';
2019-12-04 20:38:33 +05:30
mock.onGet(mediatorMockData.projectsAutocompleteEndpoint).reply(200, {});
2018-03-17 18:26:18 +05:30
spyOn(this.mediator.service, 'getProjectsAutocomplete').and.callThrough();
spyOn(this.mediator.store, 'setAutocompleteProjects').and.callThrough();
2018-12-13 13:39:08 +05:30
this.mediator
.fetchAutocompleteProjects(searchTerm)
2018-03-17 18:26:18 +05:30
.then(() => {
expect(this.mediator.service.getProjectsAutocomplete).toHaveBeenCalledWith(searchTerm);
expect(this.mediator.store.setAutocompleteProjects).toHaveBeenCalled();
})
.then(done)
.catch(done.fail);
});
2018-12-13 13:39:08 +05:30
it('moves issue', done => {
2019-12-04 20:38:33 +05:30
const mockData = Mock.responseMap.POST[mediatorMockData.moveIssueEndpoint];
2018-03-17 18:26:18 +05:30
const moveToProjectId = 7;
2019-12-04 20:38:33 +05:30
mock.onPost(mediatorMockData.moveIssueEndpoint).reply(200, mockData);
2018-03-17 18:26:18 +05:30
this.mediator.store.setMoveToProjectId(moveToProjectId);
spyOn(this.mediator.service, 'moveIssue').and.callThrough();
2018-10-15 14:42:47 +05:30
const visitUrl = spyOnDependency(SidebarMediator, 'visitUrl');
2018-03-17 18:26:18 +05:30
2018-12-13 13:39:08 +05:30
this.mediator
.moveIssue()
2018-03-17 18:26:18 +05:30
.then(() => {
expect(this.mediator.service.moveIssue).toHaveBeenCalledWith(moveToProjectId);
2019-12-04 20:38:33 +05:30
expect(visitUrl).toHaveBeenCalledWith(mockData.web_url);
2018-03-17 18:26:18 +05:30
})
.then(done)
.catch(done.fail);
});
2018-12-13 13:39:08 +05:30
it('toggle subscription', done => {
2018-03-17 18:26:18 +05:30
this.mediator.store.setSubscribedState(false);
2019-12-04 20:38:33 +05:30
mock.onPost(mediatorMockData.toggleSubscriptionEndpoint).reply(200, {});
2018-03-17 18:26:18 +05:30
spyOn(this.mediator.service, 'toggleSubscription').and.callThrough();
2018-12-13 13:39:08 +05:30
this.mediator
.toggleSubscription()
2018-03-17 18:26:18 +05:30
.then(() => {
expect(this.mediator.service.toggleSubscription).toHaveBeenCalled();
expect(this.mediator.store.subscribed).toEqual(true);
})
.then(done)
.catch(done.fail);
2017-08-17 22:00:37 +05:30
});
});