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.

Update the media e2e helpers for the new tile interaction

Damian Legawiec031323811550
diff --git a/packages/dashboard/e2e/products-helpers.ts b/packages/dashboard/e2e/products-helpers.ts
index 56c13e9a24..8ea346d1ed 100644
--- a/packages/dashboard/e2e/products-helpers.ts
+++ b/packages/dashboard/e2e/products-helpers.ts
@@ -114,26 +114,25 @@ export const variantsCard = (page: Page) => card(page, /^Variants$/)
export const mediaCard = (page: Page) => card(page, /^Media$/)
/**
- * Click a hover-revealed action on the first media thumbnail. Scrolls the
- * Media card below the sticky TopBar + PageHeader stack first — restoring
- * sticky headers (PR #14218) made Playwright's default scroll-into-view land
- * the bottom overlay buttons under the header chrome.
+ * Act on the first media thumbnail: clicking a tile opens its edit sheet, and
+ * every other action lives in the tile's right-click menu.
+ *
+ * Scrolls the Media card below the sticky TopBar + PageHeader stack first —
+ * restoring sticky headers (PR #14218) made Playwright's default
+ * scroll-into-view land the tile under the header chrome.
*/
export async function clickMediaThumbnailAction(
media: Locator,
action: 'edit' | 'delete',
): Promise<void> {
- // Hover the tile itself, not its image — a video with no poster renders an
+ // Target the tile itself, not its image — a video with no poster renders an
// icon instead, and waiting on an <img> would hang.
const thumb = media.locator('[data-slot="media-thumbnail"]').first()
- const button = media.getByRole('button', {
- name: action === 'edit' ? /^edit media$/i : /^delete image$/i,
- })
await thumb.scrollIntoViewIfNeeded()
// Keep the card below the stacked sticky TopBar + PageHeader. Playwright's
- // default click scrolls the target back into view, so we also force-click
- // after hover — otherwise the header chrome intercepts pointer events.
+ // default click scrolls the target back into view, so we force-click after
+ // positioning — otherwise the header chrome intercepts pointer events.
await media.evaluate((el) => {
const headerHeight =
Number.parseFloat(
@@ -144,9 +143,15 @@ export async function clickMediaThumbnailAction(
if (top < stickyOffset) window.scrollBy(0, top - stickyOffset)
})
- await thumb.hover()
- await expect(button).toBeVisible()
- await button.click({ force: true })
+ if (action === 'edit') {
+ await thumb.click({ force: true })
+ return
+ }
+
+ await thumb.click({ button: 'right', force: true })
+ const item = media.page().getByRole('menuitem', { name: /^delete$/i })
+ await expect(item).toBeVisible()
+ await item.click()
}
export const customFieldsCard = (page: Page) => card(page, /^Custom fields$/)
export const inventoryCard = (page: Page) => card(page, /^Inventory$/)
diff --git a/packages/dashboard/e2e/products-media-video.spec.ts b/packages/dashboard/e2e/products-media-video.spec.ts
index 3388bc78aa..1cb08f5c20 100644
--- a/packages/dashboard/e2e/products-media-video.spec.ts
+++ b/packages/dashboard/e2e/products-media-video.spec.ts
@@ -49,9 +49,14 @@ test.describe('product media — video', () => {
await dialog.getByLabel(/video link/i).fill('https://vimeo.com/123456789')
await dialog.getByRole('button', { name: /^add video$/i }).click()
+ // The add-video dialog must be gone before the edit sheet opens, or
+ // `getByRole('dialog')` resolves whichever is still mounted.
+ await expect(dialog).toBeHidden()
+
// Open the tile's editor and upload the still.
await clickMediaThumbnailAction(media, 'edit')
const sheet = page.getByRole('dialog')
+ await expect(sheet.getByText(/edit media/i)).toBeVisible()
await sheet.locator('input[type="file"]').setInputFiles(FIXTURE_IMAGE)
await expect(sheet.locator('img[src]').first()).toBeVisible({ timeout: 15_000 })
await sheet.getByRole('button', { name: /^done$/i }).click()
diff --git a/spree/core/app/services/spree/media_usage.rb b/spree/core/app/services/spree/media_usage.rb
index c3946dc496..c31a85fd45 100644
--- a/spree/core/app/services/spree/media_usage.rb
+++ b/spree/core/app/services/spree/media_usage.rb
@@ -42,10 +42,14 @@ module Spree
# reference per owner, preferring the placement, which is the library's
# own representation of the use and what the dashboard links.
def attachment_references(media, blob_ids)
- attachments = ActiveStorage::Attachment.where(blob_id: blob_ids)
+ # `record` is polymorphic, so preload it per type — reading it row by row
+ # is a query each, and a widely-reused file has many rows. Media owners
+ # additionally need their viewable, which build_reference reads.
+ attachments = ActiveStorage::Attachment.where(blob_id: blob_ids).to_a
+ owners = preloaded_owners(attachments)
references = attachments.filter_map do |attachment|
- owner = attachment.record
+ owner = owners[[attachment.record_type, attachment.record_id]]
next if owner.blank?
next unless owned_by_store?(owner, media.store_id)
@@ -57,6 +61,22 @@ module Spree
.map { |_owner, group| group.find { |reference| reference.kind == 'media' } || group.first }
end
+ # @return [Hash{Array(String, Integer) => ActiveRecord::Base}] owners by [type, id]
+ def preloaded_owners(attachments)
+ attachments.group_by(&:record_type).each_with_object({}) do |(type, rows), owners|
+ model = type.safe_constantize
+ next if model.blank?
+
+ scope = model.all
+ # A media owner's own viewable is what build_reference reports.
+ scope = scope.includes(:viewable) if model <= Spree::Media
+
+ scope.where(id: rows.map(&:record_id)).each do |record|
+ owners[[type, record.id]] = record
+ end
+ end
+ end
+
# A record is this store's if it says so. One that carries no store at all
# (the store record itself, a global) is reported — it is not another
# tenant's data.