Commit 38e06e46 authored by Miguel Rincon's avatar Miguel Rincon

Prevent overflowing of runner tags

This change prevents the overflowing of long runner tags and displays a
tooltip where tags overflow the available space.

Changelog: fixed
parent f02c40eb
<script> <script>
import { GlBadge } from '@gitlab/ui'; import { GlBadge, GlTooltipDirective, GlResizeObserverDirective } from '@gitlab/ui';
import { RUNNER_TAG_BADGE_VARIANT } from '../constants'; import { RUNNER_TAG_BADGE_VARIANT } from '../constants';
export default { export default {
components: { components: {
GlBadge, GlBadge,
}, },
directives: {
GlTooltip: GlTooltipDirective,
GlResizeObserver: GlResizeObserverDirective,
},
props: { props: {
tag: { tag: {
type: String, type: String,
...@@ -14,14 +18,39 @@ export default { ...@@ -14,14 +18,39 @@ export default {
size: { size: {
type: String, type: String,
required: false, required: false,
default: 'md', default: 'sm',
},
},
data() {
return {
overflowing: false,
};
},
computed: {
tooltip() {
if (this.overflowing) {
return this.tag;
}
return '';
},
},
methods: {
onResize() {
const { scrollWidth, offsetWidth } = this.$el;
this.overflowing = scrollWidth > offsetWidth;
}, },
}, },
RUNNER_TAG_BADGE_VARIANT, RUNNER_TAG_BADGE_VARIANT,
}; };
</script> </script>
<template> <template>
<gl-badge :size="size" :variant="$options.RUNNER_TAG_BADGE_VARIANT"> <gl-badge
v-gl-tooltip="tooltip"
v-gl-resize-observer="onResize"
class="gl-display-inline-block gl-max-w-full gl-text-truncate"
:size="size"
:variant="$options.RUNNER_TAG_BADGE_VARIANT"
>
{{ tag }} {{ tag }}
</gl-badge> </gl-badge>
</template> </template>
...@@ -14,13 +14,19 @@ export default { ...@@ -14,13 +14,19 @@ export default {
size: { size: {
type: String, type: String,
required: false, required: false,
default: 'md', default: 'sm',
}, },
}, },
}; };
</script> </script>
<template> <template>
<div> <div>
<runner-tag v-for="tag in tagList" :key="tag" :tag="tag" :size="size" /> <runner-tag
v-for="tag in tagList"
:key="tag"
class="gl-display-inline gl-mr-1"
:tag="tag"
:size="size"
/>
</div> </div>
</template> </template>
...@@ -27,7 +27,7 @@ export const I18N_NOT_CONNECTED_RUNNER_DESCRIPTION = s__( ...@@ -27,7 +27,7 @@ export const I18N_NOT_CONNECTED_RUNNER_DESCRIPTION = s__(
export const I18N_LOCKED_RUNNER_DESCRIPTION = s__('Runners|You cannot assign to other projects'); export const I18N_LOCKED_RUNNER_DESCRIPTION = s__('Runners|You cannot assign to other projects');
export const I18N_PAUSED_RUNNER_DESCRIPTION = s__('Runners|Not available to run jobs'); export const I18N_PAUSED_RUNNER_DESCRIPTION = s__('Runners|Not available to run jobs');
export const RUNNER_TAG_BADGE_VARIANT = 'info'; export const RUNNER_TAG_BADGE_VARIANT = 'neutral';
export const RUNNER_TAG_BG_CLASS = 'gl-bg-blue-100'; export const RUNNER_TAG_BG_CLASS = 'gl-bg-blue-100';
// Filtered search parameter names // Filtered search parameter names
......
import { GlBadge } from '@gitlab/ui'; import { GlBadge } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
import RunnerTag from '~/runner/components/runner_tag.vue'; import RunnerTag from '~/runner/components/runner_tag.vue';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
const mockTag = 'tag1';
describe('RunnerTag', () => { describe('RunnerTag', () => {
let wrapper; let wrapper;
const findBadge = () => wrapper.findComponent(GlBadge); const findBadge = () => wrapper.findComponent(GlBadge);
const getTooltipValue = () => getBinding(findBadge().element, 'gl-tooltip').value;
const setDimensions = ({ scrollWidth, offsetWidth }) => {
jest.spyOn(findBadge().element, 'scrollWidth', 'get').mockReturnValue(scrollWidth);
jest.spyOn(findBadge().element, 'offsetWidth', 'get').mockReturnValue(offsetWidth);
// Mock trigger resize
getBinding(findBadge().element, 'gl-resize-observer').value();
};
const createComponent = ({ props = {} } = {}) => { const createComponent = ({ props = {} } = {}) => {
wrapper = shallowMount(RunnerTag, { wrapper = shallowMount(RunnerTag, {
propsData: { propsData: {
tag: 'tag1', tag: mockTag,
...props, ...props,
}, },
directives: {
GlTooltip: createMockDirective(),
GlResizeObserver: createMockDirective(),
},
}); });
}; };
...@@ -25,21 +42,36 @@ describe('RunnerTag', () => { ...@@ -25,21 +42,36 @@ describe('RunnerTag', () => {
}); });
it('Displays tag text', () => { it('Displays tag text', () => {
expect(wrapper.text()).toBe('tag1'); expect(wrapper.text()).toBe(mockTag);
}); });
it('Displays tags with correct style', () => { it('Displays tags with correct style', () => {
expect(findBadge().props()).toMatchObject({ expect(findBadge().props()).toMatchObject({
size: 'md', size: 'sm',
variant: 'info', variant: 'neutral',
}); });
}); });
it('Displays tags with small size', () => { it('Displays tags with md size', () => {
createComponent({ createComponent({
props: { size: 'sm' }, props: { size: 'md' },
}); });
expect(findBadge().props('size')).toBe('sm'); expect(findBadge().props('size')).toBe('md');
}); });
it.each`
case | scrollWidth | offsetWidth | expectedTooltip
${'overflowing'} | ${110} | ${100} | ${mockTag}
${'not overflowing'} | ${90} | ${100} | ${''}
${'almost overflowing'} | ${100} | ${100} | ${''}
`(
'Sets "$expectedTooltip" as tooltip when $case',
async ({ scrollWidth, offsetWidth, expectedTooltip }) => {
setDimensions({ scrollWidth, offsetWidth });
await nextTick();
expect(getTooltipValue()).toBe(expectedTooltip);
},
);
}); });
...@@ -33,16 +33,16 @@ describe('RunnerTags', () => { ...@@ -33,16 +33,16 @@ describe('RunnerTags', () => {
}); });
it('Displays tags with correct style', () => { it('Displays tags with correct style', () => {
expect(findBadge().props('size')).toBe('md'); expect(findBadge().props('size')).toBe('sm');
expect(findBadge().props('variant')).toBe('info'); expect(findBadge().props('variant')).toBe('neutral');
}); });
it('Displays tags with small size', () => { it('Displays tags with md size', () => {
createComponent({ createComponent({
props: { size: 'sm' }, props: { size: 'md' },
}); });
expect(findBadge().props('size')).toBe('sm'); expect(findBadge().props('size')).toBe('md');
}); });
it('Is empty when there are no tags', () => { it('Is empty when there are no tags', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment