|
| 1 | +import { FieldControl, TextField, Validator } from '@/core/factories'; |
| 2 | +import { required } from '@/core/utils/validators'; |
| 3 | +import { useInputEvents } from './useInputEvents'; |
| 4 | + |
| 5 | +describe('UseInputEvents', () => { |
| 6 | + let props; |
| 7 | + let ctx; |
| 8 | + let input; |
| 9 | + beforeEach(() => { |
| 10 | + props = { |
| 11 | + control: FieldControl({ |
| 12 | + name: 'test-input', |
| 13 | + ...TextField({ |
| 14 | + label: 'Test Input', |
| 15 | + }), |
| 16 | + }), |
| 17 | + }; |
| 18 | + ctx = { |
| 19 | + emit: (path, obj) => ({ |
| 20 | + path, |
| 21 | + ...obj, |
| 22 | + }), |
| 23 | + }; |
| 24 | + input = document.createElement('input'); |
| 25 | + input.type = 'text'; |
| 26 | + }); |
| 27 | + |
| 28 | + it('should emit a change event with the value when element emit `input` event', async () => { |
| 29 | + const spy = jest.spyOn(ctx, 'emit'); |
| 30 | + const { onInput } = useInputEvents(props, ctx.emit); |
| 31 | + input.value = 'Awiwi'; |
| 32 | + input.addEventListener('input', onInput, false); |
| 33 | + input.dispatchEvent(new Event('input', { bubbles: true })); |
| 34 | + |
| 35 | + expect(spy).toHaveBeenCalledWith('change', { |
| 36 | + name: 'test-input', |
| 37 | + value: 'Awiwi', |
| 38 | + }); |
| 39 | + |
| 40 | + input.removeEventListener('input', onInput); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should not emit a change event if there is no value', async () => { |
| 44 | + const spy = jest.spyOn(ctx, 'emit'); |
| 45 | + const { onInput } = useInputEvents(props, ctx.emit); |
| 46 | + |
| 47 | + input.addEventListener('input', onInput, false); |
| 48 | + input.dispatchEvent(new Event('input', { bubbles: true })); |
| 49 | + |
| 50 | + expect(spy).not.toBeCalled(); |
| 51 | + |
| 52 | + input.removeEventListener('input', onInput); |
| 53 | + }); |
| 54 | + |
| 55 | + it('should stopImmediatePropagation of event', async () => { |
| 56 | + const { onInput } = useInputEvents(props, ctx.emit); |
| 57 | + |
| 58 | + const event = new Event('input', { bubbles: true }); |
| 59 | + const spy = jest.spyOn(event, 'stopImmediatePropagation'); |
| 60 | + |
| 61 | + input.addEventListener('input', onInput, false); |
| 62 | + input.dispatchEvent(event); |
| 63 | + |
| 64 | + expect(spy).toBeCalled(); |
| 65 | + |
| 66 | + input.removeEventListener('input', onInput); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should stop propagation and prevent default if event change is trigerred', () => { |
| 70 | + const { onChange } = useInputEvents(props, ctx.emit); |
| 71 | + |
| 72 | + const event = new Event('change', { bubbles: true }); |
| 73 | + const spy = jest.spyOn(event, 'stopImmediatePropagation'); |
| 74 | + const spy2 = jest.spyOn(event, 'preventDefault'); |
| 75 | + |
| 76 | + input.addEventListener('change', onChange, false); |
| 77 | + input.dispatchEvent(event); |
| 78 | + |
| 79 | + expect(spy).toBeCalled(); |
| 80 | + expect(spy2).toBeCalled(); |
| 81 | + input.removeEventListener('change', onChange); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should emit a change event with the value when element is checked', async () => { |
| 85 | + const spy = jest.spyOn(ctx, 'emit'); |
| 86 | + const { onCheck } = useInputEvents(props, ctx.emit); |
| 87 | + const checkbox = document.createElement('input'); |
| 88 | + checkbox.type = 'checkbox'; |
| 89 | + checkbox.checked = true; |
| 90 | + checkbox.addEventListener('check', onCheck, false); |
| 91 | + checkbox.dispatchEvent(new Event('check', { bubbles: true })); |
| 92 | + |
| 93 | + expect(spy).toHaveBeenCalledWith('change', { |
| 94 | + name: 'test-input', |
| 95 | + value: true, |
| 96 | + }); |
| 97 | + |
| 98 | + checkbox.removeEventListener('check', onCheck); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should emit a focus event with the value when input is focused', async () => { |
| 102 | + const spy = jest.spyOn(ctx, 'emit'); |
| 103 | + const { onFocus } = useInputEvents(props, ctx.emit); |
| 104 | + |
| 105 | + input.addEventListener('focus', onFocus, false); |
| 106 | + input.dispatchEvent(new Event('focus', { bubbles: true })); |
| 107 | + |
| 108 | + expect(spy).toHaveBeenCalledWith('focus', { |
| 109 | + name: 'test-input', |
| 110 | + }); |
| 111 | + |
| 112 | + input.removeEventListener('focus', onFocus); |
| 113 | + }); |
| 114 | + |
| 115 | + it('should emit a blur event with the value when input is blurred', async () => { |
| 116 | + const spy = jest.spyOn(ctx, 'emit'); |
| 117 | + const { onBlur } = useInputEvents(props, ctx.emit); |
| 118 | + |
| 119 | + input.addEventListener('blur', onBlur, false); |
| 120 | + input.dispatchEvent(new Event('blur', { bubbles: true })); |
| 121 | + |
| 122 | + expect(spy).toHaveBeenCalledWith('blur', { |
| 123 | + name: 'test-input', |
| 124 | + }); |
| 125 | + |
| 126 | + input.removeEventListener('blur', onBlur); |
| 127 | + }); |
| 128 | +}); |
0 commit comments