@applicaster/quick-brick-core 16.0.0-rc.5 → 16.0.0-rc.50
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/ModalProvider/ModalBottomSheet/DraggableBottomSheet/__tests__/index.test.tsx +88 -0
- package/App/ModalProvider/ModalBottomSheet/DraggableBottomSheet/index.tsx +30 -56
- package/App/NotificationToastRenderer/NotificationToastManager.ts +203 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.tsx +102 -0
- package/App/NotificationToastRenderer/NotificationToastRenderer.web.tsx +61 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastManager.test.ts +213 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastRenderer.test.tsx +85 -0
- package/App/NotificationToastRenderer/__tests__/NotificationToastRenderer.web.test.tsx +90 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationHeight.test.tsx +53 -0
- package/App/NotificationToastRenderer/__tests__/useNotificationToastState.test.ts +112 -0
- package/App/NotificationToastRenderer/index.tsx +5 -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,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
|
+
});
|