-
Notifications
You must be signed in to change notification settings - Fork 375
Expand file tree
/
Copy pathImageGallery.test.tsx
More file actions
218 lines (187 loc) · 6.56 KB
/
ImageGallery.test.tsx
File metadata and controls
218 lines (187 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import React from 'react';
import type { SharedValue } from 'react-native-reanimated';
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react-native';
import {
ImageGalleryContext,
ImageGalleryContextValue,
} from '../../../contexts/imageGalleryContext/ImageGalleryContext';
import { OverlayProvider } from '../../../contexts/overlayContext/OverlayProvider';
import { ThemeProvider } from '../../../contexts/themeContext/ThemeContext';
import { defaultTheme } from '../../../contexts/themeContext/utils/theme';
import {
generateGiphyAttachment,
generateImageAttachment,
generateVideoAttachment,
} from '../../../mock-builders/generator/attachment';
import { generateMessage } from '../../../mock-builders/generator/message';
import type { DefaultStreamChatGenerics } from '../../../types/types';
import type { MessageType } from '../../MessageList/hooks/useMessageList';
import { ImageGallery } from '../ImageGallery';
jest.mock('../../../native.ts', () => {
const View = require('react-native/Libraries/Components/View/View');
return {
isFileSystemAvailable: jest.fn(() => true),
isImageMediaLibraryAvailable: jest.fn(() => true),
isShareImageAvailable: jest.fn(() => true),
isVideoPlayerAvailable: jest.fn(() => true),
NativeHandlers: {
Video: View,
},
};
});
const getComponent = (props: Partial<ImageGalleryContextValue>) => (
<OverlayProvider>
<ImageGalleryContext.Provider value={{ ...(props as unknown as ImageGalleryContextValue) }}>
<ThemeProvider theme={defaultTheme}>
<ImageGallery overlayOpacity={{ value: 1 } as SharedValue<number>} />
</ThemeProvider>
</ImageGalleryContext.Provider>
</OverlayProvider>
);
describe('ImageGallery', () => {
it('render image gallery component', async () => {
render(
getComponent({
messages: [
generateMessage({
attachments: [
generateImageAttachment(),
generateGiphyAttachment(),
generateVideoAttachment({ type: 'video' }),
],
}),
] as unknown as MessageType<DefaultStreamChatGenerics>[],
}),
);
await waitFor(() => {
expect(screen.queryAllByLabelText('Image Item')).toHaveLength(2);
expect(screen.queryAllByLabelText('Image Gallery Video')).toHaveLength(1);
});
});
it('handle handleLoad function when video item present and payload duration is available', async () => {
const attachment = generateVideoAttachment({ type: 'video' });
const message = generateMessage({
attachments: [attachment],
});
render(
getComponent({
messages: [message] as unknown as MessageType<DefaultStreamChatGenerics>[],
}),
);
const videoItemComponent = screen.getByLabelText('Image Gallery Video');
act(() => {
fireEvent(
videoItemComponent,
'handleLoad',
`photoId-${message.id}-${attachment.asset_url}`,
10 * 1000,
);
});
const videoDurationComponent = screen.getByLabelText('Video Duration');
await waitFor(() => {
expect(videoDurationComponent.children[0]).toBe('00:10');
});
});
it('handle handleLoad function when video item present and payload duration is undefined', async () => {
render(
getComponent({
messages: [
generateMessage({
attachments: [generateVideoAttachment({ type: 'video' })],
}),
] as unknown as MessageType<DefaultStreamChatGenerics>[],
}),
);
const videoItemComponent = screen.getByLabelText('Image Gallery Video');
act(() => {
fireEvent(videoItemComponent, 'handleLoad', {
duration: undefined,
});
});
const videoDurationComponent = screen.getByLabelText('Video Duration');
await waitFor(() => {
expect(videoDurationComponent.children[0]).toBe('00:00');
});
});
it('handle handleProgress function when video item present and payload is well defined', async () => {
const attachment = generateVideoAttachment({ type: 'video' });
const message = generateMessage({
attachments: [attachment],
});
render(
getComponent({
messages: [message] as unknown as MessageType<DefaultStreamChatGenerics>[],
}),
);
const videoItemComponent = screen.getByLabelText('Image Gallery Video');
act(() => {
fireEvent(
videoItemComponent,
'handleLoad',
`photoId-${message.id}-${attachment.asset_url}`,
10,
);
fireEvent(
videoItemComponent,
'handleProgress',
`photoId-${message.id}-${attachment.asset_url}`,
0.3 * 1000,
);
});
const progressDurationComponent = screen.getByLabelText('Progress Duration');
await waitFor(() => {
expect(progressDurationComponent.children[0]).toBe('00:03');
});
});
it('handle handleProgress function when video item present and payload is not defined', async () => {
render(
getComponent({
messages: [
generateMessage({
attachments: [generateVideoAttachment({ type: 'video' })],
}),
] as unknown as MessageType<DefaultStreamChatGenerics>[],
}),
);
const videoItemComponent = screen.getByLabelText('Image Gallery Video');
act(() => {
fireEvent(videoItemComponent, 'handleLoad', {
duration: 10 * 1000,
});
fireEvent(videoItemComponent, 'handleProgress', {
currentTime: undefined,
seekableDuration: undefined,
});
});
const progressDurationComponent = screen.getByLabelText('Progress Duration');
await waitFor(() => {
expect(progressDurationComponent.children[0]).toBe('00:00');
});
});
it('handle handleEnd function when video item present', async () => {
const attachment = generateVideoAttachment({ type: 'video' });
const message = generateMessage({
attachments: [attachment],
});
render(
getComponent({
messages: [message] as unknown as MessageType<DefaultStreamChatGenerics>[],
}),
);
const videoItemComponent = screen.getByLabelText('Image Gallery Video');
act(() => {
fireEvent(
videoItemComponent,
'handleLoad',
`photoId-${message.id}-${attachment.asset_url}`,
10 * 1000,
);
fireEvent(videoItemComponent, 'handleEnd');
});
const progressDurationComponent = screen.getByLabelText('Progress Duration');
await waitFor(() => {
expect(screen.queryAllByLabelText('Play Icon').length).toBeGreaterThan(0);
expect(progressDurationComponent.children[0]).toBe('00:10');
});
});
});