@applicaster/quick-brick-core 16.0.0-rc.37 → 16.0.0-rc.39

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.
@@ -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,53 @@
1
+ import { renderHook } from "@testing-library/react-native";
2
+
3
+ let mockInsets = { top: 0 };
4
+
5
+ jest.mock("react-native-safe-area-context", () => ({
6
+ useSafeAreaInsets: () => mockInsets,
7
+ }));
8
+
9
+ jest.mock("@applicaster/zapp-react-native-utils/reactUtils", () => ({
10
+ ...jest.requireActual("@applicaster/zapp-react-native-utils/reactUtils"),
11
+ platformSelect: jest.fn(({ ios, android }) => ({ ios, android })),
12
+ }));
13
+
14
+ import { platformSelect } from "@applicaster/zapp-react-native-utils/reactUtils";
15
+ import { useNotificationHeight } from "../useNotificationHeight";
16
+
17
+ describe("useNotificationHeight", () => {
18
+ beforeEach(() => {
19
+ (platformSelect as jest.Mock).mockClear();
20
+ });
21
+
22
+ it("adds the safe area top inset to the platform nav bar height", () => {
23
+ mockInsets = { top: 44 };
24
+ (platformSelect as jest.Mock).mockReturnValueOnce(56);
25
+
26
+ const { result } = renderHook(() => useNotificationHeight());
27
+
28
+ expect(result.current).toEqual({
29
+ statusHeight: 44,
30
+ notificationHeight: 100,
31
+ });
32
+ });
33
+
34
+ it("falls back to a 0 status height when there is no top inset", () => {
35
+ mockInsets = { top: 0 };
36
+ (platformSelect as jest.Mock).mockReturnValueOnce(44);
37
+
38
+ const { result } = renderHook(() => useNotificationHeight());
39
+
40
+ expect(result.current).toEqual({
41
+ statusHeight: 0,
42
+ notificationHeight: 44,
43
+ });
44
+ });
45
+
46
+ it("selects the nav bar height per platform via platformSelect", () => {
47
+ mockInsets = { top: 20 };
48
+
49
+ renderHook(() => useNotificationHeight());
50
+
51
+ expect(platformSelect).toHaveBeenCalledWith({ ios: 44, android: 56 });
52
+ });
53
+ });
@@ -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,14 @@
1
+ import { useSafeAreaInsets } from "react-native-safe-area-context";
2
+
3
+ import { platformSelect } from "@applicaster/zapp-react-native-utils/reactUtils";
4
+
5
+ export const useNotificationHeight = () => {
6
+ const insets = useSafeAreaInsets();
7
+
8
+ const navBarHeight = platformSelect({ ios: 44, android: 56 });
9
+
10
+ const statusHeight = insets.top;
11
+ const notificationHeight = statusHeight + navBarHeight;
12
+
13
+ return { statusHeight, notificationHeight };
14
+ };
@@ -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
- <OfflineHandler>
71
- <Layout>
72
- <RouteManager />
73
- </Layout>
74
- </OfflineHandler>
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-rc.37",
3
+ "version": "16.0.0-rc.39",
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-rc.37",
32
- "@applicaster/quick-brick-core-plugins": "16.0.0-rc.37",
33
- "@applicaster/zapp-pipes-v2-client": "16.0.0-rc.37",
34
- "@applicaster/zapp-react-native-bridge": "16.0.0-rc.37",
35
- "@applicaster/zapp-react-native-redux": "16.0.0-rc.37",
36
- "@applicaster/zapp-react-native-ui-components": "16.0.0-rc.37",
37
- "@applicaster/zapp-react-native-utils": "16.0.0-rc.37",
31
+ "@applicaster/applicaster-types": "16.0.0-rc.39",
32
+ "@applicaster/quick-brick-core-plugins": "16.0.0-rc.39",
33
+ "@applicaster/zapp-pipes-v2-client": "16.0.0-rc.39",
34
+ "@applicaster/zapp-react-native-bridge": "16.0.0-rc.39",
35
+ "@applicaster/zapp-react-native-redux": "16.0.0-rc.39",
36
+ "@applicaster/zapp-react-native-ui-components": "16.0.0-rc.39",
37
+ "@applicaster/zapp-react-native-utils": "16.0.0-rc.39",
38
38
  "atob": "^2.1.2",
39
39
  "axios": "^0.28.0",
40
40
  "btoa": "^1.2.1",