Commit f15bdde3 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch '343262-overflow-tags-tooltip' into 'master'

Prevent overflowing of runner tags

See merge request gitlab-org/gitlab!74071
parents 57b6e1f6 38e06e46
<script>
import { GlBadge } from '@gitlab/ui';
import { GlBadge, GlTooltipDirective, GlResizeObserverDirective } from '@gitlab/ui';
import { RUNNER_TAG_BADGE_VARIANT } from '../constants';
export default {
components: {
GlBadge,
},
directives: {
GlTooltip: GlTooltipDirective,
GlResizeObserver: GlResizeObserverDirective,
},
props: {
tag: {
type: String,
......@@ -14,14 +18,39 @@ export default {
size: {
type: String,
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,
};
</script>
<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 }}
</gl-badge>
</template>
......@@ -14,13 +14,19 @@ export default {
size: {
type: String,
required: false,
default: 'md',
default: 'sm',
},
},
};
</script>
<template>
<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>
</template>
......@@ -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_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';
// Filtered search parameter names
......
import { GlBadge } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils';
import { nextTick } from 'vue';
import RunnerTag from '~/runner/components/runner_tag.vue';
import { createMockDirective, getBinding } from 'helpers/vue_mock_directive';
const mockTag = 'tag1';
describe('RunnerTag', () => {
let wrapper;
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 = {} } = {}) => {
wrapper = shallowMount(RunnerTag, {
propsData: {
tag: 'tag1',
tag: mockTag,
...props,
},
directives: {
GlTooltip: createMockDirective(),
GlResizeObserver: createMockDirective(),
},
});
};
......@@ -25,21 +42,36 @@ describe('RunnerTag', () => {
});
it('Displays tag text', () => {
expect(wrapper.text()).toBe('tag1');
expect(wrapper.text()).toBe(mockTag);
});
it('Displays tags with correct style', () => {
expect(findBadge().props()).toMatchObject({
size: 'md',
variant: 'info',
size: 'sm',
variant: 'neutral',
});
});
it('Displays tags with small size', () => {
it('Displays tags with md size', () => {
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', () => {
});
it('Displays tags with correct style', () => {
expect(findBadge().props('size')).toBe('md');
expect(findBadge().props('variant')).toBe('info');
expect(findBadge().props('size')).toBe('sm');
expect(findBadge().props('variant')).toBe('neutral');
});
it('Displays tags with small size', () => {
it('Displays tags with md size', () => {
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', () => {
......
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