Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Replace single image unit tests with Playwright e2e (#2583)

Olga Bulat5513185153cc
diff --git a/frontend/test/playwright/e2e/audio-detail.spec.ts b/frontend/test/playwright/e2e/audio-detail.spec.ts
index 667df76c9..2244972d8 100644
--- a/frontend/test/playwright/e2e/audio-detail.spec.ts
+++ b/frontend/test/playwright/e2e/audio-detail.spec.ts
@@ -32,37 +32,61 @@ test("shows the data that is only available in single result, not search respons
).toBeVisible()
})
-test("sends a custom event on play", async ({ page, context }) => {
- const analyticsEvents = collectAnalyticsEvents(context)
-
- await goToCustomAudioPage(page)
- await page.getByRole("button", { name: "Play" }).first().click()
- const audioInteractionEvent = analyticsEvents.find(
- (event) => event.n === "AUDIO_INTERACTION"
- )
-
- expectEventPayloadToMatch(audioInteractionEvent, {
+test.describe("analytics", () => {
+ const audioObject = {
id: "7e063ee6-343f-48e4-a4a5-f436393730f6",
- event: "play",
provider: "jamendo",
- component: "AudioDetailPage",
+ }
+ test("sends GET_MEDIA event on CTA button click", async ({
+ context,
+ page,
+ }) => {
+ const analyticsEvents = collectAnalyticsEvents(context)
+
+ await goToCustomAudioPage(page)
+
+ await page.getByRole("link", { name: /get this audio/i }).click()
+
+ const getMediaEvent = analyticsEvents.find(
+ (event) => event.n === "GET_MEDIA"
+ )
+
+ expectEventPayloadToMatch(getMediaEvent, {
+ ...audioObject,
+ mediaType: "audio",
+ })
})
-})
-test("sends a custom event on seek", async ({ page, context }) => {
- const analyticsEvents = collectAnalyticsEvents(context)
+ test("sends AUDIO_INTERACTION event on play", async ({ page, context }) => {
+ const analyticsEvents = collectAnalyticsEvents(context)
- await goToCustomAudioPage(page)
- await page.mouse.click(200, 200)
- const audioInteractionEvent = analyticsEvents.find(
- (event) => event.n === "AUDIO_INTERACTION"
- )
+ await goToCustomAudioPage(page)
+ await page.getByRole("button", { name: "Play" }).first().click()
+ const audioInteractionEvent = analyticsEvents.find(
+ (event) => event.n === "AUDIO_INTERACTION"
+ )
- expectEventPayloadToMatch(audioInteractionEvent, {
- id: "7e063ee6-343f-48e4-a4a5-f436393730f6",
- event: "seek",
- provider: "jamendo",
- component: "AudioDetailPage",
+ expectEventPayloadToMatch(audioInteractionEvent, {
+ ...audioObject,
+ event: "play",
+ component: "AudioDetailPage",
+ })
+ })
+
+ test("sends AUDIO_INTERACTION event on seek", async ({ page, context }) => {
+ const analyticsEvents = collectAnalyticsEvents(context)
+
+ await goToCustomAudioPage(page)
+ await page.mouse.click(200, 200)
+ const audioInteractionEvent = analyticsEvents.find(
+ (event) => event.n === "AUDIO_INTERACTION"
+ )
+
+ expectEventPayloadToMatch(audioInteractionEvent, {
+ ...audioObject,
+ event: "seek",
+ component: "AudioDetailPage",
+ })
})
})
diff --git a/frontend/test/playwright/e2e/image-detail.spec.ts b/frontend/test/playwright/e2e/image-detail.spec.ts
index 2a6005c91..37e96f2fd 100644
--- a/frontend/test/playwright/e2e/image-detail.spec.ts
+++ b/frontend/test/playwright/e2e/image-detail.spec.ts
@@ -2,6 +2,10 @@ import { test, expect, Page } from "@playwright/test"
import { mockProviderApis } from "~~/test/playwright/utils/route"
import { t } from "~~/test/playwright/utils/navigation"
+import {
+ collectAnalyticsEvents,
+ expectEventPayloadToMatch,
+} from "~~/test/playwright/utils/analytics"
const goToCustomImagePage = async (page: Page) => {
// Test in a custom image detail page, it should apply the same for any image.
@@ -58,3 +62,46 @@ test("shows the 404 error page when no id", async ({ page }) => {
await page.goto("image/")
await showsErrorPage(page)
})
+
+test.describe("analytics", () => {
+ test("sends GET_MEDIA event on CTA button click", async ({
+ context,
+ page,
+ }) => {
+ const analyticsEvents = collectAnalyticsEvents(context)
+
+ await goToCustomImagePage(page)
+
+ await page.getByRole("link", { name: /get this image/i }).click()
+
+ const getMediaEvent = analyticsEvents.find(
+ (event) => event.n === "GET_MEDIA"
+ )
+
+ expectEventPayloadToMatch(getMediaEvent, {
+ id: "e9d97a98-621b-4ec2-bf70-f47a74380452",
+ provider: "flickr",
+ mediaType: "image",
+ })
+ })
+
+ test("sends RIGHT_CLICK_IMAGE event on right-click", async ({
+ context,
+ page,
+ }) => {
+ const analyticsEvents = collectAnalyticsEvents(context)
+
+ await goToCustomImagePage(page)
+
+ const img = page.getByRole("img").first()
+ await img.click({ button: "right" })
+
+ const rightClickImageEvent = analyticsEvents.find(
+ (event) => event.n === "RIGHT_CLICK_IMAGE"
+ )
+
+ expectEventPayloadToMatch(rightClickImageEvent, {
+ id: "e9d97a98-621b-4ec2-bf70-f47a74380452",
+ })
+ })
+})
diff --git a/frontend/test/unit/specs/components/ImageDetails/v-image-details-page.spec.js b/frontend/test/unit/specs/components/ImageDetails/v-image-details-page.spec.js
deleted file mode 100644
index b88c42524..000000000
--- a/frontend/test/unit/specs/components/ImageDetails/v-image-details-page.spec.js
+++ /dev/null
@@ -1,56 +0,0 @@
-import { fireEvent, screen } from "@testing-library/vue"
-
-import { render } from "~~/test/unit/test-utils/render"
-
-import { useAnalytics } from "~/composables/use-analytics"
-import { IMAGE } from "~/constants/media"
-
-import VImageDetailsPage from "~/pages/image/_id/index.vue"
-
-jest.mock("~/composables/use-analytics", () => ({
- useAnalytics: jest.fn(() => ({
- sendCustomEvent: jest.fn(),
- })),
-}))
-
-jest.mock("~/stores/media/single-result", () => ({
- useSingleResultStore: jest.fn(() => ({
- mediaType: "image",
- mediaItem: imageObject,
- })),
-}))
-
-const imageObject = {
- id: "123",
- provider: "test-provider",
- foreign_landing_url: "https://test.com",
- frontendMediaType: "image",
-}
-
-describe("VImageDetailsPage", () => {
- it("should send GET_MEDIA analytics event on CTA button click", async () => {
- const sendCustomEventMock = jest.fn()
- useAnalytics.mockImplementation(() => ({
- sendCustomEvent: sendCustomEventMock,
- }))
-
- render(VImageDetailsPage, {
- mocks: {
- $route: {
- params: {
- meta: "url",
- },
- },
- },
- })
-
- const downloadButton = screen.getByText(/get this image/i)
- await fireEvent.click(downloadButton)
-
- expect(sendCustomEventMock).toHaveBeenCalledWith("GET_MEDIA", {
- id: imageObject.id,
- mediaType: IMAGE,
- provider: imageObject.provider,
- })
- })
-})
diff --git a/frontend/test/unit/specs/pages/single-image-result-page.spec.js b/frontend/test/unit/specs/pages/single-image-result-page.spec.js
deleted file mode 100644
index dffb4ee18..000000000
--- a/frontend/test/unit/specs/pages/single-image-result-page.spec.js
+++ /dev/null
@@ -1,59 +0,0 @@
-import { fireEvent } from "@testing-library/vue"
-
-import { render } from "~~/test/unit/test-utils/render"
-
-import { IMAGE } from "~/constants/media"
-import { useSingleResultStore } from "~/stores/media/single-result"
-import { useAnalytics } from "~/composables/use-analytics"
-
-import SingleImageResultPage from "~/pages/image/_id/index.vue"
-
-jest.mock("~/composables/use-analytics", () => ({
- useAnalytics: jest.fn(),
-}))
-describe("SingleImageResultPage", () => {
- const id = "123"
- let sendCustomEventMock = null
- let options = null
-
- beforeEach(() => {
- sendCustomEventMock = jest.fn()
- useAnalytics.mockImplementation(() => ({
- sendCustomEvent: sendCustomEventMock,
- }))
- options = {
- mocks: {
- $route: {
- params: {
- id,
- },
- },
- },
- }
- })
-
- it("should send RIGHT_CLICK_IMAGE analytics event", async () => {
- let singleImageStore
- const screen = render(
- SingleImageResultPage,
- options,
- (localVue, options) => {
- singleImageStore = useSingleResultStore(options.pinia)
- singleImageStore.mediaType = IMAGE
- singleImageStore.mediaItem = {
- frontendMediaType: IMAGE,
- id,
- url: "http://example.com/image.jpg",
- width: 100,
- height: 100,
- providerName: "provider",
- }
- }
- )
- const image = await screen.findByRole("img")
- await fireEvent.contextMenu(image)
- expect(sendCustomEventMock).toHaveBeenCalledWith("RIGHT_CLICK_IMAGE", {
- id,
- })
- })
-})
diff --git a/frontend/test/unit/test-utils/pinia.js b/frontend/test/unit/test-utils/pinia.js
index f0d0a4f06..0a50d9fc8 100644
--- a/frontend/test/unit/test-utils/pinia.js
+++ b/frontend/test/unit/test-utils/pinia.js
@@ -21,6 +21,7 @@ export const createPinia = () =>
name: "Spanish",
},
},
+ error: jest.fn(),
localePath: jest.fn(),
},
}))