@applicaster/quick-brick-core 15.1.5-rc.2 → 15.1.6-alpha.2015591222

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.
@@ -14,6 +14,8 @@ import {
14
14
  platformSelect,
15
15
  } from "@applicaster/zapp-react-native-utils/reactUtils";
16
16
 
17
+ import { useKeyboardHeight } from "./hooks";
18
+
17
19
  type Props = {
18
20
  children: React.ReactChild;
19
21
  backgroundColor: string;
@@ -104,6 +106,8 @@ export function ModalBottomSheetFrame(props: Props) {
104
106
 
105
107
  const sheetHeight = useGetSheetHeight(height, maxHeight);
106
108
 
109
+ const keyboardHeight = useKeyboardHeight();
110
+
107
111
  return (
108
112
  <DraggableBottomSheet
109
113
  sheetHeight={sheetHeight}
@@ -113,7 +117,11 @@ export function ModalBottomSheetFrame(props: Props) {
113
117
  header={(dragging) => <Handle {...handleProps} focused={dragging} />}
114
118
  maxWidth={maxWidth}
115
119
  {...dimensions}
116
- height={safeAreaDims.height - (Platform.OS === "ios" ? bottom : 0)}
120
+ height={
121
+ safeAreaDims.height -
122
+ (Platform.OS === "ios" ? bottom : 0) -
123
+ keyboardHeight
124
+ }
117
125
  >
118
126
  <SafeAreaView
119
127
  edges={["left", "right", "bottom"]}
@@ -0,0 +1,105 @@
1
+ import { act, renderHook } from "@testing-library/react-native";
2
+ import { Keyboard } from "react-native";
3
+
4
+ jest.mock("@applicaster/zapp-react-native-utils/reactUtils", () => ({
5
+ isApplePlatform: () => false,
6
+ }));
7
+
8
+ // Import after mocks so module-level isApple resolves to false.
9
+ // eslint-disable-next-line import/first
10
+ import { useKeyboardHeight } from "../useKeyboardHeight";
11
+
12
+ type KeyboardListener = (event?: {
13
+ endCoordinates: { height: number };
14
+ }) => void;
15
+
16
+ function setupKeyboardListenerMock() {
17
+ const listeners: Record<string, KeyboardListener> = {};
18
+ const removeMocks: Record<string, jest.Mock> = {};
19
+
20
+ const addListenerSpy = jest
21
+ .spyOn(Keyboard, "addListener")
22
+ .mockImplementation((event: string, callback: KeyboardListener) => {
23
+ listeners[event] = callback;
24
+
25
+ const remove = jest.fn();
26
+ removeMocks[event] = remove;
27
+
28
+ return { remove };
29
+ });
30
+
31
+ return { listeners, removeMocks, addListenerSpy };
32
+ }
33
+
34
+ describe("useKeyboardHeight (non-Apple platforms)", () => {
35
+ let listeners: Record<string, KeyboardListener>;
36
+ let removeMocks: Record<string, jest.Mock>;
37
+ let addListenerSpy: jest.SpyInstance;
38
+
39
+ beforeEach(() => {
40
+ const mock = setupKeyboardListenerMock();
41
+ listeners = mock.listeners;
42
+ removeMocks = mock.removeMocks;
43
+ addListenerSpy = mock.addListenerSpy;
44
+ });
45
+
46
+ afterEach(() => {
47
+ addListenerSpy.mockRestore();
48
+ });
49
+
50
+ it("returns 0 initially and subscribes to didShow / didHide events", () => {
51
+ const { result } = renderHook(() => useKeyboardHeight());
52
+
53
+ expect(result.current).toBe(0);
54
+ expect(addListenerSpy).toHaveBeenCalledTimes(2);
55
+
56
+ expect(addListenerSpy).toHaveBeenCalledWith(
57
+ "keyboardDidShow",
58
+ expect.any(Function)
59
+ );
60
+
61
+ expect(addListenerSpy).toHaveBeenCalledWith(
62
+ "keyboardDidHide",
63
+ expect.any(Function)
64
+ );
65
+ });
66
+
67
+ it("updates height when keyboardDidShow fires", () => {
68
+ const { result } = renderHook(() => useKeyboardHeight());
69
+
70
+ act(() => {
71
+ listeners.keyboardDidShow?.({
72
+ endCoordinates: { height: 412 },
73
+ });
74
+ });
75
+
76
+ expect(result.current).toBe(412);
77
+ });
78
+
79
+ it("resets height to 0 when keyboardDidHide fires", () => {
80
+ const { result } = renderHook(() => useKeyboardHeight());
81
+
82
+ act(() => {
83
+ listeners.keyboardDidShow?.({
84
+ endCoordinates: { height: 412 },
85
+ });
86
+ });
87
+
88
+ expect(result.current).toBe(412);
89
+
90
+ act(() => {
91
+ listeners.keyboardDidHide?.();
92
+ });
93
+
94
+ expect(result.current).toBe(0);
95
+ });
96
+
97
+ it("removes listeners on unmount", () => {
98
+ const { unmount } = renderHook(() => useKeyboardHeight());
99
+
100
+ unmount();
101
+
102
+ expect(removeMocks.keyboardDidShow).toHaveBeenCalledTimes(1);
103
+ expect(removeMocks.keyboardDidHide).toHaveBeenCalledTimes(1);
104
+ });
105
+ });
@@ -0,0 +1,102 @@
1
+ import { act, renderHook } from "@testing-library/react-native";
2
+ import { Keyboard } from "react-native";
3
+ import { useKeyboardHeight } from "../useKeyboardHeight";
4
+
5
+ jest.mock("@applicaster/zapp-react-native-utils/reactUtils", () => ({
6
+ isApplePlatform: () => true,
7
+ }));
8
+
9
+ type KeyboardListener = (event?: {
10
+ endCoordinates: { height: number };
11
+ }) => void;
12
+
13
+ function setupKeyboardListenerMock() {
14
+ const listeners: Record<string, KeyboardListener> = {};
15
+ const removeMocks: Record<string, jest.Mock> = {};
16
+
17
+ const addListenerSpy = jest
18
+ .spyOn(Keyboard, "addListener")
19
+ .mockImplementation((event: string, callback: KeyboardListener) => {
20
+ listeners[event] = callback;
21
+
22
+ const remove = jest.fn();
23
+ removeMocks[event] = remove;
24
+
25
+ return { remove };
26
+ });
27
+
28
+ return { listeners, removeMocks, addListenerSpy };
29
+ }
30
+
31
+ describe("useKeyboardHeight (Apple platforms)", () => {
32
+ let listeners: Record<string, KeyboardListener>;
33
+ let removeMocks: Record<string, jest.Mock>;
34
+ let addListenerSpy: jest.SpyInstance;
35
+
36
+ beforeEach(() => {
37
+ const mock = setupKeyboardListenerMock();
38
+ listeners = mock.listeners;
39
+ removeMocks = mock.removeMocks;
40
+ addListenerSpy = mock.addListenerSpy;
41
+ });
42
+
43
+ afterEach(() => {
44
+ addListenerSpy.mockRestore();
45
+ });
46
+
47
+ it("returns 0 initially and subscribes to willShow / willHide events", () => {
48
+ const { result } = renderHook(() => useKeyboardHeight());
49
+
50
+ expect(result.current).toBe(0);
51
+ expect(addListenerSpy).toHaveBeenCalledTimes(2);
52
+
53
+ expect(addListenerSpy).toHaveBeenCalledWith(
54
+ "keyboardWillShow",
55
+ expect.any(Function)
56
+ );
57
+
58
+ expect(addListenerSpy).toHaveBeenCalledWith(
59
+ "keyboardWillHide",
60
+ expect.any(Function)
61
+ );
62
+ });
63
+
64
+ it("updates height when keyboardWillShow fires", () => {
65
+ const { result } = renderHook(() => useKeyboardHeight());
66
+
67
+ act(() => {
68
+ listeners.keyboardWillShow?.({
69
+ endCoordinates: { height: 336 },
70
+ });
71
+ });
72
+
73
+ expect(result.current).toBe(336);
74
+ });
75
+
76
+ it("resets height to 0 when keyboardWillHide fires", () => {
77
+ const { result } = renderHook(() => useKeyboardHeight());
78
+
79
+ act(() => {
80
+ listeners.keyboardWillShow?.({
81
+ endCoordinates: { height: 280 },
82
+ });
83
+ });
84
+
85
+ expect(result.current).toBe(280);
86
+
87
+ act(() => {
88
+ listeners.keyboardWillHide?.();
89
+ });
90
+
91
+ expect(result.current).toBe(0);
92
+ });
93
+
94
+ it("removes listeners on unmount", () => {
95
+ const { unmount } = renderHook(() => useKeyboardHeight());
96
+
97
+ unmount();
98
+
99
+ expect(removeMocks.keyboardWillShow).toHaveBeenCalledTimes(1);
100
+ expect(removeMocks.keyboardWillHide).toHaveBeenCalledTimes(1);
101
+ });
102
+ });
@@ -0,0 +1 @@
1
+ export { useKeyboardHeight } from "./useKeyboardHeight";
@@ -0,0 +1,30 @@
1
+ import React from "react";
2
+ import { Keyboard, KeyboardEvent } from "react-native";
3
+ import { isApplePlatform } from "@applicaster/zapp-react-native-utils/reactUtils";
4
+
5
+ const isApple = isApplePlatform();
6
+
7
+ export function useKeyboardHeight() {
8
+ const [keyboardHeight, setKeyboardHeight] = React.useState(0);
9
+
10
+ React.useEffect(() => {
11
+ // iOS supports smooth 'willShow' / 'willHide' events
12
+ const showEvent = isApple ? "keyboardWillShow" : "keyboardDidShow";
13
+ const hideEvent = isApple ? "keyboardWillHide" : "keyboardDidHide";
14
+
15
+ const showListener = Keyboard.addListener(showEvent, (e: KeyboardEvent) => {
16
+ setKeyboardHeight(e.endCoordinates.height);
17
+ });
18
+
19
+ const hideListener = Keyboard.addListener(hideEvent, () => {
20
+ setKeyboardHeight(0);
21
+ });
22
+
23
+ return () => {
24
+ showListener.remove();
25
+ hideListener.remove();
26
+ };
27
+ }, []);
28
+
29
+ return keyboardHeight;
30
+ }
@@ -0,0 +1,209 @@
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 const DEFAULT_TOAST_TIMEOUT = 4000;
28
+
29
+ export interface ToastPayload {
30
+ id?: string;
31
+ message: string;
32
+ extraMessage?: string; // secondary line; TV renderers show it as a subtitle, mobile ignores it
33
+ timeout?: number; // ms; defaults to DEFAULT_TOAST_TIMEOUT (4000) if omitted
34
+ style?: ToastStyle;
35
+ [key: string]: unknown;
36
+ }
37
+
38
+ class NotificationToastManager {
39
+ private static instance: NotificationToastManager;
40
+
41
+ private eventHandler = subscriber();
42
+ private queue: ToastPayload[] = [];
43
+ private hideTimer: ReturnType<typeof setTimeout> | null = null;
44
+ private isShowing = false;
45
+ private nextId = 0;
46
+
47
+ private constructor() {
48
+ // subscribe to the shared event bus so other packages can trigger toasts
49
+ subscriberFor(
50
+ NOTIFICATION_TOAST_EVENTS.SHOW_TOAST,
51
+ this.handleShowToastEvent
52
+ );
53
+
54
+ subscriberFor(NOTIFICATION_TOAST_EVENTS.DISMISS, this.handleDismiss);
55
+
56
+ this.getCurrentState = this.getCurrentState.bind(this);
57
+ this.subscribe = this.subscribe.bind(this);
58
+ }
59
+
60
+ static getInstance(): NotificationToastManager {
61
+ if (!NotificationToastManager.instance) {
62
+ NotificationToastManager.instance = new NotificationToastManager();
63
+ }
64
+
65
+ return NotificationToastManager.instance;
66
+ }
67
+
68
+ on(event: string, callback: (...args: any[]) => void) {
69
+ this.eventHandler.on(event, callback);
70
+ }
71
+
72
+ removeHandler(event: string, callback: (...args: any[]) => void) {
73
+ this.eventHandler.removeHandler(event, callback);
74
+ }
75
+
76
+ showToast(payload: ToastPayload) {
77
+ this.handleShowToast(payload);
78
+ }
79
+
80
+ dismiss() {
81
+ this.handleDismiss();
82
+ }
83
+
84
+ getCurrentState() {
85
+ return this.queue[0] ?? null;
86
+ }
87
+
88
+ subscribe(callback) {
89
+ this.eventHandler.on(NOTIFICATION_TOAST_EVENTS.SHOW, callback);
90
+
91
+ this.eventHandler.on(NOTIFICATION_TOAST_EVENTS.HIDE, callback);
92
+
93
+ return () => {
94
+ this.eventHandler.removeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, callback);
95
+
96
+ this.eventHandler.removeHandler(NOTIFICATION_TOAST_EVENTS.HIDE, callback);
97
+ };
98
+ }
99
+
100
+ // subscriberFor's shared bus can deliver the raw payload (component emitters,
101
+ // see useSubscriberFor) or an args array (postEvent-style callers) - accept both.
102
+ private handleShowToastEvent = (payload: unknown) => {
103
+ if (!payload) {
104
+ console.warn(
105
+ "NotificationToastManager: Invalid showToast payload:",
106
+ payload
107
+ );
108
+
109
+ return;
110
+ }
111
+
112
+ const unwrappedPayload = unwrapEventPayload(payload) as ToastPayload;
113
+
114
+ if (!unwrappedPayload || typeof unwrappedPayload.message !== "string") {
115
+ console.warn(
116
+ "NotificationToastManager: Invalid showToast payload:",
117
+ payload
118
+ );
119
+
120
+ return;
121
+ }
122
+
123
+ this.handleShowToast(unwrappedPayload);
124
+ };
125
+
126
+ private handleShowToast = (payload: ToastPayload) => {
127
+ const item = {
128
+ ...payload,
129
+ id: payload.id ?? String(this.nextId++),
130
+ timeout: payload.timeout ?? DEFAULT_TOAST_TIMEOUT,
131
+ };
132
+
133
+ const queuedIndex = this.queue.findIndex((queued) => queued.id === item.id);
134
+
135
+ if (queuedIndex === -1) {
136
+ this.queue.push(item);
137
+ } else if (queuedIndex === 0 && this.isShowing) {
138
+ // this id is the toast currently on screen - swap its content in place
139
+ // (new one appears in front of it) and dismiss the old timer/content
140
+ this.clearTimer();
141
+ this.queue[0] = item;
142
+ this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, item);
143
+
144
+ if (item.timeout) {
145
+ this.hideTimer = setTimeout(() => this.finishCurrent(), item.timeout);
146
+ }
147
+
148
+ return;
149
+ } else {
150
+ // still queued, not yet shown - update it in place instead of duplicating
151
+ this.queue[queuedIndex] = item;
152
+
153
+ return;
154
+ }
155
+
156
+ if (!this.isShowing) {
157
+ this.processNext();
158
+ }
159
+ };
160
+
161
+ private handleDismiss = () => {
162
+ if (!this.isShowing) {
163
+ return;
164
+ }
165
+
166
+ this.finishCurrent();
167
+ };
168
+
169
+ private processNext() {
170
+ const next = this.queue[0];
171
+
172
+ if (!next) {
173
+ this.isShowing = false;
174
+
175
+ return;
176
+ }
177
+
178
+ this.isShowing = true;
179
+ this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.SHOW, next);
180
+
181
+ if (next.timeout) {
182
+ this.hideTimer = setTimeout(() => this.finishCurrent(), next.timeout);
183
+ }
184
+ }
185
+
186
+ private finishCurrent() {
187
+ this.clearTimer();
188
+ this.queue.shift();
189
+ this.eventHandler.invokeHandler(NOTIFICATION_TOAST_EVENTS.HIDE);
190
+ this.isShowing = false;
191
+ this.processNext();
192
+ }
193
+
194
+ private clearTimer() {
195
+ if (this.hideTimer) {
196
+ clearTimeout(this.hideTimer);
197
+ this.hideTimer = null;
198
+ }
199
+ }
200
+
201
+ public reset(): void {
202
+ this.clearTimer();
203
+ this.queue = [];
204
+ this.isShowing = false;
205
+ this.nextId = 0;
206
+ }
207
+ }
208
+
209
+ 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,237 @@
1
+ import {
2
+ notificationToastManager,
3
+ NOTIFICATION_TOAST_EVENTS,
4
+ DEFAULT_TOAST_TIMEOUT,
5
+ } from "../NotificationToastManager";
6
+ import { postEvent } from "@applicaster/zapp-react-native-utils/reactHooks/useSubscriberFor";
7
+
8
+ jest.useFakeTimers();
9
+
10
+ // notificationToastManager is a module-level singleton with a private
11
+ // queue/isShowing/nextId, so every test must register its handlers through
12
+ // `on()` (tracked below for cleanup) and fully drain the queue afterwards -
13
+ // otherwise state leaks into the next test.
14
+ describe("NotificationToastManager", () => {
15
+ let registered: Array<[string, (...args: any[]) => void]> = [];
16
+
17
+ const on = (event: string, handler: (...args: any[]) => void) => {
18
+ notificationToastManager.on(event, handler);
19
+ registered.push([event, handler]);
20
+ };
21
+
22
+ beforeAll(() => {
23
+ jest.spyOn(console, "log").mockImplementation(() => {});
24
+ });
25
+
26
+ afterAll(() => {
27
+ (console.log as jest.Mock).mockRestore();
28
+ });
29
+
30
+ afterEach(() => {
31
+ registered.forEach(([event, handler]) =>
32
+ notificationToastManager.removeHandler(event, handler)
33
+ );
34
+
35
+ registered = [];
36
+
37
+ // drain whatever is left showing/queued so the next test starts clean
38
+ notificationToastManager.reset();
39
+
40
+ jest.clearAllTimers();
41
+ jest.restoreAllMocks();
42
+ jest.spyOn(console, "log").mockImplementation(() => {});
43
+ });
44
+
45
+ it("emits SHOW with the toast payload when showToast is called", () => {
46
+ const onShow = jest.fn();
47
+ on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
48
+
49
+ notificationToastManager.showToast({ id: "t1", message: "hello" });
50
+
51
+ expect(onShow).toHaveBeenCalledWith(
52
+ expect.objectContaining({ id: "t1", message: "hello" }),
53
+ expect.any(Function)
54
+ );
55
+ });
56
+
57
+ it("auto-generates distinct ids for payloads that don't provide one", () => {
58
+ const onShow = jest.fn();
59
+ on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
60
+
61
+ notificationToastManager.showToast({ message: "first" });
62
+ const firstId = onShow.mock.calls[0][0].id;
63
+
64
+ notificationToastManager.dismiss();
65
+
66
+ notificationToastManager.showToast({ message: "second" });
67
+ const secondId = onShow.mock.calls[1][0].id;
68
+
69
+ expect(typeof firstId).toBe("string");
70
+ expect(firstId).not.toEqual(secondId);
71
+ });
72
+
73
+ it("keeps a caller-provided id instead of generating one", () => {
74
+ const onShow = jest.fn();
75
+ on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
76
+
77
+ notificationToastManager.showToast({ id: "custom-id", message: "hi" });
78
+
79
+ expect(onShow).toHaveBeenCalledWith(
80
+ expect.objectContaining({ id: "custom-id" }),
81
+ expect.any(Function)
82
+ );
83
+ });
84
+
85
+ it("queues a second toast and shows it only after the first is dismissed", () => {
86
+ const onShow = jest.fn();
87
+ on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
88
+
89
+ notificationToastManager.showToast({ id: "a", message: "first" });
90
+ notificationToastManager.showToast({ id: "b", message: "second" });
91
+
92
+ expect(onShow).toHaveBeenCalledTimes(1);
93
+
94
+ expect(onShow).toHaveBeenCalledWith(
95
+ expect.objectContaining({ id: "a" }),
96
+ expect.any(Function)
97
+ );
98
+
99
+ notificationToastManager.dismiss();
100
+
101
+ expect(onShow).toHaveBeenCalledTimes(2);
102
+
103
+ expect(onShow).toHaveBeenLastCalledWith(
104
+ expect.objectContaining({ id: "b" }),
105
+ expect.any(Function)
106
+ );
107
+ });
108
+
109
+ it("auto-hides after the timeout and advances to the next queued toast", () => {
110
+ const onShow = jest.fn();
111
+ const onHide = jest.fn();
112
+ on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
113
+ on(NOTIFICATION_TOAST_EVENTS.HIDE, onHide);
114
+
115
+ notificationToastManager.showToast({
116
+ id: "a",
117
+ message: "first",
118
+ timeout: 1000,
119
+ });
120
+
121
+ notificationToastManager.showToast({ id: "b", message: "second" });
122
+
123
+ jest.advanceTimersByTime(1000);
124
+
125
+ expect(onHide).toHaveBeenCalledTimes(1);
126
+
127
+ expect(onShow).toHaveBeenLastCalledWith(
128
+ expect.objectContaining({ id: "b" }),
129
+ expect.any(Function)
130
+ );
131
+ });
132
+
133
+ it("does nothing when dismiss is called with no toast showing", () => {
134
+ const onHide = jest.fn();
135
+ on(NOTIFICATION_TOAST_EVENTS.HIDE, onHide);
136
+
137
+ notificationToastManager.dismiss();
138
+
139
+ expect(onHide).not.toHaveBeenCalled();
140
+ });
141
+
142
+ it("updates a still-queued (not yet shown) toast in place instead of duplicating it", () => {
143
+ const onShow = jest.fn();
144
+ on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
145
+
146
+ notificationToastManager.showToast({ id: "a", message: "first" });
147
+ notificationToastManager.showToast({ id: "b", message: "second" });
148
+ notificationToastManager.showToast({ id: "b", message: "second updated" });
149
+
150
+ notificationToastManager.dismiss();
151
+
152
+ expect(onShow).toHaveBeenCalledTimes(2);
153
+
154
+ expect(onShow).toHaveBeenLastCalledWith(
155
+ expect.objectContaining({ id: "b", message: "second updated" }),
156
+ expect.any(Function)
157
+ );
158
+ });
159
+
160
+ it("swaps the content of the currently visible toast when re-shown with the same id", () => {
161
+ const onShow = jest.fn();
162
+ on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
163
+
164
+ notificationToastManager.showToast({
165
+ id: "a",
166
+ message: "first",
167
+ timeout: 5000,
168
+ });
169
+
170
+ notificationToastManager.showToast({ id: "a", message: "first updated" });
171
+
172
+ expect(onShow).toHaveBeenCalledTimes(2);
173
+
174
+ expect(onShow).toHaveBeenLastCalledWith(
175
+ expect.objectContaining({ id: "a", message: "first updated" }),
176
+ expect.any(Function)
177
+ );
178
+
179
+ // old timer must have been cleared, not still pending
180
+ jest.advanceTimersByTime(5000);
181
+ expect(onShow).toHaveBeenCalledTimes(2);
182
+ });
183
+
184
+ it("routes cross-package showToast events via the shared event bus", () => {
185
+ const onShow = jest.fn();
186
+ on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
187
+
188
+ postEvent(NOTIFICATION_TOAST_EVENTS.SHOW_TOAST, [
189
+ { id: "bus", message: "from event bus" },
190
+ ]);
191
+
192
+ expect(onShow).toHaveBeenCalledWith(
193
+ expect.objectContaining({ id: "bus", message: "from event bus" }),
194
+ expect.any(Function)
195
+ );
196
+ });
197
+
198
+ it("stops emitting to a handler after it has been removed", () => {
199
+ const onShow = jest.fn();
200
+ notificationToastManager.on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
201
+
202
+ notificationToastManager.removeHandler(
203
+ NOTIFICATION_TOAST_EVENTS.SHOW,
204
+ onShow
205
+ );
206
+
207
+ notificationToastManager.showToast({
208
+ id: "removed-handler",
209
+ message: "hello",
210
+ });
211
+
212
+ expect(onShow).not.toHaveBeenCalled();
213
+ });
214
+
215
+ it("defaults timeout to DEFAULT_TOAST_TIMEOUT (4000ms) when omitted and auto-hides", () => {
216
+ const onShow = jest.fn();
217
+ const onHide = jest.fn();
218
+ on(NOTIFICATION_TOAST_EVENTS.SHOW, onShow);
219
+ on(NOTIFICATION_TOAST_EVENTS.HIDE, onHide);
220
+
221
+ notificationToastManager.showToast({
222
+ message: "Added to queue",
223
+ });
224
+
225
+ expect(onShow).toHaveBeenCalledWith(
226
+ expect.objectContaining({
227
+ message: "Added to queue",
228
+ timeout: DEFAULT_TOAST_TIMEOUT,
229
+ }),
230
+ expect.any(Function)
231
+ );
232
+
233
+ jest.advanceTimersByTime(DEFAULT_TOAST_TIMEOUT);
234
+
235
+ expect(onHide).toHaveBeenCalledTimes(1);
236
+ });
237
+ });
@@ -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,5 @@
1
+ export { notificationToastManager } from "./NotificationToastManager";
2
+
3
+ export { NotificationToastRenderer } from "./NotificationToastRenderer";
4
+
5
+ export { useNotificationHeight } from "./useNotificationHeight";
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@applicaster/quick-brick-core",
3
- "version": "15.1.5-rc.2",
3
+ "version": "15.1.6-alpha.2015591222",
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": "15.1.5-rc.2",
32
- "@applicaster/quick-brick-core-plugins": "15.1.5-rc.2",
33
- "@applicaster/zapp-pipes-v2-client": "15.1.5-rc.2",
34
- "@applicaster/zapp-react-native-bridge": "15.1.5-rc.2",
35
- "@applicaster/zapp-react-native-redux": "15.1.5-rc.2",
36
- "@applicaster/zapp-react-native-ui-components": "15.1.5-rc.2",
37
- "@applicaster/zapp-react-native-utils": "15.1.5-rc.2",
31
+ "@applicaster/applicaster-types": "15.1.6-alpha.2015591222",
32
+ "@applicaster/quick-brick-core-plugins": "15.1.6-alpha.2015591222",
33
+ "@applicaster/zapp-pipes-v2-client": "15.1.6-alpha.2015591222",
34
+ "@applicaster/zapp-react-native-bridge": "15.1.6-alpha.2015591222",
35
+ "@applicaster/zapp-react-native-redux": "15.1.6-alpha.2015591222",
36
+ "@applicaster/zapp-react-native-ui-components": "15.1.6-alpha.2015591222",
37
+ "@applicaster/zapp-react-native-utils": "15.1.6-alpha.2015591222",
38
38
  "atob": "^2.1.2",
39
39
  "axios": "^0.28.0",
40
40
  "btoa": "^1.2.1",