]> git.r.bdr.sh - rbdr/dotfiles/blame_incremental - atom/packages/vim-mode/spec/spec-helper.coffee
Update prompt to show ruby & node envs
[rbdr/dotfiles] / atom / packages / vim-mode / spec / spec-helper.coffee
... / ...
CommitLineData
1VimState = require '../lib/vim-state'
2GlobalVimState = require '../lib/global-vim-state'
3VimMode = require '../lib/vim-mode'
4StatusBarManager = require '../lib/status-bar-manager'
5
6[globalVimState, statusBarManager] = []
7
8beforeEach ->
9 atom.workspace ||= {}
10 statusBarManager = null
11 globalVimState = null
12 spyOn(atom, 'beep')
13
14getEditorElement = (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
37mockPlatform = (editorElement, platform) ->
38 wrapper = document.createElement('div')
39 wrapper.className = platform
40 wrapper.appendChild(editorElement)
41
42unmockPlatform = (editorElement) ->
43 editorElement.parentNode.removeChild(editorElement)
44
45dispatchKeyboardEvent = (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
52dispatchTextEvent = (target, eventArgs...) ->
53 e = document.createEvent('TextEvent')
54 e.initTextEvent(eventArgs...)
55 target.dispatchEvent e
56
57keydown = (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
76module.exports = {keydown, getEditorElement, mockPlatform, unmockPlatform}