diff --git a/test/nuxt/composables/use-npm-registry.spec.ts b/test/nuxt/composables/use-npm-registry.spec.ts new file mode 100644 index 0000000000..28ca60b23d --- /dev/null +++ b/test/nuxt/composables/use-npm-registry.spec.ts @@ -0,0 +1,55 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +const mockDownloadResponse = { + downloads: 1234567, + start: '2024-01-01', + end: '2024-01-07', + package: 'vue', +} +describe('usePackageDownloads', () => { + let fetchSpy: ReturnType + + beforeEach(() => { + fetchSpy = vi.fn().mockResolvedValue(mockDownloadResponse) + vi.stubGlobal('$fetch', fetchSpy) + }) + + afterEach(() => { + vi.unstubAllGlobals() + }) + + it('should fetch download stats for a package', async () => { + const { data, status } = usePackageDownloads('vue') + + await vi.waitFor(() => { + expect(status.value).toBe('success') + }) + + expect(fetchSpy).toHaveBeenCalledWith('https://api.npmjs.org/downloads/point/last-week/vue') + expect(data.value?.downloads).toBe(1234567) + }) + + it('should use custom period when provided', async () => { + const { status } = usePackageDownloads('vue', 'last-month') + + await vi.waitFor(() => { + expect(status.value).toBe('success') + }) + + expect(fetchSpy).toHaveBeenCalledWith('https://api.npmjs.org/downloads/point/last-month/vue') + }) + + it('should encode scoped package names', async () => { + fetchSpy.mockResolvedValue({ ...mockDownloadResponse, package: '@vue/core' }) + + const { status } = usePackageDownloads('@vue/core') + + await vi.waitFor(() => { + expect(status.value).toBe('success') + }) + + expect(fetchSpy).toHaveBeenCalledWith( + 'https://api.npmjs.org/downloads/point/last-week/@vue%2Fcore', + ) + }) +}) diff --git a/vitest.config.ts b/vitest.config.ts index ab18b03c92..30837ddb59 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -23,7 +23,7 @@ export default defineConfig({ await defineVitestProject({ test: { name: 'nuxt', - include: ['test/nuxt/*.{test,spec}.ts'], + include: ['test/nuxt/**/*.{test,spec}.ts'], environment: 'nuxt', environmentOptions: { nuxt: {