-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: fix infinite loop in useStorageURL * feat: add StorageImage component * test: add unit tests for StorageImage * chore: fix loading attribute * chore: update snapshots * test: update snapshots * chore: address feedback * chore: address comments * chore: address comments * test: fix test * chore: address comments * chore: remove unused comments * chore: address comments * chore: update tests * chore: add comments to tests
- Loading branch information
Showing
15 changed files
with
223 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/react-storage/src/components/StorageImage/StorageImage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as React from 'react'; | ||
import classNames from 'classnames'; | ||
|
||
import { Image, ComponentClassNames } from '@aws-amplify/ui-react'; | ||
import { useStorageURL } from '@aws-amplify/ui-react/internal'; | ||
|
||
import type { StorageImageProps } from './types'; | ||
|
||
export const StorageImage = ({ | ||
accessLevel, | ||
className, | ||
fallbackSrc, | ||
identityId, | ||
imgKey, | ||
onStorageGetError, | ||
...rest | ||
}: StorageImageProps): JSX.Element => { | ||
const options = React.useMemo( | ||
() => ({ | ||
accessLevel, | ||
identityId, | ||
}), | ||
[accessLevel, identityId] | ||
); | ||
|
||
const errorConfig = React.useMemo( | ||
() => ({ | ||
fallbackURL: fallbackSrc, | ||
onStorageGetError, | ||
}), | ||
[fallbackSrc, onStorageGetError] | ||
); | ||
|
||
const url = useStorageURL(imgKey, options, errorConfig); | ||
|
||
return ( | ||
<Image | ||
{...rest} | ||
className={classNames(ComponentClassNames.StorageImage, className)} | ||
src={url} | ||
/> | ||
); | ||
}; |
78 changes: 78 additions & 0 deletions
78
packages/react-storage/src/components/StorageImage/_tests_/StorageImage.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import * as React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
|
||
import { Storage } from 'aws-amplify'; | ||
import { ComponentClassNames } from '@aws-amplify/ui-react'; | ||
|
||
import { StorageImage } from '../StorageImage'; | ||
|
||
describe('StorageImage', () => { | ||
const imgKey = 'test.jpg'; | ||
const imgURL = 'https://amplify.s3.amazonaws.com/path/to/test.jpg'; | ||
const fallbackSrc = 'https://amplify.s3.amazonaws.com/path/to/fallback.jpg'; | ||
const errorMessage = '500 Internal Server Error'; | ||
|
||
beforeAll(() => { | ||
jest.spyOn(Storage, 'get').mockResolvedValue(imgURL); | ||
}); | ||
|
||
it('should render default classname', async () => { | ||
render( | ||
<StorageImage alt="StorageImage" imgKey={imgKey} accessLevel="public" /> | ||
); | ||
|
||
const img = await screen.findByRole('img'); | ||
expect(img).toHaveClass(ComponentClassNames.StorageImage); | ||
}); | ||
|
||
it('should render custom classname', async () => { | ||
const className = 'MyImage'; | ||
render( | ||
<StorageImage | ||
alt="StorageImage" | ||
className={className} | ||
imgKey={imgKey} | ||
accessLevel="public" | ||
/> | ||
); | ||
|
||
const img = await screen.findByRole('img'); | ||
expect(img).toHaveClass(className); | ||
}); | ||
|
||
it('should get the presigned URL and pass it to image src attribute', async () => { | ||
const onStorageError = jest.fn(); | ||
render( | ||
<StorageImage | ||
alt="StorageImage" | ||
imgKey={imgKey} | ||
accessLevel="public" | ||
onStorageGetError={onStorageError} | ||
/> | ||
); | ||
|
||
const img = await screen.findByRole('img'); | ||
expect(onStorageError).not.toHaveBeenCalled(); | ||
expect(img).toHaveAttribute('src', imgURL); | ||
}); | ||
|
||
it('should set image src attribute to fallbackSrc and invoke onGetStorageError when Storage.get is rejected', async () => { | ||
jest.restoreAllMocks(); | ||
jest.spyOn(Storage, 'get').mockRejectedValue(errorMessage); | ||
const onStorageError = jest.fn(); | ||
render( | ||
<StorageImage | ||
alt="StorageImage" | ||
imgKey={imgKey} | ||
accessLevel="public" | ||
fallbackSrc={fallbackSrc} | ||
onStorageGetError={onStorageError} | ||
/> | ||
); | ||
|
||
const img = await screen.findByRole('img'); | ||
expect(onStorageError).toHaveBeenCalledTimes(1); | ||
expect(onStorageError).toHaveBeenCalledWith(errorMessage); | ||
expect(img).toHaveAttribute('src', fallbackSrc); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { StorageImage } from './StorageImage'; | ||
export { StorageImageProps } from './types'; |
12 changes: 12 additions & 0 deletions
12
packages/react-storage/src/components/StorageImage/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import type { StorageAccessLevel } from '@aws-amplify/storage'; | ||
import type { ImageProps } from '@aws-amplify/ui-react'; | ||
|
||
export interface StorageImageProps extends Omit<ImageProps, 'src'> { | ||
// Use imgKey instead of key because key is a reserved keyword | ||
// and cannot be accessed via props in React components | ||
imgKey: string; | ||
accessLevel: StorageAccessLevel; | ||
identityId?: string; | ||
fallbackSrc?: string; | ||
onStorageGetError?: (error: Error) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export { StorageImage, StorageImageProps } from './StorageImage'; | ||
|
||
export { | ||
StorageManager, | ||
StorageManagerProps, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export { | ||
StorageImage, | ||
StorageImageProps, | ||
StorageManager, | ||
StorageManagerProps, | ||
DropZoneProps, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.