UNPKG

3.27 kBJavaScriptView Raw
1import { shallowMount } from '@vue/test-utils';
2import { getBinding } from '~helpers/vue_mock_directive';
3import { POSITION } from './constants';
4import Truncate from './truncate.vue';
5
6const removeSpecialChar = (text) => {
7 return text.replace(/‎|\u200E/gi, '');
8};
9
10const positionOptions = Object.values(POSITION);
11
12describe('Truncate component', () => {
13 let wrapper;
14
15 const defaultProps = {
16 text: 'ee/app/assets/javascripts/vue_shared/src/utils_reports/components/utils/index.js',
17 };
18
19 const createComponent = (props) => {
20 wrapper = shallowMount(Truncate, {
21 propsData: { ...defaultProps, ...props },
22 });
23 };
24
25 describe('All', () => {
26 beforeEach(() => {
27 createComponent();
28 });
29
30 it.each(positionOptions)(
31 '%s truncation: should have title, class, original text',
32 (position) => {
33 createComponent({ position });
34 const element = wrapper.find('span');
35 expect(element.attributes('title')).toBe(defaultProps.text);
36 expect(element.attributes('class')).toBe('gl-truncate');
37 expect(removeSpecialChar(wrapper.text())).toBe(defaultProps.text);
38 }
39 );
40
41 it('should have the default position', () => {
42 expect(wrapper.props('position')).toBe('end');
43 });
44
45 it('disables the tooltip by default', () => {
46 expect(getBinding(wrapper.find('.gl-truncate').element, 'gl-tooltip').value.disabled).toBe(
47 true
48 );
49 });
50 });
51
52 describe('start truncation', () => {
53 beforeEach(() => {
54 createComponent({ position: 'start' });
55 });
56
57 it('should have the truncate-start class', () => {
58 expect(wrapper.find('.gl-truncate-start').exists()).toBe(true);
59 });
60
61 it('should have the special char surrounded', () => {
62 const spanTag = wrapper.findAll('.gl-truncate span').at(0).text();
63
64 expect(spanTag.charAt(0)).toBe('\u200E');
65 expect(spanTag.charAt(spanTag.length - 1)).toBe('\u200E');
66 });
67 });
68
69 describe('middle truncation', () => {
70 let firstSpan;
71 let secondSpan;
72
73 beforeEach(() => {
74 createComponent({ position: 'middle' });
75 firstSpan = wrapper.findAll('.gl-truncate span').at(0);
76 secondSpan = wrapper.findAll('.gl-truncate span').at(1);
77 });
78
79 it('should have appropriate classes applied', () => {
80 expect(firstSpan.classes('gl-truncate-end')).toBe(true);
81 expect(secondSpan.classes('gl-truncate-start')).toBe(true);
82 });
83
84 it('should have the spans positioned correctly', () => {
85 expect(firstSpan.text()).toBe('ee/app/assets/javascripts/vue_shared/src');
86 expect(secondSpan.text()).toBe('‎/utils_reports/components/utils/index.js‎');
87 });
88
89 it('last part should have the special char surrounded', () => {
90 const lastPart = secondSpan.text();
91
92 expect(lastPart.charAt(0)).toBe('\u200E');
93 expect(lastPart.charAt(lastPart.length - 1)).toBe('\u200E');
94 });
95 });
96
97 describe('end truncation', () => {
98 beforeEach(() => {
99 createComponent();
100 });
101
102 it('should not have the special char', () => {
103 expect(wrapper.text()).not.toContain('\u200E');
104 });
105
106 it('should have the truncate-end class', () => {
107 expect(wrapper.find('.gl-truncate-end').exists()).toBe(true);
108 });
109 });
110});