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
|
import { GlFilteredSearchToken, GlFilteredSearchTokenSegment } from '@gitlab/ui';
import { mount } from '@vue/test-utils';
import { nextTick } from 'vue';
import waitForPromises from 'helpers/wait_for_promises';
import { createAlert } from '~/alert';
import ReleaseToken from '~/vue_shared/components/filtered_search_bar/tokens/release_token.vue';
import { mockReleaseToken } from '../mock_data';
jest.mock('~/alert');
describe('ReleaseToken', () => {
const id = '123';
let wrapper;
const createComponent = ({ config = mockReleaseToken, value = { data: '' } } = {}) =>
mount(ReleaseToken, {
propsData: {
active: false,
config,
value,
cursorPosition: 'start',
},
provide: {
portalName: 'fake target',
alignSuggestions: function fakeAlignSuggestions() {},
suggestionsListClass: () => 'custom-class',
},
});
it('renders release value', async () => {
wrapper = createComponent({ value: { data: id } });
await nextTick();
const tokenSegments = wrapper.findAllComponents(GlFilteredSearchTokenSegment);
expect(tokenSegments).toHaveLength(3); // `Release` `=` `v1`
expect(tokenSegments.at(2).text()).toBe(id.toString());
});
it('fetches initial values', () => {
const fetchReleasesSpy = jest.fn().mockResolvedValue();
wrapper = createComponent({
config: { ...mockReleaseToken, fetchReleases: fetchReleasesSpy },
value: { data: id },
});
expect(fetchReleasesSpy).toHaveBeenCalledWith(id);
});
it('fetches releases on user input', () => {
const search = 'hello';
const fetchReleasesSpy = jest.fn().mockResolvedValue();
wrapper = createComponent({
config: { ...mockReleaseToken, fetchReleases: fetchReleasesSpy },
});
wrapper.findComponent(GlFilteredSearchToken).vm.$emit('input', { data: search });
expect(fetchReleasesSpy).toHaveBeenCalledWith(search);
});
it('renders error message when request fails', async () => {
const fetchReleasesSpy = jest.fn().mockRejectedValue();
wrapper = createComponent({
config: { ...mockReleaseToken, fetchReleases: fetchReleasesSpy },
});
await waitForPromises();
expect(createAlert).toHaveBeenCalledWith({
message: 'There was a problem fetching releases.',
});
});
});
|