@applicaster/quick-brick-core 16.0.0-alpha.8525514228 → 16.0.0-alpha.8716141508
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/ModalProvider/ModalBottomSheet/DraggableBottomSheet/__tests__/index.test.tsx +88 -0
- package/App/ModalProvider/ModalBottomSheet/DraggableBottomSheet/index.tsx +30 -56
- package/App/NotificationToastRenderer/NotificationToastManager.ts +203 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.tsx +102 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.web.tsx +61 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastManager.test.ts +213 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastRenderer.test.tsx +85 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastRenderer.web.test.tsx +90 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationHeight.test.tsx +58 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationToastState.test.ts +112 -0
- package/App/NotificationToastRenderer/index.tsx +5 -0
- package/App/NotificationToastRenderer/useNotificationHeight.tsx +13 -0
- package/App/NotificationToastRenderer/useNotificationToastState.tsx +12 -0
- package/App/index.tsx +8 -5
- package/package.json +8 -8
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { View } from "react-native";
|
|
3
|
+
import { render } from "@testing-library/react-native";
|
|
4
|
+
import { State } from "react-native-gesture-handler";
|
|
5
|
+
import { DraggableBottomSheet } from "../index";
|
|
6
|
+
|
|
7
|
+
// Render the gesture handlers as passthrough Views so we can mount the sheet
|
|
8
|
+
// and drive its release handler directly. State stays real (we need ACTIVE).
|
|
9
|
+
jest.mock("react-native-gesture-handler", () => {
|
|
10
|
+
const RN = require("react-native");
|
|
11
|
+
const ReactActual = require("react");
|
|
12
|
+
const actual = jest.requireActual("react-native-gesture-handler");
|
|
13
|
+
|
|
14
|
+
const Passthrough = ReactActual.forwardRef(({ children }: any, _ref: any) => (
|
|
15
|
+
<RN.View>{children}</RN.View>
|
|
16
|
+
));
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
__esModule: true,
|
|
20
|
+
...actual,
|
|
21
|
+
PanGestureHandler: Passthrough,
|
|
22
|
+
TapGestureHandler: Passthrough,
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
// height 800, sheetHeight 400, no header:
|
|
27
|
+
// SnapPoints.top = 800 - 400 = 400 (lastSnap after mount)
|
|
28
|
+
// SnapPoints.threshold = max(800 - 400 / 2, 100) = 600
|
|
29
|
+
// Release dismisses when lastSnap + translationY + 0.05 * velocityY > 600,
|
|
30
|
+
// i.e. once the drag+toss pushes past 200px.
|
|
31
|
+
const mountSheet = () => {
|
|
32
|
+
const dismiss = jest.fn();
|
|
33
|
+
const ref = React.createRef<DraggableBottomSheet>();
|
|
34
|
+
|
|
35
|
+
render(
|
|
36
|
+
<DraggableBottomSheet
|
|
37
|
+
ref={ref}
|
|
38
|
+
header={() => null}
|
|
39
|
+
dismiss={dismiss}
|
|
40
|
+
visible={true}
|
|
41
|
+
sheetHeight={400}
|
|
42
|
+
width={320}
|
|
43
|
+
height={800}
|
|
44
|
+
hasHeader={false}
|
|
45
|
+
maxWidth={320}
|
|
46
|
+
>
|
|
47
|
+
<View testID="child" />
|
|
48
|
+
</DraggableBottomSheet>
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
return { instance: ref.current as DraggableBottomSheet, dismiss };
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const release = (
|
|
55
|
+
instance: DraggableBottomSheet,
|
|
56
|
+
translationY: number,
|
|
57
|
+
velocityY = 0
|
|
58
|
+
) =>
|
|
59
|
+
instance._onHandlerStateChange({
|
|
60
|
+
nativeEvent: { oldState: State.ACTIVE, translationY, velocityY },
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("DraggableBottomSheet release", () => {
|
|
64
|
+
// Mounting an open sheet schedules a deferred animateIn via setTimeout. Keep
|
|
65
|
+
// it on fake timers so it never fires into a torn-down Jest environment.
|
|
66
|
+
beforeEach(() => jest.useFakeTimers());
|
|
67
|
+
|
|
68
|
+
afterEach(() => {
|
|
69
|
+
jest.clearAllTimers();
|
|
70
|
+
jest.useRealTimers();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("dismisses when the release lands past the threshold", () => {
|
|
74
|
+
const { instance, dismiss } = mountSheet();
|
|
75
|
+
|
|
76
|
+
release(instance, 300);
|
|
77
|
+
|
|
78
|
+
expect(dismiss).toHaveBeenCalledTimes(1);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("springs back (does not dismiss) when the release is below the threshold", () => {
|
|
82
|
+
const { instance, dismiss } = mountSheet();
|
|
83
|
+
|
|
84
|
+
release(instance, 50);
|
|
85
|
+
|
|
86
|
+
expect(dismiss).not.toHaveBeenCalled();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -1,15 +1,24 @@
|
|
|
1
|
-
import React, { Component,
|
|
1
|
+
import React, { Component, ReactElement } from "react";
|
|
2
2
|
import { Animated, StyleSheet, View, Easing } from "react-native";
|
|
3
3
|
import {
|
|
4
4
|
PanGestureHandler,
|
|
5
|
-
NativeViewGestureHandler,
|
|
6
5
|
State,
|
|
7
6
|
TapGestureHandler,
|
|
8
7
|
} from "react-native-gesture-handler";
|
|
9
8
|
|
|
10
9
|
import { SnapPoints } from "./SnapPoints";
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
import {
|
|
11
|
+
BottomSheetContext,
|
|
12
|
+
BottomSheetContextValue,
|
|
13
|
+
} from "@applicaster/zapp-react-native-utils/bottomSheet";
|
|
14
|
+
|
|
15
|
+
// Must stay false: the sheet drives _dragY from old-API PanGestureHandler
|
|
16
|
+
// onGestureEvent props via RN Animated.event. This RNGH version cannot attach
|
|
17
|
+
// a native (useNativeDriver: true) Animated.event to those handlers — it leaks
|
|
18
|
+
// the AnimatedEvent object into the JS event path and throws "Expected
|
|
19
|
+
// onGestureHandlerEvent listener to be a function". Native-threaded dragging
|
|
20
|
+
// needs the full reanimated + Gesture-API migration (tracked separately).
|
|
21
|
+
const USE_NATIVE_DRIVER = false;
|
|
13
22
|
const HEADER_HEIGHT = 44;
|
|
14
23
|
|
|
15
24
|
const ANIMATION_DURATION = 400;
|
|
@@ -24,7 +33,7 @@ const styles = StyleSheet.create({
|
|
|
24
33
|
});
|
|
25
34
|
|
|
26
35
|
type Props = {
|
|
27
|
-
children:
|
|
36
|
+
children: ReactElement;
|
|
28
37
|
header: (boolean) => ReactElement;
|
|
29
38
|
dismiss: () => void;
|
|
30
39
|
visible: boolean;
|
|
@@ -49,35 +58,24 @@ export class DraggableBottomSheet extends Component<Props, ComponentState> {
|
|
|
49
58
|
_isMounted = false;
|
|
50
59
|
_animatedIn = false;
|
|
51
60
|
_animating = false;
|
|
52
|
-
_lastScrollYValue: number;
|
|
53
|
-
_lastScrollY: Animated.Value;
|
|
54
61
|
_dragY: Animated.Value;
|
|
55
|
-
_reverseLastScrollY: Animated.AnimatedInterpolation<number>;
|
|
56
62
|
_translateYOffset: Animated.Value;
|
|
57
63
|
_translateY: Animated.AnimatedInterpolation<number>;
|
|
58
64
|
_snapPoints: SnapPoints;
|
|
59
65
|
_prevContentHeight = 0;
|
|
66
|
+
_bottomSheetContextValue: BottomSheetContextValue;
|
|
60
67
|
|
|
61
68
|
_callWhenMounted: () => void | null;
|
|
62
69
|
|
|
63
|
-
|
|
70
|
+
// One shared JS Animated.event feeds _dragY for every drag handler (top
|
|
71
|
+
// handle, header drag-area, body). Safe because useNativeDriver is false — the
|
|
72
|
+
// event is a plain JS listener that setValue's _dragY for whichever handler
|
|
73
|
+
// is active, and only one gesture is ever active at a time.
|
|
64
74
|
_onGestureEvent: (...args: any[]) => void;
|
|
65
75
|
|
|
66
76
|
constructor(props) {
|
|
67
77
|
super(props);
|
|
68
78
|
|
|
69
|
-
this._lastScrollYValue = 0;
|
|
70
|
-
this._lastScrollY = new Animated.Value(0);
|
|
71
|
-
|
|
72
|
-
this._onRegisterLastScroll = Animated.event(
|
|
73
|
-
[{ nativeEvent: { contentOffset: { y: this._lastScrollY } } }],
|
|
74
|
-
{ useNativeDriver: USE_NATIVE_DRIVER }
|
|
75
|
-
);
|
|
76
|
-
|
|
77
|
-
this._lastScrollY.addListener(({ value }) => {
|
|
78
|
-
this._lastScrollYValue = value;
|
|
79
|
-
});
|
|
80
|
-
|
|
81
79
|
this._dragY = new Animated.Value(0);
|
|
82
80
|
|
|
83
81
|
this._onGestureEvent = Animated.event(
|
|
@@ -85,10 +83,11 @@ export class DraggableBottomSheet extends Component<Props, ComponentState> {
|
|
|
85
83
|
{ useNativeDriver: USE_NATIVE_DRIVER }
|
|
86
84
|
);
|
|
87
85
|
|
|
88
|
-
this.
|
|
89
|
-
|
|
90
|
-
this.
|
|
91
|
-
|
|
86
|
+
this._bottomSheetContextValue = {
|
|
87
|
+
masterDrawerRef: this.masterdrawer,
|
|
88
|
+
onDragGestureEvent: this._onGestureEvent,
|
|
89
|
+
onDragStateChange: this._onHeaderHandlerStateChange,
|
|
90
|
+
};
|
|
92
91
|
|
|
93
92
|
const initialSnapPoints = new SnapPoints(props.height, props.sheetHeight);
|
|
94
93
|
|
|
@@ -181,7 +180,7 @@ export class DraggableBottomSheet extends Component<Props, ComponentState> {
|
|
|
181
180
|
|
|
182
181
|
this._translateY = Animated.add(
|
|
183
182
|
this._translateYOffset,
|
|
184
|
-
|
|
183
|
+
this._dragY
|
|
185
184
|
).interpolate({
|
|
186
185
|
inputRange: [this._snapPoints.top, this._snapPoints.bottom],
|
|
187
186
|
outputRange: [this._snapPoints.top, this._snapPoints.bottom],
|
|
@@ -226,10 +225,6 @@ export class DraggableBottomSheet extends Component<Props, ComponentState> {
|
|
|
226
225
|
return;
|
|
227
226
|
}
|
|
228
227
|
|
|
229
|
-
if (nativeEvent.oldState === State.BEGAN) {
|
|
230
|
-
this._lastScrollY.setValue(0);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
228
|
this._onHandlerStateChange({ nativeEvent });
|
|
234
229
|
};
|
|
235
230
|
|
|
@@ -240,8 +235,7 @@ export class DraggableBottomSheet extends Component<Props, ComponentState> {
|
|
|
240
235
|
|
|
241
236
|
if (nativeEvent.oldState === State.ACTIVE) {
|
|
242
237
|
const { velocityY } = nativeEvent;
|
|
243
|
-
|
|
244
|
-
translationY -= this._lastScrollYValue;
|
|
238
|
+
const { translationY } = nativeEvent;
|
|
245
239
|
const dragToss = 0.05;
|
|
246
240
|
|
|
247
241
|
const endOffsetY =
|
|
@@ -300,7 +294,7 @@ export class DraggableBottomSheet extends Component<Props, ComponentState> {
|
|
|
300
294
|
>
|
|
301
295
|
<PanGestureHandler
|
|
302
296
|
ref={this.drawerheader}
|
|
303
|
-
simultaneousHandlers={[this.
|
|
297
|
+
simultaneousHandlers={[this.masterdrawer]}
|
|
304
298
|
shouldCancelWhenOutside={false}
|
|
305
299
|
onGestureEvent={this._onGestureEvent}
|
|
306
300
|
onHandlerStateChange={this._onHeaderHandlerStateChange}
|
|
@@ -309,31 +303,11 @@ export class DraggableBottomSheet extends Component<Props, ComponentState> {
|
|
|
309
303
|
{this.props?.header(this.state.dragging)}
|
|
310
304
|
</Animated.View>
|
|
311
305
|
</PanGestureHandler>
|
|
312
|
-
<
|
|
313
|
-
ref={this.drawer}
|
|
314
|
-
simultaneousHandlers={[this.scroll, this.masterdrawer]}
|
|
315
|
-
shouldCancelWhenOutside={false}
|
|
316
|
-
onGestureEvent={this._onGestureEvent}
|
|
317
|
-
onHandlerStateChange={this._onHandlerStateChange}
|
|
318
|
-
>
|
|
306
|
+
<BottomSheetContext.Provider value={this._bottomSheetContextValue}>
|
|
319
307
|
<Animated.View style={styles.container}>
|
|
320
|
-
|
|
321
|
-
ref={this.scroll}
|
|
322
|
-
waitFor={this.masterdrawer}
|
|
323
|
-
simultaneousHandlers={this.drawer}
|
|
324
|
-
>
|
|
325
|
-
<Animated.ScrollView
|
|
326
|
-
bounces={false}
|
|
327
|
-
onScrollBeginDrag={this._onRegisterLastScroll}
|
|
328
|
-
scrollEventThrottle={1}
|
|
329
|
-
scrollEnabled={false}
|
|
330
|
-
showsVerticalScrollIndicator={false}
|
|
331
|
-
>
|
|
332
|
-
{this.props.children}
|
|
333
|
-
</Animated.ScrollView>
|
|
334
|
-
</NativeViewGestureHandler>
|
|
308
|
+
{this.props.children}
|
|
335
309
|
</Animated.View>
|
|
336
|
-
</
|
|
310
|
+
</BottomSheetContext.Provider>
|
|
337
311
|
</Animated.View>
|
|
338
312
|
</View>
|
|
339
313
|
</TapGestureHandler>
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
import { subscriber } from "@applicaster/zapp-react-native-utils/functionUtils";
|
|
2
|
+
import { subscriberFor } from "@applicaster/zapp-react-native-utils/reactHooks/useSubscriberFor";
|
|
3
|
+
|
|
4
|
+
// events accepted on the shared cross-package event bus (see useSubscriberFor),
|
|
5
|
+
// so packages that must not depend on quick-brick-core (e.g. OfflineHandler in
|
|
6
|
+
// zapp-react-native-ui-components) can trigger a toast by event name alone,
|
|
7
|
+
// without importing notificationToastManager directly.
|
|
8
|
+
const unwrapEventPayload = (payload: unknown) =>
|
|
9
|
+
Array.isArray(payload) ? payload[0] : payload;
|
|
10
|
+
|
|
11
|
+
export const NOTIFICATION_TOAST_EVENTS = {
|
|
12
|
+
SHOW_TOAST: "showToast", // in: request to enqueue a toast
|
|
13
|
+
DISMISS: "dismiss", // in: renderer/user dismissed the visible toast early
|
|
14
|
+
SHOW: "show", // out: renderer should display this toast
|
|
15
|
+
HIDE: "hide", // out: renderer should hide the current toast
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
export interface ToastStyle {
|
|
19
|
+
backgroundColor?: string;
|
|
20
|
+
color?: string;
|
|
21
|
+
fontFamily?: string;
|
|
22
|
+
fontSize?: number;
|
|
23
|
+
lineHeight?: number;
|
|
24
|
+
letterSpacing?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface ToastPayload {
|
|
28
|
+
id?: string;
|
|
29
|
+
message: string;
|
|
30
|
+
extraMessage?: string; // secondary line; TV renderers show it as a subtitle, mobile ignores it
|
|
31
|
+
timeout?: number; // ms; if omitted, toast stays until dismissed
|
|
32
|
+
style?: ToastStyle;
|
|
33
|
+
[key: string]: unknown;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
class NotificationToastManager {
|
|
37
|
+
private static instance: NotificationToastManager;
|
|
38
|
+
|
|
39
|
+
private eventHandler = subscriber();
|
|
40
|
+
private queue: ToastPayload[] = [];
|
|
41
|
+
private hideTimer: ReturnType<typeof setTimeout> | null = null;
|
|
42
|
+
private isShowing = false;
|
|
43
|
+
private nextId = 0;
|
|
44
|
+
|
|
45
|
+
private constructor() {
|
|
46
|
+
// subscribe to the shared event bus so other packages can trigger toasts
|
|
47
|
+
subscriberFor(
|
|
48
|
+
NOTIFICATION_TOAST_EVENTS.SHOW_TOAST,
|
|
49
|
+
this.handleShowToastEvent
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
subscriberFor(NOTIFICATION_TOAST_EVENTS.DISMISS, this.handleDismiss);
|
|
53
|
+
|
|
54
|
+
this.getCurrentState = this.getCurrentState.bind(this);
|
|
55
|
+
this.subscribe = this.subscribe.bind(this);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
static getInstance(): NotificationToastManager {
|
|
59
|
+
if (!NotificationToastManager.instance) {
|
|
60
|
+
NotificationToastManager.instance = new NotificationToastManager();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return NotificationToastManager.instance;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
on(event: string, callback: (...args: any[]) => void) {
|
|
67
|
+
this.eventHandler.on(event, callback);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
removeHandler(event: string, callback: (...args: any[]) => void) {
|
|
71
|
+
this.eventHandler.removeHandler(event, callback);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
showToast(payload: ToastPayload) {
|
|
75
|
+
this.handleShowToast(payload);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
dismiss() {
|
|
79
|
+
this.handleDismiss();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
getCurrentState() {
|
|
83
|
+
return this.queue[0] ?? null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
subscribe(callback) {
|
|
87
|
+
this.eventHandler.on(NOTIFICATION_TOAST_EVENTS.SHOW, callback);
|
|
88
|
+
|
|
89
|
+
this.eventHandler.on(NOTIFICATION_TOAST_EVENTS.HIDE, callback);
|
|
90
|
+
|
|
91
|
+
return () => {
|
|
92
|
+
this.eventHandler.removeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, callback);
|
|
93
|
+
|
|
94
|
+
this.eventHandler.removeHandler(NOTIFICATION_TOAST_EVENTS.HIDE, callback);
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// subscriberFor's shared bus can deliver the raw payload (component emitters,
|
|
99
|
+
// see useSubscriberFor) or an args array (postEvent-style callers) - accept both.
|
|
100
|
+
private handleShowToastEvent = (payload: unknown) => {
|
|
101
|
+
if (!payload) {
|
|
102
|
+
console.warn(
|
|
103
|
+
"NotificationToastManager: Invalid showToast payload:",
|
|
104
|
+
payload
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const unwrappedPayload = unwrapEventPayload(payload) as ToastPayload;
|
|
111
|
+
|
|
112
|
+
if (!unwrappedPayload || typeof unwrappedPayload.message !== "string") {
|
|
113
|
+
console.warn(
|
|
114
|
+
"NotificationToastManager: Invalid showToast payload:",
|
|
115
|
+
payload
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
this.handleShowToast(unwrappedPayload);
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
private handleShowToast = (payload: ToastPayload) => {
|
|
125
|
+
const item = { ...payload, id: payload.id ?? String(this.nextId++) };
|
|
126
|
+
|
|
127
|
+
const queuedIndex = this.queue.findIndex((queued) => queued.id === item.id);
|
|
128
|
+
|
|
129
|
+
if (queuedIndex === -1) {
|
|
130
|
+
this.queue.push(item);
|
|
131
|
+
} else if (queuedIndex === 0 && this.isShowing) {
|
|
132
|
+
// this id is the toast currently on screen - swap its content in place
|
|
133
|
+
// (new one appears in front of it) and dismiss the old timer/content
|
|
134
|
+
this.clearTimer();
|
|
135
|
+
this.queue[0] = item;
|
|
136
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, item);
|
|
137
|
+
|
|
138
|
+
if (item.timeout) {
|
|
139
|
+
this.hideTimer = setTimeout(() => this.finishCurrent(), item.timeout);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return;
|
|
143
|
+
} else {
|
|
144
|
+
// still queued, not yet shown - update it in place instead of duplicating
|
|
145
|
+
this.queue[queuedIndex] = item;
|
|
146
|
+
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (!this.isShowing) {
|
|
151
|
+
this.processNext();
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
private handleDismiss = () => {
|
|
156
|
+
if (!this.isShowing) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
this.finishCurrent();
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
private processNext() {
|
|
164
|
+
const next = this.queue[0];
|
|
165
|
+
|
|
166
|
+
if (!next) {
|
|
167
|
+
this.isShowing = false;
|
|
168
|
+
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
this.isShowing = true;
|
|
173
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, next);
|
|
174
|
+
|
|
175
|
+
if (next.timeout) {
|
|
176
|
+
this.hideTimer = setTimeout(() => this.finishCurrent(), next.timeout);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
private finishCurrent() {
|
|
181
|
+
this.clearTimer();
|
|
182
|
+
this.queue.shift();
|
|
183
|
+
this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.HIDE);
|
|
184
|
+
this.isShowing = false;
|
|
185
|
+
this.processNext();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
private clearTimer() {
|
|
189
|
+
if (this.hideTimer) {
|
|
190
|
+
clearTimeout(this.hideTimer);
|
|
191
|
+
this.hideTimer = null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
public reset(): void {
|
|
196
|
+
this.clearTimer();
|
|
197
|
+
this.queue = [];
|
|
198
|
+
this.isShowing = false;
|
|
199
|
+
this.nextId = 0;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export const notificationToastManager = NotificationToastManager.getInstance();
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import React, { useEffect, useRef, useState } from "react";
|
|
2
|
+
import {
|
|
3
|
+
View,
|
|
4
|
+
Text,
|
|
5
|
+
Animated,
|
|
6
|
+
StyleSheet,
|
|
7
|
+
TouchableWithoutFeedback,
|
|
8
|
+
ViewStyle,
|
|
9
|
+
} from "react-native";
|
|
10
|
+
|
|
11
|
+
import { notificationToastManager } from "./NotificationToastManager";
|
|
12
|
+
import { useNotificationToastState } from "./useNotificationToastState";
|
|
13
|
+
import { useNotificationHeight } from "./useNotificationHeight";
|
|
14
|
+
|
|
15
|
+
const styles = StyleSheet.create({
|
|
16
|
+
notificationView: {
|
|
17
|
+
flex: 1,
|
|
18
|
+
position: "absolute",
|
|
19
|
+
alignItems: "center",
|
|
20
|
+
justifyContent: "center",
|
|
21
|
+
top: 0,
|
|
22
|
+
left: 0,
|
|
23
|
+
right: 0,
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
type NumberBoolean = 0 | 1;
|
|
28
|
+
|
|
29
|
+
export const NotificationToastRenderer = ({ children }) => {
|
|
30
|
+
const { statusHeight, notificationHeight } = useNotificationHeight();
|
|
31
|
+
|
|
32
|
+
const toast = useNotificationToastState();
|
|
33
|
+
|
|
34
|
+
const [notificationOpacity, setNotificationOpacity] =
|
|
35
|
+
useState<NumberBoolean>(0);
|
|
36
|
+
|
|
37
|
+
const yPosition = useRef(new Animated.Value(0)).current;
|
|
38
|
+
|
|
39
|
+
const backgroundColor = toast?.style?.backgroundColor ?? "#000";
|
|
40
|
+
const color = toast?.style?.color ?? "#fff";
|
|
41
|
+
|
|
42
|
+
const textStyles = {
|
|
43
|
+
color,
|
|
44
|
+
fontFamily: toast?.style?.fontFamily,
|
|
45
|
+
fontSize: toast?.style?.fontSize,
|
|
46
|
+
lineHeight: toast?.style?.lineHeight,
|
|
47
|
+
letterSpacing: toast?.style?.letterSpacing,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
const animatedViewStyles: ViewStyle = {
|
|
51
|
+
...styles.notificationView,
|
|
52
|
+
transform: [
|
|
53
|
+
{
|
|
54
|
+
translateY: yPosition.interpolate({
|
|
55
|
+
inputRange: [0, 1],
|
|
56
|
+
outputRange: [-notificationHeight, 0],
|
|
57
|
+
}),
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
opacity: notificationOpacity,
|
|
61
|
+
height: notificationHeight,
|
|
62
|
+
width: "100%",
|
|
63
|
+
|
|
64
|
+
backgroundColor,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
if (toast) {
|
|
69
|
+
setNotificationOpacity(1);
|
|
70
|
+
|
|
71
|
+
Animated.timing(yPosition, {
|
|
72
|
+
toValue: 1,
|
|
73
|
+
useNativeDriver: true,
|
|
74
|
+
duration: 300,
|
|
75
|
+
}).start();
|
|
76
|
+
} else {
|
|
77
|
+
Animated.timing(yPosition, {
|
|
78
|
+
toValue: 0,
|
|
79
|
+
useNativeDriver: true,
|
|
80
|
+
duration: 300,
|
|
81
|
+
}).start(() => setNotificationOpacity(0));
|
|
82
|
+
}
|
|
83
|
+
}, [toast]);
|
|
84
|
+
|
|
85
|
+
return (
|
|
86
|
+
<View testID="NotificationToastRenderer" style={StyleSheet.absoluteFill}>
|
|
87
|
+
{children}
|
|
88
|
+
<TouchableWithoutFeedback
|
|
89
|
+
testID="NotificationToastRenderer-touchable"
|
|
90
|
+
style={styles.notificationView}
|
|
91
|
+
onPress={() => notificationToastManager.dismiss()}
|
|
92
|
+
>
|
|
93
|
+
<Animated.View style={animatedViewStyles}>
|
|
94
|
+
<View style={{ height: statusHeight, backgroundColor }} />
|
|
95
|
+
<Text testID="NotificationToastRenderer-message" style={textStyles}>
|
|
96
|
+
{toast?.message}
|
|
97
|
+
</Text>
|
|
98
|
+
</Animated.View>
|
|
99
|
+
</TouchableWithoutFeedback>
|
|
100
|
+
</View>
|
|
101
|
+
);
|
|
102
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { notificationToastManager } from "./NotificationToastManager";
|
|
4
|
+
import { useNotificationToastState } from "./useNotificationToastState";
|
|
5
|
+
|
|
6
|
+
const styles: Record<string, React.CSSProperties> = {
|
|
7
|
+
body: {
|
|
8
|
+
position: "absolute",
|
|
9
|
+
bottom: 0,
|
|
10
|
+
left: 0,
|
|
11
|
+
right: 0,
|
|
12
|
+
width: 1920,
|
|
13
|
+
height: 200,
|
|
14
|
+
background:
|
|
15
|
+
"linear-gradient(180deg, rgba(17,17,17,0.9) 0%, rgba(17,17,17,1) 49%, rgba(0,0,0,0.85) 100%)",
|
|
16
|
+
display: "flex",
|
|
17
|
+
alignItems: "center",
|
|
18
|
+
justifyContent: "center",
|
|
19
|
+
flexDirection: "column",
|
|
20
|
+
color: "#fff",
|
|
21
|
+
paddingBottom: 15,
|
|
22
|
+
},
|
|
23
|
+
title: {
|
|
24
|
+
fontSize: 28,
|
|
25
|
+
margin: 0,
|
|
26
|
+
padding: 0,
|
|
27
|
+
},
|
|
28
|
+
subtitle: {
|
|
29
|
+
fontSize: 20,
|
|
30
|
+
margin: 0,
|
|
31
|
+
padding: 0,
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const NotificationToastRenderer = ({ children }) => {
|
|
36
|
+
const toast = useNotificationToastState();
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<div
|
|
40
|
+
data-testid="NotificationToastRenderer"
|
|
41
|
+
onClick={() => notificationToastManager.dismiss()}
|
|
42
|
+
>
|
|
43
|
+
{children}
|
|
44
|
+
{toast ? (
|
|
45
|
+
<div
|
|
46
|
+
data-testid="NotificationToastRenderer-touchable"
|
|
47
|
+
style={styles.body}
|
|
48
|
+
>
|
|
49
|
+
<div style={styles.title}>
|
|
50
|
+
<h2 data-testid="NotificationToastRenderer-message">
|
|
51
|
+
{toast.message}
|
|
52
|
+
</h2>
|
|
53
|
+
</div>
|
|
54
|
+
{toast.extraMessage ? (
|
|
55
|
+
<h4 style={styles.subtitle}>{toast.extraMessage}</h4>
|
|
56
|
+
) : null}
|
|
57
|
+
</div>
|
|
58
|
+
) : null}
|
|
59
|
+
</div>
|
|
60
|
+
);
|
|
61
|
+
};
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import {
|
|
2
|
+
notificationToastManager,
|
|
3
|
+
NOTIFICATION_TOAST_EVENTS,
|
|
4
|
+
} from "../NotificationToastManager";
|
|
5
|
+
import { postEvent } from "@applicaster/zapp-react-native-utils/reactHooks/useSubscriberFor";
|
|
6
|
+
|
|
7
|
+
jest.useFakeTimers();
|
|
8
|
+
|
|
9
|
+
// notificationToastManager is a module-level singleton with a private
|
|
10
|
+
// queue/isShowing/nextId, so every test must register its handlers through
|
|
11
|
+
// `on()` (tracked below for cleanup) and fully drain the queue afterwards -
|
|
12
|
+
// otherwise state leaks into the next test.
|
|
13
|
+
describe("NotificationToastManager", () => {
|
|
14
|
+
let registered: Array<[string, (...args: any[]) => void]> = [];
|
|
15
|
+
|
|
16
|
+
const on = (event: string, handler: (...args: any[]) => void) => {
|
|
17
|
+
notificationToastManager.on(event, handler);
|
|
18
|
+
registered.push([event, handler]);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
beforeAll(() => {
|
|
22
|
+
jest.spyOn(console, "log").mockImplementation(() => {});
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterAll(() => {
|
|
26
|
+
(console.log as jest.Mock).mockRestore();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
afterEach(() => {
|
|
30
|
+
registered.forEach(([event, handler]) =>
|
|
31
|
+
notificationToastManager.removeHandler(event, handler)
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
registered = [];
|
|
35
|
+
|
|
36
|
+
// drain whatever is left showing/queued so the next test starts clean
|
|
37
|
+
notificationToastManager.reset();
|
|
38
|
+
|
|
39
|
+
jest.clearAllTimers();
|
|
40
|
+
jest.restoreAllMocks();
|
|
41
|
+
jest.spyOn(console, "log").mockImplementation(() => {});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("emits SHOW with the toast payload when showToast is called", () => {
|
|
45
|
+
const onShow = jest.fn();
|
|
46
|
+
on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
|
|
47
|
+
|
|
48
|
+
notificationToastManager.showToast({ id: "t1", message: "hello" });
|
|
49
|
+
|
|
50
|
+
expect(onShow).toHaveBeenCalledWith(
|
|
51
|
+
expect.objectContaining({ id: "t1", message: "hello" }),
|
|
52
|
+
expect.any(Function)
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("auto-generates distinct ids for payloads that don't provide one", () => {
|
|
57
|
+
const onShow = jest.fn();
|
|
58
|
+
on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
|
|
59
|
+
|
|
60
|
+
notificationToastManager.showToast({ message: "first" });
|
|
61
|
+
const firstId = onShow.mock.calls[0][0].id;
|
|
62
|
+
|
|
63
|
+
notificationToastManager.dismiss();
|
|
64
|
+
|
|
65
|
+
notificationToastManager.showToast({ message: "second" });
|
|
66
|
+
const secondId = onShow.mock.calls[1][0].id;
|
|
67
|
+
|
|
68
|
+
expect(typeof firstId).toBe("string");
|
|
69
|
+
expect(firstId).not.toEqual(secondId);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("keeps a caller-provided id instead of generating one", () => {
|
|
73
|
+
const onShow = jest.fn();
|
|
74
|
+
on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
|
|
75
|
+
|
|
76
|
+
notificationToastManager.showToast({ id: "custom-id", message: "hi" });
|
|
77
|
+
|
|
78
|
+
expect(onShow).toHaveBeenCalledWith(
|
|
79
|
+
expect.objectContaining({ id: "custom-id" }),
|
|
80
|
+
expect.any(Function)
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("queues a second toast and shows it only after the first is dismissed", () => {
|
|
85
|
+
const onShow = jest.fn();
|
|
86
|
+
on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
|
|
87
|
+
|
|
88
|
+
notificationToastManager.showToast({ id: "a", message: "first" });
|
|
89
|
+
notificationToastManager.showToast({ id: "b", message: "second" });
|
|
90
|
+
|
|
91
|
+
expect(onShow).toHaveBeenCalledTimes(1);
|
|
92
|
+
|
|
93
|
+
expect(onShow).toHaveBeenCalledWith(
|
|
94
|
+
expect.objectContaining({ id: "a" }),
|
|
95
|
+
expect.any(Function)
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
notificationToastManager.dismiss();
|
|
99
|
+
|
|
100
|
+
expect(onShow).toHaveBeenCalledTimes(2);
|
|
101
|
+
|
|
102
|
+
expect(onShow).toHaveBeenLastCalledWith(
|
|
103
|
+
expect.objectContaining({ id: "b" }),
|
|
104
|
+
expect.any(Function)
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("auto-hides after the timeout and advances to the next queued toast", () => {
|
|
109
|
+
const onShow = jest.fn();
|
|
110
|
+
const onHide = jest.fn();
|
|
111
|
+
on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
|
|
112
|
+
on(NOTIFICATION_TOAST_EVENTS.HIDE, onHide);
|
|
113
|
+
|
|
114
|
+
notificationToastManager.showToast({
|
|
115
|
+
id: "a",
|
|
116
|
+
message: "first",
|
|
117
|
+
timeout: 1000,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
notificationToastManager.showToast({ id: "b", message: "second" });
|
|
121
|
+
|
|
122
|
+
jest.advanceTimersByTime(1000);
|
|
123
|
+
|
|
124
|
+
expect(onHide).toHaveBeenCalledTimes(1);
|
|
125
|
+
|
|
126
|
+
expect(onShow).toHaveBeenLastCalledWith(
|
|
127
|
+
expect.objectContaining({ id: "b" }),
|
|
128
|
+
expect.any(Function)
|
|
129
|
+
);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("does nothing when dismiss is called with no toast showing", () => {
|
|
133
|
+
const onHide = jest.fn();
|
|
134
|
+
on(NOTIFICATION_TOAST_EVENTS.HIDE, onHide);
|
|
135
|
+
|
|
136
|
+
notificationToastManager.dismiss();
|
|
137
|
+
|
|
138
|
+
expect(onHide).not.toHaveBeenCalled();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("updates a still-queued (not yet shown) toast in place instead of duplicating it", () => {
|
|
142
|
+
const onShow = jest.fn();
|
|
143
|
+
on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
|
|
144
|
+
|
|
145
|
+
notificationToastManager.showToast({ id: "a", message: "first" });
|
|
146
|
+
notificationToastManager.showToast({ id: "b", message: "second" });
|
|
147
|
+
notificationToastManager.showToast({ id: "b", message: "second updated" });
|
|
148
|
+
|
|
149
|
+
notificationToastManager.dismiss();
|
|
150
|
+
|
|
151
|
+
expect(onShow).toHaveBeenCalledTimes(2);
|
|
152
|
+
|
|
153
|
+
expect(onShow).toHaveBeenLastCalledWith(
|
|
154
|
+
expect.objectContaining({ id: "b", message: "second updated" }),
|
|
155
|
+
expect.any(Function)
|
|
156
|
+
);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("swaps the content of the currently visible toast when re-shown with the same id", () => {
|
|
160
|
+
const onShow = jest.fn();
|
|
161
|
+
on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
|
|
162
|
+
|
|
163
|
+
notificationToastManager.showToast({
|
|
164
|
+
id: "a",
|
|
165
|
+
message: "first",
|
|
166
|
+
timeout: 5000,
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
notificationToastManager.showToast({ id: "a", message: "first updated" });
|
|
170
|
+
|
|
171
|
+
expect(onShow).toHaveBeenCalledTimes(2);
|
|
172
|
+
|
|
173
|
+
expect(onShow).toHaveBeenLastCalledWith(
|
|
174
|
+
expect.objectContaining({ id: "a", message: "first updated" }),
|
|
175
|
+
expect.any(Function)
|
|
176
|
+
);
|
|
177
|
+
|
|
178
|
+
// old timer must have been cleared, not still pending
|
|
179
|
+
jest.advanceTimersByTime(5000);
|
|
180
|
+
expect(onShow).toHaveBeenCalledTimes(2);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("routes cross-package showToast events via the shared event bus", () => {
|
|
184
|
+
const onShow = jest.fn();
|
|
185
|
+
on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
|
|
186
|
+
|
|
187
|
+
postEvent(NOTIFICATION_TOAST_EVENTS.SHOW_TOAST, [
|
|
188
|
+
{ id: "bus", message: "from event bus" },
|
|
189
|
+
]);
|
|
190
|
+
|
|
191
|
+
expect(onShow).toHaveBeenCalledWith(
|
|
192
|
+
expect.objectContaining({ id: "bus", message: "from event bus" }),
|
|
193
|
+
expect.any(Function)
|
|
194
|
+
);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it("stops emitting to a handler after it has been removed", () => {
|
|
198
|
+
const onShow = jest.fn();
|
|
199
|
+
notificationToastManager.on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
|
|
200
|
+
|
|
201
|
+
notificationToastManager.removeHandler(
|
|
202
|
+
NOTIFICATION_TOAST_EVENTS.SHOW,
|
|
203
|
+
onShow
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
notificationToastManager.showToast({
|
|
207
|
+
id: "removed-handler",
|
|
208
|
+
message: "hello",
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
expect(onShow).not.toHaveBeenCalled();
|
|
212
|
+
});
|
|
213
|
+
});
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Text } from "react-native";
|
|
3
|
+
import { act, fireEvent, render } from "@testing-library/react-native";
|
|
4
|
+
|
|
5
|
+
import { notificationToastManager } from "../NotificationToastManager";
|
|
6
|
+
import { NotificationToastRenderer } from "../NotificationToastRenderer";
|
|
7
|
+
|
|
8
|
+
jest.mock("react-native-safe-area-context", () => ({
|
|
9
|
+
useSafeAreaInsets: () => ({ top: 44 }),
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
describe("NotificationToastRenderer", () => {
|
|
13
|
+
beforeAll(() => {
|
|
14
|
+
jest.spyOn(console, "log").mockImplementation(() => {});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
afterAll(() => {
|
|
18
|
+
(console.log as jest.Mock).mockRestore();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
afterEach(() => {
|
|
22
|
+
// drain whatever is left showing/queued so the next test starts clean
|
|
23
|
+
for (let i = 0; i < 10; i++) {
|
|
24
|
+
notificationToastManager.dismiss();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
jest.restoreAllMocks();
|
|
28
|
+
jest.spyOn(console, "log").mockImplementation(() => {});
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("renders its children", () => {
|
|
32
|
+
const { getByText } = render(
|
|
33
|
+
<NotificationToastRenderer>
|
|
34
|
+
<Text>child content</Text>
|
|
35
|
+
</NotificationToastRenderer>
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
expect(getByText("child content")).toBeTruthy();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("shows no message before any toast is emitted", () => {
|
|
42
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
43
|
+
|
|
44
|
+
expect(
|
|
45
|
+
getByTestId("NotificationToastRenderer-message").props.children
|
|
46
|
+
).toBeUndefined();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("displays the message from a toast emitted by the manager", () => {
|
|
50
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
51
|
+
|
|
52
|
+
act(() => {
|
|
53
|
+
notificationToastManager.showToast({ message: "You are offline" });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
expect(
|
|
57
|
+
getByTestId("NotificationToastRenderer-message").props.children
|
|
58
|
+
).toBe("You are offline");
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it("applies the toast's style to the message text", () => {
|
|
62
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
63
|
+
|
|
64
|
+
act(() => {
|
|
65
|
+
notificationToastManager.showToast({
|
|
66
|
+
message: "styled",
|
|
67
|
+
style: { color: "#123456", fontSize: 20 },
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
expect(
|
|
72
|
+
getByTestId("NotificationToastRenderer-message").props.style
|
|
73
|
+
).toEqual(expect.objectContaining({ color: "#123456", fontSize: 20 }));
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("dismisses the toast when the touchable area is pressed", () => {
|
|
77
|
+
const dismissSpy = jest.spyOn(notificationToastManager, "dismiss");
|
|
78
|
+
|
|
79
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
80
|
+
|
|
81
|
+
fireEvent.press(getByTestId("NotificationToastRenderer-touchable"));
|
|
82
|
+
|
|
83
|
+
expect(dismissSpy).toHaveBeenCalled();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Text } from "react-native";
|
|
3
|
+
import TestRenderer, { act } from "react-test-renderer";
|
|
4
|
+
|
|
5
|
+
import { notificationToastManager } from "../NotificationToastManager";
|
|
6
|
+
import { NotificationToastRenderer } from "../NotificationToastRenderer.web";
|
|
7
|
+
|
|
8
|
+
// This variant renders plain DOM tags (div/h2/h4) for web, which
|
|
9
|
+
// @testing-library/react-native's `render` cannot host-detect, so it is
|
|
10
|
+
// exercised directly with react-test-renderer instead.
|
|
11
|
+
describe("NotificationToastRenderer (web)", () => {
|
|
12
|
+
beforeAll(() => {
|
|
13
|
+
jest.spyOn(console, "log").mockImplementation(() => {});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
afterAll(() => {
|
|
17
|
+
(console.log as jest.Mock).mockRestore();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
afterEach(() => {
|
|
21
|
+
for (let i = 0; i < 10; i++) {
|
|
22
|
+
notificationToastManager.dismiss();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
jest.restoreAllMocks();
|
|
26
|
+
jest.spyOn(console, "log").mockImplementation(() => {});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const createRenderer = (element: React.ReactElement) => {
|
|
30
|
+
let renderer;
|
|
31
|
+
|
|
32
|
+
act(() => {
|
|
33
|
+
renderer = TestRenderer.create(element);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return renderer;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
it("renders its children", () => {
|
|
40
|
+
const renderer = createRenderer(
|
|
41
|
+
<NotificationToastRenderer>
|
|
42
|
+
<Text>child content</Text>
|
|
43
|
+
</NotificationToastRenderer>
|
|
44
|
+
);
|
|
45
|
+
|
|
46
|
+
expect(renderer.root.findByType(Text).props.children).toBe("child content");
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("renders nothing for the toast body until a toast is shown", () => {
|
|
50
|
+
const renderer = createRenderer(<NotificationToastRenderer />);
|
|
51
|
+
|
|
52
|
+
expect(
|
|
53
|
+
renderer.root.findAll(
|
|
54
|
+
(node) =>
|
|
55
|
+
node.props["data-testid"] === "NotificationToastRenderer-touchable"
|
|
56
|
+
)
|
|
57
|
+
).toHaveLength(0);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("shows the toast message once the manager emits one", () => {
|
|
61
|
+
const renderer = createRenderer(<NotificationToastRenderer />);
|
|
62
|
+
|
|
63
|
+
act(() => {
|
|
64
|
+
notificationToastManager.showToast({ message: "You are offline" });
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const message = renderer.root.find(
|
|
68
|
+
(node) =>
|
|
69
|
+
node.props["data-testid"] === "NotificationToastRenderer-message"
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
expect(message.props.children).toBe("You are offline");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("dismisses the toast when the root element is clicked", () => {
|
|
76
|
+
const dismissSpy = jest.spyOn(notificationToastManager, "dismiss");
|
|
77
|
+
|
|
78
|
+
const renderer = createRenderer(<NotificationToastRenderer />);
|
|
79
|
+
|
|
80
|
+
const root = renderer.root.find(
|
|
81
|
+
(node) => node.props["data-testid"] === "NotificationToastRenderer"
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
act(() => {
|
|
85
|
+
root.props.onClick();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
expect(dismissSpy).toHaveBeenCalled();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react-native";
|
|
2
|
+
|
|
3
|
+
let mockInsets = { top: 0 };
|
|
4
|
+
const mockGetNavbarHeight = jest.fn();
|
|
5
|
+
|
|
6
|
+
jest.mock("react-native-safe-area-context", () => ({
|
|
7
|
+
useSafeAreaInsets: () => mockInsets,
|
|
8
|
+
}));
|
|
9
|
+
|
|
10
|
+
jest.mock("@applicaster/zapp-react-native-utils/reactHooks/layout", () => ({
|
|
11
|
+
getNavbarHeight: () => mockGetNavbarHeight(),
|
|
12
|
+
}));
|
|
13
|
+
|
|
14
|
+
import { useNotificationHeight } from "../useNotificationHeight";
|
|
15
|
+
|
|
16
|
+
describe("useNotificationHeight", () => {
|
|
17
|
+
beforeEach(() => {
|
|
18
|
+
mockGetNavbarHeight.mockClear();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("adds the safe area top inset to the nav bar height", () => {
|
|
22
|
+
mockInsets = { top: 44 };
|
|
23
|
+
mockGetNavbarHeight.mockReturnValueOnce(56);
|
|
24
|
+
|
|
25
|
+
const { result } = renderHook(() => useNotificationHeight());
|
|
26
|
+
|
|
27
|
+
expect(result.current).toEqual({
|
|
28
|
+
statusHeight: 44,
|
|
29
|
+
notificationHeight: 100,
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("falls back to a 0 status height when there is no top inset", () => {
|
|
34
|
+
mockInsets = { top: 0 };
|
|
35
|
+
mockGetNavbarHeight.mockReturnValueOnce(44);
|
|
36
|
+
|
|
37
|
+
const { result } = renderHook(() => useNotificationHeight());
|
|
38
|
+
|
|
39
|
+
expect(result.current).toEqual({
|
|
40
|
+
statusHeight: 0,
|
|
41
|
+
notificationHeight: 44,
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("uses getNavbarHeight for the nav bar height", () => {
|
|
46
|
+
mockInsets = { top: 20 };
|
|
47
|
+
mockGetNavbarHeight.mockReturnValueOnce(56);
|
|
48
|
+
|
|
49
|
+
const { result } = renderHook(() => useNotificationHeight());
|
|
50
|
+
|
|
51
|
+
expect(mockGetNavbarHeight).toHaveBeenCalled();
|
|
52
|
+
|
|
53
|
+
expect(result.current).toEqual({
|
|
54
|
+
statusHeight: 20,
|
|
55
|
+
notificationHeight: 76,
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { act, renderHook } from "@testing-library/react-native";
|
|
2
|
+
|
|
3
|
+
import { notificationToastManager } from "../NotificationToastManager";
|
|
4
|
+
import { useNotificationToastState } from "../useNotificationToastState";
|
|
5
|
+
|
|
6
|
+
jest.useFakeTimers();
|
|
7
|
+
|
|
8
|
+
describe("useNotificationToastState", () => {
|
|
9
|
+
beforeAll(() => {
|
|
10
|
+
jest.spyOn(console, "log").mockImplementation(() => {});
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
afterAll(() => {
|
|
14
|
+
(console.log as jest.Mock).mockRestore();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
// drain whatever is left showing/queued so the next test starts clean;
|
|
19
|
+
// the hook's own effect cleanup (on unmount) removes its SHOW/HIDE
|
|
20
|
+
// handlers automatically via RNTL's auto-cleanup.
|
|
21
|
+
for (let i = 0; i < 10; i++) {
|
|
22
|
+
notificationToastManager.dismiss();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
jest.restoreAllMocks();
|
|
26
|
+
jest.spyOn(console, "log").mockImplementation(() => {});
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("starts with no toast", () => {
|
|
30
|
+
const { result } = renderHook(() => useNotificationToastState());
|
|
31
|
+
|
|
32
|
+
expect(result.current).toBeNull();
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("reflects the toast emitted by the manager's SHOW event", () => {
|
|
36
|
+
const { result } = renderHook(() => useNotificationToastState());
|
|
37
|
+
|
|
38
|
+
act(() => {
|
|
39
|
+
notificationToastManager.showToast({ message: "hello" });
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
expect(result.current).toEqual(
|
|
43
|
+
expect.objectContaining({ message: "hello" })
|
|
44
|
+
);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("clears the toast when the manager emits HIDE", () => {
|
|
48
|
+
const { result } = renderHook(() => useNotificationToastState());
|
|
49
|
+
|
|
50
|
+
act(() => {
|
|
51
|
+
notificationToastManager.showToast({ message: "hello" });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
expect(result.current).not.toBeNull();
|
|
55
|
+
|
|
56
|
+
act(() => {
|
|
57
|
+
notificationToastManager.dismiss();
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
expect(result.current).toBeNull();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("stops updating after unmount", () => {
|
|
64
|
+
const { result, unmount } = renderHook(() => useNotificationToastState());
|
|
65
|
+
|
|
66
|
+
unmount();
|
|
67
|
+
|
|
68
|
+
act(() => {
|
|
69
|
+
notificationToastManager.showToast({ message: "after unmount" });
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
expect(result.current).toBeNull();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("subscribes on mount and disposes the subscription on unmount", () => {
|
|
76
|
+
const dispose = jest.fn();
|
|
77
|
+
|
|
78
|
+
const subscribeSpy = jest
|
|
79
|
+
.spyOn(notificationToastManager, "subscribe")
|
|
80
|
+
.mockReturnValue(dispose);
|
|
81
|
+
|
|
82
|
+
const { unmount } = renderHook(() => useNotificationToastState());
|
|
83
|
+
|
|
84
|
+
expect(subscribeSpy).toHaveBeenCalledWith(expect.any(Function));
|
|
85
|
+
expect(dispose).not.toHaveBeenCalled();
|
|
86
|
+
|
|
87
|
+
unmount();
|
|
88
|
+
|
|
89
|
+
expect(dispose).toHaveBeenCalledTimes(1);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("subscribe registers SHOW/HIDE handlers and its disposer removes them", () => {
|
|
93
|
+
const callback = jest.fn();
|
|
94
|
+
|
|
95
|
+
const dispose = notificationToastManager.subscribe(callback);
|
|
96
|
+
|
|
97
|
+
act(() => {
|
|
98
|
+
notificationToastManager.showToast({ message: "hello" }); // emits SHOW
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
expect(callback).toHaveBeenCalled();
|
|
102
|
+
|
|
103
|
+
callback.mockClear();
|
|
104
|
+
dispose();
|
|
105
|
+
|
|
106
|
+
act(() => {
|
|
107
|
+
notificationToastManager.showToast({ message: "again" }); // emits SHOW
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
expect(callback).not.toHaveBeenCalled();
|
|
111
|
+
});
|
|
112
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useSafeAreaInsets } from "react-native-safe-area-context";
|
|
2
|
+
import { getNavbarHeight } from "@applicaster/zapp-react-native-utils/reactHooks/layout";
|
|
3
|
+
|
|
4
|
+
export const useNotificationHeight = () => {
|
|
5
|
+
const insets = useSafeAreaInsets();
|
|
6
|
+
|
|
7
|
+
const navBarHeight = getNavbarHeight();
|
|
8
|
+
|
|
9
|
+
const statusHeight = insets.top;
|
|
10
|
+
const notificationHeight = statusHeight + navBarHeight;
|
|
11
|
+
|
|
12
|
+
return { statusHeight, notificationHeight };
|
|
13
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
|
|
3
|
+
import { notificationToastManager } from "./NotificationToastManager";
|
|
4
|
+
|
|
5
|
+
export const useNotificationToastState = () => {
|
|
6
|
+
const state = React.useSyncExternalStore(
|
|
7
|
+
notificationToastManager.subscribe,
|
|
8
|
+
notificationToastManager.getCurrentState
|
|
9
|
+
);
|
|
10
|
+
|
|
11
|
+
return state;
|
|
12
|
+
};
|
package/App/index.tsx
CHANGED
|
@@ -31,6 +31,7 @@ import { withActionExecutor } from "@applicaster/zapp-react-native-utils/actions
|
|
|
31
31
|
import { withScreenDataContextProvider } from "@applicaster/zapp-react-native-ui-components/Contexts/ScreenDataContext";
|
|
32
32
|
import { LogicAggregatorContainer } from "./LogicAggregatorContainer";
|
|
33
33
|
import { LoadingOverlay } from "./LoadingOverlay";
|
|
34
|
+
import { NotificationToastRenderer } from "./NotificationToastRenderer";
|
|
34
35
|
import { ZappAppWrapper } from "./components/ZappAppWrapper";
|
|
35
36
|
|
|
36
37
|
interface AppOptions {
|
|
@@ -67,11 +68,13 @@ const App = ({
|
|
|
67
68
|
<ErrorBoundary>
|
|
68
69
|
<URLSchemeListener>
|
|
69
70
|
<SplashLoader>
|
|
70
|
-
<
|
|
71
|
-
<
|
|
72
|
-
<
|
|
73
|
-
|
|
74
|
-
|
|
71
|
+
<NotificationToastRenderer>
|
|
72
|
+
<OfflineHandler>
|
|
73
|
+
<Layout>
|
|
74
|
+
<RouteManager />
|
|
75
|
+
</Layout>
|
|
76
|
+
</OfflineHandler>
|
|
77
|
+
</NotificationToastRenderer>
|
|
75
78
|
</SplashLoader>
|
|
76
79
|
<ModalProvider />
|
|
77
80
|
</URLSchemeListener>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/quick-brick-core",
|
|
3
|
-
"version": "16.0.0-alpha.
|
|
3
|
+
"version": "16.0.0-alpha.8716141508",
|
|
4
4
|
"description": "Core package for Applicaster's Quick Brick App",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -28,13 +28,13 @@
|
|
|
28
28
|
},
|
|
29
29
|
"homepage": "https://github.com/applicaster/quickbrick#readme",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@applicaster/applicaster-types": "16.0.0-alpha.
|
|
32
|
-
"@applicaster/quick-brick-core-plugins": "16.0.0-alpha.
|
|
33
|
-
"@applicaster/zapp-pipes-v2-client": "16.0.0-alpha.
|
|
34
|
-
"@applicaster/zapp-react-native-bridge": "16.0.0-alpha.
|
|
35
|
-
"@applicaster/zapp-react-native-redux": "16.0.0-alpha.
|
|
36
|
-
"@applicaster/zapp-react-native-ui-components": "16.0.0-alpha.
|
|
37
|
-
"@applicaster/zapp-react-native-utils": "16.0.0-alpha.
|
|
31
|
+
"@applicaster/applicaster-types": "16.0.0-alpha.8716141508",
|
|
32
|
+
"@applicaster/quick-brick-core-plugins": "16.0.0-alpha.8716141508",
|
|
33
|
+
"@applicaster/zapp-pipes-v2-client": "16.0.0-alpha.8716141508",
|
|
34
|
+
"@applicaster/zapp-react-native-bridge": "16.0.0-alpha.8716141508",
|
|
35
|
+
"@applicaster/zapp-react-native-redux": "16.0.0-alpha.8716141508",
|
|
36
|
+
"@applicaster/zapp-react-native-ui-components": "16.0.0-alpha.8716141508",
|
|
37
|
+
"@applicaster/zapp-react-native-utils": "16.0.0-alpha.8716141508",
|
|
38
38
|
"atob": "^2.1.2",
|
|
39
39
|
"axios": "^0.28.0",
|
|
40
40
|
"btoa": "^1.2.1",
|