@supportworks/multi-imagepicker 8.4.10 → 8.4.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/Photo-operation-test.js +188 -0
- package/changes.md +3 -0
- package/package.json +1 -1
- package/src/components/MultiImagePicker.js +28 -19
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/* global beforeEach, afterEach, describe, expect, it, jest */
|
|
2
|
+
import React from 'react'
|
|
3
|
+
import renderer, { act } from 'react-test-renderer'
|
|
4
|
+
import { Alert, Platform } from 'react-native'
|
|
5
|
+
import ImagePicker from 'react-native-image-crop-picker'
|
|
6
|
+
import ImageResizer from '@bam.tech/react-native-image-resizer'
|
|
7
|
+
import { showLoadingOverlay, hideLoadingOverlay } from '@supportworks/navbars'
|
|
8
|
+
import { check } from 'react-native-permissions'
|
|
9
|
+
import MultiImagePicker from '@supportworks/multi-imagepicker/src/components/MultiImagePicker'
|
|
10
|
+
import { isPhotoOperationActive } from '@supportworks/multi-imagepicker/src/components/photoOperation'
|
|
11
|
+
|
|
12
|
+
jest.mock('react-native', () => ({
|
|
13
|
+
AppState: { currentState: 'active' },
|
|
14
|
+
View: 'View',
|
|
15
|
+
TouchableOpacity: 'TouchableOpacity',
|
|
16
|
+
Modal: 'Modal',
|
|
17
|
+
Alert: { alert: jest.fn() },
|
|
18
|
+
Platform: { OS: 'ios', Version: 26, select: values => values.ios },
|
|
19
|
+
Linking: { openURL: jest.fn() }
|
|
20
|
+
}))
|
|
21
|
+
jest.mock('@supportworks/core-controls-ui', () => ({ ActionSheet: 'ActionSheet' }))
|
|
22
|
+
jest.mock('react-native-image-crop-picker', () => ({ openPicker: jest.fn(), openCamera: jest.fn() }))
|
|
23
|
+
jest.mock('@bam.tech/react-native-image-resizer', () => ({ createResizedImage: jest.fn() }))
|
|
24
|
+
jest.mock('@supportworks/api-core', () => ({ Api: { getApi: () => ({ moveLocalFile: jest.fn().mockResolvedValue() }) } }))
|
|
25
|
+
jest.mock('@supportworks/navbars', () => ({ showLoadingOverlay: jest.fn(), hideLoadingOverlay: jest.fn() }))
|
|
26
|
+
jest.mock('react-native-permissions', () => ({
|
|
27
|
+
check: jest.fn(),
|
|
28
|
+
request: jest.fn(),
|
|
29
|
+
openSettings: jest.fn(),
|
|
30
|
+
PERMISSIONS: { IOS: { CAMERA: 'camera', PHOTO_LIBRARY: 'photos' }, ANDROID: { CAMERA: 'camera' } },
|
|
31
|
+
RESULTS: { GRANTED: 'granted', DENIED: 'denied', BLOCKED: 'blocked' }
|
|
32
|
+
}))
|
|
33
|
+
jest.mock('@supportworks/multi-imagepicker/src/components/CameraModal', () => 'CameraModal')
|
|
34
|
+
|
|
35
|
+
const deferred = () => {
|
|
36
|
+
let resolve
|
|
37
|
+
const promise = new Promise(_resolve => { resolve = _resolve })
|
|
38
|
+
return { promise, resolve }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const selectPhotoOption = (sheet, index) => {
|
|
42
|
+
sheet.props.onSelect(sheet.props.options[index])
|
|
43
|
+
return sheet.props.onClose()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
describe('photo operation lifecycle', () => {
|
|
47
|
+
let component
|
|
48
|
+
let originalFrame
|
|
49
|
+
beforeEach(() => {
|
|
50
|
+
jest.clearAllMocks()
|
|
51
|
+
Platform.Version = 26
|
|
52
|
+
originalFrame = global.requestAnimationFrame
|
|
53
|
+
global.requestAnimationFrame = callback => callback()
|
|
54
|
+
check.mockResolvedValue('granted')
|
|
55
|
+
ImagePicker.openPicker.mockResolvedValue([{ path: '/photo.jpg' }])
|
|
56
|
+
ImageResizer.createResizedImage.mockResolvedValue({ path: '/resized.jpg', width: 1500, height: 2000 })
|
|
57
|
+
showLoadingOverlay.mockReturnValue('processing-token')
|
|
58
|
+
})
|
|
59
|
+
afterEach(async () => {
|
|
60
|
+
if (component) await act(async () => { component.unmount() })
|
|
61
|
+
component = null
|
|
62
|
+
global.requestAnimationFrame = originalFrame
|
|
63
|
+
expect(isPhotoOperationActive()).toBe(false)
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('opens from the imperative trigger and dismisses without starting a photo operation', async () => {
|
|
67
|
+
const pickerRef = React.createRef()
|
|
68
|
+
const callback = jest.fn()
|
|
69
|
+
await act(async () => { component = renderer.create(<MultiImagePicker ref={pickerRef} callback={callback} />) })
|
|
70
|
+
await act(async () => { pickerRef.current.showActionSheet() })
|
|
71
|
+
expect(component.root.findByType('ActionSheet').props.visible).toBe(true)
|
|
72
|
+
await act(async () => { await component.root.findByType('ActionSheet').props.onClose() })
|
|
73
|
+
expect(component.root.findByType('ActionSheet').props.visible).toBe(false)
|
|
74
|
+
expect(ImagePicker.openPicker).not.toHaveBeenCalled()
|
|
75
|
+
expect(check).not.toHaveBeenCalled()
|
|
76
|
+
expect(callback).not.toHaveBeenCalled()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('waits for dismissal before opening the library and handles duplicate dismissal once', async () => {
|
|
80
|
+
await act(async () => { component = renderer.create(<MultiImagePicker />) })
|
|
81
|
+
await act(async () => { component.root.findByType('TouchableOpacity').props.onPress() })
|
|
82
|
+
const sheet = component.root.findByType('ActionSheet')
|
|
83
|
+
await act(async () => { sheet.props.onSelect(sheet.props.options[1]) })
|
|
84
|
+
expect(ImagePicker.openPicker).not.toHaveBeenCalled()
|
|
85
|
+
expect(check).not.toHaveBeenCalled()
|
|
86
|
+
await act(async () => { await sheet.props.onClose() })
|
|
87
|
+
expect(ImagePicker.openPicker).toHaveBeenCalledTimes(1)
|
|
88
|
+
await act(async () => { await sheet.props.onClose() })
|
|
89
|
+
expect(ImagePicker.openPicker).toHaveBeenCalledTimes(1)
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
it('preserves custom option indexes and exposes Cancel without invoking the callback', async () => {
|
|
93
|
+
const callback = jest.fn()
|
|
94
|
+
await act(async () => {
|
|
95
|
+
component = renderer.create(<MultiImagePicker customButtons={['Import Inspection Photos']} callback={callback} />)
|
|
96
|
+
})
|
|
97
|
+
expect(component.root.findByType('ActionSheet').props.options.map(option => option.title)).toEqual([
|
|
98
|
+
'Import Inspection Photos', 'Take Photo...', 'Choose from Library...', 'Cancel'
|
|
99
|
+
])
|
|
100
|
+
await act(async () => { await selectPhotoOption(component.root.findByType('ActionSheet'), 3) })
|
|
101
|
+
expect(callback).not.toHaveBeenCalled()
|
|
102
|
+
await act(async () => { await selectPhotoOption(component.root.findByType('ActionSheet'), 0) })
|
|
103
|
+
expect(callback).toHaveBeenCalledWith(expect.objectContaining({ index: 0, cancelIndex: 3, imageArray: [] }))
|
|
104
|
+
expect(ImagePicker.openPicker).not.toHaveBeenCalled()
|
|
105
|
+
expect(check).not.toHaveBeenCalled()
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('blocks both repeated taps and other pickers through native selection, processing, and saving', async () => {
|
|
109
|
+
const selected = deferred()
|
|
110
|
+
const resized = deferred()
|
|
111
|
+
const saved = deferred()
|
|
112
|
+
ImagePicker.openPicker.mockReturnValueOnce(selected.promise)
|
|
113
|
+
ImageResizer.createResizedImage.mockReturnValueOnce(resized.promise)
|
|
114
|
+
const callback = jest.fn(() => saved.promise)
|
|
115
|
+
await act(async () => { component = renderer.create(<><MultiImagePicker callback={callback} /><MultiImagePicker callback={jest.fn()} /></>) })
|
|
116
|
+
let operation
|
|
117
|
+
await act(async () => { operation = selectPhotoOption(component.root.findAllByType('ActionSheet')[0], 1) })
|
|
118
|
+
await act(async () => {
|
|
119
|
+
await selectPhotoOption(component.root.findAllByType('ActionSheet')[0], 1)
|
|
120
|
+
await selectPhotoOption(component.root.findAllByType('ActionSheet')[1], 1)
|
|
121
|
+
})
|
|
122
|
+
expect(ImagePicker.openPicker).toHaveBeenCalledTimes(1)
|
|
123
|
+
expect(component.root.findAllByType('TouchableOpacity')[0].props.disabled).toBe(true)
|
|
124
|
+
await act(async () => { selected.resolve([{ path: '/photo.jpg' }]) })
|
|
125
|
+
expect(showLoadingOverlay).toHaveBeenCalledWith({ message: 'Processing Photos...' })
|
|
126
|
+
expect(isPhotoOperationActive()).toBe(true)
|
|
127
|
+
await act(async () => { resized.resolve({ path: '/resized.jpg', width: 1500, height: 2000 }) })
|
|
128
|
+
expect(callback).toHaveBeenCalledTimes(1)
|
|
129
|
+
expect(hideLoadingOverlay).not.toHaveBeenCalled()
|
|
130
|
+
await act(async () => { await selectPhotoOption(component.root.findAllByType('ActionSheet')[1], 1) })
|
|
131
|
+
expect(ImagePicker.openPicker).toHaveBeenCalledTimes(1)
|
|
132
|
+
await act(async () => { saved.resolve(); await operation })
|
|
133
|
+
expect(hideLoadingOverlay).toHaveBeenCalledWith('processing-token')
|
|
134
|
+
expect(isPhotoOperationActive()).toBe(false)
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
it('waits for camera dismissal, then keeps processing visible until the editor is ready', async () => {
|
|
138
|
+
const ready = deferred()
|
|
139
|
+
const callback = jest.fn(() => ready.promise)
|
|
140
|
+
await act(async () => { component = renderer.create(<MultiImagePicker callback={callback} />) })
|
|
141
|
+
let operation
|
|
142
|
+
await act(async () => { operation = selectPhotoOption(component.root.findByType('ActionSheet'), 0) })
|
|
143
|
+
await act(async () => { component.root.findByType('CameraModal').props.onDone([{ imageURI: '/taken.jpg' }]) })
|
|
144
|
+
expect(ImageResizer.createResizedImage).not.toHaveBeenCalled()
|
|
145
|
+
await act(async () => { component.root.findByType('Modal').props.onDismiss() })
|
|
146
|
+
expect(showLoadingOverlay).toHaveBeenCalled()
|
|
147
|
+
expect(callback.mock.calls[0][0]).toMatchObject({ imageSource: 'taken', imageArray: [{ path: '/taken.jpg', width: 1500, height: 2000 }] })
|
|
148
|
+
expect(hideLoadingOverlay).not.toHaveBeenCalled()
|
|
149
|
+
await act(async () => { ready.resolve(); await operation })
|
|
150
|
+
expect(hideLoadingOverlay).toHaveBeenCalledWith('processing-token')
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
it.each(['cancel', 'permission', 'resize', 'save'])('releases the guard after %s without leaving a loading overlay', async failure => {
|
|
154
|
+
if (failure === 'cancel') ImagePicker.openPicker.mockRejectedValue({ code: 'E_PICKER_CANCELLED' })
|
|
155
|
+
if (failure === 'permission') check.mockResolvedValue('blocked')
|
|
156
|
+
if (failure === 'resize') ImageResizer.createResizedImage.mockRejectedValue(new Error('resize failed'))
|
|
157
|
+
const callback = jest.fn(failure === 'save' ? () => Promise.reject(new Error('save failed')) : () => Promise.resolve())
|
|
158
|
+
await act(async () => { component = renderer.create(<MultiImagePicker callback={callback} />) })
|
|
159
|
+
await act(async () => { await selectPhotoOption(component.root.findByType('ActionSheet'), 1) })
|
|
160
|
+
expect(isPhotoOperationActive()).toBe(false)
|
|
161
|
+
if (failure === 'cancel') expect(Alert.alert).not.toHaveBeenCalled()
|
|
162
|
+
if (failure === 'resize' || failure === 'save') expect(hideLoadingOverlay).toHaveBeenCalledWith('processing-token')
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
it('keeps captured photos when the legacy camera is canceled and waits for saving', async () => {
|
|
166
|
+
Platform.Version = 25
|
|
167
|
+
ImagePicker.openCamera.mockResolvedValueOnce({ path: '/legacy.jpg' }).mockRejectedValueOnce({ code: 'E_PICKER_CANCELLED' })
|
|
168
|
+
const saved = deferred()
|
|
169
|
+
const callback = jest.fn(() => saved.promise)
|
|
170
|
+
await act(async () => { component = renderer.create(<MultiImagePicker callback={callback} />) })
|
|
171
|
+
let operation
|
|
172
|
+
await act(async () => { operation = selectPhotoOption(component.root.findByType('ActionSheet'), 0) })
|
|
173
|
+
expect(callback.mock.calls[0][0]).toMatchObject({ imageSource: 'taken', imageArray: [{ path: '/legacy.jpg' }] })
|
|
174
|
+
expect(isPhotoOperationActive()).toBe(true)
|
|
175
|
+
await act(async () => { saved.resolve(); await operation })
|
|
176
|
+
expect(isPhotoOperationActive()).toBe(false)
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
it('releases a camera operation if its parent unmounts before Done', async () => {
|
|
180
|
+
await act(async () => { component = renderer.create(<MultiImagePicker callback={jest.fn()} />) })
|
|
181
|
+
let operation
|
|
182
|
+
await act(async () => { operation = selectPhotoOption(component.root.findByType('ActionSheet'), 0) })
|
|
183
|
+
await act(async () => { component.unmount() })
|
|
184
|
+
await operation
|
|
185
|
+
component = null
|
|
186
|
+
expect(isPhotoOperationActive()).toBe(false)
|
|
187
|
+
})
|
|
188
|
+
})
|
package/changes.md
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
# v8.4.11 2026-11-14
|
|
2
|
+
* DEV-10323: Use the shared Add Photos action sheet with Close/Cancel; start camera, library, and custom actions after dismissal.
|
|
3
|
+
|
|
1
4
|
# Unreleased
|
|
2
5
|
* DEV-10192: Protect the full-screen selected-media preview from Android navigation buttons/taskbar. Keep the annotation wrapper top-only because Canvas owns Samsung bottom padding; the attachment editor and image-details modal already protect both edges.
|
|
3
6
|
|
package/package.json
CHANGED
|
@@ -8,7 +8,7 @@ import {
|
|
|
8
8
|
Linking,
|
|
9
9
|
Modal
|
|
10
10
|
} from 'react-native'
|
|
11
|
-
import ActionSheet from '@
|
|
11
|
+
import { ActionSheet } from '@supportworks/core-controls-ui'
|
|
12
12
|
import ImagePicker from 'react-native-image-crop-picker'
|
|
13
13
|
import ImageResizer from '@bam.tech/react-native-image-resizer'
|
|
14
14
|
import { Api } from '@supportworks/api-core'
|
|
@@ -53,12 +53,12 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
53
53
|
const [showCameraModal, setShowCameraModal] = useState(false)
|
|
54
54
|
const [isCameraModalMounted, setIsCameraModalMounted] = useState(false)
|
|
55
55
|
const [isBusy, setIsBusy] = useState(false)
|
|
56
|
-
const [
|
|
56
|
+
const [showPhotoOptions, setShowPhotoOptions] = useState(false)
|
|
57
57
|
|
|
58
58
|
const imgArrRef = useRef([]) // temp array for holding selected images to attach, before title & desc updated
|
|
59
59
|
const imgDescArrRef = useRef([]) // temp array for holding descriptions of selected images, before saved to project
|
|
60
60
|
const imgValidArrRef = useRef([]) // temp array for holding boolean value specifying whether each image title is valid or not
|
|
61
|
-
const
|
|
61
|
+
const pendingPhotoOptionRef = useRef(null)
|
|
62
62
|
const pendingCameraResultRef = useRef([])
|
|
63
63
|
const cameraResultResolverRef = useRef(null)
|
|
64
64
|
const isMountedRef = useRef(true)
|
|
@@ -94,8 +94,8 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
94
94
|
)
|
|
95
95
|
|
|
96
96
|
const resetActionSheet = useCallback(() => {
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
pendingPhotoOptionRef.current = null
|
|
98
|
+
setShowPhotoOptions(false)
|
|
99
99
|
}, [])
|
|
100
100
|
|
|
101
101
|
const showSelectionLimitAlert = useCallback(() => {
|
|
@@ -369,16 +369,15 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
369
369
|
}
|
|
370
370
|
}, [checkAndOpenPicker, pickerDone, remainingCount, requestCameraPermission, resetActionSheet, showPermissionAlert, showSelectionLimitAlert, takePictures])
|
|
371
371
|
|
|
372
|
-
const
|
|
372
|
+
const handleOpenPhotoOptions = useCallback(() => {
|
|
373
373
|
if (isPhotoOperationActive() || touchableProps.disabled) return
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
}
|
|
374
|
+
pendingPhotoOptionRef.current = null
|
|
375
|
+
setShowPhotoOptions(true)
|
|
377
376
|
}, [touchableProps.disabled])
|
|
378
377
|
|
|
379
378
|
useImperativeHandle(ref, () => ({
|
|
380
|
-
showActionSheet
|
|
381
|
-
}), [
|
|
379
|
+
showActionSheet: handleOpenPhotoOptions
|
|
380
|
+
}), [handleOpenPhotoOptions])
|
|
382
381
|
|
|
383
382
|
const buttonArr = useMemo(
|
|
384
383
|
() => [...customButtons, useCamera, useLibrary, 'Cancel'],
|
|
@@ -386,19 +385,29 @@ const MultiImagePicker = forwardRef((props, ref) => {
|
|
|
386
385
|
)
|
|
387
386
|
const cancelIndex = buttonArr.length - 1
|
|
388
387
|
|
|
388
|
+
const handleSelectPhotoOption = useCallback((option) => {
|
|
389
|
+
if (isPhotoOperationActive() || pendingPhotoOptionRef.current !== null) return
|
|
390
|
+
pendingPhotoOptionRef.current = option.index
|
|
391
|
+
}, [])
|
|
392
|
+
|
|
393
|
+
const handleClosePhotoOptions = useCallback(() => {
|
|
394
|
+
const index = pendingPhotoOptionRef.current
|
|
395
|
+
pendingPhotoOptionRef.current = null
|
|
396
|
+
setShowPhotoOptions(false)
|
|
397
|
+
if (index !== null) return handleUsePicker(index, cancelIndex)
|
|
398
|
+
}, [handleUsePicker, cancelIndex])
|
|
399
|
+
|
|
389
400
|
return (
|
|
390
401
|
<View style={parentStyle}>
|
|
391
402
|
<ActionSheet
|
|
392
|
-
|
|
393
|
-
ref={(node) => {
|
|
394
|
-
actionSheetRef.current = node
|
|
395
|
-
}}
|
|
403
|
+
visible={showPhotoOptions}
|
|
396
404
|
title={title}
|
|
397
|
-
options={buttonArr}
|
|
398
|
-
|
|
399
|
-
|
|
405
|
+
options={buttonArr.map((title, index) => ({ title, index }))}
|
|
406
|
+
showCheckmark={false}
|
|
407
|
+
onSelect={handleSelectPhotoOption}
|
|
408
|
+
onClose={handleClosePhotoOptions}
|
|
400
409
|
/>
|
|
401
|
-
<TouchableOpacity {...touchableProps} disabled={touchableProps.disabled || isBusy} onPress={
|
|
410
|
+
<TouchableOpacity {...touchableProps} disabled={touchableProps.disabled || isBusy} onPress={handleOpenPhotoOptions}>
|
|
402
411
|
{children}
|
|
403
412
|
</TouchableOpacity>
|
|
404
413
|
{Platform.OS === 'ios' && isCameraModalMounted && (
|