|
| 1 | +import { afterEach, describe, expect, it } from 'vitest' |
| 2 | +import { mountSuspended, mockNuxtImport } from '@nuxt/test-utils/runtime' |
| 3 | +import type { PackageComparisonData } from '~/composables/usePackageComparison' |
| 4 | + |
| 5 | +const mockPackagesData = ref<(PackageComparisonData | null)[]>([]) |
| 6 | +const mockStatus = ref<'idle' | 'pending' | 'success' | 'error'>('idle') |
| 7 | + |
| 8 | +mockNuxtImport('usePackageComparison', () => { |
| 9 | + return () => ({ |
| 10 | + packagesData: mockPackagesData, |
| 11 | + status: mockStatus, |
| 12 | + getFacetValues: () => [], |
| 13 | + isFacetLoading: () => false, |
| 14 | + isColumnLoading: () => false, |
| 15 | + }) |
| 16 | +}) |
| 17 | + |
| 18 | +mockNuxtImport('useCompareReplacements', () => { |
| 19 | + return () => ({ |
| 20 | + noDepSuggestions: computed(() => []), |
| 21 | + infoSuggestions: computed(() => []), |
| 22 | + replacements: computed(() => new Map()), |
| 23 | + }) |
| 24 | +}) |
| 25 | + |
| 26 | +function makePackageData(name: string): PackageComparisonData { |
| 27 | + return { |
| 28 | + package: { name, version: '1.0.0' } as ComparisonPackage, |
| 29 | + directDeps: 0, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +async function mountComparePage(route: string) { |
| 34 | + const ComparePage = await import('~/pages/compare.vue') |
| 35 | + return mountSuspended(ComparePage.default, { route }) |
| 36 | +} |
| 37 | + |
| 38 | +describe('compare page command palette commands', () => { |
| 39 | + afterEach(() => { |
| 40 | + mockPackagesData.value = [] |
| 41 | + mockStatus.value = 'idle' |
| 42 | + const commandPalette = useCommandPalette() |
| 43 | + commandPalette.close() |
| 44 | + commandPalette.contextCommands.value = [] |
| 45 | + }) |
| 46 | + |
| 47 | + it('does not register copy-markdown command with only 1 package', async () => { |
| 48 | + mockPackagesData.value = [makePackageData('react')] |
| 49 | + mockStatus.value = 'success' |
| 50 | + |
| 51 | + const wrapper = await mountComparePage('/compare?packages=react') |
| 52 | + const { contextCommands } = useCommandPalette() |
| 53 | + |
| 54 | + const allCommands = contextCommands.value.flatMap(entry => entry.commands) |
| 55 | + expect(allCommands.find(c => c.id === 'compare-copy-markdown')).toBeUndefined() |
| 56 | + expect(allCommands.find(c => c.id === 'compare-select-all')).toBeTruthy() |
| 57 | + |
| 58 | + wrapper.unmount() |
| 59 | + }) |
| 60 | + |
| 61 | + it('does not register copy-markdown command while data is loading', async () => { |
| 62 | + mockPackagesData.value = [null, null] |
| 63 | + mockStatus.value = 'pending' |
| 64 | + |
| 65 | + const wrapper = await mountComparePage('/compare?packages=react,vue') |
| 66 | + const { contextCommands } = useCommandPalette() |
| 67 | + |
| 68 | + const allCommands = contextCommands.value.flatMap(entry => entry.commands) |
| 69 | + expect(allCommands.find(c => c.id === 'compare-copy-markdown')).toBeUndefined() |
| 70 | + |
| 71 | + wrapper.unmount() |
| 72 | + }) |
| 73 | + |
| 74 | + it('registers copy-markdown command with 2+ packages and loaded data', async () => { |
| 75 | + mockPackagesData.value = [makePackageData('react'), makePackageData('vue')] |
| 76 | + mockStatus.value = 'success' |
| 77 | + |
| 78 | + const wrapper = await mountComparePage('/compare?packages=react,vue') |
| 79 | + const { contextCommands } = useCommandPalette() |
| 80 | + |
| 81 | + const allCommands = contextCommands.value.flatMap(entry => entry.commands) |
| 82 | + expect(allCommands.find(c => c.id === 'compare-copy-markdown')).toBeTruthy() |
| 83 | + |
| 84 | + wrapper.unmount() |
| 85 | + }) |
| 86 | +}) |
0 commit comments