Skip to content

Commit

Permalink
update/fix tests for SourceDownloadLink
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnC-80 committed Sep 27, 2023
1 parent e98f8f2 commit 4e3542b
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions src/routes/JobSummary/components/SourceDownloadLink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@ import '../../../../test/jest/__mock__';
import { SourceDownloadLink } from './SourceDownloadLink';
import '../../../utils/multipartUpload';

// the indirectly used ky library extends JS Errors with JS mockHTTPError.
// this is used to simulate how we handle a 404 response.
class mockHTTPError extends Error {
constructor(response) {

Check failure on line 13 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected tab character

Check failure on line 13 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected indentation of 2 spaces but found 1 tab

Check failure on line 13 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected tab character

Check failure on line 13 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected indentation of 2 spaces but found 1 tab
super(

Check failure on line 14 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected tab character

Check failure on line 14 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected indentation of 4 spaces but found 2 tabs

Check failure on line 14 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected tab character

Check failure on line 14 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected indentation of 4 spaces but found 2 tabs
response.status

Check failure on line 15 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected tab character

Check failure on line 15 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected indentation of 6 spaces but found 3 tabs

Check failure on line 15 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected tab character

Check failure on line 15 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected indentation of 6 spaces but found 3 tabs
);

Check failure on line 16 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected tab character

Check failure on line 16 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected indentation of 4 spaces but found 2 tabs

Check failure on line 16 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected tab character

Check failure on line 16 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected indentation of 4 spaces but found 2 tabs
this.name = 'HTTPError';

Check failure on line 17 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected tab character

Check failure on line 17 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected indentation of 4 spaces but found 2 tabs

Check failure on line 17 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Unexpected tab character

Check failure on line 17 in src/routes/JobSummary/components/SourceDownloadLink.test.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

Expected indentation of 4 spaces but found 2 tabs
this.response = response;
}
}

const mockResponse = jest.fn();
const mocklinkMethod = jest.fn(() => Promise.resolve(mockResponse()));
jest.mock('../../../utils/multipartUpload', () => ({
...jest.requireActual('../../../utils/multipartUpload'),
getObjectStorageDownloadURL: mocklinkMethod
getObjectStorageDownloadURL: (ky, id) => {
if (id === 'file-removed') {
throw (new mockHTTPError({ status: '404'}));
}
return Promise.resolve(mockResponse())
}
}));

jest.mock('@folio/stripes/components', () => ({
Expand All @@ -22,7 +38,7 @@ jest.mock('@folio/stripes/components', () => ({

jest.mock('@folio/stripes/core', () => ({
...jest.requireActual('@folio/stripes/core'),
useOkapiKy: () => ({}),
useOkapiKy: jest.fn(() => {}),
useCallout: jest.fn(() => ({
sendCallout: jest.fn(() => {})
})),
Expand All @@ -39,31 +55,30 @@ describe('SourceDownloadLinkComponent', () => {

it('renders a loading spinner..', async () => {
mockResponse.mockResolvedValue({ url: 'testUrl' });
const { getByText } = await renderSourceDownloadLink({});
const { getByText } = await renderSourceDownloadLink({ id:'testId1' });

expect(getByText('Loading')).toBeInTheDocument();
});

it('renders the filename in the link', async () => {
mockResponse.mockResolvedValue({ url: 'testUrl' });
const { findByText } = await renderSourceDownloadLink({});
const { findByText } = await renderSourceDownloadLink({ id:'testId2' });

const text = await findByText('testFilename');
expect(text).toBeDefined();
});

it('renders the provided url to the link href', async () => {
mockResponse.mockResolvedValue({ url: 'http://www.testUrl' });
const { findByRole } = await renderSourceDownloadLink({});
const { findByRole } = await renderSourceDownloadLink({ id:'testId3' });

const link = await findByRole('link');
expect(link.href).toBe('http://www.testurl/');
});

it('renders unavailable message if the url is unavailable', async () => {
mockResponse.mockResolvedValue('Not found');
mocklinkMethod.mockRejectedValue(new Error({ message: '404' }));
const { findByText } = await renderSourceDownloadLink({});
const { findByText } = await renderSourceDownloadLink({ id:'file-removed' });
const message = await findByText('Unavailable');
expect(message).toBeInTheDocument();
});
Expand Down

0 comments on commit 4e3542b

Please sign in to comment.