|
| 1 | +import { mount } from '@vue/test-utils' |
| 2 | +import { describe, expect, it } from 'vitest' |
| 3 | +import { OkuAspectRatio } from './aspect-ratio' |
| 4 | + |
| 5 | +describe('OkuAspectRatio', () => { |
| 6 | + it('renders the component correctly', () => { |
| 7 | + const wrapper = mount(OkuAspectRatio) |
| 8 | + |
| 9 | + expect(wrapper.exists()).toBe(true) |
| 10 | + expect(wrapper.find('[data-radix-aspect-ratio-wrapper]').exists()).toBe( |
| 11 | + true, |
| 12 | + ) |
| 13 | + }) |
| 14 | + |
| 15 | + it('calculates the aspect ratio correctly', () => { |
| 16 | + const ratio = 16 / 9 |
| 17 | + const wrapper = mount(OkuAspectRatio, { |
| 18 | + props: { |
| 19 | + ratio, |
| 20 | + }, |
| 21 | + }) |
| 22 | + |
| 23 | + const wrapperElement = wrapper.find('[data-radix-aspect-ratio-wrapper]') |
| 24 | + |
| 25 | + expect(wrapperElement.attributes('style')).toContain( |
| 26 | + `padding-bottom: ${100 / ratio}%`, |
| 27 | + ) |
| 28 | + }) |
| 29 | + |
| 30 | + it('updates aspect ratio when the prop changes', async () => { |
| 31 | + const wrapper = mount(OkuAspectRatio, { |
| 32 | + props: { |
| 33 | + ratio: 4 / 3, |
| 34 | + }, |
| 35 | + }) |
| 36 | + |
| 37 | + await wrapper.setProps({ ratio: 3 / 2 }) |
| 38 | + |
| 39 | + const wrapperElement = wrapper.find('[data-radix-aspect-ratio-wrapper]') |
| 40 | + const computedStyle = wrapperElement.attributes('style') |
| 41 | + |
| 42 | + const actualRatio = parseFloat( |
| 43 | + computedStyle?.match(/padding-bottom: (.*?);/)[1], |
| 44 | + ) |
| 45 | + |
| 46 | + const expectedRatio = 66.6667 |
| 47 | + |
| 48 | + expect(actualRatio).toBeCloseTo(expectedRatio, 4) |
| 49 | + }) |
| 50 | +}) |
0 commit comments