|
| 1 | +import { mount } from '@vue/test-utils'; |
| 2 | +import Vue from 'vue'; |
| 3 | +import Mentions from '..'; |
| 4 | +import { asyncExpect } from '@/tests/utils'; |
| 5 | +import focusTest from '../../../tests/shared/focusTest'; |
| 6 | + |
| 7 | +const { getMentions } = Mentions; |
| 8 | + |
| 9 | +function $$(className) { |
| 10 | + return document.body.querySelectorAll(className); |
| 11 | +} |
| 12 | + |
| 13 | +function triggerInput(wrapper, text = '') { |
| 14 | + wrapper.find('textarea').element.value = text; |
| 15 | + wrapper.find('textarea').element.selectionStart = text.length; |
| 16 | + wrapper.find('textarea').trigger('keydown'); |
| 17 | + wrapper.find('textarea').trigger('change'); |
| 18 | + wrapper.find('textarea').trigger('keyup'); |
| 19 | +} |
| 20 | + |
| 21 | +describe('Mentions', () => { |
| 22 | + beforeAll(() => { |
| 23 | + jest.useFakeTimers(); |
| 24 | + }); |
| 25 | + |
| 26 | + afterAll(() => { |
| 27 | + jest.useRealTimers(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('getMentions', () => { |
| 31 | + const mentions = getMentions('@light #bamboo cat', { prefix: ['@', '#'] }); |
| 32 | + expect(mentions).toEqual([ |
| 33 | + { |
| 34 | + prefix: '@', |
| 35 | + value: 'light', |
| 36 | + }, |
| 37 | + { |
| 38 | + prefix: '#', |
| 39 | + value: 'bamboo', |
| 40 | + }, |
| 41 | + ]); |
| 42 | + }); |
| 43 | + |
| 44 | + it('focus', () => { |
| 45 | + const onFocus = jest.fn(); |
| 46 | + const onBlur = jest.fn(); |
| 47 | + |
| 48 | + const wrapper = mount({ |
| 49 | + render() { |
| 50 | + return <Mentions onFocus={onFocus} onBlur={onBlur} />; |
| 51 | + }, |
| 52 | + }); |
| 53 | + wrapper.find('textarea').trigger('focus'); |
| 54 | + expect(wrapper.find('.ant-mentions').classes('ant-mentions-focused')).toBeTruthy(); |
| 55 | + expect(onFocus).toHaveBeenCalled(); |
| 56 | + |
| 57 | + wrapper.find('textarea').trigger('blur'); |
| 58 | + jest.runAllTimers(); |
| 59 | + expect(wrapper.classes()).not.toContain('ant-mentions-focused'); |
| 60 | + expect(onBlur).toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('loading', done => { |
| 64 | + const wrapper = mount( |
| 65 | + { |
| 66 | + render() { |
| 67 | + return <Mentions loading />; |
| 68 | + }, |
| 69 | + }, |
| 70 | + { sync: false }, |
| 71 | + ); |
| 72 | + triggerInput(wrapper, '@'); |
| 73 | + Vue.nextTick(() => { |
| 74 | + mount( |
| 75 | + { |
| 76 | + render() { |
| 77 | + return wrapper.find({ name: 'Trigger' }).vm.getComponent(); |
| 78 | + }, |
| 79 | + }, |
| 80 | + { sync: false }, |
| 81 | + ); |
| 82 | + Vue.nextTick(() => { |
| 83 | + expect($$('.ant-mentions-dropdown-menu-item').length).toBeTruthy(); |
| 84 | + expect($$('.ant-spin')).toBeTruthy(); |
| 85 | + done(); |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + focusTest(Mentions); |
| 91 | +}); |
0 commit comments