Skip to content

Commit 1e71404

Browse files
fix: "copy as markdown" button appearing too eagerly (#2506)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 10d1f9d commit 1e71404

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

app/pages/compare.vue

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,14 @@ function addNoDep() {
7575
// Get loading state for each column
7676
const columnLoading = computed(() => packages.value.map((_, i) => isColumnLoading(i)))
7777
78-
// FIXME(serhalp): canCompare only checks package count, not whether data has loaded.
79-
// Copy-markdown and view-switching commands appear as soon as one package loads, even if
80-
// other packages are still loading. The UI copy button has the same issue.
78+
// Makes sense to compare if there's 2 or more packages
8179
const canCompare = computed(() => packages.value.length >= 2)
8280
81+
// Allow copying only after all data is loaded
82+
const canCopyTable = computed(
83+
() => packagesData.value.length >= 1 && packagesData.value.every(data => data !== null),
84+
)
85+
8386
const comparisonView = usePermalink<'table' | 'charts'>('view', 'table')
8487
const hasChartableFacets = computed(() => selectedFacets.value.some(facet => facet.chartable))
8588
@@ -173,7 +176,7 @@ useCommandPaletteContextCommands(
173176
},
174177
]
175178
176-
if (canCompare.value && packagesData.value && packagesData.value.some(p => p !== null)) {
179+
if (canCompare.value && canCopyTable.value) {
177180
commands.push({
178181
id: 'compare-copy-markdown',
179182
group: 'actions',
@@ -335,7 +338,7 @@ useSeoMeta({
335338
<!-- Comparison grid -->
336339
<section v-if="canCompare" class="mt-10" aria-labelledby="comparison-heading">
337340
<CopyToClipboardButton
338-
v-if="packagesData && packagesData.some(p => p !== null)"
341+
v-if="canCopyTable"
339342
:copied="copied"
340343
:copy-text="$t('compare.packages.copy_as_markdown')"
341344
class="mb-4"

test/nuxt/pages/ComparePage.spec.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { afterEach, describe, expect, it } from 'vitest'
22
import { mountSuspended, mockNuxtImport } from '@nuxt/test-utils/runtime'
33
import type { PackageComparisonData } from '~/composables/usePackageComparison'
4+
import CopyToClipboardButtonComponent from '~/components/CopyToClipboardButton.vue'
45

56
const mockPackagesData = ref<(PackageComparisonData | null)[]>([])
67
const mockStatus = ref<'idle' | 'pending' | 'success' | 'error'>('idle')
@@ -84,3 +85,52 @@ describe('compare page command palette commands', () => {
8485
wrapper.unmount()
8586
})
8687
})
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

Comments
 (0)