|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { mountSuspended } from '@nuxt/test-utils/runtime' |
| 3 | +import FacetBarChart from '~/components/Compare/FacetBarChart.vue' |
| 4 | + |
| 5 | +// Stub the heavy chart library |
| 6 | +vi.mock('vue-data-ui/vue-ui-horizontal-bar', () => ({ |
| 7 | + VueUiHorizontalBar: { template: '<div class="chart-stub"><slot name="fallback" /></div>' }, |
| 8 | +})) |
| 9 | +vi.mock('vue-data-ui/style.css', () => ({})) |
| 10 | + |
| 11 | +describe('FacetBarChart skeleton loaders', () => { |
| 12 | + const baseProps = { |
| 13 | + values: [null, null, null], |
| 14 | + packages: ['react', 'vue', 'svelte'], |
| 15 | + label: 'Weekly Downloads', |
| 16 | + description: 'Downloads per week', |
| 17 | + facetLoading: true, |
| 18 | + } |
| 19 | + |
| 20 | + it('renders skeleton loaders when facetLoading is true', async () => { |
| 21 | + const wrapper = await mountSuspended(FacetBarChart, { |
| 22 | + props: baseProps, |
| 23 | + }) |
| 24 | + |
| 25 | + const html = wrapper.html() |
| 26 | + // Should show skeleton elements |
| 27 | + expect(html).toContain('h-7') |
| 28 | + }) |
| 29 | + |
| 30 | + it('skeleton bar label widths vary across packages (not all the same)', () => { |
| 31 | + // The formula used is: width = `${40 + (i * 17) % 40}%` |
| 32 | + // For 3 packages: i=0 -> 40%, i=1 -> 57%, i=2 -> 74% |
| 33 | + const packages = ['react', 'vue', 'svelte'] |
| 34 | + const widths = packages.map((_, i) => `${40 + (i * 17) % 40}%`) |
| 35 | + |
| 36 | + // Verify they are not all the same (the bug was all bars had the same width) |
| 37 | + const uniqueWidths = new Set(widths) |
| 38 | + expect(uniqueWidths.size).toBeGreaterThan(1) |
| 39 | + }) |
| 40 | + |
| 41 | + it('skeleton width formula produces values within 40-80% range', () => { |
| 42 | + const indices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 43 | + for (const i of indices) { |
| 44 | + const width = 40 + (i * 17) % 40 |
| 45 | + expect(width).toBeGreaterThanOrEqual(40) |
| 46 | + expect(width).toBeLessThan(80) |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + it('renders one row per package in the skeleton', async () => { |
| 51 | + const wrapper = await mountSuspended(FacetBarChart, { |
| 52 | + props: { |
| 53 | + ...baseProps, |
| 54 | + packages: ['pkg-a', 'pkg-b', 'pkg-c'], |
| 55 | + facetLoading: true, |
| 56 | + }, |
| 57 | + }) |
| 58 | + |
| 59 | + // The skeleton renders one flex row per package |
| 60 | + const html = wrapper.html() |
| 61 | + // flex items-center gap-3 = the skeleton row class |
| 62 | + const rowMatches = html.match(/flex items-center gap-3/g) |
| 63 | + expect(rowMatches).not.toBeNull() |
| 64 | + }) |
| 65 | +}) |
0 commit comments