Convert FileFinder to a proper extension and remove references from RootView code

This commit is contained in:
Nathan Sobo
2012-05-07 19:30:07 -06:00
parent 81b7105a52
commit 31163f1d8c
8 changed files with 132 additions and 146 deletions

View File

@@ -1,40 +1,82 @@
RootView = require 'root-view'
FileFinder = require 'file-finder'
$ = require 'jquery'
{$$} = require 'space-pen'
describe 'FileFinder', ->
finder = null
paths = null
[rootView, finder] = []
beforeEach ->
paths = ['app.coffee', 'buffer.coffee', 'atom/app.coffee', 'atom/buffer.coffee']
finder = new FileFinder({paths})
finder.enableKeymap()
rootView = new RootView(pathToOpen: require.resolve('fixtures/sample.js'))
rootView.enableKeymap()
rootView.activateExtension(FileFinder)
finder = FileFinder.instance
describe "initialize", ->
it "populates the ol with all paths and selects the first element", ->
expect(finder.pathList.find('li').length).toBe paths.length
expect(finder.pathList.find('li:first')).toHaveClass('selected')
expect(finder.pathList.find('li.selected').length).toBe 1
describe "when the file-finder:toggle event is triggered on the root view", ->
describe "when there is a project", ->
it "shows or hides the FileFinder, returning focus to the active editor when hiding it", ->
rootView.attachToDom()
expect(rootView.find('.file-finder')).not.toExist()
rootView.find('.editor').trigger 'split-right'
[editor1, editor2] = rootView.find('.editor').map -> $(this).view()
rootView.trigger 'file-finder:toggle'
expect(rootView.find('.file-finder')).toExist()
expect(rootView.find('.file-finder input:focus')).toExist()
rootView.trigger 'file-finder:toggle'
expect(editor1.isFocused).toBeFalsy()
expect(editor2.isFocused).toBeTruthy()
expect(rootView.find('.file-finder')).not.toExist()
it "shows all relative file paths for the current project and selects the first", ->
rootView.trigger 'file-finder:toggle'
rootView.project.getFilePaths().done (paths) ->
expect(finder.pathList.children('li').length).toBe paths.length
for path in paths
expect(finder.pathList.find("li:contains(#{path})")).toExist()
expect(finder.pathList.children().first()).toHaveClass 'selected'
describe "when root view's project has no path", ->
beforeEach ->
rootView.project.path = undefined
it "does not open the FileFinder", ->
expect(rootView.find('.file-finder')).not.toExist()
rootView.trigger 'file-finder:toggle'
expect(rootView.find('.file-finder')).not.toExist()
describe "file-finder:cancel event", ->
it "hides the finder", ->
rootView.trigger 'file-finder:toggle'
expect(finder.hasParent()).toBeTruthy()
finder.trigger 'file-finder:cancel'
expect(finder.hasParent()).toBeFalsy()
describe "when characters are typed into the input element", ->
it "displays matching paths in the ol element and selects the first", ->
finder.editor.insertText('ap')
rootView.trigger 'file-finder:toggle'
expect(finder.pathList.children().length).toBe 2
expect(finder.pathList.find('li:contains(app.coffee)').length).toBe 2
expect(finder.pathList.find('li:contains(atom/app.coffee)').length).toBe 1
listLengthBefore = finder.pathList.children().length
finder.editor.insertText('samp')
expect(finder.pathList.children().length).toBeLessThan(listLengthBefore)
expect(finder.pathList.find('li:first')).toHaveClass 'selected'
expect(finder.pathList.find('li.selected').length).toBe 1
# we should clear the list before re-populating it
finder.editor.setCursorScreenPosition([0, 0])
finder.editor.insertText('a/')
finder.editor.insertText('txt')
expect(finder.pathList.children().length).toBe 1
expect(finder.pathList.find('li:contains(atom/app.coffee)').length).toBe 1
expect(finder.pathList.find('li:first')).toHaveClass 'selected'
expect(finder.pathList.find('li:first')).toHaveText 'sample.txt'
describe "move-down / move-up events", ->
beforeEach ->
rootView.trigger 'file-finder:toggle'
it "selects the next / previous path in the list", ->
expect(finder.find('li:eq(0)')).toHaveClass "selected"
expect(finder.find('li:eq(2)')).not.toHaveClass "selected"
@@ -56,41 +98,55 @@ describe 'FileFinder', ->
finder.editor.trigger keydownEvent('up')
expect(finder.find('li:first')).toHaveClass "selected"
for i in [1..paths.length+10]
for i in [1..finder.pathList.children().length+2]
finder.editor.trigger keydownEvent('down')
expect(finder.find('li:last')).toHaveClass "selected"
describe "select", ->
selectedCallback = jasmine.createSpy 'selected'
describe "select-file events", ->
[editor1, editor2] = []
beforeEach ->
finder = new FileFinder({paths, selected: selectedCallback})
finder.enableKeymap()
rootView.find('.editor').trigger 'split-right'
[editor1, editor2] = rootView.find('.editor').map -> $(this).view()
it "when a file is selected Editor.open is called", ->
spyOn(finder, 'remove')
finder.moveDown()
finder.editor.trigger keydownEvent('enter')
expect(selectedCallback).toHaveBeenCalledWith(paths[1])
expect(finder.remove).toHaveBeenCalled()
rootView.trigger 'file-finder:toggle'
it "when no file is selected, does nothing", ->
spyOn(atom, 'open')
finder.editor.insertText('this-will-match-nothing-hopefully')
finder.populatePathList()
finder.editor.trigger keydownEvent('enter')
expect(atom.open).not.toHaveBeenCalled()
describe "when there is a path selected", ->
it "opens the file associated with that path in the editor", ->
finder.trigger 'move-down'
selectedLi = finder.find('li:eq(1)')
expectedPath = rootView.project.resolve(selectedLi.text())
expect(editor1.buffer.path).not.toBe expectedPath
expect(editor2.buffer.path).not.toBe expectedPath
finder.trigger 'file-finder:select-file'
expect(finder.hasParent()).toBeFalsy()
expect(editor1.buffer.path).not.toBe expectedPath
expect(editor2.buffer.path).toBe expectedPath
expect(editor2.isFocused).toBeTruthy()
describe "when there is no path selected", ->
it "does nothing", ->
finder.editor.insertText('this should match nothing, because no one wants to drink battery acid')
finder.trigger 'file-finder:select-file'
expect(finder.hasParent()).toBeTruthy()
describe "findMatches(queryString)", ->
it "returns up to finder.maxResults paths if queryString is empty", ->
expect(paths.length).toBeLessThan finder.maxResults
expect(finder.findMatches('').length).toBe paths.length
beforeEach ->
rootView.trigger 'file-finder:toggle'
finder.maxResults = paths.length - 1
it "returns up to finder.maxResults paths if queryString is empty", ->
expect(finder.paths.length).toBeLessThan finder.maxResults
expect(finder.findMatches('').length).toBe finder.paths.length
finder.maxResults = finder.paths.length - 1
expect(finder.findMatches('').length).toBe finder.maxResults
it "returns paths sorted by score of match against the given query", ->
expect(finder.findMatches('ap')).toEqual ["app.coffee", "atom/app.coffee"]
expect(finder.findMatches('a/ap')).toEqual ["atom/app.coffee"]
finder.paths = ["app.coffee", "atom/app.coffee"]
expect(finder.findMatches('app.co')).toEqual ["app.coffee", "atom/app.coffee"]
expect(finder.findMatches('atom/app.co')).toEqual ["atom/app.coffee"]