debian-mirror-gitlab/spec/frontend/badges/components/badge_spec.js

79 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-11-25 23:54:43 +05:30
import { nextTick } from 'vue';
import { mount } from '@vue/test-utils';
2018-05-09 12:01:36 +05:30
import { DUMMY_IMAGE_URL, TEST_HOST } from 'spec/test_constants';
2020-01-01 13:55:28 +05:30
import Badge from '~/badges/components/badge.vue';
2018-05-09 12:01:36 +05:30
describe('Badge component', () => {
const dummyProps = {
imageUrl: DUMMY_IMAGE_URL,
linkUrl: `${TEST_HOST}/badge/link/url`,
};
2022-11-25 23:54:43 +05:30
let wrapper;
2018-05-09 12:01:36 +05:30
const findElements = () => {
2022-11-25 23:54:43 +05:30
const buttons = wrapper.findAll('button');
2018-05-09 12:01:36 +05:30
return {
2022-11-25 23:54:43 +05:30
badgeImage: wrapper.find('img.project-badge'),
loadingIcon: wrapper.find('.gl-spinner'),
reloadButton: buttons.at(buttons.length - 1),
2018-05-09 12:01:36 +05:30
};
};
2022-11-25 23:54:43 +05:30
const createComponent = (propsData) => {
wrapper = mount(Badge, { propsData });
2018-05-09 12:01:36 +05:30
};
afterEach(() => {
2022-11-25 23:54:43 +05:30
wrapper.destroy();
2018-05-09 12:01:36 +05:30
});
2022-11-25 23:54:43 +05:30
beforeEach(() => {
return createComponent({ ...dummyProps }, '#dummy-element');
2018-05-09 12:01:36 +05:30
});
2022-11-25 23:54:43 +05:30
it('shows a badge image after loading', async () => {
const { badgeImage, loadingIcon, reloadButton } = findElements();
badgeImage.element.dispatchEvent(new Event('load'));
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
await nextTick();
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
expect(badgeImage.isVisible()).toBe(true);
expect(loadingIcon.isVisible()).toBe(false);
expect(reloadButton.isVisible()).toBe(false);
expect(wrapper.find('.btn-group').isVisible()).toBe(false);
});
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
it('shows a loading icon when loading', () => {
const { badgeImage, loadingIcon, reloadButton } = findElements();
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
expect(badgeImage.isVisible()).toBe(false);
expect(loadingIcon.isVisible()).toBe(true);
expect(reloadButton.isVisible()).toBe(false);
expect(wrapper.find('.btn-group').isVisible()).toBe(false);
});
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
it('shows an error and reload button if loading failed', async () => {
const { badgeImage, loadingIcon, reloadButton } = findElements();
badgeImage.element.dispatchEvent(new Event('error'));
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
await nextTick();
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
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');
});
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
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();
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
await reloadButton.trigger('click');
2018-05-09 12:01:36 +05:30
2022-11-25 23:54:43 +05:30
expect(badgeImage.attributes('src')).toBe(`${dummyProps.imageUrl}#retries=1`);
2018-05-09 12:01:36 +05:30
});
});