|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + * |
| 7 | + * @format |
| 8 | + * @flow |
| 9 | + * @emails oncall+react_native |
| 10 | + */ |
| 11 | + |
| 12 | +'use strict'; |
| 13 | + |
| 14 | +const Keyboard = require('Keyboard'); |
| 15 | +const dismissKeyboard = require('dismissKeyboard'); |
| 16 | +const LayoutAnimation = require('LayoutAnimation'); |
| 17 | + |
| 18 | +const NativeEventEmitter = require('NativeEventEmitter'); |
| 19 | +const NativeModules = require('NativeModules'); |
| 20 | + |
| 21 | +jest.mock('LayoutAnimation'); |
| 22 | + |
| 23 | +describe('Keyboard', () => { |
| 24 | + beforeEach(() => { |
| 25 | + jest.resetAllMocks(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('exposes KeyboardEventEmitter methods', () => { |
| 29 | + const KeyboardObserver = NativeModules.KeyboardObserver; |
| 30 | + const KeyboardEventEmitter = new NativeEventEmitter(KeyboardObserver); |
| 31 | + |
| 32 | + // $FlowFixMe |
| 33 | + expect(Keyboard._subscriber).toBe(KeyboardEventEmitter._subscriber); |
| 34 | + expect(Keyboard._nativeModule).toBe(KeyboardEventEmitter._nativeModule); |
| 35 | + }); |
| 36 | + |
| 37 | + it('uses dismissKeyboard utility', () => { |
| 38 | + expect(Keyboard.dismiss).toBe(dismissKeyboard); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('scheduling layout animation', () => { |
| 42 | + const scheduleLayoutAnimation = ( |
| 43 | + duration: number | null, |
| 44 | + easing: string | null, |
| 45 | + ): void => Keyboard.scheduleLayoutAnimation({duration, easing}); |
| 46 | + |
| 47 | + it('triggers layout animation', () => { |
| 48 | + scheduleLayoutAnimation(12, 'spring'); |
| 49 | + expect(LayoutAnimation.configureNext).toHaveBeenCalledWith({ |
| 50 | + duration: 12, |
| 51 | + update: { |
| 52 | + duration: 12, |
| 53 | + type: 'spring', |
| 54 | + }, |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + it('does not trigger animation when duration is null', () => { |
| 59 | + scheduleLayoutAnimation(null, 'spring'); |
| 60 | + expect(LayoutAnimation.configureNext).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('does not trigger animation when duration is 0', () => { |
| 64 | + scheduleLayoutAnimation(0, 'spring'); |
| 65 | + expect(LayoutAnimation.configureNext).not.toHaveBeenCalled(); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('animation update type', () => { |
| 69 | + const assertAnimationUpdateType = type => |
| 70 | + expect(LayoutAnimation.configureNext).toHaveBeenCalledWith( |
| 71 | + expect.objectContaining({ |
| 72 | + duration: expect.anything(), |
| 73 | + update: {duration: expect.anything(), type}, |
| 74 | + }), |
| 75 | + ); |
| 76 | + |
| 77 | + it('retrieves type from LayoutAnimation', () => { |
| 78 | + scheduleLayoutAnimation(12, 'linear'); |
| 79 | + assertAnimationUpdateType('linear'); |
| 80 | + }); |
| 81 | + |
| 82 | + it("defaults to 'keyboard' when key in LayoutAnimation is not found", () => { |
| 83 | + scheduleLayoutAnimation(12, 'some-unknown-animation-type'); |
| 84 | + assertAnimationUpdateType('keyboard'); |
| 85 | + }); |
| 86 | + |
| 87 | + it("defaults to 'keyboard' when easing is null", () => { |
| 88 | + scheduleLayoutAnimation(12, null); |
| 89 | + assertAnimationUpdateType('keyboard'); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments