@applicaster/quick-brick-core 16.0.0-rc.102 → 16.0.0-rc.105
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/App/NetworkStatusProvider/NetworkStatusProvider.tsx +32 -4
- package/App/NetworkStatusProvider/ResumeVerificationController.ts +282 -0
- package/App/NetworkStatusProvider/__tests__/NetworkStatusProvider.test.tsx +152 -1
- package/App/NetworkStatusProvider/__tests__/ResumeVerificationController.test.ts +471 -0
- package/App/NetworkStatusProvider/__tests__/resumeVerification.test.ts +126 -0
- package/App/NetworkStatusProvider/__tests__/useResumeVerifiedNetInfo.test.ts +189 -0
- package/App/NetworkStatusProvider/resumeVerification.ts +54 -0
- package/App/NetworkStatusProvider/useResumeVerifiedNetInfo.ts +33 -0
- package/package.json +8 -8
|
@@ -12,6 +12,11 @@ import {
|
|
|
12
12
|
unsubscribeToDeviceNetworkChanges,
|
|
13
13
|
} from "./utils";
|
|
14
14
|
import { usePrevious } from "@applicaster/zapp-react-native-utils/reactHooks/utils";
|
|
15
|
+
import { coreAppLogger } from "../logger";
|
|
16
|
+
import { useResumeVerifiedNetInfo } from "./useResumeVerifiedNetInfo";
|
|
17
|
+
import { describeNetInfo } from "./resumeVerification";
|
|
18
|
+
|
|
19
|
+
const logger = coreAppLogger.addSubsystem("NetworkStatusProvider");
|
|
15
20
|
|
|
16
21
|
// Use default netInfo config for all platforms (important for web platform which otherwise
|
|
17
22
|
// will use backend root which is not responding for HEAD request)
|
|
@@ -36,10 +41,23 @@ export const NetworkStatusProvider = ({
|
|
|
36
41
|
// NetInfo library does not have accurate information for Tizen SmartTVs, and possibly WebOS
|
|
37
42
|
// This combined info helps us gather data from the native connection status APIs
|
|
38
43
|
// and includes it in our definition of... online.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
deviceStatus
|
|
42
|
-
|
|
44
|
+
// stable reference between real changes: every new reference is a network update
|
|
45
|
+
const liveNetInfo = React.useMemo<
|
|
46
|
+
NetInfoState & { deviceStatus?: NetStatus }
|
|
47
|
+
>(
|
|
48
|
+
() => ({ ...currentNetInfo, deviceStatus }),
|
|
49
|
+
[currentNetInfo, deviceStatus]
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
// NetInfo reports stale offline status right after resume/unlock (iOS),
|
|
53
|
+
// publish the network status verified after the app returns to foreground
|
|
54
|
+
const { netInfo: verifiedNetInfo, stillOfflineAfterResumeCount } =
|
|
55
|
+
useResumeVerifiedNetInfo(liveNetInfo);
|
|
56
|
+
|
|
57
|
+
const combinedNetInfo: NetInfoState & {
|
|
58
|
+
deviceStatus?: NetStatus;
|
|
59
|
+
stillOfflineAfterResumeCount: number;
|
|
60
|
+
} = { ...verifiedNetInfo, stillOfflineAfterResumeCount };
|
|
43
61
|
|
|
44
62
|
const { onConnectionTypeChanged, onConnectionLost, onConnectionRestored } =
|
|
45
63
|
React.useMemo<NetworkHooks>(() => getNetworkHooks(plugins), []);
|
|
@@ -57,6 +75,16 @@ export const NetworkStatusProvider = ({
|
|
|
57
75
|
if (!combinedNetInfo.details) return;
|
|
58
76
|
|
|
59
77
|
if (previousOnline !== undefined && previousOnline !== online) {
|
|
78
|
+
logger.info({
|
|
79
|
+
message: "NetworkStatus: online status changed",
|
|
80
|
+
data: {
|
|
81
|
+
online,
|
|
82
|
+
previousOnline,
|
|
83
|
+
published: describeNetInfo(combinedNetInfo),
|
|
84
|
+
live: describeNetInfo(liveNetInfo),
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
|
|
60
88
|
online
|
|
61
89
|
? onConnectionRestored &&
|
|
62
90
|
callAllWith(onConnectionRestored, combinedNetInfo)
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { AppState, AppStateStatus } from "react-native";
|
|
2
|
+
import { NetInfoState } from "@react-native-community/netinfo";
|
|
3
|
+
|
|
4
|
+
import { coreAppLogger } from "../logger";
|
|
5
|
+
import { isOnline } from "./utils";
|
|
6
|
+
import {
|
|
7
|
+
RESUME_VERIFICATION_DELAY_MS,
|
|
8
|
+
describeNetInfo,
|
|
9
|
+
getResumeDecision,
|
|
10
|
+
isBackgroundAppState,
|
|
11
|
+
isResumeVerificationEnabled,
|
|
12
|
+
} from "./resumeVerification";
|
|
13
|
+
|
|
14
|
+
const logger = coreAppLogger.addSubsystem("NetworkStatusProvider");
|
|
15
|
+
|
|
16
|
+
export type ConnectionInfo = NetInfoState & { deviceStatus?: NetStatus };
|
|
17
|
+
|
|
18
|
+
export type ResumeVerifiedState<T extends ConnectionInfo> = {
|
|
19
|
+
netInfo: T;
|
|
20
|
+
stillOfflineAfterResumeCount: number;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type Phase = "live" | "frozen" | "verifying";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Holds the published network status while the app is in background and for
|
|
27
|
+
* RESUME_VERIFICATION_DELAY_MS after it becomes active again, then publishes the
|
|
28
|
+
* current state. Works around stale iOS NetInfo reachability failures on resume/unlock.
|
|
29
|
+
*
|
|
30
|
+
* start()/stop() manage the AppState subscription and timer (useLayoutEffect),
|
|
31
|
+
* setNetInfo() feeds live NetInfo, subscribe()/getState() back useSyncExternalStore.
|
|
32
|
+
*/
|
|
33
|
+
export class ResumeVerificationController<T extends ConnectionInfo> {
|
|
34
|
+
private phase: Phase = "live";
|
|
35
|
+
private appState: AppStateStatus;
|
|
36
|
+
private live: T;
|
|
37
|
+
private state: ResumeVerifiedState<T>;
|
|
38
|
+
private listeners = new Set<() => void>();
|
|
39
|
+
private subscription: { remove: () => void } | null = null;
|
|
40
|
+
private timer: ReturnType<typeof setTimeout> | null = null;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @param initialNetInfo - live NetInfo at mount, published as the initial state.
|
|
44
|
+
* Starts "frozen" when the app is created in background (e.g. iOS background launch).
|
|
45
|
+
*/
|
|
46
|
+
constructor(initialNetInfo: T) {
|
|
47
|
+
this.appState = AppState.currentState;
|
|
48
|
+
this.live = initialNetInfo;
|
|
49
|
+
this.state = { netInfo: initialNetInfo, stillOfflineAfterResumeCount: 0 };
|
|
50
|
+
|
|
51
|
+
if (isResumeVerificationEnabled() && isBackgroundAppState(this.appState)) {
|
|
52
|
+
this.phase = "frozen";
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
this.subscribe = this.subscribe.bind(this);
|
|
56
|
+
this.getState = this.getState.bind(this);
|
|
57
|
+
this.applyAppState = this.applyAppState.bind(this);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* useSyncExternalStore subscribe: listener is called whenever getState() changes.
|
|
62
|
+
* @param listener - called with no arguments after each publish
|
|
63
|
+
* @returns unsubscribe function
|
|
64
|
+
*/
|
|
65
|
+
subscribe(listener: () => void): () => void {
|
|
66
|
+
this.listeners.add(listener);
|
|
67
|
+
|
|
68
|
+
return () => {
|
|
69
|
+
this.listeners.delete(listener);
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* useSyncExternalStore snapshot: same reference until a new state is published.
|
|
75
|
+
* @returns the verified network status and the still-offline-after-resume counter
|
|
76
|
+
*/
|
|
77
|
+
getState(): ResumeVerifiedState<T> {
|
|
78
|
+
return this.state;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Subscribes to AppState changes and catches up with the current app state.
|
|
83
|
+
* Idempotent; no-op on platforms without resume verification (web, TV).
|
|
84
|
+
* Call from useLayoutEffect, paired with stop().
|
|
85
|
+
*/
|
|
86
|
+
start(): void {
|
|
87
|
+
if (!isResumeVerificationEnabled() || this.subscription) return;
|
|
88
|
+
|
|
89
|
+
this.subscription = AppState.addEventListener("change", this.applyAppState);
|
|
90
|
+
|
|
91
|
+
// AppState may have changed since construction (e.g. initial "background"
|
|
92
|
+
// corrected to "active"), or stop() interrupted a window: catch up
|
|
93
|
+
this.applyAppState(AppState.currentState);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Removes the AppState subscription and cancels a pending verification.
|
|
98
|
+
* An interrupted window falls back to "frozen" and restarts on the next start() while active.
|
|
99
|
+
*/
|
|
100
|
+
stop(): void {
|
|
101
|
+
this.subscription?.remove();
|
|
102
|
+
this.subscription = null;
|
|
103
|
+
this.clearTimer();
|
|
104
|
+
|
|
105
|
+
// an interrupted window is restarted by the next start() while active
|
|
106
|
+
if (this.phase === "verifying") {
|
|
107
|
+
this.phase = "frozen";
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Feeds the latest live NetInfo. Published immediately while "live",
|
|
113
|
+
* held (and logged as ignored) while "frozen" or "verifying".
|
|
114
|
+
* @param netInfo - live NetInfo; the same reference is a no-op
|
|
115
|
+
*/
|
|
116
|
+
setNetInfo(netInfo: T): void {
|
|
117
|
+
if (netInfo === this.live) return;
|
|
118
|
+
|
|
119
|
+
this.live = netInfo;
|
|
120
|
+
|
|
121
|
+
if (this.phase === "live") {
|
|
122
|
+
this.publish(netInfo);
|
|
123
|
+
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
logger.info({
|
|
128
|
+
message: "NetworkStatus: network change ignored until verified",
|
|
129
|
+
data: {
|
|
130
|
+
appState: this.appState,
|
|
131
|
+
phase: this.phase,
|
|
132
|
+
published: describeNetInfo(this.state.netInfo),
|
|
133
|
+
live: describeNetInfo(netInfo),
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* AppState change handler: "background" freezes, "active" after a freeze starts verification,
|
|
140
|
+
* anything else ("inactive", "unknown") keeps the current phase.
|
|
141
|
+
* @param nextAppState - new AppState value
|
|
142
|
+
*/
|
|
143
|
+
private applyAppState(nextAppState: AppStateStatus): void {
|
|
144
|
+
const previousAppState = this.appState;
|
|
145
|
+
const nextPhase = this.getNextPhase(nextAppState);
|
|
146
|
+
this.appState = nextAppState;
|
|
147
|
+
|
|
148
|
+
if (previousAppState === nextAppState && nextPhase === this.phase) return;
|
|
149
|
+
|
|
150
|
+
logger.info({
|
|
151
|
+
message: "NetworkStatus: app state changed",
|
|
152
|
+
data: {
|
|
153
|
+
appState: nextAppState,
|
|
154
|
+
previousAppState,
|
|
155
|
+
phase: this.phase,
|
|
156
|
+
nextPhase,
|
|
157
|
+
published: describeNetInfo(this.state.netInfo),
|
|
158
|
+
live: describeNetInfo(this.live),
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
if (nextPhase === "frozen" && this.phase !== "frozen") {
|
|
163
|
+
this.freeze();
|
|
164
|
+
} else if (nextPhase === "verifying" && this.phase === "frozen") {
|
|
165
|
+
this.startVerification();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* @param appState - new AppState value
|
|
171
|
+
* @returns the phase the controller moves to for this app state
|
|
172
|
+
*/
|
|
173
|
+
private getNextPhase(appState: AppStateStatus): Phase {
|
|
174
|
+
if (isBackgroundAppState(appState)) return "frozen";
|
|
175
|
+
|
|
176
|
+
if (appState === "active" && this.phase === "frozen") return "verifying";
|
|
177
|
+
|
|
178
|
+
return this.phase;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Stops publishing live changes; cancels a running verification window.
|
|
183
|
+
*/
|
|
184
|
+
private freeze(): void {
|
|
185
|
+
if (this.phase === "verifying") {
|
|
186
|
+
this.clearTimer();
|
|
187
|
+
|
|
188
|
+
logger.info({
|
|
189
|
+
message: "NetworkStatus: resume verification cancelled",
|
|
190
|
+
data: {
|
|
191
|
+
appState: this.appState,
|
|
192
|
+
snapshot: describeNetInfo(this.state.netInfo),
|
|
193
|
+
live: describeNetInfo(this.live),
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
this.phase = "frozen";
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Opens the RESUME_VERIFICATION_DELAY_MS window. When it ends, compares the state published
|
|
203
|
+
* before background with the current live state, publishes the live state and bumps
|
|
204
|
+
* stillOfflineAfterResumeCount when the app is still offline.
|
|
205
|
+
*/
|
|
206
|
+
private startVerification(): void {
|
|
207
|
+
this.phase = "verifying";
|
|
208
|
+
|
|
209
|
+
const snapshot = this.state.netInfo;
|
|
210
|
+
const startedAt = Date.now();
|
|
211
|
+
|
|
212
|
+
logger.info({
|
|
213
|
+
message: "NetworkStatus: resume verification started",
|
|
214
|
+
data: {
|
|
215
|
+
delayMs: RESUME_VERIFICATION_DELAY_MS,
|
|
216
|
+
appState: this.appState,
|
|
217
|
+
snapshot: describeNetInfo(snapshot),
|
|
218
|
+
live: describeNetInfo(this.live),
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
this.timer = setTimeout(() => {
|
|
223
|
+
this.timer = null;
|
|
224
|
+
|
|
225
|
+
if (this.phase !== "verifying") return;
|
|
226
|
+
|
|
227
|
+
const decision = getResumeDecision(
|
|
228
|
+
isOnline(snapshot) === true,
|
|
229
|
+
isOnline(this.live) === true
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
logger.info({
|
|
233
|
+
message: "NetworkStatus: resume verification finished",
|
|
234
|
+
data: {
|
|
235
|
+
decision,
|
|
236
|
+
elapsedMs: Date.now() - startedAt,
|
|
237
|
+
appState: this.appState,
|
|
238
|
+
snapshot: describeNetInfo(snapshot),
|
|
239
|
+
live: describeNetInfo(this.live),
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
this.phase = "live";
|
|
244
|
+
|
|
245
|
+
this.publish(
|
|
246
|
+
this.live,
|
|
247
|
+
this.state.stillOfflineAfterResumeCount +
|
|
248
|
+
(decision === "still_offline" ? 1 : 0)
|
|
249
|
+
);
|
|
250
|
+
}, RESUME_VERIFICATION_DELAY_MS);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Cancels the pending verification timer, if any.
|
|
255
|
+
*/
|
|
256
|
+
private clearTimer(): void {
|
|
257
|
+
if (this.timer !== null) {
|
|
258
|
+
clearTimeout(this.timer);
|
|
259
|
+
this.timer = null;
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Replaces the snapshot and notifies subscribers; no-op when nothing changed.
|
|
265
|
+
* @param netInfo - NetInfo to publish
|
|
266
|
+
* @param stillOfflineAfterResumeCount - counter to publish, defaults to the current one
|
|
267
|
+
*/
|
|
268
|
+
private publish(
|
|
269
|
+
netInfo: T,
|
|
270
|
+
stillOfflineAfterResumeCount = this.state.stillOfflineAfterResumeCount
|
|
271
|
+
): void {
|
|
272
|
+
if (
|
|
273
|
+
netInfo === this.state.netInfo &&
|
|
274
|
+
stillOfflineAfterResumeCount === this.state.stillOfflineAfterResumeCount
|
|
275
|
+
) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
this.state = { netInfo, stillOfflineAfterResumeCount };
|
|
280
|
+
this.listeners.forEach((listener) => listener());
|
|
281
|
+
}
|
|
282
|
+
}
|
|
@@ -5,6 +5,54 @@ import { NetworkStatusContext } from "@applicaster/zapp-react-native-ui-componen
|
|
|
5
5
|
import { NetworkStatusProvider } from "../NetworkStatusProvider";
|
|
6
6
|
import { WrappedWithProviders } from "@applicaster/zapp-react-native-utils/testUtils";
|
|
7
7
|
import NetInfo from "@react-native-community/netinfo";
|
|
8
|
+
import { AppState } from "react-native";
|
|
9
|
+
import { RESUME_VERIFICATION_DELAY_MS } from "../resumeVerification";
|
|
10
|
+
|
|
11
|
+
const mockLogger = {
|
|
12
|
+
warning: jest.fn(),
|
|
13
|
+
debug: jest.fn(),
|
|
14
|
+
info: jest.fn(),
|
|
15
|
+
log: jest.fn(),
|
|
16
|
+
error: jest.fn(),
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// addSubsystem runs at module load, before mockLogger is initialised: delegate lazily
|
|
20
|
+
jest.mock("../../logger", () => ({
|
|
21
|
+
coreAppLogger: {
|
|
22
|
+
addSubsystem: () => ({
|
|
23
|
+
warning: (event) => mockLogger.warning(event),
|
|
24
|
+
debug: (event) => mockLogger.debug(event),
|
|
25
|
+
info: (event) => mockLogger.info(event),
|
|
26
|
+
log: (event) => mockLogger.log(event),
|
|
27
|
+
error: (event) => mockLogger.error(event),
|
|
28
|
+
}),
|
|
29
|
+
},
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
jest.mock("react-native/Libraries/AppState/AppState", () => {
|
|
33
|
+
const { EventEmitter } = require("events");
|
|
34
|
+
const emitter = new EventEmitter();
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
emitter,
|
|
38
|
+
currentState: "active",
|
|
39
|
+
addEventListener: (type, handler) => {
|
|
40
|
+
emitter.addListener(type, handler);
|
|
41
|
+
|
|
42
|
+
return { remove: () => emitter.removeListener(type, handler) };
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const mockAppState = AppState as any;
|
|
48
|
+
|
|
49
|
+
const changeAppState = (state: string) =>
|
|
50
|
+
act(() => {
|
|
51
|
+
mockAppState.currentState = state;
|
|
52
|
+
mockAppState.emitter.emit("change", state);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
const OFFLINE_PATCH = { isConnected: false, isInternetReachable: false };
|
|
8
56
|
|
|
9
57
|
const NetInfoMock = {
|
|
10
58
|
type: "cellular",
|
|
@@ -49,7 +97,10 @@ describe("NetworkStatusProvider", function () {
|
|
|
49
97
|
getWrapper(store)
|
|
50
98
|
);
|
|
51
99
|
|
|
52
|
-
expect(result.current).toStrictEqual(
|
|
100
|
+
expect(result.current).toStrictEqual({
|
|
101
|
+
...NetInfoMock,
|
|
102
|
+
stillOfflineAfterResumeCount: 0,
|
|
103
|
+
});
|
|
53
104
|
});
|
|
54
105
|
|
|
55
106
|
it("should call onConnectionLost callback when connection type changes to none", async function () {
|
|
@@ -135,3 +186,103 @@ describe("NetworkStatusProvider", function () {
|
|
|
135
186
|
expect(onConnectionTypeChanged).toBeCalledWith("wifi");
|
|
136
187
|
});
|
|
137
188
|
});
|
|
189
|
+
|
|
190
|
+
describe("NetworkStatusProvider – app resume", () => {
|
|
191
|
+
beforeEach(() => {
|
|
192
|
+
jest.useFakeTimers();
|
|
193
|
+
mockAppState.currentState = "active";
|
|
194
|
+
mockAppState.emitter.removeAllListeners();
|
|
195
|
+
// @ts-ignore
|
|
196
|
+
jest.spyOn(NetInfo, "useNetInfo").mockImplementation(useNetInfoMock);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
afterEach(() => {
|
|
200
|
+
jest.useRealTimers();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("does not call onConnectionLost/Restored for a reachability blip right after resume", () => {
|
|
204
|
+
const onConnectionLost = jest.fn();
|
|
205
|
+
const onConnectionRestored = jest.fn();
|
|
206
|
+
|
|
207
|
+
const store = {
|
|
208
|
+
plugins: [
|
|
209
|
+
{
|
|
210
|
+
module: { networkHooks: { onConnectionLost, onConnectionRestored } },
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
renderHook(() => React.useContext(NetworkStatusContext), getWrapper(store));
|
|
216
|
+
|
|
217
|
+
changeAppState("background");
|
|
218
|
+
changeAppState("active");
|
|
219
|
+
|
|
220
|
+
act(() => setNetInfoState(R.mergeRight(NetInfoMock, OFFLINE_PATCH)));
|
|
221
|
+
act(() => jest.advanceTimersByTime(2000));
|
|
222
|
+
act(() => setNetInfoState(NetInfoMock));
|
|
223
|
+
act(() => jest.advanceTimersByTime(RESUME_VERIFICATION_DELAY_MS));
|
|
224
|
+
|
|
225
|
+
expect(onConnectionLost).not.toHaveBeenCalled();
|
|
226
|
+
expect(onConnectionRestored).not.toHaveBeenCalled();
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("calls onConnectionLost 5s after resume when the device is really offline", () => {
|
|
230
|
+
const onConnectionLost = jest.fn();
|
|
231
|
+
|
|
232
|
+
const store = {
|
|
233
|
+
plugins: [{ module: { networkHooks: { onConnectionLost } } }],
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
renderHook(() => React.useContext(NetworkStatusContext), getWrapper(store));
|
|
237
|
+
|
|
238
|
+
changeAppState("background");
|
|
239
|
+
changeAppState("active");
|
|
240
|
+
act(() => setNetInfoState(R.mergeRight(NetInfoMock, OFFLINE_PATCH)));
|
|
241
|
+
|
|
242
|
+
act(() => jest.advanceTimersByTime(RESUME_VERIFICATION_DELAY_MS - 1));
|
|
243
|
+
expect(onConnectionLost).not.toHaveBeenCalled();
|
|
244
|
+
|
|
245
|
+
act(() => jest.advanceTimersByTime(1));
|
|
246
|
+
expect(onConnectionLost).toHaveBeenCalledTimes(1);
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("exposes stillOfflineAfterResumeCount when the device is still offline after resume", () => {
|
|
250
|
+
const store = { plugins: [{}] };
|
|
251
|
+
|
|
252
|
+
const { result } = renderHook(
|
|
253
|
+
() => React.useContext(NetworkStatusContext) as any,
|
|
254
|
+
getWrapper(store)
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
act(() => setNetInfoState(R.mergeRight(NetInfoMock, OFFLINE_PATCH)));
|
|
258
|
+
expect(result.current.stillOfflineAfterResumeCount).toBe(0);
|
|
259
|
+
|
|
260
|
+
changeAppState("background");
|
|
261
|
+
changeAppState("active");
|
|
262
|
+
act(() => jest.advanceTimersByTime(RESUME_VERIFICATION_DELAY_MS));
|
|
263
|
+
|
|
264
|
+
expect(result.current.stillOfflineAfterResumeCount).toBe(1);
|
|
265
|
+
expect(result.current.isInternetReachable).toBe(false);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
it("logs online status changes at info level", () => {
|
|
269
|
+
mockLogger.info.mockClear();
|
|
270
|
+
mockLogger.warning.mockClear();
|
|
271
|
+
|
|
272
|
+
renderHook(
|
|
273
|
+
() => React.useContext(NetworkStatusContext),
|
|
274
|
+
getWrapper({ plugins: [{}] })
|
|
275
|
+
);
|
|
276
|
+
|
|
277
|
+
act(() => setNetInfoState(R.mergeRight(NetInfoMock, OFFLINE_PATCH)));
|
|
278
|
+
|
|
279
|
+
expect(mockLogger.info).toHaveBeenCalledWith(
|
|
280
|
+
expect.objectContaining({
|
|
281
|
+
message: "NetworkStatus: online status changed",
|
|
282
|
+
data: expect.objectContaining({ online: false, previousOnline: true }),
|
|
283
|
+
})
|
|
284
|
+
);
|
|
285
|
+
|
|
286
|
+
expect(mockLogger.warning).not.toHaveBeenCalled();
|
|
287
|
+
});
|
|
288
|
+
});
|