1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
import { shallowMount } from '@vue/test-utils';
import Vue, { nextTick } from 'vue';
import axios from 'axios';
import AxiosMockAdapter from 'axios-mock-adapter';
import VueApollo from 'vue-apollo';
import createMockApollo from 'helpers/mock_apollo_helper';
import SidebarReviewers from '~/sidebar/components/reviewers/sidebar_reviewers.vue';
import SidebarService from '~/sidebar/services/sidebar_service';
import SidebarMediator from '~/sidebar/sidebar_mediator';
import SidebarStore from '~/sidebar/stores/sidebar_store';
import { fetchUserCounts } from '~/super_sidebar/user_counts_fetch';
import Mock from '../../mock_data';
jest.mock('~/super_sidebar/user_counts_fetch');
Vue.use(VueApollo);
describe('sidebar reviewers', () => {
const apolloMock = createMockApollo();
let wrapper;
let mediator;
let axiosMock;
const createComponent = (props) => {
wrapper = shallowMount(SidebarReviewers, {
apolloProvider: apolloMock,
propsData: {
issuableIid: '1',
issuableId: 1,
mediator,
field: '',
projectPath: 'projectPath',
changing: false,
...props,
},
// Attaching to document is required because this component emits something from the parent element :/
attachTo: document.body,
});
};
beforeEach(() => {
axiosMock = new AxiosMockAdapter(axios);
mediator = new SidebarMediator(Mock.mediator);
jest.spyOn(mediator, 'saveReviewers').mockResolvedValue({});
jest.spyOn(mediator, 'addSelfReview');
});
afterEach(() => {
SidebarService.singleton = null;
SidebarStore.singleton = null;
SidebarMediator.singleton = null;
axiosMock.restore();
});
it('calls the mediator when it saves the reviewers', () => {
createComponent();
expect(mediator.saveReviewers).not.toHaveBeenCalled();
wrapper.vm.saveReviewers();
expect(mediator.saveReviewers).toHaveBeenCalled();
});
it('re-fetches user counts after saving reviewers', async () => {
createComponent();
expect(fetchUserCounts).not.toHaveBeenCalled();
wrapper.vm.saveReviewers();
await nextTick();
expect(fetchUserCounts).toHaveBeenCalled();
});
it('calls the mediator when "reviewBySelf" method is called', () => {
createComponent();
expect(mediator.addSelfReview).not.toHaveBeenCalled();
expect(mediator.store.reviewers.length).toBe(0);
wrapper.vm.reviewBySelf();
expect(mediator.addSelfReview).toHaveBeenCalled();
expect(mediator.store.reviewers.length).toBe(1);
});
});
|