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
123
124
125
126
127
128
129
130
131
132
133
|
import { GlToggle, GlBadge } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import waitForPromises from 'helpers/wait_for_promises';
import { createAlert } from '~/alert';
import toast from '~/vue_shared/plugins/global_toast';
import { updateApplicationSettings } from '~/rest_api';
import SilentModeSettingsApp from '~/silent_mode_settings/components/app.vue';
jest.mock('~/rest_api.js');
jest.mock('~/alert');
jest.mock('~/vue_shared/plugins/global_toast');
const MOCK_DEFAULT_SILENT_MODE_ENABLED = false;
describe('SilentModeSettingsApp', () => {
let wrapper;
const createComponent = (props = {}) => {
const defaultProps = {
isSilentModeEnabled: MOCK_DEFAULT_SILENT_MODE_ENABLED,
};
wrapper = shallowMount(SilentModeSettingsApp, {
propsData: {
...defaultProps,
...props,
},
});
};
const findGlToggle = () => wrapper.findComponent(GlToggle);
const findGlBadge = () => wrapper.findComponent(GlBadge);
describe('template', () => {
describe('experiment badge', () => {
beforeEach(() => {
createComponent();
});
it('renders properly', () => {
expect(findGlBadge().exists()).toBe(true);
});
});
describe('when silent mode is already enabled', () => {
beforeEach(() => {
createComponent({ isSilentModeEnabled: true });
});
it('renders the component with the GlToggle set to true', () => {
expect(findGlToggle().attributes('value')).toBe('true');
});
});
describe('when silent mode is no already enabled', () => {
beforeEach(() => {
createComponent({ isSilentModeEnabled: false });
});
it('renders the component with the GlToggle set to undefined', () => {
expect(findGlToggle().attributes('value')).toBeUndefined();
});
});
});
describe.each`
enabled | message
${false} | ${'Silent mode disabled'}
${true} | ${'Silent mode enabled'}
`(`toast message`, ({ enabled, message }) => {
beforeEach(() => {
updateApplicationSettings.mockImplementation(() => Promise.resolve());
createComponent();
});
it(`when successfully toggled to ${enabled}, toast message is ${message}`, async () => {
await findGlToggle().vm.$emit('change', enabled);
await waitForPromises();
expect(toast).toHaveBeenCalledWith(message);
});
});
describe.each`
description | mockApi | toastMsg | error
${'onSuccess'} | ${() => Promise.resolve()} | ${'Silent mode enabled'} | ${false}
${'onError'} | ${() => Promise.reject()} | ${false} | ${'There was an error updating the Silent Mode Settings.'}
`(`when submitting the form $description`, ({ mockApi, toastMsg, error }) => {
beforeEach(() => {
updateApplicationSettings.mockImplementation(mockApi);
createComponent();
});
it('calls updateApplicationSettings correctly', () => {
findGlToggle().vm.$emit('change', !MOCK_DEFAULT_SILENT_MODE_ENABLED);
expect(updateApplicationSettings).toHaveBeenCalledWith({
silent_mode_enabled: !MOCK_DEFAULT_SILENT_MODE_ENABLED,
});
});
it('handles the loading icon correctly', async () => {
expect(findGlToggle().props('isLoading')).toBe(false);
await findGlToggle().vm.$emit('change', !MOCK_DEFAULT_SILENT_MODE_ENABLED);
expect(findGlToggle().props('isLoading')).toBe(true);
await waitForPromises();
expect(findGlToggle().props('isLoading')).toBe(false);
});
it(`does ${toastMsg ? '' : 'not '}render an success toast message`, async () => {
await findGlToggle().vm.$emit('change', !MOCK_DEFAULT_SILENT_MODE_ENABLED);
await waitForPromises();
return toastMsg
? expect(toast).toHaveBeenCalledWith(toastMsg)
: expect(toast).not.toHaveBeenCalled();
});
it(`does ${error ? '' : 'not '}render an error message`, async () => {
await findGlToggle().vm.$emit('change', !MOCK_DEFAULT_SILENT_MODE_ENABLED);
await waitForPromises();
return error
? expect(createAlert).toHaveBeenCalledWith({ message: error })
: expect(createAlert).not.toHaveBeenCalled();
});
});
});
|