From 292a91a77792f9b2beba7bea1fb270a7a8f0a19e Mon Sep 17 00:00:00 2001 From: afc163 Date: Tue, 22 May 2018 16:39:17 +0800 Subject: [PATCH] Add test cases for Mention --- components/mention/__tests__/index.test.js | 58 ++++++++++++++++++++++ tests/setup.js | 1 + 2 files changed, 59 insertions(+) create mode 100644 components/mention/__tests__/index.test.js diff --git a/components/mention/__tests__/index.test.js b/components/mention/__tests__/index.test.js new file mode 100644 index 0000000000..bb866523a5 --- /dev/null +++ b/components/mention/__tests__/index.test.js @@ -0,0 +1,58 @@ +import React from 'react'; +import { mount } from 'enzyme'; +import Mention from '..'; + +const { toContentState } = Mention; + +describe('Mention', () => { + beforeAll(() => { + jest.useFakeTimers(); + }); + + afterAll(() => { + jest.useRealTimers(); + }); + + it('should has focus function', () => { + const handleFocus = jest.fn(); + const wrapper = mount( + + ); + wrapper.instance().focus(); + jest.runAllTimers(); + expect(handleFocus).toBeCalled(); + }); + + it('basic suggestion', () => { + const handleSearch = jest.fn(); + const wrapper = mount( + + ); + wrapper.find('DraftEditorContents').simulate('focus'); + const ed = wrapper.find('.public-DraftEditor-content'); + ed.simulate('beforeInput', { data: '@a' }); + jest.runAllTimers(); + expect(handleSearch).toBeCalledWith('a', '@'); + }); + + it('change suggestions', () => { + const wrapper = mount( + + ); + wrapper.find('DraftEditorContents').simulate('focus'); + const ed = wrapper.find('.public-DraftEditor-content'); + ed.simulate('beforeInput', { data: '@' }); + expect(wrapper.find('Nav').length).toBe(2); + expect(wrapper.find('Nav').first().text()).toBe('afc163'); + wrapper.setState({ suggestions: ['yesmeck', 'yiminghe', 'lucy'] }); + expect(wrapper.find('Nav').length).toBe(3); + expect(wrapper.find('Nav').first().text()).toBe('yesmeck'); + }); +}); diff --git a/tests/setup.js b/tests/setup.js index 5b0168f9a5..a893dddc08 100644 --- a/tests/setup.js +++ b/tests/setup.js @@ -5,6 +5,7 @@ if (typeof window !== 'undefined') { global.window.innerHeight = height || global.window.innerHeight; global.window.dispatchEvent(new Event('resize')); }; + global.window.scrollTo = () => {}; } // The built-in requestAnimationFrame and cancelAnimationFrame not working with jest.runFakeTimes()