Skip to content

Commit 874fda4

Browse files
committed
Add failing tests
1 parent 7f1e259 commit 874fda4

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

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)