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
|
import { nextTick } from 'vue';
import { mount } from '@vue/test-utils';
import { DUMMY_IMAGE_URL, TEST_HOST } from 'spec/test_constants';
import Badge from '~/badges/components/badge.vue';
describe('Badge component', () => {
const dummyProps = {
imageUrl: DUMMY_IMAGE_URL,
linkUrl: `${TEST_HOST}/badge/link/url`,
};
let wrapper;
const findElements = () => {
const buttons = wrapper.findAll('button');
return {
badgeImage: wrapper.find('img.project-badge'),
loadingIcon: wrapper.find('.gl-spinner'),
reloadButton: buttons.at(buttons.length - 1),
};
};
const createComponent = (propsData) => {
wrapper = mount(Badge, { propsData });
};
beforeEach(() => {
return createComponent({ ...dummyProps }, '#dummy-element');
});
it('shows a badge image after loading', async () => {
const { badgeImage, loadingIcon, reloadButton } = findElements();
badgeImage.element.dispatchEvent(new Event('load'));
await nextTick();
expect(badgeImage.isVisible()).toBe(true);
expect(loadingIcon.isVisible()).toBe(false);
expect(reloadButton.isVisible()).toBe(false);
expect(wrapper.find('.btn-group').isVisible()).toBe(false);
});
it('shows a loading icon when loading', () => {
const { badgeImage, loadingIcon, reloadButton } = findElements();
expect(badgeImage.isVisible()).toBe(false);
expect(loadingIcon.isVisible()).toBe(true);
expect(reloadButton.isVisible()).toBe(false);
expect(wrapper.find('.btn-group').isVisible()).toBe(false);
});
it('shows an error and reload button if loading failed', async () => {
const { badgeImage, loadingIcon, reloadButton } = findElements();
badgeImage.element.dispatchEvent(new Event('error'));
await nextTick();
expect(badgeImage.isVisible()).toBe(false);
expect(loadingIcon.isVisible()).toBe(false);
expect(reloadButton.isVisible()).toBe(true);
expect(reloadButton.element).toHaveSpriteIcon('retry');
expect(wrapper.text()).toBe('No badge image');
});
it('retries an image when loading failed and reload button is clicked', async () => {
const { badgeImage, reloadButton } = findElements();
badgeImage.element.dispatchEvent(new Event('error'));
await nextTick();
await reloadButton.trigger('click');
expect(badgeImage.attributes('src')).toBe(`${dummyProps.imageUrl}#retries=1`);
});
});
|