Skip to content

Commit e5c05cc

Browse files
rubennortefacebook-github-bot
authored andcommitted
Migrate LogBox Fantom test to use new document APIs (#53269)
Summary: Pull Request resolved: #53269 Changelog: [internal] Just a small refactor of the tests for LogBox to take advantage of the new `document.getElementById` API in RN. Reviewed By: rshest Differential Revision: D69528892 fbshipit-source-id: 807f03364e260baa9c3a94cfb1831b7eb24e0d26
1 parent a902267 commit e5c05cc

File tree

1 file changed

+30
-77
lines changed

1 file changed

+30
-77
lines changed

packages/react-native/Libraries/LogBox/__tests__/fantomHelpers.js

Lines changed: 30 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
* @format
99
*/
1010

11-
import ensureInstance from '../../../src/private/__tests__/utilities/ensureInstance';
12-
import ReadOnlyElement from '../../../src/private/webapis/dom/nodes/ReadOnlyElement';
13-
import View from '../../Components/View/View';
11+
import type ReactNativeDocument from '../../../src/private/webapis/dom/nodes/ReactNativeDocument';
12+
import type ReadOnlyElement from '../../../src/private/webapis/dom/nodes/ReadOnlyElement';
13+
1414
import AppContainer from '../../ReactNative/AppContainer';
1515
import LogBoxInspectorContainer from '../LogBoxInspectorContainer';
1616
import * as Fantom from '@react-native/fantom';
@@ -34,36 +34,6 @@ interface NotificationUI {
3434
message: ?string;
3535
}
3636

37-
function findById(node: ReadOnlyElement, id: string): ?ReadOnlyElement {
38-
if (node.id === id) {
39-
return node;
40-
}
41-
42-
for (const child of node.children) {
43-
const found = findById(child, id);
44-
if (found) {
45-
return found;
46-
}
47-
}
48-
49-
return null;
50-
}
51-
52-
function findTextById(node: ReadOnlyElement, id: string): ?string {
53-
if (node.id === id) {
54-
return node.textContent;
55-
}
56-
57-
for (const child of node.children) {
58-
const found = findTextById(child, id);
59-
if (found != null) {
60-
return found;
61-
}
62-
}
63-
64-
return null;
65-
}
66-
6737
function findTextByIds(
6838
node: ReadOnlyElement,
6939
id: string,
@@ -82,26 +52,28 @@ function findTextByIds(
8252
}
8353

8454
// Finds the LogBox notification UI by searching for the text IDs.
85-
function findLogBoxNotificationUI(node: ReadOnlyElement): NotificationUI {
55+
function findLogBoxNotificationUI(doc: ReactNativeDocument): NotificationUI {
8656
return {
87-
count: findTextById(node, 'logbox_notification_count_text'),
88-
message: findTextById(node, 'logbox_notification_message_text'),
57+
count: doc.getElementById('logbox_notification_count_text')?.textContent,
58+
message: doc.getElementById('logbox_notification_message_text')
59+
?.textContent,
8960
};
9061
}
9162

9263
// Finds the LogBox inspector UI by searching for the text IDs.
93-
function findLogBoxInspectorUI(node: ReadOnlyElement): InspectorUI {
64+
function findLogBoxInspectorUI(doc: ReactNativeDocument): InspectorUI {
9465
return {
95-
header: findTextById(node, 'logbox_header_title_text'),
96-
title: findTextById(node, 'logbox_message_title_text'),
97-
message: findTextById(node, 'logbox_message_contents_text'),
66+
header: doc.getElementById('logbox_header_title_text')?.textContent,
67+
title: doc.getElementById('logbox_message_title_text')?.textContent,
68+
message: doc.getElementById('logbox_message_contents_text')?.textContent,
9869
// codeFrames: undefined,
9970
componentStackFrames: findTextByIds(
100-
node,
71+
doc.documentElement,
10172
'logbox_component_stack_frame_text',
10273
),
103-
stackFrames: getStackFrames(node),
104-
isDismissable: findTextById(node, 'logbox_dismissable_text') == null,
74+
stackFrames: getStackFrames(doc.documentElement),
75+
isDismissable:
76+
doc.getElementById('logbox_dismissable_text')?.textContent == null,
10577
};
10678
}
10779

@@ -173,100 +145,81 @@ export function renderLogBox(
173145
// Returns the LogBox notification UI as an object.
174146
getNotificationUI: () => ?NotificationUI,
175147
} {
176-
let inspectorNode;
177-
178148
const logBoxRoot = Fantom.createRoot();
179149
Fantom.runTask(() => {
180-
logBoxRoot.render(
181-
<View
182-
ref={node => {
183-
inspectorNode = node;
184-
}}>
185-
<LogBoxInspectorContainer />
186-
</View>,
187-
);
150+
logBoxRoot.render(<LogBoxInspectorContainer />);
188151
});
189152

190153
expect(logBoxRoot.getRenderedOutput().toJSX()).toBe(null);
191154

192-
const logBoxInstance = ensureInstance(inspectorNode, ReadOnlyElement);
155+
const logBoxDoc = logBoxRoot.document;
193156

194-
let appNode;
195157
const root = Fantom.createRoot();
196158
Fantom.runTask(() => {
197159
root.render(
198-
<View
199-
ref={node => {
200-
appNode = node;
201-
}}>
202-
<AppContainer rootTag={root.getRootTag()}>{children}</AppContainer>
203-
</View>,
160+
<AppContainer rootTag={root.getRootTag()}>{children}</AppContainer>,
204161
);
205162
});
206163

164+
const appDoc = root.document;
165+
207166
return {
208167
isOpen: () => {
209168
return logBoxRoot.getRenderedOutput().toJSX() != null;
210169
},
211170
openNotification: () => {
212-
const appInstance = ensureInstance(appNode, ReadOnlyElement);
213171
const button = nullthrows(
214-
findById(appInstance, 'logbox_open_button_error'),
172+
appDoc.getElementById('logbox_open_button_error'),
215173
);
216174

217175
Fantom.dispatchNativeEvent(button, 'click');
218176
},
219177
dimissNotification: () => {
220-
const appInstance = ensureInstance(appNode, ReadOnlyElement);
221178
const button = nullthrows(
222-
findById(appInstance, 'logbox_dismiss_button_error'),
179+
appDoc.getElementById('logbox_dismiss_button_error'),
223180
);
224181

225182
Fantom.dispatchNativeEvent(button, 'click');
226183
},
227184
mimimizeInspector: () => {
228185
const button = nullthrows(
229-
findById(logBoxInstance, 'logbox_footer_button_minimize'),
186+
logBoxDoc.getElementById('logbox_footer_button_minimize'),
230187
);
231188

232189
Fantom.dispatchNativeEvent(button, 'click');
233190
},
234191
dismissInspector: () => {
235192
const button = nullthrows(
236-
findById(logBoxInstance, 'logbox_footer_button_dismiss'),
193+
logBoxDoc.getElementById('logbox_footer_button_dismiss'),
237194
);
238195

239196
Fantom.dispatchNativeEvent(button, 'click');
240197
},
241198
nextLog: () => {
242199
const button = nullthrows(
243-
findById(logBoxInstance, 'logbox_header_button_next'),
200+
logBoxDoc.getElementById('logbox_header_button_next'),
244201
);
245202

246203
Fantom.dispatchNativeEvent(button, 'click');
247204
},
248205
previousLog: () => {
249206
const button = nullthrows(
250-
findById(logBoxInstance, 'logbox_header_button_prev'),
207+
logBoxDoc.getElementById('logbox_header_button_prev'),
251208
);
252209

253210
Fantom.dispatchNativeEvent(button, 'click');
254211
},
255212
getInspectorUI: () => {
256-
if (findById(logBoxInstance, 'logbox_inspector') == null) {
213+
if (logBoxDoc.getElementById('logbox_inspector') == null) {
257214
return null;
258215
}
259-
return findLogBoxInspectorUI(logBoxInstance);
216+
return findLogBoxInspectorUI(logBoxDoc);
260217
},
261218
getNotificationUI: () => {
262-
if (appNode == null) {
263-
return null;
264-
}
265-
const appInstance = ensureInstance(appNode, ReadOnlyElement);
266-
if (findById(appInstance, 'logbox_notification') == null) {
219+
if (appDoc.getElementById('logbox_notification') == null) {
267220
return null;
268221
}
269-
return findLogBoxNotificationUI(appInstance);
222+
return findLogBoxNotificationUI(appDoc);
270223
},
271224
};
272225
}

0 commit comments

Comments
 (0)