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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
import { GlButton, GlEmptyState, GlTable } from '@gitlab/ui';
import { mount, shallowMount } from '@vue/test-utils';
import waitForPromises from 'helpers/wait_for_promises';
import * as JiraConnectApi from '~/jira_connect/api';
import SubscriptionsList from '~/jira_connect/components/subscriptions_list.vue';
import createStore from '~/jira_connect/store';
import { SET_ALERT } from '~/jira_connect/store/mutation_types';
import { reloadPage } from '~/jira_connect/utils';
import { mockSubscription } from '../mock_data';
jest.mock('~/jira_connect/utils');
describe('SubscriptionsList', () => {
let wrapper;
let store;
const createComponent = ({ mountFn = shallowMount, provide = {} } = {}) => {
store = createStore();
wrapper = mountFn(SubscriptionsList, {
provide,
store,
});
};
afterEach(() => {
wrapper.destroy();
});
const findGlEmptyState = () => wrapper.findComponent(GlEmptyState);
const findGlTable = () => wrapper.findComponent(GlTable);
const findUnlinkButton = () => findGlTable().findComponent(GlButton);
const clickUnlinkButton = () => findUnlinkButton().trigger('click');
describe('template', () => {
it('renders GlEmptyState when subscriptions is empty', () => {
createComponent();
expect(findGlEmptyState().exists()).toBe(true);
expect(findGlTable().exists()).toBe(false);
});
it('renders GlTable when subscriptions are present', () => {
createComponent({
provide: {
subscriptions: [mockSubscription],
},
});
expect(findGlEmptyState().exists()).toBe(false);
expect(findGlTable().exists()).toBe(true);
});
});
describe('on "Unlink" button click', () => {
let removeSubscriptionSpy;
beforeEach(() => {
createComponent({
mountFn: mount,
provide: {
subscriptions: [mockSubscription],
},
});
removeSubscriptionSpy = jest.spyOn(JiraConnectApi, 'removeSubscription').mockResolvedValue();
});
it('sets button to loading and sends request', async () => {
expect(findUnlinkButton().props('loading')).toBe(false);
clickUnlinkButton();
await wrapper.vm.$nextTick();
expect(findUnlinkButton().props('loading')).toBe(true);
await waitForPromises();
expect(removeSubscriptionSpy).toHaveBeenCalledWith(mockSubscription.unlink_path);
});
describe('when request is successful', () => {
it('reloads the page', async () => {
clickUnlinkButton();
await waitForPromises();
expect(reloadPage).toHaveBeenCalled();
});
});
describe('when request has errors', () => {
const mockErrorMessage = 'error message';
const mockError = { response: { data: { error: mockErrorMessage } } };
beforeEach(() => {
jest.spyOn(JiraConnectApi, 'removeSubscription').mockRejectedValue(mockError);
jest.spyOn(store, 'commit');
});
it('sets alert', async () => {
clickUnlinkButton();
await waitForPromises();
expect(reloadPage).not.toHaveBeenCalled();
expect(store.commit.mock.calls).toEqual(
expect.arrayContaining([
[
SET_ALERT,
{
message: mockErrorMessage,
variant: 'danger',
},
],
]),
);
});
});
});
});
|