Files
react-native/Libraries/Components/Keyboard/__tests__/Keyboard-test.js
T
Tim Yung 4a1820dbdf EventEmitter: Import {Native,RCTDevice}EventEmitter
Summary:
Upgrades `require` expressions of `NativeEventEmitter` and `RCTDeviceEventEmitter` to `import`.

Changelog:
[Internal]

Reviewed By: cpojer

Differential Revision: D22203919

fbshipit-source-id: 4fdf01b66ba0501d0e0714931923531c97d29be2
2020-06-27 02:18:10 -07:00

95 lines
2.9 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow strict-local
* @emails oncall+react_native
*/
'use strict';
const NativeModules = require('../../../BatchedBridge/NativeModules');
const LayoutAnimation = require('../../../LayoutAnimation/LayoutAnimation');
const dismissKeyboard = require('../../../Utilities/dismissKeyboard');
const Keyboard = require('../Keyboard');
import NativeEventEmitter from '../../../EventEmitter/NativeEventEmitter';
jest.mock('../../../LayoutAnimation/LayoutAnimation');
describe('Keyboard', () => {
beforeEach(() => {
jest.resetAllMocks();
});
it('exposes KeyboardEventEmitter methods', () => {
const KeyboardObserver = NativeModules.KeyboardObserver;
const KeyboardEventEmitter = new NativeEventEmitter(KeyboardObserver);
// $FlowFixMe
expect(Keyboard._subscriber).toBe(KeyboardEventEmitter._subscriber);
// $FlowFixMe Cannot access private property
expect(Keyboard._nativeModule).toBe(KeyboardEventEmitter._nativeModule);
});
it('uses dismissKeyboard utility', () => {
expect(Keyboard.dismiss).toBe(dismissKeyboard);
});
describe('scheduling layout animation', () => {
const scheduleLayoutAnimation = (
duration: number | null,
easing: string | null,
): void => Keyboard.scheduleLayoutAnimation({duration, easing});
it('triggers layout animation', () => {
scheduleLayoutAnimation(12, 'spring');
expect(LayoutAnimation.configureNext).toHaveBeenCalledWith({
duration: 12,
update: {
duration: 12,
type: 'spring',
},
});
});
it('does not trigger animation when duration is null', () => {
scheduleLayoutAnimation(null, 'spring');
expect(LayoutAnimation.configureNext).not.toHaveBeenCalled();
});
it('does not trigger animation when duration is 0', () => {
scheduleLayoutAnimation(0, 'spring');
expect(LayoutAnimation.configureNext).not.toHaveBeenCalled();
});
describe('animation update type', () => {
const assertAnimationUpdateType = type =>
expect(LayoutAnimation.configureNext).toHaveBeenCalledWith(
expect.objectContaining({
duration: expect.anything(),
update: {duration: expect.anything(), type},
}),
);
it('retrieves type from LayoutAnimation', () => {
scheduleLayoutAnimation(12, 'linear');
assertAnimationUpdateType('linear');
});
it("defaults to 'keyboard' when key in LayoutAnimation is not found", () => {
scheduleLayoutAnimation(12, 'some-unknown-animation-type');
assertAnimationUpdateType('keyboard');
});
it("defaults to 'keyboard' when easing is null", () => {
scheduleLayoutAnimation(12, null);
assertAnimationUpdateType('keyboard');
});
});
});
});