Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/components/LcTooltip/LcTooltip.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ Small.args = {
...Base.args,
size: 'small',
}

export const Clickable = Template.bind({}) as any
Clickable.args = {
...White.args,
clickable: true,
}
15 changes: 10 additions & 5 deletions src/components/LcTooltip/LcTooltip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@
>
<slot name="element" />

<div v-if="show && $slots['text']" :class="tooltipClass">
<div
v-if="show && $slots['text']"
:class="tooltipClass"
data-testid="tooltip"
>
<slot name="text" />
<lc-icon
v-if="clickable"
class="lc_tooltip-icon"
data-testid="tooltip__close"
name="exit"
size="xs"
@click.stop="toggleTooltip"
Expand Down Expand Up @@ -49,7 +54,7 @@ export default defineComponent({
position: {
type: String,
default: 'top',
validator: (value) => {
validator: (value: string) => {
return vPosition.includes(value)
},
},
Expand All @@ -64,14 +69,14 @@ export default defineComponent({
size: {
type: String,
default: 'medium',
validator: (value) => {
validator: (value: string) => {
return vSize.includes(value)
},
},
variant: {
type: String,
default: 'grey',
validator: (value) => {
validator: (value: string) => {
return vVariant.includes(value)
},
},
Expand All @@ -82,7 +87,7 @@ export default defineComponent({
}
},
computed: {
tooltipClass() {
tooltipClass(): string | string[] {
const baseClass = 'lc_tooltip-content'

if (!this.hoveringTooltip) return `text-center ${baseClass}`
Expand Down
114 changes: 114 additions & 0 deletions src/components/LcTooltip/__tests__/LcTooltip.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { mount } from '@vue/test-utils'
import LcTooltip from '../LcTooltip'

let wrapper: any

afterEach(() => {
wrapper?.unmount()
})

describe('LcTooltip', () => {
it('is a Vue instance', () => {
wrapper = mount(LcTooltip)
expect(wrapper.vm).toBeTruthy()
})

describe('Props format', () => {
it('should have a clickable props (default: false)', () => {
const { clickable } = LcTooltip.props

expect(clickable.default).toBe(false)
expect(clickable.required).toBeFalsy()
expect(clickable.type).toBe(Boolean)
})

it('should have a position props (defaul: top)', () => {
const { position } = LcTooltip.props
const validPosition = ['top', 'left', 'right', 'bottom']

expect(position.default).toBe('top')
expect(position.type).toBe(String)
validPosition.forEach(pos => expect(position.validator(pos)).toBeTruthy())
expect(position.validator('upper')).toBeFalsy()
})
})

describe('Slots', () => {
it('should render an element which should have the tooltip', () => {
const buttonEl = '<button>Click to see tooltip!</button>'

wrapper = mount(LcTooltip, {
slots: { element: buttonEl },
})

expect(wrapper.html()).toContain(buttonEl)
expect(wrapper.html()).toMatchSnapshot()
})

it('should display some text in the tooltip', () => {
const buttonEl = '<button>Click to see tooltip!</button>'
const tooltipMessage = 'Hi! I\'m the tooltip message.'

wrapper = mount(LcTooltip, {
props: { show: true },
slots: {
element: buttonEl,
text: tooltipMessage,
},
})

expect(wrapper.html()).toContain(tooltipMessage)
expect(wrapper.html()).toMatchSnapshot()
})
})

describe('Position', () => {
it('should be at the top if position=top', () => {
wrapper = mount(LcTooltip, {
props: { show: true },
slots: { text: 'Hello' },
})

const tooltip = wrapper.get('[data-testid="tooltip"]')

expect(tooltip.classes('lc_tooltip-content--top')).toBeTruthy()
})

it('should be on the left if position=left', () => {
wrapper = mount(LcTooltip, {
props: { position: 'left', show: true },
slots: { text: 'Hello' },
})

const tooltip = wrapper.get('[data-testid="tooltip"]')

expect(tooltip.classes('lc_tooltip-content--left')).toBeTruthy()
})
})

describe('If tooltip is clickable', () => {
beforeEach(() => {
wrapper = mount(LcTooltip, {
props: { show: true, clickable: true },
slots: { text: 'Tooltip text' },
})
})

it('should appear on click', async() => {
expect(wrapper.classes('lc_tooltip--appear')).toBeFalsy()

await wrapper.trigger('click')
expect(wrapper.classes('lc_tooltip--appear')).toBeTruthy()
})

it('should hide when close button is clicked', async() => {
await wrapper.trigger('click')
expect(wrapper.classes('lc_tooltip--appear')).toBeTruthy()

const closeIcon = wrapper.get('[data-testid="tooltip__close"]')

await closeIcon.trigger('click')
expect(wrapper.classes('lc_tooltip--appear')).toBeFalsy()
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`LcTooltip Slots should display some text in the tooltip 1`] = `
"<div class=\\"relative lc_tooltip lc_tooltip--hover\\"><button>Click to see tooltip!</button>
<div class=\\"lc_tooltip-content lc_tooltip-content--top lc_tooltip-content--medium lc_tooltip-content--grey lc_tooltip-content--absolute\\" data-testid=\\"tooltip\\">Hi! I'm the tooltip message.
<!--v-if-->
</div>
</div>"
`;

exports[`LcTooltip Slots should render an element which should have the tooltip 1`] = `
"<div class=\\"relative lc_tooltip lc_tooltip--hover\\"><button>Click to see tooltip!</button>
<!--v-if-->
</div>"
`;