|
| 1 | +import { mount } from 'enzyme'; |
| 2 | +import React from 'react'; |
| 3 | +import Mentions from '../src'; |
| 4 | +import { simulateInput } from './shared/input'; |
| 5 | + |
| 6 | +const { Option } = Mentions; |
| 7 | + |
| 8 | +describe('Mentions', () => { |
| 9 | + function createMentions(props) { |
| 10 | + return mount( |
| 11 | + <Mentions {...props}> |
| 12 | + <Option value="bamboo">Bamboo</Option> |
| 13 | + <Option value="light">Light</Option> |
| 14 | + <Option value="cat">Cat</Option> |
| 15 | + </Mentions>, |
| 16 | + ); |
| 17 | + } |
| 18 | + |
| 19 | + describe('focus test', () => { |
| 20 | + beforeEach(() => { |
| 21 | + jest.useFakeTimers(); |
| 22 | + }); |
| 23 | + |
| 24 | + afterEach(() => { |
| 25 | + jest.useRealTimers(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('autoFocus', () => { |
| 29 | + const wrapper = createMentions({ autoFocus: true }); |
| 30 | + expect(document.activeElement).toBe(wrapper.find('textarea').instance()); |
| 31 | + }); |
| 32 | + |
| 33 | + it('not lose focus if click on dropdown', () => { |
| 34 | + const onBlur = jest.fn(); |
| 35 | + const wrapper = createMentions({ autoFocus: true, defaultValue: '@', onBlur }); |
| 36 | + |
| 37 | + // Inject to trigger measure |
| 38 | + wrapper.instance().startMeasure('b', '@', 1); |
| 39 | + jest.runAllTimers(); |
| 40 | + wrapper.update(); |
| 41 | + |
| 42 | + wrapper.find('MenuItem').simulate('focus'); |
| 43 | + wrapper.find('textarea').simulate('blur'); |
| 44 | + wrapper.find('MenuItem').simulate('click'); |
| 45 | + wrapper.find('textarea').simulate('focus'); // This is not good but code focus not work in simulate |
| 46 | + jest.runAllTimers(); |
| 47 | + |
| 48 | + expect(onBlur).not.toBeCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('focus', () => { |
| 52 | + const onFocus = jest.fn(); |
| 53 | + const wrapper = createMentions({ onFocus }); |
| 54 | + wrapper.find('textarea').simulate('focus'); |
| 55 | + expect(onFocus).toBeCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('blur', () => { |
| 59 | + const onBlur = jest.fn(); |
| 60 | + const wrapper = createMentions({ onBlur }); |
| 61 | + wrapper.find('textarea').simulate('blur'); |
| 62 | + jest.runAllTimers(); |
| 63 | + expect(onBlur).toBeCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('focus() & blur()', () => { |
| 67 | + const wrapper = createMentions(); |
| 68 | + wrapper.instance().focus(); |
| 69 | + expect(document.activeElement).toBe(wrapper.find('textarea').instance()); |
| 70 | + |
| 71 | + wrapper.instance().blur(); |
| 72 | + expect(document.activeElement).not.toBe(wrapper.find('textarea').instance()); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe('value', () => { |
| 77 | + it('defaultValue', () => { |
| 78 | + const wrapper = createMentions({ defaultValue: 'light' }); |
| 79 | + expect(wrapper.find('textarea').props().value).toBe('light'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('controlled value', () => { |
| 83 | + const wrapper = createMentions({ value: 'bamboo' }); |
| 84 | + expect(wrapper.find('textarea').props().value).toBe('bamboo'); |
| 85 | + |
| 86 | + wrapper.setProps({ value: 'cat' }); |
| 87 | + expect(wrapper.find('textarea').props().value).toBe('cat'); |
| 88 | + }); |
| 89 | + |
| 90 | + it('onChange', () => { |
| 91 | + const onChange = jest.fn(); |
| 92 | + const wrapper = createMentions({ onChange }); |
| 93 | + wrapper.find('textarea').simulate('change', { |
| 94 | + target: { value: 'bamboo' }, |
| 95 | + }); |
| 96 | + expect(onChange).toBeCalledWith('bamboo'); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe('filterOption', () => { |
| 101 | + it('false', () => { |
| 102 | + const wrapper = createMentions({ filterOption: false }); |
| 103 | + simulateInput(wrapper, '@notExist'); |
| 104 | + expect(wrapper.find('DropdownMenu').props().options.length).toBe(3); |
| 105 | + }); |
| 106 | + |
| 107 | + it('function', () => { |
| 108 | + const wrapper = createMentions({ filterOption: (_, { value }) => value.includes('a') }); |
| 109 | + simulateInput(wrapper, '@notExist'); |
| 110 | + expect(wrapper.find('DropdownMenu').props().options.length).toBe(2); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + it('notFoundContent', () => { |
| 115 | + const notFoundContent = 'Bamboo Light'; |
| 116 | + const wrapper = createMentions({ notFoundContent }); |
| 117 | + simulateInput(wrapper, '@a'); |
| 118 | + simulateInput(wrapper, '@notExist'); |
| 119 | + expect(wrapper.find('DropdownMenu').props().options.length).toBe(0); |
| 120 | + expect(wrapper.find('MenuItem').props().children).toBe(notFoundContent); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('accessibility', () => { |
| 124 | + it('hover', () => { |
| 125 | + const wrapper = createMentions(); |
| 126 | + simulateInput(wrapper, '@'); |
| 127 | + wrapper |
| 128 | + .find('MenuItem') |
| 129 | + .last() |
| 130 | + .simulate('mouseEnter'); |
| 131 | + expect(wrapper.find('Menu').props().activeKey).toBe('cat'); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments