]> git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/vim-mode/spec/spec-helper.coffee
Update atoms
[rbdr/dotfiles] / atom / packages / vim-mode / spec / spec-helper.coffee
1 VimState = require '../lib/vim-state'
2 GlobalVimState = require '../lib/global-vim-state'
3 VimMode = require '../lib/vim-mode'
4 StatusBarManager = require '../lib/status-bar-manager'
5
6 [globalVimState, statusBarManager] = []
7
8 beforeEach ->
9 atom.workspace ||= {}
10 statusBarManager = null
11 globalVimState = null
12 spyOn(atom, 'beep')
13
14 getEditorElement = (callback) ->
15 textEditor = null
16
17 waitsForPromise ->
18 atom.project.open().then (e) ->
19 textEditor = e
20
21 runs ->
22 element = document.createElement("atom-text-editor")
23 element.setModel(textEditor)
24 element.classList.add('vim-mode')
25 statusBarManager ?= new StatusBarManager
26 globalVimState ?= new GlobalVimState
27 element.vimState = new VimState(element, statusBarManager, globalVimState)
28
29 element.addEventListener "keydown", (e) ->
30 atom.keymaps.handleKeyboardEvent(e)
31
32 # mock parent element for the text editor
33 document.createElement('html').appendChild(atom.views.getView(textEditor))
34
35 callback(element)
36
37 mockPlatform = (editorElement, platform) ->
38 wrapper = document.createElement('div')
39 wrapper.className = platform
40 wrapper.appendChild(editorElement)
41
42 unmockPlatform = (editorElement) ->
43 editorElement.parentNode.removeChild(editorElement)
44
45 dispatchKeyboardEvent = (target, eventArgs...) ->
46 e = document.createEvent('KeyboardEvent')
47 e.initKeyboardEvent(eventArgs...)
48 # 0 is the default, and it's valid ASCII, but it's wrong.
49 Object.defineProperty(e, 'keyCode', get: -> undefined) if e.keyCode is 0
50 target.dispatchEvent e
51
52 dispatchTextEvent = (target, eventArgs...) ->
53 e = document.createEvent('TextEvent')
54 e.initTextEvent(eventArgs...)
55 target.dispatchEvent e
56
57 keydown = (key, {element, ctrl, shift, alt, meta, raw}={}) ->
58 key = "U+#{key.charCodeAt(0).toString(16)}" unless key is 'escape' or raw?
59 element ||= document.activeElement
60 eventArgs = [
61 true, # bubbles
62 true, # cancelable
63 null, # view
64 key, # key
65 0, # location
66 ctrl, alt, shift, meta
67 ]
68
69 canceled = not dispatchKeyboardEvent(element, 'keydown', eventArgs...)
70 dispatchKeyboardEvent(element, 'keypress', eventArgs...)
71 if not canceled
72 if dispatchTextEvent(element, 'textInput', eventArgs...)
73 element.value += key
74 dispatchKeyboardEvent(element, 'keyup', eventArgs...)
75
76 module.exports = {keydown, getEditorElement, mockPlatform, unmockPlatform}