Skip to content

Commit e5e5d3f

Browse files
author
Vir Desai
committed
updating tests, adding types, adding custom override for FileUploadPreview and ImageUploadPreview components
1 parent 8145482 commit e5e5d3f

12 files changed

+1017
-627
lines changed

src/components/MessageInput/FileUploadPreview.js

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import UploadProgressIndicator from './UploadProgressIndicator';
77

88
import FileIcon from '../Attachment/FileIcon';
99

10+
import closeRound from '../../images/icons/close-round.png';
1011
import { themed } from '../../styles/theme';
1112
import { FileState, ProgressIndicatorTypes } from '../../utils/utils';
1213

@@ -39,6 +40,25 @@ const Container = styled.View`
3940
${({ theme }) => theme.messageInput.fileUploadPreview.container.css};
4041
`;
4142

43+
const Dismiss = styled.TouchableOpacity`
44+
align-items: center;
45+
background-color: #fff;
46+
border-radius: 20px;
47+
height: 20px;
48+
justify-content: center;
49+
position: absolute;
50+
right: 5;
51+
top: 5;
52+
width: 20px;
53+
${({ theme }) => theme.messageInput.fileUploadPreview.dismiss.css};
54+
`;
55+
56+
const DismissImage = styled.Image`
57+
height: 10px;
58+
width: 10px;
59+
${({ theme }) => theme.messageInput.fileUploadPreview.dismissImage.css};
60+
`;
61+
4262
const FilenameText = styled.Text`
4363
padding-left: 10px;
4464
${({ theme }) => theme.messageInput.fileUploadPreview.filenameText.css};
@@ -68,27 +88,38 @@ const FileUploadPreview = ({
6888
}
6989

7090
return (
71-
<UploadProgressIndicator
72-
action={() => {
73-
retryUpload ? retryUpload(item.id) : null;
74-
}}
75-
active={item.state !== FileState.UPLOADED}
76-
cancel={() => {
77-
removeFile ? removeFile(item.id) : null;
78-
}}
79-
type={type}
80-
>
81-
<AttachmentContainerView>
82-
<AttachmentView>
83-
<AttachmentFileIcon mimeType={item.file.type} size={20} />
84-
<FilenameText>
85-
{item.file.name.length > 35
86-
? item.file.name.substring(0, 35).concat('...')
87-
: item.file.name}
88-
</FilenameText>
89-
</AttachmentView>
90-
</AttachmentContainerView>
91-
</UploadProgressIndicator>
91+
<>
92+
<UploadProgressIndicator
93+
action={() => {
94+
if (retryUpload) {
95+
retryUpload(item.id);
96+
}
97+
}}
98+
active={item.state !== FileState.UPLOADED}
99+
type={type}
100+
>
101+
<AttachmentContainerView>
102+
<AttachmentView>
103+
<AttachmentFileIcon mimeType={item.file.type} size={20} />
104+
<FilenameText>
105+
{item.file.name.length > 35
106+
? item.file.name.substring(0, 35).concat('...')
107+
: item.file.name}
108+
</FilenameText>
109+
</AttachmentView>
110+
</AttachmentContainerView>
111+
</UploadProgressIndicator>
112+
<Dismiss
113+
onPress={() => {
114+
if (removeFile) {
115+
removeFile(item.id);
116+
}
117+
}}
118+
testID='remove-file-upload-preview'
119+
>
120+
<DismissImage source={closeRound} />
121+
</Dismiss>
122+
</>
92123
);
93124
};
94125

src/components/MessageInput/ImageUploadPreview.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ const ImageUploadPreview = ({ imageUploads, removeImage, retryUpload }) => {
6969
return (
7070
<ItemContainer>
7171
<UploadProgressIndicator
72-
action={() => (retryUpload ? retryUpload(item.id) : null)}
72+
action={() => {
73+
if (retryUpload) {
74+
retryUpload(item.id);
75+
}
76+
}}
7377
active={item.state !== FileState.UPLOADED}
7478
type={type}
7579
>
@@ -79,7 +83,11 @@ const ImageUploadPreview = ({ imageUploads, removeImage, retryUpload }) => {
7983
/>
8084
</UploadProgressIndicator>
8185
<Dismiss
82-
onPress={() => removeImage(item.id)}
86+
onPress={() => {
87+
if (removeImage) {
88+
removeImage(item.id);
89+
}
90+
}}
8391
testID='remove-image-upload-preview'
8492
>
8593
<DismissImage source={closeRound} />

src/components/MessageInput/MessageInput.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { logChatPromiseExecution } from 'stream-chat';
88

99
import ActionSheetAttachmentDefault from './ActionSheetAttachment';
1010
import AttachButtonDefault from './AttachButton';
11-
import FileUploadPreview from './FileUploadPreview';
12-
import ImageUploadPreview from './ImageUploadPreview';
11+
import FileUploadPreviewDefault from './FileUploadPreview';
12+
import ImageUploadPreviewDefault from './ImageUploadPreview';
1313
import SendButtonDefault from './SendButton';
1414

1515
import { useMessageDetailsForState } from './hooks/useMessageDetailsForState';
@@ -123,8 +123,10 @@ const MessageInput = (props) => {
123123
compressImageQuality,
124124
doDocUploadRequest,
125125
doImageUploadRequest,
126+
FileUploadPreview = FileUploadPreviewDefault,
126127
hasFilePicker = true,
127128
hasImagePicker = true,
129+
ImageUploadPreview = ImageUploadPreviewDefault,
128130
initialValue,
129131
Input,
130132
maxNumberOfFiles,
@@ -870,10 +872,26 @@ MessageInput.propTypes = {
870872
* @param channel Current channel object
871873
* */
872874
doImageUploadRequest: PropTypes.func,
875+
/**
876+
* Custom UI component for FileUploadPreview.
877+
* Defaults to and accepts same props as: https://github.com/GetStream/stream-chat-react-native/blob/master/src/components/MessageInput/FileUploadPreview.js
878+
*/
879+
FileUploadPreview: PropTypes.oneOfType([
880+
PropTypes.node,
881+
PropTypes.elementType,
882+
]),
873883
/** If component should have file picker functionality */
874884
hasFilePicker: PropTypes.bool,
875885
/** If component should have image picker functionality */
876886
hasImagePicker: PropTypes.bool,
887+
/**
888+
* Custom UI component for ImageUploadPreview.
889+
* Defaults to and accepts same props as: https://github.com/GetStream/stream-chat-react-native/blob/master/src/components/MessageInput/ImageUploadPreview.js
890+
*/
891+
ImageUploadPreview: PropTypes.oneOfType([
892+
PropTypes.node,
893+
PropTypes.elementType,
894+
]),
877895
/** Initial value to set on input */
878896
initialValue: PropTypes.string,
879897
/** Limit on allowed number of files to attach at a time. */

src/components/MessageInput/UploadProgressIndicator.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ const ActivityIndicatorContainer = styled.View`
3838
top: 0;
3939
`;
4040

41-
const DismissText = styled.Text`
42-
align-self: flex-end;
43-
margin-right: 10px;
44-
${({ theme }) => theme.messageInput.uploadProgressIndicator.dismiss.css};
45-
`;
46-
4741
const RetryButtonContainer = styled.TouchableOpacity`
4842
align-items: center;
4943
bottom: 0;
@@ -54,7 +48,7 @@ const RetryButtonContainer = styled.TouchableOpacity`
5448
top: 0;
5549
`;
5650

57-
const UploadProgressIndicator = ({ action, active, cancel, children, type }) =>
51+
const UploadProgressIndicator = ({ action, active, children, type }) =>
5852
!active ? (
5953
<View testID='inactive-upload-progress-indicator'>{children}</View>
6054
) : (
@@ -63,7 +57,7 @@ const UploadProgressIndicator = ({ action, active, cancel, children, type }) =>
6357
<Overlay />
6458
<Container>
6559
{type === ProgressIndicatorTypes.IN_PROGRESS && (
66-
<ActivityIndicatorContainer style={{}}>
60+
<ActivityIndicatorContainer>
6761
<ActivityIndicator
6862
color='grey'
6963
testID='upload-progress-indicator'
@@ -79,20 +73,15 @@ const UploadProgressIndicator = ({ action, active, cancel, children, type }) =>
7973
/>
8074
</RetryButtonContainer>
8175
)}
82-
<DismissText onPress={cancel} testID='remove-file-upload-preview'>
83-
X
84-
</DismissText>
8576
</Container>
8677
</View>
8778
);
8879

8980
UploadProgressIndicator.propTypes = {
9081
/** Action triggered when clicked indicator */
9182
action: PropTypes.func,
92-
active: PropTypes.bool,
93-
/** Action triggered when clicked cancel button */
94-
cancel: PropTypes.func,
9583
/** Boolean status of upload progress */
84+
active: PropTypes.bool,
9685
/** Type of active indicator */
9786
type: PropTypes.oneOf([
9887
ProgressIndicatorTypes.IN_PROGRESS,

src/components/MessageInput/__tests__/FileUploadPreview.test.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,6 @@ describe('FileUploadPreview', () => {
4949
expect(retryUpload).toHaveBeenCalledTimes(0);
5050
});
5151

52-
fireEvent.press(getAllByTestId('active-upload-progress-indicator')[0]);
53-
54-
await waitFor(() => {
55-
expect(removeFile).toHaveBeenCalledTimes(1);
56-
expect(retryUpload).toHaveBeenCalledTimes(1);
57-
});
58-
5952
rerender(
6053
<FileUploadPreview
6154
fileUploads={fileUploads.map((file, index) => ({
@@ -170,7 +163,7 @@ describe('FileUploadPreview', () => {
170163
expect(retryUpload).toHaveBeenCalledTimes(0);
171164
});
172165

173-
fireEvent.press(getAllByTestId('active-upload-progress-indicator')[0]);
166+
fireEvent.press(getAllByTestId('retry-upload-progress-indicator')[0]);
174167

175168
await waitFor(() => {
176169
expect(removeFile).toHaveBeenCalledTimes(1);

src/components/MessageInput/__tests__/ImageUploadPreview.test.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,6 @@ describe('ImageUploadPreview', () => {
4949
expect(retryUpload).toHaveBeenCalledTimes(0);
5050
});
5151

52-
fireEvent.press(getAllByTestId('active-upload-progress-indicator')[0]);
53-
54-
await waitFor(() => {
55-
expect(removeImage).toHaveBeenCalledTimes(1);
56-
expect(retryUpload).toHaveBeenCalledTimes(1);
57-
});
58-
5952
rerender(
6053
<ImageUploadPreview
6154
imageUploads={imageUploads.map((image, index) => ({
@@ -170,7 +163,7 @@ describe('ImageUploadPreview', () => {
170163
expect(retryUpload).toHaveBeenCalledTimes(0);
171164
});
172165

173-
fireEvent.press(getAllByTestId('active-upload-progress-indicator')[0]);
166+
fireEvent.press(getAllByTestId('retry-upload-progress-indicator')[0]);
174167

175168
await waitFor(() => {
176169
expect(removeImage).toHaveBeenCalledTimes(1);

src/components/MessageInput/__tests__/UploadProgressIndicator.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('UploadProgressIndicator', () => {
5555
it('should render an active IN_PROGRESS UploadProgressIndicator', async () => {
5656
const action = jest.fn();
5757

58-
const { getByTestId, queryByTestId, toJSON } = render(
58+
const { queryByTestId, toJSON } = render(
5959
<UploadProgressIndicator
6060
action={action}
6161
active
@@ -66,13 +66,11 @@ describe('UploadProgressIndicator', () => {
6666
await waitFor(() => {
6767
expect(queryByTestId('active-upload-progress-indicator')).toBeTruthy();
6868
expect(queryByTestId('inactive-upload-progress-indicator')).toBeFalsy();
69+
expect(queryByTestId('upload-progress-indicator')).toBeTruthy();
70+
expect(queryByTestId('retry-upload-progress-indicator')).toBeFalsy();
6971
expect(action).toHaveBeenCalledTimes(0);
7072
});
7173

72-
fireEvent.press(getByTestId('active-upload-progress-indicator'));
73-
74-
await waitFor(() => expect(action).toHaveBeenCalledTimes(1));
75-
7674
const snapshot = toJSON();
7775

7876
await waitFor(() => {
@@ -94,10 +92,12 @@ describe('UploadProgressIndicator', () => {
9492
await waitFor(() => {
9593
expect(queryByTestId('active-upload-progress-indicator')).toBeTruthy();
9694
expect(queryByTestId('inactive-upload-progress-indicator')).toBeFalsy();
95+
expect(queryByTestId('upload-progress-indicator')).toBeFalsy();
96+
expect(queryByTestId('retry-upload-progress-indicator')).toBeTruthy();
9797
expect(action).toHaveBeenCalledTimes(0);
9898
});
9999

100-
fireEvent.press(getByTestId('active-upload-progress-indicator'));
100+
fireEvent.press(getByTestId('retry-upload-progress-indicator'));
101101

102102
await waitFor(() => expect(action).toHaveBeenCalledTimes(1));
103103

0 commit comments

Comments
 (0)