Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add digital files E2E and fix two upload-flow bugs it surfaced

Damian Legawiec094ff4a2b648
diff --git a/packages/dashboard/e2e/digital-assets.spec.ts b/packages/dashboard/e2e/digital-assets.spec.ts
new file mode 100644
index 0000000000..7de94f7402
--- /dev/null
+++ b/packages/dashboard/e2e/digital-assets.spec.ts
@@ -0,0 +1,67 @@
+import * as path from 'node:path'
+import { fileURLToPath } from 'node:url'
+import { expect, test } from '@playwright/test'
+import { login, openRowMenu } from './helpers'
+import { createProduct } from './products-helpers'
+
+// Any file works — the card serves whatever is attached, it doesn't restrict
+// the type. Reuse the shared fixture so no new binary ships with the suite.
+const FIXTURE_FILE = path.join(
+ path.dirname(fileURLToPath(import.meta.url)),
+ 'fixtures/test-image.png',
+)
+
+test.describe('product digital files', () => {
+ test('uploads a file, edits its download limits, then removes it', async ({ page }) => {
+ const creds = await login(page)
+ await createProduct(page, creds.store_id, `E2E Digital ${Date.now()}`)
+
+ // Scope to the Digital files card — the Media card on the same page also
+ // has a hidden file input, so target this card's own input by its title.
+ const digitalCard = page
+ .locator('[data-slot="card"]')
+ .filter({ has: page.locator('[data-slot="card-title"]', { hasText: /^Digital files$/ }) })
+ await expect(digitalCard).toHaveCount(1)
+
+ // The empty card is a dropzone with a hidden file input behind it — set the
+ // file directly rather than simulating the click-through.
+ const fileName = path.basename(FIXTURE_FILE)
+ await digitalCard.locator('input[type="file"]').setInputFiles(FIXTURE_FILE)
+
+ // The uploaded file appears as a row in this card once the asset is created.
+ const fileRow = digitalCard.locator('tr').filter({ hasText: fileName })
+ await expect(fileRow).toBeVisible({ timeout: 30_000 })
+
+ // Edit its per-file limits through the row menu sheet.
+ await openRowMenu(page, fileName)
+ await page.getByRole('menuitem', { name: /^edit$/i }).click()
+
+ const sheet = page.getByRole('dialog')
+ await expect(sheet).toBeVisible()
+ await sheet.getByLabel(/downloads allowed/i).fill('5')
+ await sheet.getByLabel(/days available/i).fill('7')
+ await sheet.getByRole('button', { name: /^save$/i }).click()
+ await expect(sheet).toBeHidden()
+
+ // The chosen limits show in the row rather than the store default — proof
+ // the update persisted and the list refetched.
+ await expect(fileRow.getByRole('cell', { name: '5', exact: true })).toBeVisible({
+ timeout: 15_000,
+ })
+ await expect(fileRow.getByRole('cell', { name: '7', exact: true })).toBeVisible()
+
+ // Remove it — the row-menu delete goes through a destructive confirm whose
+ // action button is the default "Confirm" (the card passes no confirmLabel).
+ await openRowMenu(page, fileName)
+ await page.getByRole('menuitem', { name: /^delete$/i }).click()
+ await expect(page.getByRole('heading', { name: /delete this file\?/i })).toBeVisible()
+ await page
+ .getByRole('dialog')
+ .getByRole('button', { name: /^confirm$/i })
+ .click()
+
+ // The row is gone and the card falls back to its empty dropzone.
+ await expect(fileRow).toBeHidden({ timeout: 15_000 })
+ await expect(page.getByText(/drag & drop files here/i)).toBeVisible()
+ })
+})
diff --git a/packages/dashboard/src/components/spree/products/digital-assets-card.tsx b/packages/dashboard/src/components/spree/products/digital-assets-card.tsx
index e9315d1a5e..4da02ca88e 100644
--- a/packages/dashboard/src/components/spree/products/digital-assets-card.tsx
+++ b/packages/dashboard/src/components/spree/products/digital-assets-card.tsx
@@ -85,9 +85,12 @@ export function DigitalAssetsCard({
}
function handleFileInput(event: React.ChangeEvent<HTMLInputElement>) {
- const files = event.target.files
+ // `event.target.files` is a live FileList — clearing the input's value
+ // empties it, so snapshot to a real array before resetting the input
+ // (the reset lets the same file be re-selected later).
+ const files = Array.from(event.target.files ?? [])
event.target.value = ''
- if (files) void handleFiles(files)
+ if (files.length > 0) void handleFiles(files)
}
function handleDrop(event: React.DragEvent) {
diff --git a/packages/dashboard/src/components/spree/products/digital-files-form-card.tsx b/packages/dashboard/src/components/spree/products/digital-files-form-card.tsx
index 84a34dc80f..e1f6acf2d3 100644
--- a/packages/dashboard/src/components/spree/products/digital-files-form-card.tsx
+++ b/packages/dashboard/src/components/spree/products/digital-files-form-card.tsx
@@ -81,9 +81,12 @@ export function DigitalFilesFormCard({
)
function handleFileInput(event: React.ChangeEvent<HTMLInputElement>) {
- const files = event.target.files
+ // `event.target.files` is a live FileList — clearing the input's value
+ // empties it, so snapshot to a real array before resetting the input
+ // (the reset lets the same file be re-selected later).
+ const files = Array.from(event.target.files ?? [])
event.target.value = ''
- if (files) void handleFiles(files)
+ if (files.length > 0) void handleFiles(files)
}
function handleDrop(event: React.DragEvent) {
diff --git a/packages/dashboard/src/hooks/use-digital-assets.ts b/packages/dashboard/src/hooks/use-digital-assets.ts
index 193ee91ada..f12bc0691e 100644
--- a/packages/dashboard/src/hooks/use-digital-assets.ts
+++ b/packages/dashboard/src/hooks/use-digital-assets.ts
@@ -1,14 +1,15 @@
-import { adminClient, i18n, useResourceMutation, useStore } from '@spree/dashboard-core'
+import { adminClient, i18n, useResourceKey, useResourceMutation } from '@spree/dashboard-core'
import { useQuery } from '@tanstack/react-query'
type DigitalAssetCreateParams = Parameters<typeof adminClient.products.digitalAssets.create>[1]
type DigitalAssetUpdateParams = Parameters<typeof adminClient.products.digitalAssets.update>[2]
-export function useDigitalAssets(productId: string, page = 1, enabled = true) {
- const { storeId } = useStore()
+// The logical invalidation key (tenant id auto-injected by useResourceMutation).
+const digitalAssetsKey = (productId: string) => ['products', productId, 'digital-assets']
+export function useDigitalAssets(productId: string, page = 1, enabled = true) {
return useQuery({
- queryKey: [storeId, 'products', productId, 'digital-assets', page],
+ queryKey: useResourceKey('products', productId, 'digital-assets', page),
queryFn: () => adminClient.products.digitalAssets.list(productId, { page, per_page: 25 }),
enabled: enabled && Boolean(productId),
})
@@ -18,10 +19,8 @@ export function useDigitalAssets(productId: string, page = 1, enabled = true) {
// so a host with no extra provider gets a one-entry list and the card keeps its
// plain "Add file" button.
export function useDigitalAssetProviders(productId: string, enabled = true) {
- const { storeId } = useStore()
-
return useQuery({
- queryKey: [storeId, 'products', productId, 'digital-asset-providers'],
+ queryKey: useResourceKey('products', productId, 'digital-asset-providers'),
queryFn: () => adminClient.products.digitalAssets.providers(productId),
enabled: enabled && Boolean(productId),
staleTime: Number.POSITIVE_INFINITY, // the registry doesn't change at runtime
@@ -29,33 +28,27 @@ export function useDigitalAssetProviders(productId: string, enabled = true) {
}
export function useCreateDigitalAsset(productId: string) {
- const { storeId } = useStore()
-
return useResourceMutation({
mutationFn: (params: DigitalAssetCreateParams) =>
adminClient.products.digitalAssets.create(productId, params),
- invalidate: [[storeId, 'products', productId, 'digital-assets']],
+ invalidate: [digitalAssetsKey(productId)],
successMessage: i18n.t('admin.messages.digital_asset_saved'),
})
}
export function useUpdateDigitalAsset(productId: string) {
- const { storeId } = useStore()
-
return useResourceMutation({
mutationFn: ({ id, ...params }: DigitalAssetUpdateParams & { id: string }) =>
adminClient.products.digitalAssets.update(productId, id, params),
- invalidate: [[storeId, 'products', productId, 'digital-assets']],
+ invalidate: [digitalAssetsKey(productId)],
successMessage: i18n.t('admin.messages.digital_asset_saved'),
})
}
export function useDeleteDigitalAsset(productId: string) {
- const { storeId } = useStore()
-
return useResourceMutation({
mutationFn: (id: string) => adminClient.products.digitalAssets.delete(productId, id),
- invalidate: [[storeId, 'products', productId, 'digital-assets']],
+ invalidate: [digitalAssetsKey(productId)],
successMessage: i18n.t('admin.messages.digital_asset_removed'),
})
}
@@ -68,11 +61,9 @@ export function useResendDigitalLinks(orderId: string) {
}
export function useResetDigitalLink(orderId: string) {
- const { storeId } = useStore()
-
return useResourceMutation({
mutationFn: (id: string) => adminClient.digitalLinks.reset(id),
- invalidate: [[storeId, 'orders', orderId]],
+ invalidate: [['orders', orderId]],
successMessage: i18n.t('admin.messages.digital_link_reset'),
})
}