2014-10-03 03:10:33 +04:00
|
|
|
TextEditorElement = require '../src/text-editor-element'
|
2014-11-13 01:45:54 +03:00
|
|
|
TextEditor = require '../src/text-editor'
|
2014-10-03 03:10:33 +04:00
|
|
|
|
|
|
|
# The rest of text-editor-component-spec will be moved to this file when React
|
|
|
|
# is eliminated. This covers only concerns related to the wrapper element for now
|
|
|
|
describe "TextEditorElement", ->
|
|
|
|
jasmineContent = null
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
jasmineContent = document.body.querySelector('#jasmine-content')
|
|
|
|
|
|
|
|
describe "instantiation", ->
|
2015-01-12 21:18:49 +03:00
|
|
|
it "honors the 'mini' attribute", ->
|
2014-10-09 01:11:57 +04:00
|
|
|
jasmineContent.innerHTML = "<atom-text-editor mini>"
|
2014-10-03 03:10:33 +04:00
|
|
|
element = jasmineContent.firstChild
|
|
|
|
expect(element.getModel().isMini()).toBe true
|
|
|
|
|
2015-01-12 21:18:49 +03:00
|
|
|
it "honors the 'placeholder-text' attribute", ->
|
2014-10-09 01:11:57 +04:00
|
|
|
jasmineContent.innerHTML = "<atom-text-editor placeholder-text='testing'>"
|
2014-10-03 03:10:33 +04:00
|
|
|
element = jasmineContent.firstChild
|
|
|
|
expect(element.getModel().getPlaceholderText()).toBe 'testing'
|
|
|
|
|
2015-01-12 21:19:23 +03:00
|
|
|
it "honors the 'gutter-hidden' attribute", ->
|
|
|
|
jasmineContent.innerHTML = "<atom-text-editor gutter-hidden>"
|
|
|
|
element = jasmineContent.firstChild
|
|
|
|
expect(element.getModel().isGutterVisible()).toBe false
|
|
|
|
|
2014-12-05 20:30:33 +03:00
|
|
|
it "honors the text content", ->
|
|
|
|
jasmineContent.innerHTML = "<atom-text-editor>testing</atom-text-editor>"
|
|
|
|
element = jasmineContent.firstChild
|
|
|
|
expect(element.getModel().getText()).toBe 'testing'
|
|
|
|
|
2014-11-13 01:45:54 +03:00
|
|
|
describe "when the model is assigned", ->
|
|
|
|
it "adds the 'mini' attribute if .isMini() returns true on the model", ->
|
|
|
|
element = new TextEditorElement
|
|
|
|
model = new TextEditor(mini: true)
|
|
|
|
element.setModel(model)
|
|
|
|
expect(element.hasAttribute('mini')).toBe true
|
|
|
|
|
2014-12-17 01:36:51 +03:00
|
|
|
describe "when the editor is attached to the DOM", ->
|
|
|
|
describe "when the editor.useShadowDOM config option is true", ->
|
|
|
|
it "mounts the react component and unmounts when removed from the dom", ->
|
|
|
|
atom.config.set('editor.useShadowDOM', true)
|
|
|
|
|
|
|
|
element = new TextEditorElement
|
|
|
|
jasmine.attachToDOM(element)
|
|
|
|
|
|
|
|
component = element.component
|
|
|
|
expect(component.isMounted()).toBe true
|
2015-01-06 21:35:18 +03:00
|
|
|
element.remove()
|
2014-12-17 01:36:51 +03:00
|
|
|
expect(component.isMounted()).toBe false
|
|
|
|
|
2015-01-06 21:35:18 +03:00
|
|
|
jasmine.attachToDOM(element)
|
|
|
|
expect(element.component.isMounted()).toBe true
|
|
|
|
|
2014-12-17 01:36:51 +03:00
|
|
|
describe "when the editor.useShadowDOM config option is false", ->
|
|
|
|
it "mounts the react component and unmounts when removed from the dom", ->
|
|
|
|
atom.config.set('editor.useShadowDOM', false)
|
|
|
|
|
|
|
|
element = new TextEditorElement
|
|
|
|
jasmine.attachToDOM(element)
|
|
|
|
|
|
|
|
component = element.component
|
|
|
|
expect(component.isMounted()).toBe true
|
|
|
|
element.getModel().destroy()
|
|
|
|
expect(component.isMounted()).toBe false
|
|
|
|
|
2014-11-05 01:02:52 +03:00
|
|
|
describe "focus and blur handling", ->
|
|
|
|
describe "when the editor.useShadowDOM config option is true", ->
|
|
|
|
it "proxies focus/blur events to/from the hidden input inside the shadow root", ->
|
|
|
|
atom.config.set('editor.useShadowDOM', true)
|
|
|
|
|
|
|
|
element = new TextEditorElement
|
|
|
|
jasmineContent.appendChild(element)
|
|
|
|
|
|
|
|
blurCalled = false
|
|
|
|
element.addEventListener 'blur', -> blurCalled = true
|
|
|
|
|
|
|
|
element.focus()
|
|
|
|
expect(blurCalled).toBe false
|
|
|
|
expect(element.hasFocus()).toBe true
|
|
|
|
expect(document.activeElement).toBe element
|
|
|
|
expect(element.shadowRoot.activeElement).toBe element.shadowRoot.querySelector('input')
|
|
|
|
|
|
|
|
document.body.focus()
|
|
|
|
expect(blurCalled).toBe true
|
|
|
|
|
|
|
|
describe "when the editor.useShadowDOM config option is false", ->
|
|
|
|
it "proxies focus/blur events to/from the hidden input", ->
|
|
|
|
atom.config.set('editor.useShadowDOM', false)
|
|
|
|
|
|
|
|
element = new TextEditorElement
|
|
|
|
jasmineContent.appendChild(element)
|
|
|
|
|
|
|
|
blurCalled = false
|
|
|
|
element.addEventListener 'blur', -> blurCalled = true
|
|
|
|
|
|
|
|
element.focus()
|
|
|
|
expect(blurCalled).toBe false
|
|
|
|
expect(element.hasFocus()).toBe true
|
|
|
|
expect(document.activeElement).toBe element.querySelector('input')
|
|
|
|
|
|
|
|
document.body.focus()
|
|
|
|
expect(blurCalled).toBe true
|
2014-11-11 04:58:46 +03:00
|
|
|
|
2015-01-10 02:00:29 +03:00
|
|
|
describe "when focused while a parent node is being attached to the DOM", ->
|
|
|
|
class ElementThatFocusesChild extends HTMLDivElement
|
|
|
|
attachedCallback: ->
|
|
|
|
@firstChild.focus()
|
|
|
|
|
|
|
|
document.registerElement("element-that-focuses-child",
|
|
|
|
prototype: ElementThatFocusesChild.prototype
|
|
|
|
)
|
|
|
|
|
|
|
|
it "proxies the focus event to the hidden input", ->
|
|
|
|
element = new TextEditorElement
|
|
|
|
parentElement = document.createElement("element-that-focuses-child")
|
|
|
|
parentElement.appendChild(element)
|
|
|
|
jasmineContent.appendChild(parentElement)
|
|
|
|
expect(element.shadowRoot.activeElement).toBe element.shadowRoot.querySelector('input')
|
|
|
|
|
2014-11-11 04:58:46 +03:00
|
|
|
describe "when the themes finish loading", ->
|
|
|
|
[themeReloadCallback, initialThemeLoadComplete, element] = []
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
themeReloadCallback = null
|
|
|
|
initialThemeLoadComplete = false
|
|
|
|
|
|
|
|
spyOn(atom.themes, 'isInitialLoadComplete').andCallFake ->
|
|
|
|
initialThemeLoadComplete
|
2015-01-09 04:51:09 +03:00
|
|
|
spyOn(atom.themes, 'onDidChangeActiveThemes').andCallFake (fn) ->
|
2014-11-11 04:58:46 +03:00
|
|
|
themeReloadCallback = fn
|
|
|
|
|
|
|
|
atom.config.set("editor.useShadowDOM", false)
|
|
|
|
|
|
|
|
element = new TextEditorElement()
|
|
|
|
element.style.height = '200px'
|
|
|
|
element.getModel().setText [0..20].join("\n")
|
|
|
|
|
|
|
|
it "re-renders the scrollbar", ->
|
|
|
|
jasmineContent.appendChild(element)
|
|
|
|
|
|
|
|
atom.styles.addStyleSheet """
|
|
|
|
::-webkit-scrollbar {
|
|
|
|
width: 8px;
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
initialThemeLoadComplete = true
|
|
|
|
themeReloadCallback()
|
|
|
|
|
|
|
|
verticalScrollbarNode = element.querySelector(".vertical-scrollbar")
|
|
|
|
scrollbarWidth = verticalScrollbarNode.offsetWidth - verticalScrollbarNode.clientWidth
|
|
|
|
expect(scrollbarWidth).toEqual(8)
|
2014-11-27 03:51:13 +03:00
|
|
|
|
2014-12-03 04:02:52 +03:00
|
|
|
describe "::onDidAttach and ::onDidDetach", ->
|
|
|
|
it "invokes callbacks when the element is attached and detached", ->
|
|
|
|
element = new TextEditorElement
|
|
|
|
|
|
|
|
attachedCallback = jasmine.createSpy("attachedCallback")
|
|
|
|
detachedCallback = jasmine.createSpy("detachedCallback")
|
|
|
|
|
|
|
|
element.onDidAttach(attachedCallback)
|
|
|
|
element.onDidDetach(detachedCallback)
|
|
|
|
|
|
|
|
jasmine.attachToDOM(element)
|
|
|
|
|
|
|
|
expect(attachedCallback).toHaveBeenCalled()
|
|
|
|
expect(detachedCallback).not.toHaveBeenCalled()
|
|
|
|
|
|
|
|
attachedCallback.reset()
|
|
|
|
element.remove()
|
|
|
|
|
|
|
|
expect(attachedCallback).not.toHaveBeenCalled()
|
|
|
|
expect(detachedCallback).toHaveBeenCalled()
|
|
|
|
|
2014-11-27 03:51:13 +03:00
|
|
|
describe "::setUpdatedSynchronously", ->
|
2014-12-02 21:14:59 +03:00
|
|
|
it "controls whether the text editor is updated synchronously", ->
|
2014-12-02 21:37:11 +03:00
|
|
|
spyOn(window, 'requestAnimationFrame').andCallFake (fn) -> fn()
|
|
|
|
|
2014-11-27 03:51:13 +03:00
|
|
|
element = new TextEditorElement
|
|
|
|
jasmine.attachToDOM(element)
|
|
|
|
|
|
|
|
element.setUpdatedSynchronously(false)
|
|
|
|
expect(element.isUpdatedSynchronously()).toBe false
|
|
|
|
|
|
|
|
element.getModel().setText("hello")
|
2014-12-02 21:37:11 +03:00
|
|
|
expect(window.requestAnimationFrame).toHaveBeenCalled()
|
2014-11-27 03:51:13 +03:00
|
|
|
|
2014-12-02 21:37:11 +03:00
|
|
|
expect(element.shadowRoot.textContent).toContain "hello"
|
2014-11-27 03:51:13 +03:00
|
|
|
|
2014-12-02 21:37:11 +03:00
|
|
|
window.requestAnimationFrame.reset()
|
|
|
|
element.setUpdatedSynchronously(true)
|
|
|
|
element.getModel().setText("goodbye")
|
|
|
|
expect(window.requestAnimationFrame).not.toHaveBeenCalled()
|
|
|
|
expect(element.shadowRoot.textContent).toContain "goodbye"
|
2014-12-09 03:11:13 +03:00
|
|
|
|
|
|
|
describe "::getDefaultCharacterWidth", ->
|
2014-12-09 03:56:46 +03:00
|
|
|
it "returns null before the element is attached", ->
|
2014-12-09 03:11:13 +03:00
|
|
|
element = new TextEditorElement
|
2014-12-09 03:56:46 +03:00
|
|
|
expect(element.getDefaultCharacterWidth()).toBeNull()
|
2014-12-09 03:11:13 +03:00
|
|
|
|
|
|
|
it "returns the width of a character in the root scope", ->
|
|
|
|
element = new TextEditorElement
|
|
|
|
jasmine.attachToDOM(element)
|
|
|
|
expect(element.getDefaultCharacterWidth()).toBeGreaterThan(0)
|