From a11a2d77d835d1c65e93c75c634e2e6e13c18446 Mon Sep 17 00:00:00 2001
From: Ellen Kraffmiller <ekraffmiller@hmdc.harvard.edu>
Date: Tue, 17 Dec 2024 09:06:56 -0500
Subject: [PATCH] feat: add e2e test for sort

---
 .../collection/CollectionItemsPanel.spec.ts   | 28 ++++++++++++++++---
 .../shared/datasets/DatasetHelper.ts          | 20 +++++++++++--
 2 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/tests/e2e-integration/e2e/sections/collection/CollectionItemsPanel.spec.ts b/tests/e2e-integration/e2e/sections/collection/CollectionItemsPanel.spec.ts
index e4a678ff2..0dd5730e9 100644
--- a/tests/e2e-integration/e2e/sections/collection/CollectionItemsPanel.spec.ts
+++ b/tests/e2e-integration/e2e/sections/collection/CollectionItemsPanel.spec.ts
@@ -8,7 +8,16 @@ import { TestsUtils } from '@tests/e2e-integration/shared/TestsUtils'
 import { Interception } from 'cypress/types/net-stubbing'
 
 const numbersOfDatasetsToCreate = [1, 2, 3, 4, 5, 6, 7, 8]
-
+const datasetTitles = [
+  'Darwin',
+  'Einstein',
+  'Galileo',
+  'Newton',
+  'Tesla',
+  'Curie',
+  'Hawking',
+  'Sagan'
+]
 const SEARCH_ENDPOINT_REGEX = /^\/api\/v1\/search(\?.*)?$/
 
 function extractInfoFromInterceptedResponse(interception: Interception) {
@@ -46,7 +55,7 @@ describe('Collection Items Panel', () => {
 
     // Creates 8 datasets with 1 file each
     for (const _number of numbersOfDatasetsToCreate) {
-      await DatasetHelper.createWithFile(FileHelper.create())
+      await DatasetHelper.createWithFileAndTitle(FileHelper.create(), datasetTitles[_number - 1])
     }
   })
 
@@ -79,7 +88,7 @@ describe('Collection Items Panel', () => {
     })
 
     // 1 - Now select the Files checkbox
-    cy.findByRole('checkbox', { name: /Files/ }).click()
+    cy.findByRole('checkbox', { name: /Files/ }).click({ force: true })
 
     cy.wait('@getCollectionItems').then((interception) => {
       const { totalItemsInResponse, collectionsInResponse, datasetsInResponse, filesInResponse } =
@@ -268,7 +277,7 @@ describe('Collection Items Panel', () => {
     })
 
     // 7 - Selects a facet filter
-    cy.findByRole('button', { name: /Finch, Fiona/ }).click()
+    cy.findByRole('button', { name: /Finch, Fiona/ }).click({ force: true })
 
     cy.wait('@getCollectionItems').then((interception) => {
       const { totalItemsInResponse, collectionsInResponse, datasetsInResponse, filesInResponse } =
@@ -307,5 +316,16 @@ describe('Collection Items Panel', () => {
         .should('exist')
         .should('have.length', 2)
     })
+    // 8 Sort by Name (Z-A)
+    cy.visit(`/spa/collections`)
+    cy.findByRole('button', { name: /Sort/ }).click({ force: true })
+    cy.contains('Name (Z-A)').click({ force: true })
+
+    cy.findAllByTestId('dataset-card').first().contains('Tesla')
+    const sortExpectedUrl = new URLSearchParams({
+      [CollectionItemsQueryParams.SORT]: 'name',
+      [CollectionItemsQueryParams.ORDER]: 'desc'
+    }).toString()
+    cy.url().should('include', `/collections?${sortExpectedUrl}`)
   })
 })
diff --git a/tests/e2e-integration/shared/datasets/DatasetHelper.ts b/tests/e2e-integration/shared/datasets/DatasetHelper.ts
index 219b059e8..caa9d5077 100644
--- a/tests/e2e-integration/shared/datasets/DatasetHelper.ts
+++ b/tests/e2e-integration/shared/datasets/DatasetHelper.ts
@@ -26,9 +26,16 @@ export class DatasetHelper extends DataverseApiHelper {
     )
   }
 
-  static async createWithTitle(title: string): Promise<DatasetResponse> {
+  static async createWithTitle(
+    title: string,
+    collectionId = ROOT_COLLECTION_ALIAS
+  ): Promise<DatasetResponse> {
     newDatasetData.datasetVersion.metadataBlocks.citation.fields[0].value = title
-    return this.request<DatasetResponse>(`/dataverses/root/datasets`, 'POST', newDatasetData)
+    return this.request<DatasetResponse>(
+      `/dataverses/${collectionId}/datasets`,
+      'POST',
+      newDatasetData
+    )
   }
 
   static async destroy(persistentId: string): Promise<DatasetResponse | undefined> {
@@ -157,6 +164,15 @@ export class DatasetHelper extends DataverseApiHelper {
     return { ...datasetResponse, file: file }
   }
 
+  static async createWithFileAndTitle(
+    fileData: FileData,
+    title: string,
+    collectionId = ROOT_COLLECTION_ALIAS
+  ): Promise<DatasetResponse> {
+    const datasetResponse = await this.createWithTitle(title, collectionId)
+    const file = await this.uploadFile(datasetResponse.persistentId, fileData)
+    return { ...datasetResponse, file: file }
+  }
   static async createWithFileAndPublish(
     fileData: FileData,
     collectionId = ROOT_COLLECTION_ALIAS