Skip to content

Commit

Permalink
RN: Cleanup Testing Output
Browse files Browse the repository at this point in the history
Summary:
Cleans up all the Jest tests to minimize spurious console output.

Changelog:
[Internal]

Reviewed By: TheSavior

Differential Revision: D18289690

fbshipit-source-id: cdcecca879b3b85d3dccf9e0ab617ea7dc1e0777
  • Loading branch information
yungsters authored and facebook-github-bot committed Nov 3, 2019
1 parent a70987c commit 2504114
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
52 changes: 46 additions & 6 deletions Libraries/Animated/src/__tests__/Animated-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ describe('Animated tests', () => {
};
c.UNSAFE_componentWillMount();

Animated.timing(anim, {toValue: 10, duration: 1000}).start(callback);
Animated.timing(anim, {
toValue: 10,
duration: 1000,
useNativeDriver: false,
}).start(callback);
c._component = {};
c.componentWillUnmount();

Expand All @@ -139,7 +143,11 @@ describe('Animated tests', () => {
it('triggers callback when spring is at rest', () => {
const anim = new Animated.Value(0);
const callback = jest.fn();
Animated.spring(anim, {toValue: 0, velocity: 0}).start(callback);
Animated.spring(anim, {
toValue: 0,
velocity: 0,
useNativeDriver: false,
}).start(callback);
expect(callback).toBeCalled();
});

Expand All @@ -149,7 +157,7 @@ describe('Animated tests', () => {
const anim = new Animated.Value(0);
const listener = jest.fn();
anim.addListener(listener);
Animated.spring(anim, {toValue: 15}).start();
Animated.spring(anim, {toValue: 15, useNativeDriver: false}).start();
jest.runAllTimers();
const lastValue =
listener.mock.calls[listener.mock.calls.length - 2][0].value;
Expand All @@ -166,6 +174,7 @@ describe('Animated tests', () => {
stiffness: 8000,
damping: 2000,
toValue: 15,
useNativeDriver: false,
}).start();
jest.runAllTimers();
const lastValue =
Expand All @@ -178,6 +187,21 @@ describe('Animated tests', () => {
it('convert to JSON', () => {
expect(JSON.stringify(new Animated.Value(10))).toBe('10');
});

it('warns if `useNativeDriver` is missing', () => {
jest.spyOn(console, 'warn').mockImplementationOnce(() => {});

Animated.spring(new Animated.Value(0), {
toValue: 0,
velocity: 0,
// useNativeDriver
}).start();

expect(console.warn).toBeCalledWith(
'Animated: `useNativeDriver` was not specified. This is a required option and must be explicitly set to `true` or `false`',
);
console.warn.mockRestore();
});
});

describe('Animated Sequence', () => {
Expand Down Expand Up @@ -601,14 +625,19 @@ describe('Animated tests', () => {
describe('Animated Events', () => {
it('should map events', () => {
const value = new Animated.Value(0);
const handler = Animated.event([null, {state: {foo: value}}]);
const handler = Animated.event([null, {state: {foo: value}}], {
useNativeDriver: false,
});
handler({bar: 'ignoreBar'}, {state: {baz: 'ignoreBaz', foo: 42}});
expect(value.__getValue()).toBe(42);
});
it('should call listeners', () => {
const value = new Animated.Value(0);
const listener = jest.fn();
const handler = Animated.event([{foo: value}], {listener});
const handler = Animated.event([{foo: value}], {
listener,
useNativeDriver: false,
});
handler({foo: 42});
expect(value.__getValue()).toBe(42);
expect(listener.mock.calls.length).toBe(1);
Expand All @@ -617,7 +646,10 @@ describe('Animated tests', () => {
it('should call forked event listeners, with Animated.event() listener', () => {
const value = new Animated.Value(0);
const listener = jest.fn();
const handler = Animated.event([{foo: value}], {listener});
const handler = Animated.event([{foo: value}], {
listener,
useNativeDriver: false,
});
const listener2 = jest.fn();
const forkedHandler = Animated.forkEvent(handler, listener2);
forkedHandler({foo: 42});
Expand Down Expand Up @@ -671,6 +703,7 @@ describe('Animated tests', () => {
Animated.timing(value, {
toValue: 100,
duration: 100,
useNativeDriver: false,
}).start(callback);
jest.runAllTimers();

Expand All @@ -686,6 +719,7 @@ describe('Animated tests', () => {
toValue: 100,
duration: 100,
isInteraction: false,
useNativeDriver: false,
}).start(callback);
jest.runAllTimers();

Expand All @@ -702,6 +736,7 @@ describe('Animated tests', () => {
Animated.timing(value2, {
toValue: value1,
duration: 0,
useNativeDriver: false,
}).start();
value1.setValue(42);
expect(value2.__getValue()).toBe(42);
Expand All @@ -718,6 +753,7 @@ describe('Animated tests', () => {
outputRange: [0, 1],
}),
duration: 0,
useNativeDriver: false,
}).start();
value1.setValue(42);
expect(value2.__getValue()).toBe(42 / 2);
Expand All @@ -729,12 +765,14 @@ describe('Animated tests', () => {
Animated.timing(value2, {
toValue: value1,
duration: 0,
useNativeDriver: false,
}).start();
value1.setValue(42);
expect(value2.__getValue()).toBe(42);
Animated.timing(value2, {
toValue: 7,
duration: 0,
useNativeDriver: false,
}).start();
value1.setValue(1492);
expect(value2.__getValue()).toBe(7);
Expand Down Expand Up @@ -795,6 +833,7 @@ describe('Animated tests', () => {
Animated.timing(value2, {
toValue: value1,
duration: 0,
useNativeDriver: false,
}).start();
value1.setValue({x: 42, y: 1492});
expect(value2.__getValue()).toEqual({x: 42, y: 1492});
Expand All @@ -812,6 +851,7 @@ describe('Animated tests', () => {
toValue: value1,
tension: 3000, // faster spring for faster test
friction: 60,
useNativeDriver: false,
}).start();
value1.setValue({x: 1, y: 1});
jest.runAllTimers();
Expand Down
10 changes: 8 additions & 2 deletions Libraries/Animated/src/__tests__/TimingAnimation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const TimingAnimation = require('../animations/TimingAnimation');

describe('Timing Animation', () => {
it('should return array of 61 items', () => {
const timingAnim = new TimingAnimation({duration: 1000});
const timingAnim = new TimingAnimation({
duration: 1000,
useNativeDriver: false,
});
const config = timingAnim.__getNativeAnimationConfig();

expect(config.frames.length).toBe(61);
Expand All @@ -23,7 +26,10 @@ describe('Timing Animation', () => {
});

it('should cope with zero duration', () => {
const timingAnim = new TimingAnimation({duration: 0});
const timingAnim = new TimingAnimation({
duration: 0,
useNativeDriver: false,
});
const config = timingAnim.__getNativeAnimationConfig();

expect(config.frames.length).toBe(1);
Expand Down
20 changes: 14 additions & 6 deletions Libraries/Lists/__tests__/VirtualizedList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('VirtualizedList', () => {
});

it('warns if both renderItem or ListItemComponent are specified. Uses ListItemComponent', () => {
jest.spyOn(global.console, 'warn');
jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
function ListItemComponent({item}) {
return <item value={item.key} testID={`${item.key}-ListItemComponent`} />;
}
Expand All @@ -60,16 +60,22 @@ describe('VirtualizedList', () => {
/>,
);

expect(console.warn.mock.calls).toEqual([
[
'VirtualizedList: Both ListItemComponent and renderItem props are present. ListItemComponent will take precedence over renderItem.',
],
]);
expect(console.warn).toBeCalledWith(
'VirtualizedList: Both ListItemComponent and renderItem props are present. ListItemComponent will take precedence over renderItem.',
);
expect(component).toMatchSnapshot();
console.warn.mockRestore();
});

it('throws if no renderItem or ListItemComponent', () => {
// Silence the React error boundary warning; we expect an uncaught error.
jest.spyOn(console, 'error').mockImplementation(message => {
if (message.startsWith('The above error occured in the ')) {
return;
}
console.errorDebug(message);
});

const componentFactory = () =>
ReactTestRenderer.create(
<VirtualizedList
Expand All @@ -81,6 +87,8 @@ describe('VirtualizedList', () => {
expect(componentFactory).toThrow(
'VirtualizedList: Either ListItemComponent or renderItem props are required but none were found.',
);

console.error.mockRestore();
});

it('renders empty list', () => {
Expand Down
7 changes: 7 additions & 0 deletions Libraries/Network/__tests__/XMLHttpRequest-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,17 @@ describe('XMLHttpRequest', function() {
it('should expose responseType correctly', function() {
expect(xhr.responseType).toBe('');

jest.spyOn(console, 'error').mockImplementationOnce(() => {});

// Setting responseType to an unsupported value has no effect.
xhr.responseType = 'arrayblobbuffertextfile';
expect(xhr.responseType).toBe('');

expect(console.error).toBeCalledWith(
"Warning: The provided value 'arrayblobbuffertextfile' is not a valid 'responseType'.",
);
console.error.mockRestore();

xhr.responseType = 'arraybuffer';
expect(xhr.responseType).toBe('arraybuffer');

Expand Down

0 comments on commit 2504114

Please sign in to comment.