From 17ab75b8d9f32066342bb22624875a07d6b6e2ef Mon Sep 17 00:00:00 2001 From: afc163 Date: Mon, 27 May 2019 18:53:44 +0800 Subject: [PATCH] :white_check_mark: Add test case for TransButton --- components/_util/__tests__/util.test.js | 53 +++++++++++++++++++++++++ components/_util/wave.tsx | 2 +- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/components/_util/__tests__/util.test.js b/components/_util/__tests__/util.test.js index c56e320c4e..264becdc84 100644 --- a/components/_util/__tests__/util.test.js +++ b/components/_util/__tests__/util.test.js @@ -1,11 +1,14 @@ import raf from 'raf'; import React from 'react'; import { mount } from 'enzyme'; +import KeyCode from 'rc-util/lib/KeyCode'; import delayRaf from '../raf'; import throttleByAnimationFrame from '../throttleByAnimationFrame'; import getDataOrAriaProps from '../getDataOrAriaProps'; import triggerEvent from '../triggerEvent'; import Wave from '../wave'; +import TransButton from '../transButton'; +import openAnimation from '../openAnimation'; describe('Test utils function', () => { beforeAll(() => { @@ -163,5 +166,55 @@ describe('Test utils function', () => { ).instance(); expect(wrapper.bindAnimationEvent()).toBe(undefined); }); + + it('should not throw when click it', () => { + expect(() => { + const wrapper = mount(
); + wrapper.simulate('click'); + }).not.toThrow(); + }); + + it('should not throw when no children', () => { + expect(() => mount()).not.toThrow(); + }); + }); + + describe('TransButton', () => { + it('can be focus/blur', () => { + const wrapper = mount( + TransButton, + ); + expect(typeof wrapper.instance().focus).toBe('function'); + expect(typeof wrapper.instance().blur).toBe('function'); + }); + + it('should trigger onClick when press enter', () => { + const onClick = jest.fn(); + const preventDefault = jest.fn(); + const wrapper = mount( + TransButton, + ); + wrapper.simulate('keyUp', { keyCode: KeyCode.ENTER }); + expect(onClick).toHaveBeenCalled(); + wrapper.simulate('keyDown', { keyCode: KeyCode.ENTER, preventDefault }); + expect(preventDefault).toHaveBeenCalled(); + }); + }); + + describe('openAnimation', () => { + it('should support openAnimation', () => { + const done = jest.fn(); + const domNode = document.createElement('div'); + expect(typeof openAnimation.enter).toBe('function'); + expect(typeof openAnimation.leave).toBe('function'); + expect(typeof openAnimation.appear).toBe('function'); + const appear = openAnimation.appear(domNode, done); + const enter = openAnimation.enter(domNode, done); + const leave = openAnimation.leave(domNode, done); + expect(typeof appear.stop).toBe('function'); + expect(typeof enter.stop).toBe('function'); + expect(typeof leave.stop).toBe('function'); + expect(done).toHaveBeenCalled(); + }); }); }); diff --git a/components/_util/wave.tsx b/components/_util/wave.tsx index abaf86e423..2185455ba6 100644 --- a/components/_util/wave.tsx +++ b/components/_util/wave.tsx @@ -158,7 +158,7 @@ export default class Wave extends React.Component<{ insertExtraNode?: boolean }> componentDidMount() { const node = findDOMNode(this) as HTMLElement; - if (node.nodeType !== 1) { + if (!node || node.nodeType !== 1) { return; } this.instance = this.bindAnimationEvent(node);