* prepareEvent works with firePreparedEvent. * * It allows test to: * - create the event * - spy a method on this event * - fire the event * * Example: * var p = document.querySelector('p'); * var evt = prepareEvent(p, 'keydown', { keyCode: MediumEditor.util.keyCode.ENTE
(element, eventName, options)
| 131 | * You can see a live example for tests related to `disableDoubleReturn` |
| 132 | */ |
| 133 | function prepareEvent (element, eventName, options) { |
| 134 | var evt; |
| 135 | |
| 136 | options = options || {}; |
| 137 | |
| 138 | if (document.createEvent) { |
| 139 | // dispatch for firefox + others |
| 140 | evt = document.createEvent('HTMLEvents'); |
| 141 | evt.initEvent(eventName, true, true); // event type,bubbling,cancelable |
| 142 | |
| 143 | evt.currentTarget = options.currentTarget ? options.currentTarget : element; |
| 144 | |
| 145 | if (options.keyCode) { |
| 146 | evt.keyCode = options.keyCode; |
| 147 | evt.which = options.keyCode; |
| 148 | } |
| 149 | |
| 150 | if (options.ctrlKey) { |
| 151 | evt.ctrlKey = true; |
| 152 | } |
| 153 | |
| 154 | if (options.metaKey) { |
| 155 | evt.metaKey = true; |
| 156 | } |
| 157 | |
| 158 | if (options.target) { |
| 159 | evt.target = options.target; |
| 160 | } |
| 161 | |
| 162 | if (options.relatedTarget) { |
| 163 | evt.relatedTarget = options.relatedTarget; |
| 164 | } |
| 165 | |
| 166 | if (options.shiftKey) { |
| 167 | evt.shiftKey = true; |
| 168 | } |
| 169 | |
| 170 | if (options.altKey) { |
| 171 | evt.altKey = true; |
| 172 | } |
| 173 | |
| 174 | if (eventName.indexOf('drag') !== -1 || eventName === 'drop') { |
| 175 | evt.dataTransfer = { |
| 176 | dropEffect: '' |
| 177 | }; |
| 178 | // File API only allows access to 'files' on drop, not on any other event |
| 179 | if (!isIE9() && eventName === 'drop') { |
| 180 | var file = dataURItoBlob('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'); |
| 181 | if (!file.type) { |
| 182 | file.type = 'image/gif'; |
| 183 | } |
| 184 | evt.dataTransfer.files = [file]; |
| 185 | } |
| 186 | } |
| 187 | } else { |
| 188 | // dispatch for IE |
| 189 | evt = document.createEventObject(); |
| 190 | } |
no test coverage detected
searching dependent graphs…