2017-10-17 09:26:09 -04:00
|
|
|
import Vue from 'vue';
|
2019-11-11 07:06:23 -05:00
|
|
|
import { mount } from '@vue/test-utils';
|
2020-05-22 14:08:21 -04:00
|
|
|
import mountComponent from 'helpers/vue_mount_component_helper';
|
2019-12-06 04:06:39 -05:00
|
|
|
import Icon from '~/vue_shared/components/icon.vue';
|
2020-05-22 14:08:21 -04:00
|
|
|
import iconsPath from '@gitlab/svgs/dist/icons.svg';
|
2017-10-17 09:26:09 -04:00
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
jest.mock('@gitlab/svgs/dist/icons.svg', () => 'testing');
|
|
|
|
|
|
|
|
describe('Sprite Icon Component', () => {
|
|
|
|
describe('Initialization', () => {
|
2017-10-18 03:19:29 -04:00
|
|
|
let icon;
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
beforeEach(() => {
|
2017-10-18 03:19:29 -04:00
|
|
|
const IconComponent = Vue.extend(Icon);
|
|
|
|
|
|
|
|
icon = mountComponent(IconComponent, {
|
2018-07-18 12:56:19 -04:00
|
|
|
name: 'commit',
|
2017-11-27 14:34:03 -05:00
|
|
|
size: 32,
|
2017-10-18 03:19:29 -04:00
|
|
|
});
|
|
|
|
});
|
2017-10-17 09:26:09 -04:00
|
|
|
|
2017-10-18 03:19:29 -04:00
|
|
|
afterEach(() => {
|
|
|
|
icon.$destroy();
|
2017-10-17 09:26:09 -04:00
|
|
|
});
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
it('should return a defined Vue component', () => {
|
2017-10-18 03:19:29 -04:00
|
|
|
expect(icon).toBeDefined();
|
2017-10-17 09:26:09 -04:00
|
|
|
});
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
it('should have <svg> as a child element', () => {
|
2017-10-18 03:19:29 -04:00
|
|
|
expect(icon.$el.tagName).toBe('svg');
|
2017-10-17 09:26:09 -04:00
|
|
|
});
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
it('should have <use> as a child element with the correct href', () => {
|
2017-10-18 03:19:29 -04:00
|
|
|
expect(icon.$el.firstChild.tagName).toBe('use');
|
2020-05-22 14:08:21 -04:00
|
|
|
expect(icon.$el.firstChild.getAttribute('xlink:href')).toBe(`${iconsPath}#commit`);
|
2017-10-17 09:26:09 -04:00
|
|
|
});
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
it('should properly compute iconSizeClass', () => {
|
2017-11-27 14:34:03 -05:00
|
|
|
expect(icon.iconSizeClass).toBe('s32');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('forbids invalid size prop', () => {
|
|
|
|
expect(icon.$options.props.size.validator(NaN)).toBeFalsy();
|
|
|
|
expect(icon.$options.props.size.validator(0)).toBeFalsy();
|
|
|
|
expect(icon.$options.props.size.validator(9001)).toBeFalsy();
|
2017-10-17 09:26:09 -04:00
|
|
|
});
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
it('should properly render img css', () => {
|
2018-06-16 17:50:13 -04:00
|
|
|
const { classList } = icon.$el;
|
2017-11-27 14:34:03 -05:00
|
|
|
const containsSizeClass = classList.contains('s32');
|
2018-10-09 14:03:09 -04:00
|
|
|
|
2017-10-17 09:26:09 -04:00
|
|
|
expect(containsSizeClass).toBe(true);
|
|
|
|
});
|
2018-07-18 12:56:19 -04:00
|
|
|
|
|
|
|
it('`name` validator should return false for non existing icons', () => {
|
2020-05-22 14:08:21 -04:00
|
|
|
jest.spyOn(console, 'warn').mockImplementation();
|
|
|
|
|
2018-07-18 12:56:19 -04:00
|
|
|
expect(Icon.props.name.validator('non_existing_icon_sprite')).toBe(false);
|
|
|
|
});
|
|
|
|
|
2020-05-22 14:08:21 -04:00
|
|
|
it('`name` validator should return true for existing icons', () => {
|
2018-07-18 12:56:19 -04:00
|
|
|
expect(Icon.props.name.validator('commit')).toBe(true);
|
|
|
|
});
|
2017-10-17 09:26:09 -04:00
|
|
|
});
|
2019-11-11 07:06:23 -05:00
|
|
|
|
|
|
|
it('should call registered listeners when they are triggered', () => {
|
2020-05-22 14:08:21 -04:00
|
|
|
const clickHandler = jest.fn();
|
2019-11-11 07:06:23 -05:00
|
|
|
const wrapper = mount(Icon, {
|
|
|
|
propsData: { name: 'commit' },
|
|
|
|
listeners: { click: clickHandler },
|
|
|
|
});
|
|
|
|
|
|
|
|
wrapper.find('svg').trigger('click');
|
|
|
|
|
|
|
|
expect(clickHandler).toHaveBeenCalled();
|
|
|
|
});
|
2017-10-17 09:26:09 -04:00
|
|
|
});
|