-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathuseChooseMedia.test.tsx
More file actions
137 lines (114 loc) · 4.4 KB
/
Copy pathuseChooseMedia.test.tsx
File metadata and controls
137 lines (114 loc) · 4.4 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
import { renderHook, waitFor } from '@testing-library/react-native';
import { useChooseMedia } from './useChooseMedia';
jest.mock('expo-document-picker', () => ({
getDocumentAsync: jest.fn()
}));
jest.mock('../../../lib/hooks/useAppSelector', () => ({
useAppSelector: jest.fn()
}));
jest.mock('../context', () => ({
useMessageComposerApi: jest.fn()
}));
jest.mock('../../../views/RoomView/context', () => ({
useRoomContext: jest.fn()
}));
jest.mock('../../../views/RoomView/InteractionStore', () => ({
useMessageAction: jest.fn(),
useSelectedMessages: jest.fn()
}));
jest.mock('../../../lib/hooks/useAltTextSupported', () => ({
useAltTextSupported: jest.fn()
}));
jest.mock('../../../lib/database/services/Subscription', () => ({
getSubscriptionByRoomId: jest.fn()
}));
jest.mock('../../../lib/database/services/Thread', () => ({
getThreadById: jest.fn()
}));
jest.mock('../../../lib/navigation/appNavigation', () => ({
navigate: jest.fn()
}));
jest.mock('../../../lib/methods/helpers/ImagePicker/ImagePicker', () => ({
__esModule: true,
default: {
openCamera: jest.fn(),
openPicker: jest.fn()
}
}));
const mockGetDocumentAsync = require('expo-document-picker').getDocumentAsync as jest.Mock;
const mockUseAppSelector = require('../../../lib/hooks/useAppSelector').useAppSelector as jest.Mock;
const mockUseMessageComposerApi = require('../context').useMessageComposerApi as jest.Mock;
const mockUseRoomContext = require('../../../views/RoomView/context').useRoomContext as jest.Mock;
const mockUseMessageAction = require('../../../views/RoomView/InteractionStore').useMessageAction as jest.Mock;
const mockUseSelectedMessages = require('../../../views/RoomView/InteractionStore').useSelectedMessages as jest.Mock;
const mockUseAltTextSupported = require('../../../lib/hooks/useAltTextSupported').useAltTextSupported as jest.Mock;
const mockGetSubscriptionByRoomId = require('../../../lib/database/services/Subscription').getSubscriptionByRoomId as jest.Mock;
const mockGetThreadById = require('../../../lib/database/services/Thread').getThreadById as jest.Mock;
const mockNavigate = require('../../../lib/navigation/appNavigation').navigate as jest.Mock;
describe('useChooseMedia', () => {
const addAttachments = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
mockUseAppSelector.mockImplementation(selector =>
selector({
settings: {
FileUpload_MediaTypeWhiteList: '*',
FileUpload_MaxFileSize: 1000
}
})
);
mockUseMessageComposerApi.mockReturnValue({ addAttachments });
mockUseRoomContext.mockReturnValue({
setQuotesAndText: jest.fn(),
getText: jest.fn(() => 'draft')
});
mockUseMessageAction.mockReturnValue(null);
mockUseSelectedMessages.mockReturnValue([]);
mockGetSubscriptionByRoomId.mockResolvedValue({ rid: 'room-id', t: 'c' });
mockGetThreadById.mockResolvedValue({ id: 'thread-id' });
});
it('opens ShareView on servers below 8.4', async () => {
mockUseAltTextSupported.mockReturnValue(false);
mockGetDocumentAsync.mockResolvedValue({
canceled: false,
assets: [{ name: 'legacy.pdf', size: 12, mimeType: 'application/pdf', uri: 'file:///tmp/legacy.pdf' }]
});
const { result } = renderHook(() => useChooseMedia({ rid: 'room-id', tmid: 'thread-id', permissionToUpload: true }));
await result.current.chooseFile();
await waitFor(() => {
expect(mockNavigate).toHaveBeenCalledWith(
'ShareView',
expect.objectContaining({
room: expect.objectContaining({ rid: 'room-id' }),
thread: expect.objectContaining({ id: 'thread-id' }),
attachments: [
expect.objectContaining({
filename: 'legacy.pdf',
path: 'file:///tmp/legacy.pdf'
})
]
})
);
});
expect(addAttachments).not.toHaveBeenCalled();
});
it('keeps inline composer attachments on servers 8.4 and above', async () => {
mockUseAltTextSupported.mockReturnValue(true);
mockGetDocumentAsync.mockResolvedValue({
canceled: false,
assets: [{ name: 'modern.pdf', size: 12, mimeType: 'application/pdf', uri: 'file:///tmp/modern.pdf' }]
});
const { result } = renderHook(() => useChooseMedia({ rid: 'room-id', tmid: 'thread-id', permissionToUpload: true }));
await result.current.chooseFile();
await waitFor(() => {
expect(addAttachments).toHaveBeenCalledWith([
expect.objectContaining({
filename: 'modern.pdf',
path: 'file:///tmp/modern.pdf',
canUpload: true
})
]);
});
expect(mockNavigate).not.toHaveBeenCalled();
});
});