Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(AnimatedMock): make mocks .start callback async + implemented on all animations #31992

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions Libraries/Animated/AnimatedMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ export type CompositeAnimation = {
};

const emptyAnimation = {
start: () => {},
start: (callback?: ?EndCallback) => {
// we use setTimeout here so that the callbacks are async
if (callback) {
setTimeout(() => callback({finished: true}), 0);
}
},
stop: () => {},
reset: () => {},
_startNativeLoop: () => {},
Expand All @@ -57,7 +62,7 @@ const spring = function(
...emptyAnimation,
start: (callback?: ?EndCallback): void => {
anyValue.setValue(config.toValue);
callback && callback({finished: true});
emptyAnimation.start(callback);
},
};
};
Expand All @@ -71,7 +76,7 @@ const timing = function(
...emptyAnimation,
start: (callback?: ?EndCallback): void => {
anyValue.setValue(config.toValue);
callback && callback({finished: true});
emptyAnimation.start(callback);
},
};
};
Expand Down
79 changes: 79 additions & 0 deletions Libraries/Animated/__tests__/AnimatedMock-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ const AnimatedMock = require('../AnimatedMock');
const AnimatedImplementation = require('../AnimatedImplementation');

describe('Animated Mock', () => {
const mockStartCallback = jest.fn();
jest.useFakeTimers();

beforeEach(() => {
jest.clearAllMocks();
});

it('matches implementation keys', () => {
expect(Object.keys(AnimatedMock)).toEqual(
Object.keys(AnimatedImplementation),
Expand Down Expand Up @@ -49,4 +56,76 @@ describe('Animated Mock', () => {
});
done();
});

const itCallsStartCallback = () => {
jest.runAllTimers();

expect(mockStartCallback).toHaveBeenCalledTimes(1);
};

describe('Animated.parallel', () => {
it('calls the start callback', () => {
AnimatedMock.parallel([]).start(mockStartCallback);

jest.runAllTimers();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we call runAllTimers twice? Do we need to call it in the test if its part of itCallsStartCallback?


itCallsStartCallback();
});
});

describe('Animated.sequence', () => {
it('calls the start callback', () => {
AnimatedMock.sequence([]).start(mockStartCallback);

jest.runAllTimers();

itCallsStartCallback();
});
});

describe('Animated.loop', () => {
it('calls the start callback', () => {
AnimatedMock.loop(AnimatedMock.timing(new AnimatedMock.Value(0))).start(
mockStartCallback,
);

itCallsStartCallback();
});
});

describe('Animated.delay', () => {
it('calls the start callback', () => {
AnimatedMock.decay(new AnimatedMock.Value(0)).start(mockStartCallback);

itCallsStartCallback();
});
});

describe('Animated.stagger', () => {
it('calls the start callback', () => {
AnimatedMock.stagger(50, []).start(mockStartCallback);

itCallsStartCallback();
});
});

describe('Animated.timing', () => {
it('calls the start callback', () => {
AnimatedMock.timing(new AnimatedMock.Value(0), {toValue: 20}).start(
mockStartCallback,
);

itCallsStartCallback();
});
});

describe('Animated.spring', () => {
it('calls the start callback', () => {
AnimatedMock.spring(new AnimatedMock.Value(0), {toValue: 20}).start(
mockStartCallback,
);

itCallsStartCallback();
});
});
});