| |
| |
| |
| |
| |
| |
|
|
| import { mount } from '@vue/test-utils'; |
| import EvolutionBadge from './EvolutionBadge.vue'; |
|
|
| function mountBadge(props: Record<string, unknown>) { |
| return mount(EvolutionBadge, { props }); |
| } |
|
|
| describe('CoreVisualizations/EvolutionBadge.vue', () => { |
| it('renders an increase as a positive (green) badge pointing up', () => { |
| const wrapper = mountBadge({ percent: 4 }); |
|
|
| expect(wrapper.vm.direction).toBe('up'); |
| expect(wrapper.classes()).toContain('evolutionBadge'); |
| expect(wrapper.classes()).toContain('evolutionBadge--positive'); |
| expect(wrapper.find('.evolutionBadge__icon path').exists()).toBe(true); |
| expect(wrapper.find('.evolutionBadge__value').text()).toBe('+4%'); |
| }); |
|
|
| it('renders a decrease as a negative (red) badge pointing down', () => { |
| const wrapper = mountBadge({ percent: -4 }); |
|
|
| expect(wrapper.vm.direction).toBe('down'); |
| expect(wrapper.classes()).toContain('evolutionBadge--negative'); |
| expect(wrapper.find('.evolutionBadge__icon path').exists()).toBe(true); |
| expect(wrapper.find('.evolutionBadge__value').text()).toBe('-4%'); |
| }); |
|
|
| it('renders no change as a neutral (grey) badge with a dash', () => { |
| const wrapper = mountBadge({ percent: 0 }); |
|
|
| expect(wrapper.vm.direction).toBe('neutral'); |
| expect(wrapper.classes()).toContain('evolutionBadge--neutral'); |
| |
| expect(wrapper.find('.evolutionBadge__icon rect').exists()).toBe(true); |
| expect(wrapper.find('.evolutionBadge__icon path').exists()).toBe(false); |
| expect(wrapper.find('.evolutionBadge__value').text()).toBe('0%'); |
| }); |
|
|
| it('inverts the colour when isLowerValueBetter, keeping the arrow direction', () => { |
| const increased = mountBadge({ percent: 4, isLowerValueBetter: true }); |
| |
| expect(increased.vm.direction).toBe('up'); |
| expect(increased.classes()).toContain('evolutionBadge--negative'); |
|
|
| const decreased = mountBadge({ percent: -4, isLowerValueBetter: true }); |
| |
| expect(decreased.vm.direction).toBe('down'); |
| expect(decreased.classes()).toContain('evolutionBadge--positive'); |
| }); |
|
|
| it('uses the trend prop as the authoritative direction over the percent sign', () => { |
| |
| const wrapper = mountBadge({ percent: '4%', trend: -10 }); |
|
|
| expect(wrapper.vm.direction).toBe('down'); |
| expect(wrapper.classes()).toContain('evolutionBadge--negative'); |
| |
| expect(wrapper.find('.evolutionBadge__value').text()).toBe('4%'); |
| }); |
|
|
| it('accepts a pre-formatted percent string and prefixes a "+" for increases', () => { |
| const wrapper = mountBadge({ percent: '4%' }); |
|
|
| expect(wrapper.vm.direction).toBe('up'); |
| expect(wrapper.find('.evolutionBadge__value').text()).toBe('+4%'); |
| }); |
|
|
| it('keeps an existing sign on a pre-formatted negative percent string', () => { |
| const wrapper = mountBadge({ percent: '-4%' }); |
|
|
| expect(wrapper.vm.direction).toBe('down'); |
| expect(wrapper.find('.evolutionBadge__value').text()).toBe('-4%'); |
| }); |
|
|
| it('reads a localised minus (U+2212) as a decrease when no trend is given', () => { |
| |
| const wrapper = mountBadge({ percent: '\u22124%' }); |
|
|
| expect(wrapper.vm.direction).toBe('down'); |
| expect(wrapper.classes()).toContain('evolutionBadge--negative'); |
| |
| expect(wrapper.find('.evolutionBadge__value').text()).toBe('\u22124%'); |
| }); |
|
|
| it('exposes the tooltip as the title attribute and omits it when empty', () => { |
| const withTooltip = mountBadge({ percent: 4, tooltip: '10 visits vs 8 visits' }); |
| expect(withTooltip.attributes('title')).toBe('10 visits vs 8 visits'); |
|
|
| const withoutTooltip = mountBadge({ percent: 4 }); |
| expect(withoutTooltip.attributes('title')).toBeUndefined(); |
| }); |
|
|
| it('renders a long value on a single line without truncating it', () => { |
| const wrapper = mountBadge({ percent: '+1,234,567%', trend: 5 }); |
|
|
| expect(wrapper.vm.direction).toBe('up'); |
| expect(wrapper.find('.evolutionBadge__value').text()).toBe('+1,234,567%'); |
| }); |
| }); |
|
|