uilib-native 5.0.0-snapshot.7228 → 5.0.0-snapshot.7239
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.
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
type SafeAreaInsetsType = {
|
|
1
|
+
export type SafeAreaInsetsType = {
|
|
2
2
|
top: number;
|
|
3
3
|
left: number;
|
|
4
4
|
bottom: number;
|
|
5
5
|
right: number;
|
|
6
6
|
} | null;
|
|
7
|
+
export type SafeAreaChangedDelegateType = {
|
|
8
|
+
onSafeAreaInsetsDidChangeEvent?: (insets: SafeAreaInsetsType) => void;
|
|
9
|
+
};
|
|
7
10
|
declare class SafeAreaInsetsManager {
|
|
8
11
|
_defaultInsets: SafeAreaInsetsType;
|
|
9
12
|
_safeAreaInsets: SafeAreaInsetsType;
|
|
10
|
-
_safeAreaChangedDelegates: Array<
|
|
13
|
+
_safeAreaChangedDelegates: Array<SafeAreaChangedDelegateType>;
|
|
11
14
|
_nativeModule: any;
|
|
12
15
|
constructor();
|
|
13
16
|
setupNativeConnection(): void;
|
|
@@ -16,8 +19,8 @@ declare class SafeAreaInsetsManager {
|
|
|
16
19
|
notifyDelegates(insets: SafeAreaInsetsType): void;
|
|
17
20
|
_updateInsets(): Promise<void>;
|
|
18
21
|
getSafeAreaInsets(): Promise<SafeAreaInsetsType>;
|
|
19
|
-
addSafeAreaChangedDelegate(delegate:
|
|
20
|
-
removeSafeAreaChangedDelegate(delegateToRemove:
|
|
22
|
+
addSafeAreaChangedDelegate(delegate: SafeAreaChangedDelegateType): void;
|
|
23
|
+
removeSafeAreaChangedDelegate(delegateToRemove: SafeAreaChangedDelegateType): void;
|
|
21
24
|
get defaultInsets(): SafeAreaInsetsType;
|
|
22
25
|
refreshSafeAreaInsets(): Promise<void>;
|
|
23
26
|
}
|
|
@@ -11,12 +11,7 @@ class SafeAreaInsetsManager {
|
|
|
11
11
|
bottom: 34,
|
|
12
12
|
right: 0
|
|
13
13
|
}; // Common iPhone safe area values
|
|
14
|
-
|
|
15
|
-
top: 47,
|
|
16
|
-
left: 0,
|
|
17
|
-
bottom: 34,
|
|
18
|
-
right: 0
|
|
19
|
-
};
|
|
14
|
+
|
|
20
15
|
_safeAreaChangedDelegates = [];
|
|
21
16
|
_nativeModule = null;
|
|
22
17
|
constructor() {
|
|
@@ -46,7 +41,7 @@ class SafeAreaInsetsManager {
|
|
|
46
41
|
}
|
|
47
42
|
setupEventListener() {
|
|
48
43
|
try {
|
|
49
|
-
// Use DeviceEventEmitter instead of NativeEventEmitter to avoid getConstants
|
|
44
|
+
// Use DeviceEventEmitter instead of NativeEventEmitter to avoid getConstants
|
|
50
45
|
DeviceEventEmitter.addListener('SafeAreaInsetsDidChangeEvent', data => {
|
|
51
46
|
if (data) {
|
|
52
47
|
SafeAreaInsetsCache = data;
|
|
@@ -83,8 +78,13 @@ class SafeAreaInsetsManager {
|
|
|
83
78
|
async _updateInsets() {
|
|
84
79
|
if (this._nativeModule && SafeAreaInsetsCache === null) {
|
|
85
80
|
try {
|
|
86
|
-
|
|
87
|
-
|
|
81
|
+
const insets = await this._nativeModule.getSafeAreaInsets();
|
|
82
|
+
if (insets) {
|
|
83
|
+
SafeAreaInsetsCache = insets;
|
|
84
|
+
this._safeAreaInsets = SafeAreaInsetsCache;
|
|
85
|
+
} else {
|
|
86
|
+
this._safeAreaInsets = this._defaultInsets;
|
|
87
|
+
}
|
|
88
88
|
} catch (error) {
|
|
89
89
|
console.warn('SafeAreaInsetsManager: Failed to get native insets:', error);
|
|
90
90
|
this._safeAreaInsets = this._defaultInsets;
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import {NativeModules, DeviceEventEmitter} from 'react-native';
|
|
2
|
+
|
|
3
|
+
describe('SafeAreaInsetsManager', () => {
|
|
4
|
+
beforeEach(() => {
|
|
5
|
+
// Reset mocks
|
|
6
|
+
jest.clearAllMocks();
|
|
7
|
+
|
|
8
|
+
// Reset the SafeAreaInsetsCache by creating a fresh instance
|
|
9
|
+
jest.resetModules();
|
|
10
|
+
|
|
11
|
+
// Spy on console methods to verify logging
|
|
12
|
+
jest.spyOn(console, 'log').mockImplementation(() => {});
|
|
13
|
+
jest.spyOn(console, 'warn').mockImplementation(() => {});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
// Restore console methods
|
|
18
|
+
jest.restoreAllMocks();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe('getSafeAreaInsets', () => {
|
|
22
|
+
it('should return default insets when native module is not available', async () => {
|
|
23
|
+
// Arrange
|
|
24
|
+
NativeModules.SafeAreaManager = null;
|
|
25
|
+
const SafeAreaInsetsManager = require('../SafeAreaInsetsManager').default;
|
|
26
|
+
|
|
27
|
+
// Act
|
|
28
|
+
const result = await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
29
|
+
|
|
30
|
+
// Assert
|
|
31
|
+
expect(result).toEqual({top: 47, left: 0, bottom: 34, right: 0});
|
|
32
|
+
expect(console.log).toHaveBeenCalledWith('SafeAreaInsetsManager: Native SafeAreaManager not available, using defaults');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('should return insets from native module when available', async () => {
|
|
36
|
+
// Arrange
|
|
37
|
+
const mockInsets = {top: 50, left: 10, bottom: 30, right: 10};
|
|
38
|
+
NativeModules.SafeAreaManager = {
|
|
39
|
+
getSafeAreaInsets: jest.fn().mockResolvedValue(mockInsets)
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const SafeAreaInsetsManager = require('../SafeAreaInsetsManager').default;
|
|
43
|
+
|
|
44
|
+
// Act
|
|
45
|
+
const result = await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
46
|
+
|
|
47
|
+
// Assert
|
|
48
|
+
expect(result).toEqual(mockInsets);
|
|
49
|
+
expect(NativeModules.SafeAreaManager.getSafeAreaInsets).toHaveBeenCalled();
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it.skip('should return cached insets on subsequent calls', async () => {
|
|
53
|
+
// Arrange
|
|
54
|
+
const mockInsets = {top: 44, left: 0, bottom: 34, right: 0};
|
|
55
|
+
NativeModules.SafeAreaManager = {
|
|
56
|
+
getSafeAreaInsets: jest.fn().mockResolvedValue(mockInsets)
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const SafeAreaInsetsManager = require('../SafeAreaInsetsManager').default;
|
|
60
|
+
|
|
61
|
+
// Act
|
|
62
|
+
const result1 = await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
63
|
+
const result2 = await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
64
|
+
|
|
65
|
+
// Assert
|
|
66
|
+
expect(result1).toEqual(mockInsets);
|
|
67
|
+
expect(result2).toEqual(mockInsets);
|
|
68
|
+
expect(NativeModules.SafeAreaManager.getSafeAreaInsets).toHaveBeenCalledTimes(1); // Should only call native once due to caching
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('should handle native module errors gracefully', async () => {
|
|
72
|
+
// Arrange
|
|
73
|
+
const mockError = new Error('Native module error');
|
|
74
|
+
NativeModules.SafeAreaManager = {
|
|
75
|
+
getSafeAreaInsets: jest.fn().mockRejectedValue(mockError)
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const SafeAreaInsetsManager = require('../SafeAreaInsetsManager').default;
|
|
79
|
+
|
|
80
|
+
// Act
|
|
81
|
+
const result = await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
82
|
+
|
|
83
|
+
// Assert
|
|
84
|
+
expect(result).toEqual({top: 47, left: 0, bottom: 34, right: 0}); // Should fallback to defaults
|
|
85
|
+
expect(console.warn).toHaveBeenCalledWith('SafeAreaInsetsManager: Failed to get initial insets:', mockError);
|
|
86
|
+
expect(console.warn).toHaveBeenCalledWith('SafeAreaInsetsManager: Failed to get native insets:', mockError);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('should handle native module setup errors gracefully', async () => {
|
|
90
|
+
// Arrange
|
|
91
|
+
Object.defineProperty(NativeModules, 'SafeAreaManager', {
|
|
92
|
+
get: () => {
|
|
93
|
+
throw new Error('Setup error');
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const SafeAreaInsetsManager = require('../SafeAreaInsetsManager').default;
|
|
98
|
+
|
|
99
|
+
// Act
|
|
100
|
+
const result = await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
101
|
+
|
|
102
|
+
// Assert
|
|
103
|
+
expect(result).toEqual({top: 47, left: 0, bottom: 34, right: 0}); // Should fallback to defaults
|
|
104
|
+
expect(console.warn).toHaveBeenCalledWith('SafeAreaInsetsManager: Failed to connect to native module:', expect.any(Error));
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should update insets when they change during the test', async () => {
|
|
108
|
+
// Arrange
|
|
109
|
+
const initialInsets = {top: 44, left: 0, bottom: 34, right: 0};
|
|
110
|
+
const updatedInsets = {top: 50, left: 0, bottom: 40, right: 0};
|
|
111
|
+
|
|
112
|
+
NativeModules.SafeAreaManager = {
|
|
113
|
+
// TODO: this will need to be changed when the we get caching to work in tests ("should return cached insets on subsequent calls")
|
|
114
|
+
// getSafeAreaInsets: jest.fn().mockResolvedValueOnce(initialInsets).mockResolvedValueOnce(updatedInsets)
|
|
115
|
+
getSafeAreaInsets: jest
|
|
116
|
+
.fn()
|
|
117
|
+
.mockResolvedValueOnce(initialInsets)
|
|
118
|
+
.mockResolvedValueOnce(initialInsets)
|
|
119
|
+
.mockResolvedValueOnce(updatedInsets)
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const SafeAreaInsetsManager = require('../SafeAreaInsetsManager').default;
|
|
123
|
+
|
|
124
|
+
// Act & Assert - Initial insets
|
|
125
|
+
const result1 = await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
126
|
+
expect(result1).toEqual(initialInsets);
|
|
127
|
+
|
|
128
|
+
// Force refresh of insets
|
|
129
|
+
await SafeAreaInsetsManager.refreshSafeAreaInsets();
|
|
130
|
+
|
|
131
|
+
// Simulate insets change event from native side
|
|
132
|
+
DeviceEventEmitter.emit('SafeAreaInsetsDidChangeEvent', updatedInsets);
|
|
133
|
+
|
|
134
|
+
// Get insets again - should reflect the change
|
|
135
|
+
const result2 = await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
136
|
+
expect(result2).toEqual(updatedInsets);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should notify delegates when insets change during the test', async () => {
|
|
140
|
+
// Arrange
|
|
141
|
+
const initialInsets = {top: 44, left: 0, bottom: 34, right: 0};
|
|
142
|
+
const updatedInsets = {top: 50, left: 0, bottom: 40, right: 0};
|
|
143
|
+
|
|
144
|
+
NativeModules.SafeAreaManager = {
|
|
145
|
+
getSafeAreaInsets: jest.fn().mockResolvedValue(initialInsets)
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
const SafeAreaInsetsManager = require('../SafeAreaInsetsManager').default;
|
|
149
|
+
|
|
150
|
+
// Add a mock delegate
|
|
151
|
+
const mockDelegate = {
|
|
152
|
+
onSafeAreaInsetsDidChangeEvent: jest.fn()
|
|
153
|
+
};
|
|
154
|
+
SafeAreaInsetsManager.addSafeAreaChangedDelegate(mockDelegate);
|
|
155
|
+
|
|
156
|
+
// Act - Get initial insets
|
|
157
|
+
await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
158
|
+
|
|
159
|
+
// Simulate insets change event from native side
|
|
160
|
+
DeviceEventEmitter.emit('SafeAreaInsetsDidChangeEvent', updatedInsets);
|
|
161
|
+
|
|
162
|
+
// Assert - Delegate should be notified
|
|
163
|
+
expect(mockDelegate.onSafeAreaInsetsDidChangeEvent).toHaveBeenCalledWith(updatedInsets);
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('should handle refreshSafeAreaInsets correctly', async () => {
|
|
167
|
+
// Arrange
|
|
168
|
+
const initialInsets = {top: 44, left: 0, bottom: 34, right: 0};
|
|
169
|
+
const refreshedInsets = {top: 48, left: 0, bottom: 36, right: 0};
|
|
170
|
+
|
|
171
|
+
NativeModules.SafeAreaManager = {
|
|
172
|
+
// TODO: this will need to be changed when the we get caching to work in tests ("should return cached insets on subsequent calls")
|
|
173
|
+
// getSafeAreaInsets: jest.fn().mockResolvedValueOnce(initialInsets).mockResolvedValueOnce(updatedInsets)
|
|
174
|
+
getSafeAreaInsets: jest
|
|
175
|
+
.fn()
|
|
176
|
+
.mockResolvedValueOnce(initialInsets)
|
|
177
|
+
.mockResolvedValueOnce(initialInsets)
|
|
178
|
+
.mockResolvedValueOnce(refreshedInsets)
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const SafeAreaInsetsManager = require('../SafeAreaInsetsManager').default;
|
|
182
|
+
|
|
183
|
+
// Act
|
|
184
|
+
const result1 = await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
185
|
+
expect(result1).toEqual(initialInsets);
|
|
186
|
+
|
|
187
|
+
// Refresh insets
|
|
188
|
+
await SafeAreaInsetsManager.refreshSafeAreaInsets();
|
|
189
|
+
|
|
190
|
+
const result2 = await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
191
|
+
|
|
192
|
+
// Assert
|
|
193
|
+
expect(result2).toEqual(refreshedInsets);
|
|
194
|
+
// TODO: this will need to be changed when the we get caching to work in tests ("should return cached insets on subsequent calls")
|
|
195
|
+
expect(NativeModules.SafeAreaManager.getSafeAreaInsets).toHaveBeenCalledTimes(3);
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
it('should not notify delegates when insets remain the same after refresh', async () => {
|
|
199
|
+
// Arrange
|
|
200
|
+
const sameInsets = {top: 44, left: 0, bottom: 34, right: 0};
|
|
201
|
+
|
|
202
|
+
NativeModules.SafeAreaManager = {
|
|
203
|
+
getSafeAreaInsets: jest.fn().mockResolvedValue(sameInsets)
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const SafeAreaInsetsManager = require('../SafeAreaInsetsManager').default;
|
|
207
|
+
|
|
208
|
+
// Add a mock delegate
|
|
209
|
+
const mockDelegate = {
|
|
210
|
+
onSafeAreaInsetsDidChangeEvent: jest.fn()
|
|
211
|
+
};
|
|
212
|
+
SafeAreaInsetsManager.addSafeAreaChangedDelegate(mockDelegate);
|
|
213
|
+
|
|
214
|
+
// Act
|
|
215
|
+
await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
216
|
+
await SafeAreaInsetsManager.refreshSafeAreaInsets();
|
|
217
|
+
|
|
218
|
+
// TODO: this will need to be changed when the we get caching to work in tests ("should return cached insets on subsequent calls")
|
|
219
|
+
expect(NativeModules.SafeAreaManager.getSafeAreaInsets).toHaveBeenCalledTimes(3);
|
|
220
|
+
|
|
221
|
+
// Assert - Delegate should not be notified since insets didn't change
|
|
222
|
+
expect(mockDelegate.onSafeAreaInsetsDidChangeEvent).not.toHaveBeenCalled();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('should return default insets when native getSafeAreaInsets returns null', async () => {
|
|
226
|
+
// Arrange
|
|
227
|
+
NativeModules.SafeAreaManager = {
|
|
228
|
+
getSafeAreaInsets: jest.fn().mockResolvedValue(null)
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const SafeAreaInsetsManager = require('../SafeAreaInsetsManager').default;
|
|
232
|
+
|
|
233
|
+
// Act
|
|
234
|
+
const result = await SafeAreaInsetsManager.getSafeAreaInsets();
|
|
235
|
+
|
|
236
|
+
// Assert
|
|
237
|
+
expect(result).toEqual({top: 47, left: 0, bottom: 34, right: 0});
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('should properly manage delegate lifecycle', async () => {
|
|
241
|
+
// Arrange
|
|
242
|
+
NativeModules.SafeAreaManager = {
|
|
243
|
+
getSafeAreaInsets: jest.fn().mockResolvedValue({top: 44, left: 0, bottom: 34, right: 0})
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
const SafeAreaInsetsManager = require('../SafeAreaInsetsManager').default;
|
|
247
|
+
|
|
248
|
+
const mockDelegate1 = {
|
|
249
|
+
onSafeAreaInsetsDidChangeEvent: jest.fn()
|
|
250
|
+
};
|
|
251
|
+
const mockDelegate2 = {
|
|
252
|
+
onSafeAreaInsetsDidChangeEvent: jest.fn()
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
// Act
|
|
256
|
+
SafeAreaInsetsManager.addSafeAreaChangedDelegate(mockDelegate1);
|
|
257
|
+
SafeAreaInsetsManager.addSafeAreaChangedDelegate(mockDelegate2);
|
|
258
|
+
|
|
259
|
+
// Remove one delegate
|
|
260
|
+
SafeAreaInsetsManager.removeSafeAreaChangedDelegate(mockDelegate1);
|
|
261
|
+
|
|
262
|
+
// Trigger notification
|
|
263
|
+
const newInsets = {top: 50, left: 0, bottom: 40, right: 0};
|
|
264
|
+
SafeAreaInsetsManager.notifyDelegates(newInsets);
|
|
265
|
+
|
|
266
|
+
// Assert
|
|
267
|
+
expect(mockDelegate1.onSafeAreaInsetsDidChangeEvent).not.toHaveBeenCalled();
|
|
268
|
+
expect(mockDelegate2.onSafeAreaInsetsDidChangeEvent).toHaveBeenCalledWith(newInsets);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
});
|
package/package.json
CHANGED