Merge pull request #15013 from atom/as-fix-calling-editor-element-set-model

Swap underlying editor correctly when calling setModel on editor element
This commit is contained in:
Bryant Ung 2017-07-13 09:59:22 -07:00 committed by GitHub
commit 9be492fcb3
2 changed files with 35 additions and 0 deletions

View File

@ -9,6 +9,10 @@ describe('TextEditorElement', () => {
beforeEach(() => {
jasmineContent = document.body.querySelector('#jasmine-content')
// Force scrollbars to be visible regardless of local system configuration
const scrollbarStyle = document.createElement('style')
scrollbarStyle.textContent = '::-webkit-scrollbar { -webkit-appearance: none }'
jasmine.attachToDOM(scrollbarStyle)
})
function buildTextEditorElement (options = {}) {
@ -201,6 +205,33 @@ describe('TextEditorElement', () => {
})
})
describe('::setModel', () => {
describe('when the element does not have an editor yet', () => {
it('uses the supplied one', () => {
const element = buildTextEditorElement({attach: false})
const editor = new TextEditor()
element.setModel(editor)
jasmine.attachToDOM(element)
expect(editor.element).toBe(element)
expect(element.getModel()).toBe(editor)
})
})
describe('when the element already has an editor', () => {
it('unbinds it and then swaps it with the supplied one', async () => {
const element = buildTextEditorElement({attach: true})
const previousEditor = element.getModel()
expect(previousEditor.element).toBe(element)
const newEditor = new TextEditor()
element.setModel(newEditor)
expect(previousEditor.element).not.toBe(element)
expect(newEditor.element).toBe(element)
expect(element.getModel()).toBe(newEditor)
})
})
})
describe('::onDidAttach and ::onDidDetach', () =>
it('invokes callbacks when the element is attached and detached', () => {
const element = buildTextEditorElement({attach: false})

View File

@ -174,6 +174,10 @@ class TextEditorComponent {
}
update (props) {
if (props.model !== this.props.model) {
this.props.model.component = null
props.model.component = this
}
this.props = props
this.scheduleUpdate()
}