@jobber/components-native 0.91.0 → 0.91.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jobber/components-native",
3
- "version": "0.91.0",
3
+ "version": "0.91.1",
4
4
  "license": "MIT",
5
5
  "description": "React Native implementation of Atlantis",
6
6
  "repository": {
@@ -94,5 +94,5 @@
94
94
  "react-native-safe-area-context": "^5.4.0",
95
95
  "react-native-svg": ">=12.0.0"
96
96
  },
97
- "gitHead": "29381f7508578c0518fd4cecd07d021df83e08fc"
97
+ "gitHead": "5f68cfee448c36f14f589edd6359c9cb30c8ab79"
98
98
  }
@@ -0,0 +1,283 @@
1
+ import React from "react";
2
+ import { fireEvent, render, waitFor } from "@testing-library/react-native";
3
+ import { MediaView } from "./MediaView";
4
+ import type { FormattedFile } from "../../types";
5
+ import { StatusCode } from "../../types";
6
+ import { AtlantisFormatFileContext } from "../../context/FormatFileContext";
7
+
8
+ jest.mock("../../../hooks/useAtlantisI18n", () => ({
9
+ useAtlantisI18n: () => ({ t: (key: string) => key }),
10
+ }));
11
+
12
+ describe("MediaView", () => {
13
+ const mockFile: FormattedFile = {
14
+ showPreview: true,
15
+ source: "https://example.com/image1.jpg",
16
+ thumbnailUrl: undefined,
17
+ name: "test.jpg",
18
+ size: 1024,
19
+ external: false,
20
+ progress: 0,
21
+ status: StatusCode.Completed,
22
+ error: false,
23
+ type: "image/jpeg",
24
+ isMedia: true,
25
+ showFileTypeIndicator: false,
26
+ };
27
+
28
+ const defaultProps = {
29
+ accessibilityLabel: "Test image",
30
+ showOverlay: false,
31
+ showError: false,
32
+ file: mockFile,
33
+ styleInGrid: false,
34
+ onUploadComplete: jest.fn(),
35
+ };
36
+
37
+ const mockContextValue = {
38
+ useCreateThumbnail: () => ({ thumbnail: undefined, error: false }),
39
+ };
40
+
41
+ const renderWithContext = (props = defaultProps) => {
42
+ return render(
43
+ <AtlantisFormatFileContext.Provider value={mockContextValue}>
44
+ <MediaView {...props} />
45
+ </AtlantisFormatFileContext.Provider>,
46
+ );
47
+ };
48
+
49
+ describe("Normal loading flow", () => {
50
+ it("shows loading indicator when onLoadStart fires", () => {
51
+ const { getByTestId, queryByTestId } = renderWithContext();
52
+ const image = getByTestId("test-image");
53
+
54
+ expect(queryByTestId("ActivityIndicator")).toBeNull();
55
+
56
+ fireEvent(image, "loadStart");
57
+
58
+ expect(queryByTestId("ActivityIndicator")).toBeTruthy();
59
+ });
60
+
61
+ it("hides loading indicator when onLoadEnd fires", () => {
62
+ const { getByTestId, queryByTestId } = renderWithContext();
63
+ const image = getByTestId("test-image");
64
+
65
+ fireEvent(image, "loadStart");
66
+ expect(queryByTestId("ActivityIndicator")).toBeTruthy();
67
+
68
+ fireEvent(image, "loadEnd");
69
+
70
+ expect(queryByTestId("ActivityIndicator")).toBeNull();
71
+ });
72
+ });
73
+
74
+ describe("Race condition handling (cached images)", () => {
75
+ it("does not get stuck loading when onLoadEnd fires before onLoadStart", () => {
76
+ const { getByTestId, queryByTestId } = renderWithContext();
77
+ const image = getByTestId("test-image");
78
+
79
+ // Simulate cached image: LoadEnd fires BEFORE LoadStart
80
+ fireEvent(image, "loadEnd");
81
+ expect(queryByTestId("ActivityIndicator")).toBeNull();
82
+
83
+ fireEvent(image, "loadStart");
84
+
85
+ expect(queryByTestId("ActivityIndicator")).toBeNull();
86
+ });
87
+
88
+ it("does not show infinite spinner when load events fire out of order", () => {
89
+ const { getByTestId, queryByTestId } = renderWithContext();
90
+ const image = getByTestId("test-image");
91
+
92
+ // Race condition scenario
93
+ fireEvent(image, "loadEnd");
94
+ fireEvent(image, "loadStart");
95
+ fireEvent(image, "loadEnd");
96
+
97
+ expect(queryByTestId("ActivityIndicator")).toBeNull();
98
+ });
99
+ });
100
+
101
+ describe("URI changes", () => {
102
+ it("shows loading indicator when URI changes to a new image", async () => {
103
+ const { getByTestId, queryByTestId, rerender } = renderWithContext();
104
+ const image = getByTestId("test-image");
105
+
106
+ // First image: simulate cached load (LoadEnd before LoadStart)
107
+ fireEvent(image, "loadEnd");
108
+ fireEvent(image, "loadStart");
109
+ expect(queryByTestId("ActivityIndicator")).toBeNull();
110
+
111
+ // Change URI to a new image
112
+ const newFile = {
113
+ ...mockFile,
114
+ source: "https://example.com/image2.jpg",
115
+ };
116
+
117
+ rerender(
118
+ <AtlantisFormatFileContext.Provider value={mockContextValue}>
119
+ <MediaView {...defaultProps} file={newFile} />
120
+ </AtlantisFormatFileContext.Provider>,
121
+ );
122
+
123
+ await waitFor(() => {
124
+ const updatedImage = getByTestId("test-image");
125
+
126
+ // New image starts loading
127
+ fireEvent(updatedImage, "loadStart");
128
+
129
+ expect(queryByTestId("ActivityIndicator")).toBeTruthy();
130
+ });
131
+ });
132
+
133
+ it("shows loading indicator on second URI change", async () => {
134
+ const { getByTestId, queryByTestId, rerender } = renderWithContext();
135
+
136
+ const image1 = getByTestId("test-image");
137
+ fireEvent(image1, "loadStart");
138
+ fireEvent(image1, "loadEnd");
139
+ expect(queryByTestId("ActivityIndicator")).toBeNull();
140
+
141
+ const file2 = { ...mockFile, source: "https://example.com/image2.jpg" };
142
+ rerender(
143
+ <AtlantisFormatFileContext.Provider value={mockContextValue}>
144
+ <MediaView {...defaultProps} file={file2} />
145
+ </AtlantisFormatFileContext.Provider>,
146
+ );
147
+
148
+ await waitFor(() => {
149
+ const image2 = getByTestId("test-image");
150
+ fireEvent(image2, "loadStart");
151
+
152
+ expect(queryByTestId("ActivityIndicator")).toBeTruthy();
153
+ });
154
+ });
155
+
156
+ it("shows loading indicator on third URI change", async () => {
157
+ const { getByTestId, queryByTestId, rerender } = renderWithContext();
158
+
159
+ const image1 = getByTestId("test-image");
160
+ fireEvent(image1, "loadStart");
161
+ fireEvent(image1, "loadEnd");
162
+
163
+ const file2 = { ...mockFile, source: "https://example.com/image2.jpg" };
164
+ rerender(
165
+ <AtlantisFormatFileContext.Provider value={mockContextValue}>
166
+ <MediaView {...defaultProps} file={file2} />
167
+ </AtlantisFormatFileContext.Provider>,
168
+ );
169
+ const image2 = getByTestId("test-image");
170
+ fireEvent(image2, "loadEnd");
171
+
172
+ const file3 = { ...mockFile, source: "https://example.com/image3.jpg" };
173
+ rerender(
174
+ <AtlantisFormatFileContext.Provider value={mockContextValue}>
175
+ <MediaView {...defaultProps} file={file3} />
176
+ </AtlantisFormatFileContext.Provider>,
177
+ );
178
+
179
+ await waitFor(() => {
180
+ const image3 = getByTestId("test-image");
181
+ fireEvent(image3, "loadStart");
182
+
183
+ expect(queryByTestId("ActivityIndicator")).toBeTruthy();
184
+ });
185
+ });
186
+
187
+ it("handles URI change from cached to uncached image correctly", async () => {
188
+ const { getByTestId, queryByTestId, rerender } = renderWithContext();
189
+
190
+ // First image: cached (LoadEnd before LoadStart)
191
+ const image1 = getByTestId("test-image");
192
+ fireEvent(image1, "loadEnd");
193
+ fireEvent(image1, "loadStart");
194
+ expect(queryByTestId("ActivityIndicator")).toBeNull();
195
+
196
+ // Second image: uncached (normal LoadStart then LoadEnd)
197
+ const file2 = { ...mockFile, source: "https://example.com/image2.jpg" };
198
+ rerender(
199
+ <AtlantisFormatFileContext.Provider value={mockContextValue}>
200
+ <MediaView {...defaultProps} file={file2} />
201
+ </AtlantisFormatFileContext.Provider>,
202
+ );
203
+
204
+ await waitFor(() => {
205
+ const image2 = getByTestId("test-image");
206
+ fireEvent(image2, "loadStart");
207
+
208
+ expect(queryByTestId("ActivityIndicator")).toBeTruthy();
209
+
210
+ fireEvent(image2, "loadEnd");
211
+ expect(queryByTestId("ActivityIndicator")).toBeNull();
212
+ });
213
+ });
214
+ });
215
+
216
+ describe("thumbnailUrl changes", () => {
217
+ it("shows loading indicator when thumbnailUrl changes", async () => {
218
+ const { getByTestId, queryByTestId, rerender } = renderWithContext();
219
+
220
+ // First load completes
221
+ const image1 = getByTestId("test-image");
222
+ fireEvent(image1, "loadStart");
223
+ fireEvent(image1, "loadEnd");
224
+ expect(queryByTestId("ActivityIndicator")).toBeNull();
225
+
226
+ // Thumbnail URL changes (common when thumbnail generation completes)
227
+ const fileWithThumbnail = {
228
+ ...mockFile,
229
+ thumbnailUrl: "https://example.com/thumbnail.jpg",
230
+ };
231
+
232
+ rerender(
233
+ <AtlantisFormatFileContext.Provider value={mockContextValue}>
234
+ <MediaView {...defaultProps} file={fileWithThumbnail} />
235
+ </AtlantisFormatFileContext.Provider>,
236
+ );
237
+
238
+ await waitFor(() => {
239
+ const image2 = getByTestId("test-image");
240
+ fireEvent(image2, "loadStart");
241
+
242
+ expect(queryByTestId("ActivityIndicator")).toBeTruthy();
243
+ });
244
+ });
245
+ });
246
+
247
+ describe("Context thumbnail changes", () => {
248
+ it("shows loading indicator when context thumbnail changes", async () => {
249
+ const mockContextWithThumbnail = {
250
+ useCreateThumbnail: () => ({
251
+ thumbnail: "https://example.com/context-thumbnail.jpg",
252
+ error: false,
253
+ }),
254
+ };
255
+
256
+ const { getByTestId, queryByTestId, rerender } = render(
257
+ <AtlantisFormatFileContext.Provider value={mockContextValue}>
258
+ <MediaView {...defaultProps} />
259
+ </AtlantisFormatFileContext.Provider>,
260
+ );
261
+
262
+ // First load completes
263
+ const image1 = getByTestId("test-image");
264
+ fireEvent(image1, "loadStart");
265
+ fireEvent(image1, "loadEnd");
266
+ expect(queryByTestId("ActivityIndicator")).toBeNull();
267
+
268
+ // Context provides a new thumbnail
269
+ rerender(
270
+ <AtlantisFormatFileContext.Provider value={mockContextWithThumbnail}>
271
+ <MediaView {...defaultProps} />
272
+ </AtlantisFormatFileContext.Provider>,
273
+ );
274
+
275
+ await waitFor(() => {
276
+ const image2 = getByTestId("test-image");
277
+ fireEvent(image2, "loadStart");
278
+
279
+ expect(queryByTestId("ActivityIndicator")).toBeTruthy();
280
+ });
281
+ });
282
+ });
283
+ });
@@ -1,4 +1,4 @@
1
- import React, { useState } from "react";
1
+ import React, { useEffect, useRef, useState } from "react";
2
2
  import { ImageBackground, View } from "react-native";
3
3
  import { useStyles } from "./MediaView.style";
4
4
  import type { FormattedFile } from "../../types";
@@ -32,6 +32,12 @@ export function MediaView({
32
32
  const { useCreateThumbnail } = useAtlantisFormatFileContext();
33
33
  const { thumbnail, error } = useCreateThumbnail(file);
34
34
  const [isLoading, setIsLoading] = useState(false);
35
+ /**
36
+ * Tracks whether onLoadEnd has fired to prevent race conditions.
37
+ * ImageBackground can fire onLoadEnd before onLoadStart when loading cached images,
38
+ * which would cause isLoading to get stuck at true, showing an infinite spinner.
39
+ */
40
+ const hasLoadedRef = useRef(false);
35
41
 
36
42
  const a11yLabel = computeA11yLabel({
37
43
  accessibilityLabel,
@@ -40,10 +46,25 @@ export function MediaView({
40
46
  t,
41
47
  });
42
48
 
43
- const hasError = showError || error;
44
- const uri = thumbnail || file.thumbnailUrl || file.source;
49
+ const hasError = showError || error,
50
+ uri = thumbnail || file.thumbnailUrl || file.source,
51
+ styles = useStyles();
45
52
 
46
- const styles = useStyles();
53
+ const handleLoadStart = () => {
54
+ if (!hasLoadedRef.current) {
55
+ setIsLoading(true);
56
+ }
57
+ };
58
+
59
+ const handleLoadEnd = () => {
60
+ hasLoadedRef.current = true;
61
+ setIsLoading(false);
62
+ };
63
+
64
+ useEffect(() => {
65
+ hasLoadedRef.current = false;
66
+ setIsLoading(false);
67
+ }, [uri]);
47
68
 
48
69
  return (
49
70
  <View accessible={true} accessibilityLabel={a11yLabel}>
@@ -55,8 +76,8 @@ export function MediaView({
55
76
  resizeMode={styleInGrid ? "cover" : "contain"}
56
77
  source={{ uri }}
57
78
  testID={"test-image"}
58
- onLoadStart={() => setIsLoading(true)}
59
- onLoadEnd={() => setIsLoading(false)}
79
+ onLoadStart={handleLoadStart}
80
+ onLoadEnd={handleLoadEnd}
60
81
  >
61
82
  <Overlay
62
83
  isLoading={isLoading}