Skip to content

Commit

Permalink
feat(dataDownloadListActions): Updated unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jarvisraymond-uchicago committed Nov 9, 2023
1 parent f5e8476 commit 30d9119
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const ActionButtons = ({
const studyIDs = [resourceInfo?.study_id];

return (
<div className='discovery-modal_buttons-row'>
<div className='discovery-modal_buttons-row' data-testid='actionButtons'>
<DownloadAllModal
downloadStatus={downloadStatus}
setDownloadStatus={setDownloadStatus}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import ActionButtons from './ActionButtons';

Expand All @@ -21,6 +21,8 @@ describe('ActionButtons', () => {
};

const mockData = { /* mock data */ };

/* Helper Functions */
const checkResourceInfoConditional = (buttonText: string) => {
const { queryByText } = render(
<ActionButtons
Expand Down Expand Up @@ -58,6 +60,17 @@ describe('ActionButtons', () => {
};

/* TESTS */
test('Renders test id for ActionButtons', () => {
render(
<ActionButtons
discoveryConfig={mockDiscoveryConfig}
resourceInfo={mockResourceInfo}
data={mockData}
/>,
);
expect(screen.queryByTestId('actionButtons')).toBeInTheDocument();
});

test('renders Download Study-Level Metadata button based on conditionals', () => {
const buttonText = 'Study-Level Metadata';
checkExportToWorkspaceConditional(buttonText, 'enableDownloadStudyMetadata');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import HandleDownloadManifestClick from './HandleDownloadManifestClick';
import DownloadJsonFile from './DownloadJsonFile';
import { DiscoveryConfig } from '../../../../DiscoveryConfig';

// Mock the DownloadJsonFile module
jest.mock('./DownloadJsonFile');

describe('HandleDownloadManifestClick', () => {
it('should not call DownloadJsonFile when healICPSRLoginNeeded is true', () => {
// Mock data
const config = {
features: {
exportToWorkspace: {
manifestFieldName: 'manifestFieldName',
},
},
} as DiscoveryConfig;
const selectedResources = [{ manifestFieldName: [{ item: 'value' }] }];
const healICPSRLoginNeeded = true;

// Call the function
HandleDownloadManifestClick(config, selectedResources, healICPSRLoginNeeded);

// Assertions
expect(DownloadJsonFile).not.toHaveBeenCalled();
});

it('should throw an error when manifestFieldName is missing in the configuration', () => {
// Mock data
const config = {
features: {
exportToWorkspace: {},
},
} as DiscoveryConfig;
const selectedResources = [{ manifestFieldName: [{ item: 'value' }] }];
const healICPSRLoginNeeded = false;

// Assertions
expect(() => HandleDownloadManifestClick(config, selectedResources, healICPSRLoginNeeded)).toThrowError(
'Missing required configuration field `config.features.exportToWorkspace.manifestFieldName`',
);
});
it('should call DownloadJsonFile with the correct arguments when healICPSRLoginNeeded is false', () => {
// Mock data
const config = {
features: {
exportToWorkspace: {
manifestFieldName: 'manifestFieldName',
},
},
} as DiscoveryConfig;
const selectedResources = [{ manifestFieldName: [{ item: 'value' }] }];
const healICPSRLoginNeeded = false;

// Call the function
HandleDownloadManifestClick(config, selectedResources, healICPSRLoginNeeded);

// Assertions
expect(DownloadJsonFile).toHaveBeenCalledWith('manifest', [{ item: 'value' }]);
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react';
import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import DataDownloadList from './DataDownloadList';

describe('DataDownloadList', () => {
it('renders the component with titles and descriptions when sourceFieledData has titles and descriptions', () => {
it('renders the component with titles and descriptions and action buttons when sourceFieledData has titles and descriptions', () => {
const sourceFieldData = [
[
{
Expand All @@ -18,13 +18,18 @@ describe('DataDownloadList', () => {
],
];
const { getByText } = render(
<DataDownloadList sourceFieldData={sourceFieldData} />,
<DataDownloadList
discoveryConfig={null}
resourceInfo={null}
sourceFieldData={sourceFieldData}
/>,
);
// Verify that the component renders successfully
sourceFieldData[0].forEach((obj) => {
expect(getByText(obj.title)).toBeInTheDocument();
expect(getByText(obj.description)).toBeInTheDocument();
});
expect(screen.queryByTestId('actionButtons')).toBeInTheDocument();
});

it('renders the component with titles and descriptions when sourceFieledData has file_names and descriptions', () => {
Expand Down Expand Up @@ -71,7 +76,7 @@ describe('DataDownloadList', () => {
});
});

it('does not render the component when data is missing title and file_name', () => {
it('does not render the component or action buttons when data is missing title and file_name', () => {
const sourceFieldData = [
[
{
Expand All @@ -86,5 +91,6 @@ describe('DataDownloadList', () => {
);
// Verify that the component does not render (returns null)
expect(container.firstChild).toBeNull();
expect(screen.queryByTestId('actionButtons')).not.toBeInTheDocument();
});
});

0 comments on commit 30d9119

Please sign in to comment.