Skip to content

Commit

Permalink
Remove existing logs and warnings from tests (facebook#48391)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#48391

Changelog: [internal]

When we enabled log streaming in Fantom, we saw a lot of logs that we were previously not forwarding (console.error, console.warn) in existing tests.

This removes all the warnings and errors from those tests.

Reviewed By: rshest

Differential Revision: D67602299
  • Loading branch information
rubennorte authored and facebook-github-bot committed Jan 1, 2025
1 parent 39b3c5d commit bd53327
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 58 deletions.
68 changes: 43 additions & 25 deletions packages/react-native-fantom/src/__tests__/Fantom-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,30 @@ describe('Fantom', () => {

// TODO: when error handling is fixed, this should verify using `toThrow`
it('should throw when running a task inside another task', () => {
let lastCallbackExecuted = 0;
let threw = false;

runTask(() => {
lastCallbackExecuted = 1;
runTask(() => {
lastCallbackExecuted = 2;
throw new Error('Recursive runTask should be unreachable');
});
// TODO replace with expect(() => { ... }).toThrow() when error handling is fixed
try {
runTask(() => {});
} catch {
threw = true;
}
});
expect(lastCallbackExecuted).toBe(1);
expect(threw).toBe(true);

threw = false;

runTask(() => {
queueMicrotask(() => {
lastCallbackExecuted = 3;
runTask(() => {
lastCallbackExecuted = 4;
throw new Error(
'Recursive runTask from micro-task should be unreachable',
);
});
try {
runTask(() => {});
} catch {
threw = true;
}
});
});
expect(lastCallbackExecuted).toBe(3);
expect(threw).toBe(true);
});
});

Expand Down Expand Up @@ -125,16 +127,24 @@ describe('Fantom', () => {
runTask(() => {
root.render(
<>
<View style={{width: 100, height: 100}} collapsable={false} />
<View style={{width: 100, height: 100}} collapsable={false} />
<View
key="first"
style={{width: 100, height: 100}}
collapsable={false}
/>
<View
key="second"
style={{width: 100, height: 100}}
collapsable={false}
/>
</>,
);
});

expect(root.getRenderedOutput().toJSX()).toEqual(
<>
<rn-view width="100.000000" height="100.000000" />
<rn-view width="100.000000" height="100.000000" />
<rn-view key="0" width="100.000000" height="100.000000" />
<rn-view key="1" width="100.000000" height="100.000000" />
</>,
);

Expand Down Expand Up @@ -233,18 +243,26 @@ describe('Fantom', () => {
runTask(() => {
root.render(
<>
<View style={{width: 100, height: 100}} collapsable={false} />
<Text>hello world!</Text>
<View style={{width: 200, height: 300}} collapsable={false} />
<View
key="first"
style={{width: 100, height: 100}}
collapsable={false}
/>
<Text key="second">hello world!</Text>
<View
key="third"
style={{width: 200, height: 300}}
collapsable={false}
/>
</>,
);
});

expect(root.getRenderedOutput({props: []}).toJSX()).toEqual(
<>
<rn-view />
<rn-paragraph>hello world!</rn-paragraph>
<rn-view />
<rn-view key="0" />
<rn-paragraph key="1">hello world!</rn-paragraph>
<rn-view key="2" />
</>,
);

Expand Down
11 changes: 8 additions & 3 deletions packages/react-native-fantom/src/getFantomRenderedOutput.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,20 @@ function convertRawJsonToJSX(
function createJSXElementForTestComparison(
type: string,
props: mixed,
key?: ?string,
): React.Node {
const Tag = type;
return <Tag {...props} />;
return <Tag key={key} {...props} />;
}

function rnTypeToTestType(type: string): string {
return `rn-${type.substring(0, 1).toLowerCase() + type.substring(1)}`;
}

function jsonChildToJSXChild(jsonChild: FantomJsonObject | string): React.Node {
function jsonChildToJSXChild(
jsonChild: FantomJsonObject | string,
index?: ?number,
): React.Node {
if (typeof jsonChild === 'string') {
return jsonChild;
} else {
Expand All @@ -172,6 +176,7 @@ function jsonChildToJSXChild(jsonChild: FantomJsonObject | string): React.Node {
jsxChildren == null
? jsonChild.props
: {...jsonChild.props, children: jsxChildren},
index != null ? String(index) : undefined,
);
}
}
Expand All @@ -184,7 +189,7 @@ function jsonChildrenToJSXChildren(jsonChildren: FantomJsonObject['children']) {
let allJSXChildrenAreStrings = true;
let jsxChildrenString = '';
for (let i = 0; i < jsonChildren.length; i++) {
const jsxChild = jsonChildToJSXChild(jsonChildren[i]);
const jsxChild = jsonChildToJSXChild(jsonChildren[i], i);
jsxChildren.push(jsxChild);
if (allJSXChildrenAreStrings) {
if (typeof jsxChild === 'string') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,6 @@ describe('IntersectionObserver', () => {
maybeNode = receivedNode;
}}
/>
,
</ScrollView>,
);
});
Expand Down Expand Up @@ -549,7 +548,6 @@ describe('IntersectionObserver', () => {
maybeNode = receivedNode;
}}
/>
,
</ScrollView>,
);
});
Expand Down Expand Up @@ -610,7 +608,6 @@ describe('IntersectionObserver', () => {
maybeNode = receivedNode;
}}
/>
,
</ScrollView>,
);
});
Expand Down Expand Up @@ -942,7 +939,6 @@ describe('IntersectionObserver', () => {
maybeNode = receivedNode;
}}
/>
,
</ScrollView>,
);
});
Expand Down Expand Up @@ -1003,7 +999,6 @@ describe('IntersectionObserver', () => {
maybeNode = receivedNode;
}}
/>
,
</ScrollView>,
);
});
Expand Down Expand Up @@ -1314,13 +1309,9 @@ describe('IntersectionObserver', () => {
});
expect(node.isConnected).toBe(false);

Fantom.runTask(() => {
observer = new IntersectionObserver(() => {});
observer.observe(node);
// TODO what happens if this throws an exception?
observer.unobserve(node);
throw new Error('unobserve should not throw');
});
observer = new IntersectionObserver(() => {});
observer.observe(node);
observer.unobserve(node);
});

it('should not report the initial state if the target is unobserved before it is delivered', () => {
Expand Down Expand Up @@ -1497,19 +1488,16 @@ describe('IntersectionObserver', () => {

const node = ensureReactNativeElement(maybeNode);

Fantom.runTask(() => {
observer1 = new IntersectionObserver(() => {});
observer2 = new IntersectionObserver(() => {});
observer1 = new IntersectionObserver(() => {});
observer2 = new IntersectionObserver(() => {});

observer1.observe(node);
observer2.observe(node);
observer1.observe(node);
observer2.observe(node);

observer1.unobserve(node);
observer1.unobserve(node);

// The second call shouldn't log errors (that would make the test fail).
observer2.unobserve(node);
throw new Error('unobserve should not throw');
});
// The second call shouldn't log errors (that would make the test fail).
observer2.unobserve(node);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,14 +383,14 @@ describe('MutationObserver', () => {
<View
key="node1"
ref={receivedNode => {
maybeObservedNode = ensureReactNativeElement(receivedNode);
maybeObservedNode = receivedNode;
}}>
<View key="node1-1" />
</View>,
);
});

const observedNode = nullthrows(maybeObservedNode);
const observedNode = ensureReactNativeElement(maybeObservedNode);

const observerCallback = jest.fn();
const observer = new MutationObserver(observerCallback);
Expand Down Expand Up @@ -968,13 +968,13 @@ describe('MutationObserver', () => {
<View
key="node1"
ref={receivedNode => {
maybeObservedNode = ensureReactNativeElement(receivedNode);
maybeObservedNode = receivedNode;
}}
/>,
);
});

const observedNode = nullthrows(maybeObservedNode);
const observedNode = ensureReactNativeElement(maybeObservedNode);

const observerCallback = jest.fn();
const observer = new MutationObserver(observerCallback);
Expand Down Expand Up @@ -1016,13 +1016,13 @@ describe('MutationObserver', () => {
<View
key="node1"
ref={receivedNode => {
maybeObservedNode = ensureReactNativeElement(receivedNode);
maybeObservedNode = receivedNode;
}}
/>,
);
});

const observedNode = nullthrows(maybeObservedNode);
const observedNode = ensureReactNativeElement(maybeObservedNode);

const observerCallback = jest.fn();
const observer = new MutationObserver(observerCallback);
Expand All @@ -1048,13 +1048,13 @@ describe('MutationObserver', () => {
<View
key="node1"
ref={receivedNode => {
maybeObservedNode = ensureReactNativeElement(receivedNode);
maybeObservedNode = receivedNode;
}}
/>,
);
});

const observedNode = nullthrows(maybeObservedNode);
const observedNode = ensureReactNativeElement(maybeObservedNode);

Fantom.runTask(() => {
root.render(<></>);
Expand Down

0 comments on commit bd53327

Please sign in to comment.