@applicaster/quick-brick-core 16.0.0-rc.8 → 16.0.0-rc.81
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/ActionsProvider/ActionsProvider.tsx +31 -9
- package/App/ActionsProvider/LegacyActionsRegistryAdapter.tsx +107 -0
- package/App/DeepLinking/URLSchemeHandler/SchemeHandlerHooks/__tests__/useOpenSchemeHandler.test.tsx +25 -15
- package/App/ModalProvider/ModalBottomSheet/DraggableBottomSheet/__tests__/index.test.tsx +88 -0
- package/App/ModalProvider/ModalBottomSheet/DraggableBottomSheet/index.tsx +30 -56
- package/App/ModalProvider/ModalBottomSheet/ModalBottomSheetFrame.tsx +9 -2
- package/App/ModalProvider/ModalBottomSheet/hooks/__tests__/useKeyboardHeight.android.test.ts +105 -0
- package/App/ModalProvider/ModalBottomSheet/hooks/__tests__/useKeyboardHeight.test.ts +102 -0
- package/App/ModalProvider/ModalBottomSheet/hooks/index.ts +1 -0
- package/App/ModalProvider/ModalBottomSheet/hooks/useKeyboardHeight.ts +30 -0
- package/App/ModalProvider/ModalBottomSheet/index.tsx +5 -2
- package/App/NavigationProvider/NavigationProvider.tsx +51 -1
- package/App/NavigationProvider/__tests__/utils.test.ts +31 -1
- package/App/NavigationProvider/utils.ts +14 -0
- package/App/NotificationToastRenderer/NotificationToastManager.ts +214 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.tsx +134 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.tv.tsx +10 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.web.tsx +61 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastManager.test.ts +237 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastRenderer.test.tsx +233 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastRenderer.web.test.tsx +90 -0
- package/App/NotificationToastRenderer/__tests__/resolveConfirmationToastStyle.test.ts +118 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationHeight.test.tsx +58 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationToastState.test.ts +112 -0
- package/App/NotificationToastRenderer/index.tsx +9 -0
- package/App/NotificationToastRenderer/resolveConfirmationToastStyle.ts +33 -0
- package/App/NotificationToastRenderer/useNotificationHeight.tsx +14 -0
- package/App/NotificationToastRenderer/useNotificationToastState.tsx +12 -0
- package/App/index.tsx +8 -5
- package/package.json +8 -8
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import React, { useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
import { View, Text, Animated, StyleSheet, ViewStyle } from "react-native";
|
|
3
|
+
import {
|
|
4
|
+
Directions,
|
|
5
|
+
Gesture,
|
|
6
|
+
GestureDetector,
|
|
7
|
+
} from "react-native-gesture-handler";
|
|
8
|
+
import { useTheme } from "@applicaster/zapp-react-native-utils/theme";
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
CONFIRMATION_TOAST_SOURCE,
|
|
12
|
+
notificationToastManager,
|
|
13
|
+
} from "./NotificationToastManager";
|
|
14
|
+
import { useNotificationToastState } from "./useNotificationToastState";
|
|
15
|
+
import { useNotificationHeight } from "./useNotificationHeight";
|
|
16
|
+
import { resolveConfirmationToastStyle } from "./resolveConfirmationToastStyle";
|
|
17
|
+
|
|
18
|
+
const DEFAULT_TOAST_BACKGROUND_COLOR = "#000";
|
|
19
|
+
const DEFAULT_TOAST_MESSAGE_COLOR = "#fff";
|
|
20
|
+
|
|
21
|
+
const styles = StyleSheet.create({
|
|
22
|
+
notificationView: {
|
|
23
|
+
flex: 1,
|
|
24
|
+
position: "absolute",
|
|
25
|
+
alignItems: "center",
|
|
26
|
+
justifyContent: "center",
|
|
27
|
+
top: 0,
|
|
28
|
+
left: 0,
|
|
29
|
+
right: 0,
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
type NumberBoolean = 0 | 1;
|
|
34
|
+
|
|
35
|
+
export const NotificationToastRenderer = ({ children }) => {
|
|
36
|
+
const { statusHeight, notificationHeight } = useNotificationHeight();
|
|
37
|
+
const theme = useTheme<BaseThemePropertiesMobile>();
|
|
38
|
+
|
|
39
|
+
const toast = useNotificationToastState();
|
|
40
|
+
|
|
41
|
+
const [notificationOpacity, setNotificationOpacity] =
|
|
42
|
+
useState<NumberBoolean>(0);
|
|
43
|
+
|
|
44
|
+
const yPosition = useRef(new Animated.Value(0)).current;
|
|
45
|
+
|
|
46
|
+
const resolvedStyle =
|
|
47
|
+
toast?.source === CONFIRMATION_TOAST_SOURCE
|
|
48
|
+
? resolveConfirmationToastStyle(theme, toast.style)
|
|
49
|
+
: toast?.style;
|
|
50
|
+
|
|
51
|
+
const backgroundColor =
|
|
52
|
+
resolvedStyle?.backgroundColor ?? DEFAULT_TOAST_BACKGROUND_COLOR;
|
|
53
|
+
|
|
54
|
+
const color = resolvedStyle?.color ?? DEFAULT_TOAST_MESSAGE_COLOR;
|
|
55
|
+
|
|
56
|
+
const textStyles = {
|
|
57
|
+
color,
|
|
58
|
+
fontFamily: resolvedStyle?.fontFamily,
|
|
59
|
+
fontSize: resolvedStyle?.fontSize,
|
|
60
|
+
lineHeight: resolvedStyle?.lineHeight,
|
|
61
|
+
letterSpacing: resolvedStyle?.letterSpacing,
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const animatedViewStyles: ViewStyle = {
|
|
65
|
+
...styles.notificationView,
|
|
66
|
+
transform: [
|
|
67
|
+
{
|
|
68
|
+
translateY: yPosition.interpolate({
|
|
69
|
+
inputRange: [0, 1],
|
|
70
|
+
outputRange: [-notificationHeight, 0],
|
|
71
|
+
}),
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
opacity: notificationOpacity,
|
|
75
|
+
height: notificationHeight,
|
|
76
|
+
width: "100%",
|
|
77
|
+
|
|
78
|
+
backgroundColor,
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const flingGesture = useMemo(
|
|
82
|
+
() =>
|
|
83
|
+
Gesture.Fling()
|
|
84
|
+
.direction(Directions.UP)
|
|
85
|
+
.enabled(!!toast)
|
|
86
|
+
.runOnJS(true)
|
|
87
|
+
.onEnd((_event, success) => {
|
|
88
|
+
if (success) {
|
|
89
|
+
notificationToastManager.dismiss();
|
|
90
|
+
}
|
|
91
|
+
}),
|
|
92
|
+
[toast]
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
if (toast) {
|
|
97
|
+
setNotificationOpacity(1);
|
|
98
|
+
|
|
99
|
+
Animated.timing(yPosition, {
|
|
100
|
+
toValue: 1,
|
|
101
|
+
useNativeDriver: true,
|
|
102
|
+
duration: 300,
|
|
103
|
+
}).start();
|
|
104
|
+
} else {
|
|
105
|
+
Animated.timing(yPosition, {
|
|
106
|
+
toValue: 0,
|
|
107
|
+
useNativeDriver: true,
|
|
108
|
+
duration: 300,
|
|
109
|
+
}).start(() => setNotificationOpacity(0));
|
|
110
|
+
}
|
|
111
|
+
}, [toast]);
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<View
|
|
115
|
+
testID="NotificationToastRenderer"
|
|
116
|
+
style={StyleSheet.absoluteFill}
|
|
117
|
+
pointerEvents="box-none"
|
|
118
|
+
>
|
|
119
|
+
{children}
|
|
120
|
+
<GestureDetector gesture={flingGesture}>
|
|
121
|
+
<Animated.View
|
|
122
|
+
testID="NotificationToastRenderer-fling"
|
|
123
|
+
pointerEvents={toast ? "auto" : "none"}
|
|
124
|
+
style={animatedViewStyles}
|
|
125
|
+
>
|
|
126
|
+
<View style={{ height: statusHeight, backgroundColor }} />
|
|
127
|
+
<Text testID="NotificationToastRenderer-message" style={textStyles}>
|
|
128
|
+
{toast?.message}
|
|
129
|
+
</Text>
|
|
130
|
+
</Animated.View>
|
|
131
|
+
</GestureDetector>
|
|
132
|
+
</View>
|
|
133
|
+
);
|
|
134
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* NotificationToastRenderer for TV platforms.
|
|
5
|
+
* Currently, it does not render anything except for the children passed to it.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export const NotificationToastRenderer = ({ children }) => {
|
|
9
|
+
return <>{children}</>;
|
|
10
|
+
};
|
|
@@ -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,233 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { Text } from "react-native";
|
|
3
|
+
import { act, fireEvent, render } from "@testing-library/react-native";
|
|
4
|
+
import { useTheme } from "@applicaster/zapp-react-native-utils/theme";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
CONFIRMATION_TOAST_SOURCE,
|
|
8
|
+
notificationToastManager,
|
|
9
|
+
} from "../NotificationToastManager";
|
|
10
|
+
import { NotificationToastRenderer } from "../NotificationToastRenderer";
|
|
11
|
+
|
|
12
|
+
jest.mock("react-native-safe-area-context", () => ({
|
|
13
|
+
useSafeAreaInsets: () => ({ top: 44 }),
|
|
14
|
+
}));
|
|
15
|
+
|
|
16
|
+
jest.mock("@applicaster/zapp-react-native-utils/theme", () => ({
|
|
17
|
+
useTheme: jest.fn(),
|
|
18
|
+
}));
|
|
19
|
+
|
|
20
|
+
jest.mock("react-native-gesture-handler", () => {
|
|
21
|
+
const RN = require("react-native");
|
|
22
|
+
const ReactActual = require("react");
|
|
23
|
+
const actual = jest.requireActual("react-native-gesture-handler");
|
|
24
|
+
|
|
25
|
+
const GestureDetector = ReactActual.forwardRef(
|
|
26
|
+
({ gesture, children }: any, ref: any) => {
|
|
27
|
+
const onEnd = gesture?.handlers?.onEnd;
|
|
28
|
+
|
|
29
|
+
return (
|
|
30
|
+
<RN.View
|
|
31
|
+
ref={ref}
|
|
32
|
+
testID="NotificationToastRenderer-fling-handler"
|
|
33
|
+
accessibilityState={{ disabled: gesture?.config?.enabled === false }}
|
|
34
|
+
// drive Gesture.Fling().onEnd via a test-only event
|
|
35
|
+
onEnd={() => onEnd?.({}, true)}
|
|
36
|
+
onEndFailed={() => onEnd?.({}, false)}
|
|
37
|
+
>
|
|
38
|
+
{children}
|
|
39
|
+
</RN.View>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
__esModule: true,
|
|
46
|
+
...actual,
|
|
47
|
+
GestureDetector,
|
|
48
|
+
};
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const mockedUseTheme = useTheme as jest.MockedFunction<typeof useTheme>;
|
|
52
|
+
|
|
53
|
+
const confirmationTheme: Partial<BaseThemePropertiesMobile> = {
|
|
54
|
+
confirmation_toast_style_background_color: "#0188FF",
|
|
55
|
+
confirmation_toast_style_color: "#EFEFEF",
|
|
56
|
+
confirmation_toast_style_ios_font_family: "SFProText-Semibold",
|
|
57
|
+
confirmation_toast_style_android_font_family: "Roboto-Medium",
|
|
58
|
+
confirmation_toast_style_ios_font_size: 14,
|
|
59
|
+
confirmation_toast_style_android_font_size: 14,
|
|
60
|
+
confirmation_toast_style_ios_line_height: 44,
|
|
61
|
+
confirmation_toast_style_android_line_height: 44,
|
|
62
|
+
confirmation_toast_style_ios_letter_spacing: -1.4,
|
|
63
|
+
confirmation_toast_style_android_letter_spacing: -1.4,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
describe("NotificationToastRenderer", () => {
|
|
67
|
+
beforeAll(() => {
|
|
68
|
+
jest.spyOn(console, "log").mockImplementation(() => {});
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
afterAll(() => {
|
|
72
|
+
(console.log as jest.Mock).mockRestore();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
beforeEach(() => {
|
|
76
|
+
mockedUseTheme.mockReturnValue(
|
|
77
|
+
confirmationTheme as BaseThemePropertiesMobile
|
|
78
|
+
);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
afterEach(() => {
|
|
82
|
+
// drain whatever is left showing/queued so the next test starts clean
|
|
83
|
+
for (let i = 0; i < 10; i++) {
|
|
84
|
+
notificationToastManager.dismiss();
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
jest.restoreAllMocks();
|
|
88
|
+
jest.spyOn(console, "log").mockImplementation(() => {});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("renders its children", () => {
|
|
92
|
+
const { getByText } = render(
|
|
93
|
+
<NotificationToastRenderer>
|
|
94
|
+
<Text>child content</Text>
|
|
95
|
+
</NotificationToastRenderer>
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
expect(getByText("child content")).toBeTruthy();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("shows no message before any toast is emitted", () => {
|
|
102
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
103
|
+
|
|
104
|
+
expect(
|
|
105
|
+
getByTestId("NotificationToastRenderer-message").props.children
|
|
106
|
+
).toBeUndefined();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("displays the message from a toast emitted by the manager", () => {
|
|
110
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
111
|
+
|
|
112
|
+
act(() => {
|
|
113
|
+
notificationToastManager.showToast({ message: "You are offline" });
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
expect(
|
|
117
|
+
getByTestId("NotificationToastRenderer-message").props.children
|
|
118
|
+
).toBe("You are offline");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("applies the toast's style to the message text", () => {
|
|
122
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
123
|
+
|
|
124
|
+
act(() => {
|
|
125
|
+
notificationToastManager.showToast({
|
|
126
|
+
message: "styled",
|
|
127
|
+
style: { color: "#123456", fontSize: 20 },
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
expect(
|
|
132
|
+
getByTestId("NotificationToastRenderer-message").props.style
|
|
133
|
+
).toEqual(expect.objectContaining({ color: "#123456", fontSize: 20 }));
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it("applies Theme confirmation styles when source is confirmation", () => {
|
|
137
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
138
|
+
|
|
139
|
+
act(() => {
|
|
140
|
+
notificationToastManager.showToast({
|
|
141
|
+
message: "Added to queue",
|
|
142
|
+
source: CONFIRMATION_TOAST_SOURCE,
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
expect(
|
|
147
|
+
getByTestId("NotificationToastRenderer-message").props.style
|
|
148
|
+
).toEqual(
|
|
149
|
+
expect.objectContaining({
|
|
150
|
+
color: "#EFEFEF",
|
|
151
|
+
fontSize: 14,
|
|
152
|
+
lineHeight: 44,
|
|
153
|
+
letterSpacing: -1.4,
|
|
154
|
+
})
|
|
155
|
+
);
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it("shallow-merges style overrides onto Theme confirmation defaults", () => {
|
|
159
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
160
|
+
|
|
161
|
+
act(() => {
|
|
162
|
+
notificationToastManager.showToast({
|
|
163
|
+
message: "Playlist created",
|
|
164
|
+
source: CONFIRMATION_TOAST_SOURCE,
|
|
165
|
+
style: { backgroundColor: "#00FF00", fontSize: 20 },
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
expect(
|
|
170
|
+
getByTestId("NotificationToastRenderer-message").props.style
|
|
171
|
+
).toEqual(
|
|
172
|
+
expect.objectContaining({
|
|
173
|
+
color: "#EFEFEF",
|
|
174
|
+
fontSize: 20,
|
|
175
|
+
lineHeight: 44,
|
|
176
|
+
})
|
|
177
|
+
);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("does not apply Theme confirmation styles for non-confirmation toasts", () => {
|
|
181
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
182
|
+
|
|
183
|
+
act(() => {
|
|
184
|
+
notificationToastManager.showToast({
|
|
185
|
+
message: "You are offline",
|
|
186
|
+
style: { color: "#abcdef" },
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
expect(
|
|
191
|
+
getByTestId("NotificationToastRenderer-message").props.style
|
|
192
|
+
).toEqual(
|
|
193
|
+
expect.objectContaining({
|
|
194
|
+
color: "#abcdef",
|
|
195
|
+
})
|
|
196
|
+
);
|
|
197
|
+
|
|
198
|
+
expect(
|
|
199
|
+
getByTestId("NotificationToastRenderer-message").props.style.fontSize
|
|
200
|
+
).toBeUndefined();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("dismisses the toast on a successful upward fling", () => {
|
|
204
|
+
const dismissSpy = jest.spyOn(notificationToastManager, "dismiss");
|
|
205
|
+
|
|
206
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
207
|
+
|
|
208
|
+
act(() => {
|
|
209
|
+
notificationToastManager.showToast({ message: "Added to queue" });
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
fireEvent(getByTestId("NotificationToastRenderer-fling-handler"), "end");
|
|
213
|
+
|
|
214
|
+
expect(dismissSpy).toHaveBeenCalled();
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
it("does not dismiss when the fling ends unsuccessfully", () => {
|
|
218
|
+
const dismissSpy = jest.spyOn(notificationToastManager, "dismiss");
|
|
219
|
+
|
|
220
|
+
const { getByTestId } = render(<NotificationToastRenderer />);
|
|
221
|
+
|
|
222
|
+
act(() => {
|
|
223
|
+
notificationToastManager.showToast({ message: "Added to queue" });
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
fireEvent(
|
|
227
|
+
getByTestId("NotificationToastRenderer-fling-handler"),
|
|
228
|
+
"endFailed"
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
expect(dismissSpy).not.toHaveBeenCalled();
|
|
232
|
+
});
|
|
233
|
+
});
|