Move view-level specs for active pane item changing to pane-element-spec

This commit is contained in:
Nathan Sobo
2015-09-03 11:22:08 -06:00
parent 6fec11780b
commit dfda3adb96
2 changed files with 104 additions and 99 deletions

View File

@@ -0,0 +1,104 @@
PaneContainer = require '../src/pane-container'
describe "PaneElement", ->
describe "when the active item changes", ->
it "hides all item elements except the active one", ->
container = new PaneContainer
pane = container.getRoot()
item1 = document.createElement('div')
item2 = document.createElement('div')
item3 = document.createElement('div')
pane.addItem(item1)
pane.addItem(item2)
pane.addItem(item3)
paneElement = atom.views.getView(pane)
expect(pane.getActiveItem()).toBe item1
expect(item1.parentElement).toBeDefined()
expect(item1.style.display).toBe ''
expect(item2.parentElement).toBeNull()
expect(item3.parentElement).toBeNull()
pane.activateItem(item2)
expect(item2.parentElement).toBeDefined()
expect(item1.style.display).toBe 'none'
expect(item2.style.display).toBe ''
expect(item3.parentElement).toBeNull()
pane.activateItem(item3)
expect(item3.parentElement).toBeDefined()
expect(item1.style.display).toBe 'none'
expect(item2.style.display).toBe 'none'
expect(item3.style.display).toBe ''
it "transfers focus to the new item if the previous item was focused", ->
container = new PaneContainer
pane = container.getRoot()
item1 = document.createElement('div')
item1.tabIndex = -1
item2 = document.createElement('div')
item2.tabIndex = -1
pane.addItem(item1)
pane.addItem(item2)
paneElement = atom.views.getView(pane)
jasmine.attachToDOM(paneElement)
paneElement.focus()
expect(document.activeElement).toBe item1
pane.activateItem(item2)
expect(document.activeElement).toBe item2
describe "if the active item is a model object", ->
it "retrieves the associated view from atom.views and appends it", ->
class TestModel
atom.views.addViewProvider TestModel, (model) ->
view = document.createElement('div')
view.model = model
view
item1 = new TestModel
item2 = new TestModel
container = new PaneContainer
pane = container.getRoot()
pane.addItem(item1)
pane.addItem(item2)
paneElement = atom.views.getView(pane)
expect(paneElement.itemViews.children[0].model).toBe item1
expect(paneElement.itemViews.children[0].style.display).toBe ''
pane.activateItem(item2)
expect(paneElement.itemViews.children[1].model).toBe item2
expect(paneElement.itemViews.children[0].style.display).toBe 'none'
expect(paneElement.itemViews.children[1].style.display).toBe ''
describe "when the new active implements .getPath()", ->
it "adds the file path and file name as a data attribute on the pane", ->
container = new PaneContainer
pane = container.getRoot()
item1 = document.createElement('div')
item1.getPath = -> '/foo/bar.txt'
item2 = document.createElement('div')
pane.addItem(item1)
pane.addItem(item2)
paneElement = atom.views.getView(pane)
expect(paneElement.dataset.activeItemPath).toBe '/foo/bar.txt'
expect(paneElement.dataset.activeItemName).toBe 'bar.txt'
pane.activateItem(item2)
expect(paneElement.dataset.activeItemPath).toBeUndefined()
expect(paneElement.dataset.activeItemName).toBeUndefined()
pane.activateItem(item1)
expect(paneElement.dataset.activeItemPath).toBe '/foo/bar.txt'
expect(paneElement.dataset.activeItemName).toBe 'bar.txt'
pane.destroyItems()
expect(paneElement.dataset.activeItemPath).toBeUndefined()
expect(paneElement.dataset.activeItemName).toBeUndefined()

View File

@@ -46,105 +46,6 @@ describe "PaneView", ->
deserializerDisposable.dispose()
jasmine.restoreDeprecationsSnapshot()
describe "when the active pane item changes", ->
it "hides all item views except the active one", ->
expect(pane.getActiveItem()).toBe view1
expect(view1.css('display')).not.toBe 'none'
pane.activateItem(view2)
expect(view1.css('display')).toBe 'none'
expect(view2.css('display')).not.toBe 'none'
it "triggers 'pane:active-item-changed'", ->
itemChangedHandler = jasmine.createSpy("itemChangedHandler")
container.on 'pane:active-item-changed', itemChangedHandler
expect(pane.getActiveItem()).toBe view1
paneModel.activateItem(view2)
paneModel.activateItem(view2)
expect(itemChangedHandler.callCount).toBe 1
expect(itemChangedHandler.argsForCall[0][1]).toBe view2
itemChangedHandler.reset()
paneModel.activateItem(editor1)
expect(itemChangedHandler).toHaveBeenCalled()
expect(itemChangedHandler.argsForCall[0][1]).toBe editor1
itemChangedHandler.reset()
it "transfers focus to the new active view if the previous view was focused", ->
container.attachToDom()
pane.focus()
expect(pane.activeView).not.toBe view2
expect(pane.activeView).toMatchSelector ':focus'
paneModel.activateItem(view2)
expect(view2).toMatchSelector ':focus'
describe "when the new activeItem is a model", ->
it "shows the item's view or creates and shows a new view for the item if none exists", ->
initialViewCount = pane.itemViews.find('.test-view').length
model1 =
id: 'test-model-1'
text: 'Test Model 1'
serialize: -> {@id, @text}
getViewClass: -> TestView
model2 =
id: 'test-model-2'
text: 'Test Model 2'
serialize: -> {@id, @text}
getViewClass: -> TestView
paneModel.activateItem(model1)
paneModel.activateItem(model2)
expect(pane.itemViews.find('.test-view').length).toBe initialViewCount + 2
paneModel.activatePreviousItem()
expect(pane.itemViews.find('.test-view').length).toBe initialViewCount + 2
paneModel.destroyItem(model2)
expect(pane.itemViews.find('.test-view').length).toBe initialViewCount + 1
paneModel.destroyItem(model1)
expect(pane.itemViews.find('.test-view').length).toBe initialViewCount
describe "when the new activeItem is a view", ->
it "appends it to the itemViews div if it hasn't already been appended and shows it", ->
expect(pane.itemViews.find('#view-2')).not.toExist()
paneModel.activateItem(view2)
expect(pane.itemViews.find('#view-2')).toExist()
paneModel.activateItem(view1)
paneModel.activateItem(view2)
expect(pane.itemViews.find('#view-2').length).toBe 1
describe "when the new activeItem implements ::getPath", ->
beforeEach ->
paneModel.activateItem(editor1)
it "adds the file path as a data attribute to the pane", ->
expect(pane).toHaveAttr('data-active-item-path')
it "adds the file name as a data attribute to the pane", ->
expect(pane).toHaveAttr('data-active-item-name')
describe "when the activeItem is destroyed", ->
it "removes the data attributes", ->
pane.destroyItems()
expect(pane).not.toHaveAttr('data-active-item-path')
expect(pane).not.toHaveAttr('data-active-item-name')
describe "when the new activeItem does not implement ::getPath", ->
beforeEach ->
paneModel.activateItem(editor1)
paneModel.activateItem(document.createElement('div'))
it "does not add the file path as a data attribute to the pane", ->
expect(pane).not.toHaveAttr('data-active-item-path')
it "does not add the file name as data attribute to the pane", ->
expect(pane).not.toHaveAttr('data-active-item-name')
describe "when an item is destroyed", ->
it "triggers the 'pane:item-removed' event with the item and its former index", ->
itemRemovedHandler = jasmine.createSpy("itemRemovedHandler")