|
1 | 1 | import { afterEach, describe, expect, it } from 'vitest' |
2 | 2 | import { mountSuspended, mockNuxtImport } from '@nuxt/test-utils/runtime' |
3 | 3 | import type { PackageComparisonData } from '~/composables/usePackageComparison' |
| 4 | +import CopyToClipboardButtonComponent from '~/components/CopyToClipboardButton.vue' |
4 | 5 |
|
5 | 6 | const mockPackagesData = ref<(PackageComparisonData | null)[]>([]) |
6 | 7 | const mockStatus = ref<'idle' | 'pending' | 'success' | 'error'>('idle') |
@@ -84,3 +85,52 @@ describe('compare page command palette commands', () => { |
84 | 85 | wrapper.unmount() |
85 | 86 | }) |
86 | 87 | }) |
| 88 | + |
| 89 | +describe('compare page copy-as-markdown button', () => { |
| 90 | + afterEach(() => { |
| 91 | + mockPackagesData.value = [] |
| 92 | + mockStatus.value = 'idle' |
| 93 | + const commandPalette = useCommandPalette() |
| 94 | + commandPalette.close() |
| 95 | + commandPalette.contextCommands.value = [] |
| 96 | + }) |
| 97 | + |
| 98 | + it('shows copy-as-markdown button when all packages have loaded data', async () => { |
| 99 | + mockPackagesData.value = [makePackageData('react'), makePackageData('vue')] |
| 100 | + mockStatus.value = 'success' |
| 101 | + |
| 102 | + const wrapper = await mountComparePage('/compare?packages=react,vue') |
| 103 | + |
| 104 | + expect(wrapper.findComponent(CopyToClipboardButtonComponent).exists()).toBe(true) |
| 105 | + |
| 106 | + wrapper.unmount() |
| 107 | + }) |
| 108 | + |
| 109 | + it('does not show copy-as-markdown button when only some packages have loaded data', async () => { |
| 110 | + // Simulate a partial-load race: 2 packages in the URL but only the first has data yet. |
| 111 | + mockPackagesData.value = [makePackageData('react'), null] |
| 112 | + mockStatus.value = 'pending' |
| 113 | + |
| 114 | + const wrapper = await mountComparePage('/compare?packages=react,vue') |
| 115 | + |
| 116 | + // The button must not appear until every requested package has loaded its data. |
| 117 | + expect(wrapper.findComponent(CopyToClipboardButtonComponent).exists()).toBe(false) |
| 118 | + |
| 119 | + wrapper.unmount() |
| 120 | + }) |
| 121 | + |
| 122 | + it('does not register copy-markdown command when only some packages have loaded data', async () => { |
| 123 | + // Same race: 2 packages in the URL but only the first has data yet. |
| 124 | + mockPackagesData.value = [makePackageData('react'), null] |
| 125 | + mockStatus.value = 'pending' |
| 126 | + |
| 127 | + const wrapper = await mountComparePage('/compare?packages=react,vue') |
| 128 | + const { contextCommands } = useCommandPalette() |
| 129 | + |
| 130 | + const allCommands = contextCommands.value.flatMap(entry => entry.commands) |
| 131 | + // The command-palette entry must not appear until every requested package has loaded. |
| 132 | + expect(allCommands.find(c => c.id === 'compare-copy-markdown')).toBeUndefined() |
| 133 | + |
| 134 | + wrapper.unmount() |
| 135 | + }) |
| 136 | +}) |
0 commit comments