debian-mirror-gitlab/spec/frontend/design_management/components/image_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

147 lines
3.6 KiB
JavaScript
Raw Normal View History

2020-05-24 23:13:21 +05:30
import { GlIcon } from '@gitlab/ui';
2021-03-11 19:13:27 +05:30
import { shallowMount } from '@vue/test-utils';
2022-04-04 11:22:00 +05:30
import { nextTick } from 'vue';
2022-08-13 15:12:31 +05:30
import { stubPerformanceWebAPI } from 'helpers/performance';
2020-05-24 23:13:21 +05:30
import DesignImage from '~/design_management/components/image.vue';
describe('Design management large image component', () => {
let wrapper;
function createComponent(propsData, data = {}) {
wrapper = shallowMount(DesignImage, {
propsData,
});
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2020-05-24 23:13:21 +05:30
wrapper.setData(data);
}
2022-08-13 15:12:31 +05:30
beforeEach(() => {
stubPerformanceWebAPI();
});
2020-05-24 23:13:21 +05:30
afterEach(() => {
wrapper.destroy();
});
it('renders loading state', () => {
createComponent({
isLoading: true,
});
expect(wrapper.element).toMatchSnapshot();
});
it('renders image', () => {
createComponent({
isLoading: false,
image: 'test.jpg',
name: 'test',
});
expect(wrapper.element).toMatchSnapshot();
});
2023-04-23 21:23:45 +05:30
it('renders SVG with proper height and width', () => {
createComponent({
isLoading: false,
image: 'mockImage.svg',
name: 'test',
});
expect(wrapper.element).toMatchSnapshot();
});
2022-04-04 11:22:00 +05:30
it('sets correct classes and styles if imageStyle is set', async () => {
2020-05-24 23:13:21 +05:30
createComponent(
{
isLoading: false,
image: 'test.jpg',
name: 'test',
},
{
imageStyle: {
width: '100px',
height: '100px',
},
},
);
2022-04-04 11:22:00 +05:30
await nextTick();
expect(wrapper.element).toMatchSnapshot();
2020-05-24 23:13:21 +05:30
});
2022-04-04 11:22:00 +05:30
it('renders media broken icon on error', async () => {
2020-05-24 23:13:21 +05:30
createComponent({
isLoading: false,
image: 'test.jpg',
name: 'test',
});
const image = wrapper.find('img');
image.trigger('error');
2022-04-04 11:22:00 +05:30
await nextTick();
expect(image.isVisible()).toBe(false);
2022-08-27 11:52:29 +05:30
expect(wrapper.findComponent(GlIcon).element).toMatchSnapshot();
2020-05-24 23:13:21 +05:30
});
describe('zoom', () => {
const baseImageWidth = 100;
const baseImageHeight = 100;
beforeEach(() => {
createComponent(
{
isLoading: false,
image: 'test.jpg',
name: 'test',
},
{
imageStyle: {
width: `${baseImageWidth}px`,
height: `${baseImageHeight}px`,
},
baseImageSize: {
width: baseImageWidth,
height: baseImageHeight,
},
},
);
jest.spyOn(wrapper.vm.$refs.contentImg, 'offsetWidth', 'get').mockReturnValue(baseImageWidth);
jest
.spyOn(wrapper.vm.$refs.contentImg, 'offsetHeight', 'get')
.mockReturnValue(baseImageHeight);
});
2022-04-04 11:22:00 +05:30
it('emits @resize event on zoom', async () => {
2020-05-24 23:13:21 +05:30
const zoomAmount = 2;
wrapper.vm.zoom(zoomAmount);
2022-04-04 11:22:00 +05:30
await nextTick();
expect(wrapper.emitted('resize')).toEqual([
[{ width: baseImageWidth * zoomAmount, height: baseImageHeight * zoomAmount }],
]);
2020-05-24 23:13:21 +05:30
});
2022-04-04 11:22:00 +05:30
it('emits @resize event with base image size when scale=1', async () => {
2020-05-24 23:13:21 +05:30
wrapper.vm.zoom(1);
2022-04-04 11:22:00 +05:30
await nextTick();
expect(wrapper.emitted('resize')).toEqual([
[{ width: baseImageWidth, height: baseImageHeight }],
]);
2020-05-24 23:13:21 +05:30
});
2022-04-04 11:22:00 +05:30
it('sets image style when zoomed', async () => {
2020-05-24 23:13:21 +05:30
const zoomAmount = 2;
wrapper.vm.zoom(zoomAmount);
expect(wrapper.vm.imageStyle).toEqual({
width: `${baseImageWidth * zoomAmount}px`,
height: `${baseImageHeight * zoomAmount}px`,
});
2022-04-04 11:22:00 +05:30
await nextTick();
expect(wrapper.element).toMatchSnapshot();
2020-05-24 23:13:21 +05:30
});
});
});