2020-05-07 08:09:46 -04:00
|
|
|
import { GlIcon } from '@gitlab/ui';
|
2021-02-14 13:09:20 -05:00
|
|
|
import { shallowMount } from '@vue/test-utils';
|
2022-01-25 10:12:32 -05:00
|
|
|
import { nextTick } from 'vue';
|
2020-05-07 08:09:46 -04:00
|
|
|
import DesignImage from '~/design_management/components/image.vue';
|
|
|
|
|
|
|
|
describe('Design management large image component', () => {
|
|
|
|
let wrapper;
|
|
|
|
|
|
|
|
function createComponent(propsData, data = {}) {
|
|
|
|
wrapper = shallowMount(DesignImage, {
|
|
|
|
propsData,
|
|
|
|
});
|
2021-12-29 10:10:45 -05:00
|
|
|
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
2020-05-07 08:09:46 -04:00
|
|
|
wrapper.setData(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
it('sets correct classes and styles if imageStyle is set', async () => {
|
2020-05-07 08:09:46 -04:00
|
|
|
createComponent(
|
|
|
|
{
|
|
|
|
isLoading: false,
|
|
|
|
image: 'test.jpg',
|
|
|
|
name: 'test',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
imageStyle: {
|
|
|
|
width: '100px',
|
|
|
|
height: '100px',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
|
|
|
expect(wrapper.element).toMatchSnapshot();
|
2020-05-07 08:09:46 -04:00
|
|
|
});
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
it('renders media broken icon on error', async () => {
|
2020-05-07 08:09:46 -04:00
|
|
|
createComponent({
|
|
|
|
isLoading: false,
|
|
|
|
image: 'test.jpg',
|
|
|
|
name: 'test',
|
|
|
|
});
|
|
|
|
|
|
|
|
const image = wrapper.find('img');
|
|
|
|
image.trigger('error');
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
|
|
|
expect(image.isVisible()).toBe(false);
|
|
|
|
expect(wrapper.find(GlIcon).element).toMatchSnapshot();
|
2020-05-07 08:09:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
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-01-25 10:12:32 -05:00
|
|
|
it('emits @resize event on zoom', async () => {
|
2020-05-07 08:09:46 -04:00
|
|
|
const zoomAmount = 2;
|
|
|
|
wrapper.vm.zoom(zoomAmount);
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
|
|
|
expect(wrapper.emitted('resize')).toEqual([
|
|
|
|
[{ width: baseImageWidth * zoomAmount, height: baseImageHeight * zoomAmount }],
|
|
|
|
]);
|
2020-05-07 08:09:46 -04:00
|
|
|
});
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
it('emits @resize event with base image size when scale=1', async () => {
|
2020-05-07 08:09:46 -04:00
|
|
|
wrapper.vm.zoom(1);
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
|
|
|
expect(wrapper.emitted('resize')).toEqual([
|
|
|
|
[{ width: baseImageWidth, height: baseImageHeight }],
|
|
|
|
]);
|
2020-05-07 08:09:46 -04:00
|
|
|
});
|
|
|
|
|
2022-01-25 10:12:32 -05:00
|
|
|
it('sets image style when zoomed', async () => {
|
2020-05-07 08:09:46 -04:00
|
|
|
const zoomAmount = 2;
|
|
|
|
wrapper.vm.zoom(zoomAmount);
|
|
|
|
expect(wrapper.vm.imageStyle).toEqual({
|
|
|
|
width: `${baseImageWidth * zoomAmount}px`,
|
|
|
|
height: `${baseImageHeight * zoomAmount}px`,
|
|
|
|
});
|
2022-01-25 10:12:32 -05:00
|
|
|
await nextTick();
|
|
|
|
expect(wrapper.element).toMatchSnapshot();
|
2020-05-07 08:09:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|