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
|
import { GlLoadingIcon } from '@gitlab/ui';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises';
import DeleteWithContributions from '~/admin/users/components/actions/delete_with_contributions.vue';
import eventHub, {
EVENT_OPEN_DELETE_USER_MODAL,
} from '~/admin/users/components/modals/delete_user_modal_event_hub';
import { associationsCount } from '~/api/user_api';
import {
paths,
associationsCount as associationsCountData,
userDeletionObstacles,
} from '../../mock_data';
jest.mock('~/admin/users/components/modals/delete_user_modal_event_hub', () => ({
...jest.requireActual('~/admin/users/components/modals/delete_user_modal_event_hub'),
__esModule: true,
default: {
$emit: jest.fn(),
},
}));
jest.mock('~/api/user_api', () => ({
associationsCount: jest.fn(),
}));
describe('DeleteWithContributions', () => {
let wrapper;
const defaultPropsData = {
username: 'John Doe',
userId: 1,
paths,
userDeletionObstacles,
};
const createComponent = () => {
wrapper = mountExtended(DeleteWithContributions, { propsData: defaultPropsData });
};
describe('when action is clicked', () => {
describe('when API request is loading', () => {
beforeEach(() => {
associationsCount.mockReturnValueOnce(new Promise(() => {}));
createComponent();
});
it('displays loading icon and disables button', async () => {
await wrapper.trigger('click');
expect(wrapper.findComponent(GlLoadingIcon).exists()).toBe(true);
expect(wrapper.findByRole('menuitem').attributes()).toMatchObject({
disabled: 'disabled',
'aria-busy': 'true',
});
});
});
describe('when API request is successful', () => {
beforeEach(() => {
associationsCount.mockResolvedValueOnce({
data: associationsCountData,
});
createComponent();
});
it('emits event with association counts', async () => {
await wrapper.trigger('click');
await waitForPromises();
expect(associationsCount).toHaveBeenCalledWith(defaultPropsData.userId);
expect(eventHub.$emit).toHaveBeenCalledWith(
EVENT_OPEN_DELETE_USER_MODAL,
expect.objectContaining({
associationsCount: associationsCountData,
username: defaultPropsData.username,
blockPath: paths.block,
deletePath: paths.deleteWithContributions,
userDeletionObstacles,
}),
);
});
});
describe('when API request is not successful', () => {
beforeEach(() => {
associationsCount.mockRejectedValueOnce();
createComponent();
});
it('emits event with error', async () => {
await wrapper.trigger('click');
await waitForPromises();
expect(eventHub.$emit).toHaveBeenCalledWith(
EVENT_OPEN_DELETE_USER_MODAL,
expect.objectContaining({
associationsCount: new Error(),
}),
);
});
});
});
});
|