From ec98c1f3a446de89e693c39212fdc053de1ed97e Mon Sep 17 00:00:00 2001 From: MellyGray Date: Mon, 4 Sep 2023 17:35:04 +0200 Subject: [PATCH] feat(FilesFilterIntegration): add e2e test --- .../e2e/sections/dataset/Dataset.spec.tsx | 108 +++++++++++++++++- .../files/FileJSDataverseRepository.spec.ts | 12 +- .../shared/datasets/DatasetHelper.ts | 42 ++----- .../shared/files/FileHelper.ts | 47 +++++++- 4 files changed, 163 insertions(+), 46 deletions(-) diff --git a/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx b/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx index f67c47fe6..d8a459ad2 100644 --- a/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx +++ b/tests/e2e-integration/e2e/sections/dataset/Dataset.spec.tsx @@ -218,7 +218,7 @@ describe('Dataset', () => { }) it('loads the restricted files when the user is logged in as owner', () => { - cy.wrap(DatasetHelper.createWithFilesRestricted(FileHelper.createMany(1))) + cy.wrap(DatasetHelper.createWithFiles(FileHelper.createManyRestricted(1))) .its('persistentId') .then((persistentId: string) => { cy.visit(`/spa/datasets?persistentId=${persistentId}`) @@ -238,7 +238,7 @@ describe('Dataset', () => { it('loads the restricted files when the user is not logged in as owner', () => { cy.wrap( - DatasetHelper.createWithFilesRestricted(FileHelper.createMany(1)).then((dataset) => + DatasetHelper.createWithFiles(FileHelper.createManyRestricted(1)).then((dataset) => DatasetHelper.publish(dataset.persistentId) ) ) @@ -272,6 +272,8 @@ describe('Dataset', () => { ) .its('persistentId') .then((persistentId: string) => { + cy.wait(1500) // Wait for the files to be embargoed + cy.visit(`/spa/datasets?persistentId=${persistentId}`) cy.findByText('Files').should('exist') @@ -283,5 +285,107 @@ describe('Dataset', () => { cy.findByText('Embargoed').should('exist') }) }) + + it.skip('applies filters to the Files Table in the correct order', () => { + // TODO - Integrate fileCountInfo + const files = [ + FileHelper.create('csv', { + description: 'Some description', + categories: ['category'], + restrict: 'true', + tabIngest: 'false' + }), + FileHelper.create('csv', { + description: 'Some description', + restrict: 'true', + tabIngest: 'false' + }), + FileHelper.create('csv', { + description: 'Some description', + categories: ['category'], + tabIngest: 'false' + }), + FileHelper.create('txt', { + description: 'Some description', + categories: ['category'], + restrict: 'true' + }), + FileHelper.create('csv', { + description: 'Some description', + categories: ['category'], + restrict: 'true', + tabIngest: 'false' + }), + FileHelper.create('csv', { + description: 'Some description', + categories: ['category'], + restrict: 'true', + tabIngest: 'false' + }) + ] + cy.wrap(DatasetHelper.createWithFiles(files)) + .its('persistentId') + .then((persistentId: string) => { + cy.visit(`/spa/datasets?persistentId=${persistentId}`) + + cy.findByText('Files').should('exist') + + cy.findByText('blob').should('exist') + cy.findByText('blob-1').should('exist') + cy.findByText('blob-2').should('exist') + cy.findByText('blob-3').should('exist') + cy.findByText('blob-4').should('exist') + cy.findByText('blob-5').should('exist') + + cy.findByLabelText('Search').type('blob-{enter}', { force: true }) + + cy.findByText('blob').should('not.exist') + cy.findByText('blob-1').should('exist') + cy.findByText('blob-2').should('exist') + cy.findByText('blob-3').should('exist') + cy.findByText('blob-4').should('exist') + cy.findByText('blob-5').should('exist') + + cy.findByRole('button', { name: 'Filter Tag: All' }).click({ force: true }) + cy.findByText('category').should('exist').click({ force: true }) + + cy.findByText('blob').should('not.exist') + cy.findByText('blob-1').should('not.exist') + cy.findByText('blob-2').should('exist') + cy.findByText('blob-3').should('exist') + cy.findByText('blob-4').should('exist') + cy.findByText('blob-5').should('exist') + + cy.findByRole('button', { name: 'Access: All' }).click() + cy.findByText('Restricted').should('exist').click() + + cy.findByText('blob').should('not.exist') + cy.findByText('blob-1').should('not.exist') + cy.findByText('blob-2').should('not.exist') + cy.findByText('blob-3').should('exist') + cy.findByText('blob-4').should('exist') + cy.findByText('blob-5').should('exist') + + cy.findByRole('button', { name: 'Filter Type: All' }).click() + cy.findByText('text/csv').should('exist').click() + + cy.findByText('blob').should('not.exist') + cy.findByText('blob-1').should('not.exist') + cy.findByText('blob-2').should('not.exist') + cy.findByText('blob-3').should('not.exist') + cy.findByText('blob-4').should('exist') + cy.findByText('blob-5').should('exist') + + cy.findByRole('button', { name: /Sort/ }).click() + cy.findByText('Name (Z-A)').should('exist').click() + + cy.findByText('blob').should('not.exist') + cy.findByText('blob-1').should('not.exist') + cy.findByText('blob-2').should('not.exist') + cy.findByText('blob-3').should('not.exist') + cy.get('table > tbody > tr').eq(0).should('contain', 'blob-5') + cy.get('table > tbody > tr').eq(1).should('contain', 'blob-4') + }) + }) }) }) diff --git a/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts b/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts index d8a1bb359..d96a7b582 100644 --- a/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts +++ b/tests/e2e-integration/integration/files/FileJSDataverseRepository.spec.ts @@ -24,7 +24,7 @@ import { FileSortByOption } from '../../../../src/files/domain/models/FileCriteria' import { DatasetHelper } from '../../shared/datasets/DatasetHelper' -import { FileHelper } from '../../shared/files/FileHelper' +import { FileData, FileHelper } from '../../shared/files/FileHelper' chai.use(chaiAsPromised) const expect = chai.expect @@ -101,9 +101,11 @@ describe('File JSDataverse Repository', () => { it('gets all the files by dataset persistentId with the correct size', async () => { const expectedSize = new FileSize(25, FileSizeUnit.BYTES) - const dataset = await DatasetHelper.createWithFiles([ - new Blob([new ArrayBuffer(expectedSize.value)], { type: 'text/csv' }) - ]).then((datasetResponse) => + const fileData: FileData = { + file: new Blob([new ArrayBuffer(expectedSize.value)], { type: 'text/csv' }), + jsonData: JSON.stringify({ description: 'This is an example file' }) + } + const dataset = await DatasetHelper.createWithFiles([fileData]).then((datasetResponse) => datasetRepository.getByPersistentId(datasetResponse.persistentId) ) if (!dataset) throw new Error('Dataset not found') @@ -385,7 +387,7 @@ describe('File JSDataverse Repository', () => { }) }) - it.only('gets all the files by dataset persistentId when passing searchText criteria', async () => { + it('gets all the files by dataset persistentId when passing searchText criteria', async () => { const dataset = await DatasetHelper.createWithFiles(FileHelper.createMany(3)).then( (datasetResponse) => datasetRepository.getByPersistentId(datasetResponse.persistentId) ) diff --git a/tests/e2e-integration/shared/datasets/DatasetHelper.ts b/tests/e2e-integration/shared/datasets/DatasetHelper.ts index 36e243144..b5c0af9e3 100644 --- a/tests/e2e-integration/shared/datasets/DatasetHelper.ts +++ b/tests/e2e-integration/shared/datasets/DatasetHelper.ts @@ -1,6 +1,6 @@ import newDatasetData from '../../fixtures/dataset-finch1.json' import { DataverseApiHelper } from '../DataverseApiHelper' -import { forEach } from 'react-bootstrap/ElementChildren' +import { FileData } from '../files/FileHelper' export interface DatasetResponse { persistentId: string @@ -56,32 +56,12 @@ export class DatasetHelper extends DataverseApiHelper { ) } - static async createWithFiles( - filesBinary: Blob[], - filesMetadata?: { [key: string]: string } - ): Promise { + static async createWithFiles(filesData: FileData[]): Promise { const datasetResponse = await this.create() - const files = await this.uploadFiles(datasetResponse.persistentId, filesBinary, filesMetadata) + const files = await this.uploadFiles(datasetResponse.persistentId, filesData) return { ...datasetResponse, files: files } } - static async createWithFilesRestricted(filesBinary: Blob[]): Promise { - const datasetResponse = await this.createWithFiles(filesBinary, { - description: 'This is an example file', - restrict: 'true' - }) - return datasetResponse - } - - static async createWithFilesEmbargoed(filesBinary: Blob[]): Promise { - const datasetResponse = await this.createWithFiles(filesBinary, { - description: 'This is an example file', - restrict: 'true', - embargoDate: '2021-01-01' - }) - return datasetResponse - } - static async embargoFiles( persistentId: string, filesIds: number[], @@ -97,13 +77,12 @@ export class DatasetHelper extends DataverseApiHelper { private static async uploadFiles( datasetPersistentId: string, - filesBinary: Blob[], - filesMetadata: { [key: string]: string } = { description: 'This is an example file' } + filesData: FileData[] ): Promise { // TODO - Instead of uploading the files one by one, upload them all at once - do this refactor when integrating the pagination const files = [] - for (const fileBinary of filesBinary) { - const file = await this.uploadFile(datasetPersistentId, fileBinary, filesMetadata) + for (const fileData of filesData) { + const file = await this.uploadFile(datasetPersistentId, fileData) files.push(file) } return files @@ -111,17 +90,12 @@ export class DatasetHelper extends DataverseApiHelper { private static async uploadFile( datasetPersistentId: string, - fileBinary: Blob, - filesMetadata: { [key: string]: string } = { description: 'This is an example file' } + fileData: FileData ): Promise { - const data = { - file: fileBinary, - jsonData: JSON.stringify(filesMetadata) - } const { files } = await this.request<{ files: [{ dataFile: { id: number } }] }>( `/datasets/:persistentId/add?persistentId=${datasetPersistentId}`, 'POST', - data, + fileData, true ) diff --git a/tests/e2e-integration/shared/files/FileHelper.ts b/tests/e2e-integration/shared/files/FileHelper.ts index 1d0e64045..c7700f456 100644 --- a/tests/e2e-integration/shared/files/FileHelper.ts +++ b/tests/e2e-integration/shared/files/FileHelper.ts @@ -6,20 +6,57 @@ interface FileResponse { id: number } +export interface FileData { + file: Blob + jsonData: string +} + +interface FileMetadata { + description: string + restrict?: string + categories?: string[] + tabIngest?: string +} + export class FileHelper extends DataverseApiHelper { - static createMany(count: number, type: 'csv' | 'txt' = 'txt'): Blob[] { + static createManyRestricted( + count: number, + type: 'csv' | 'txt' = 'txt', + fileMetadata: FileMetadata = { description: 'This is an example file' } + ): FileData[] { const files = [] for (let i = 0; i < count; i++) { - files.push(this.create(type)) + files.push(this.create(type, { ...fileMetadata, restrict: 'true' })) } return files } - static create(type: 'csv' | 'txt' = 'txt'): Blob { + static createMany( + count: number, + type: 'csv' | 'txt' = 'txt', + fileMetadata: FileMetadata = { description: 'This is an example file' } + ): FileData[] { + const files = [] + for (let i = 0; i < count; i++) { + files.push(this.create(type, fileMetadata)) + } + return files + } + + static create( + type: 'csv' | 'txt' = 'txt', + fileMetadata: FileMetadata = { description: 'This is an example file' } + ): FileData { + let fileBinary = new Blob([this.generateTxtData()], { type: 'text/plain' }) + if (type === 'csv') { - return new Blob([this.generateCsvData()], { type: 'text/csv' }) + fileBinary = new Blob([this.generateCsvData()], { type: 'text/csv' }) + } + + return { + file: fileBinary, + jsonData: JSON.stringify(fileMetadata) } - return new Blob([this.generateTxtData()], { type: 'text/plain' }) } static generateCsvData(): string {