pulsar/spec/app/project-spec.coffee
Nathan Sobo 331984148f Project.proto.open emits a 'new-buffer' event if a new buffer is created
RootView.proto.open always uses Project's open method now. RootView creates a project with no path when editing a new buffer, so there's always a project.
2012-04-17 17:15:19 -06:00

56 lines
2.4 KiB
CoffeeScript

Project = require 'project'
fs = require 'fs'
describe "Project", ->
project = null
beforeEach ->
project = new Project(require.resolve('fixtures/dir'))
describe ".open(path)", ->
[absolutePath, newBufferHandler] = []
beforeEach ->
absolutePath = require.resolve('fixtures/dir/a')
newBufferHandler = jasmine.createSpy('newBufferHandler')
project.on 'new-buffer', newBufferHandler
describe "when given an absolute path that hasn't been opened previously", ->
it "returns a new buffer for the given path and emits a 'new-buffer' event", ->
buffer = project.open(absolutePath)
expect(buffer.path).toBe absolutePath
expect(newBufferHandler).toHaveBeenCalledWith buffer
describe "when given a relative path that hasn't been opened previously", ->
it "returns a buffer for the given path (relative to the project root) and emits a 'new-buffer' event", ->
buffer = project.open('a')
expect(buffer.path).toBe absolutePath
expect(newBufferHandler).toHaveBeenCalledWith buffer
describe "when passed the path to a buffer that has already been opened", ->
it "returns the previously opened buffer", ->
buffer = project.open(absolutePath)
newBufferHandler.reset()
expect(project.open(absolutePath)).toBe buffer
expect(project.open('a')).toBe buffer
expect(newBufferHandler).not.toHaveBeenCalled()
describe "when not passed a path", ->
it "returns a new buffer and emits a new-buffer event", ->
buffer = project.open()
expect(buffer.path).toBeUndefined()
expect(newBufferHandler).toHaveBeenCalledWith(buffer)
describe ".getFilePaths()", ->
it "returns a promise which resolves to a list of all file paths in the project, recursively", ->
expectedPaths = (path.replace(project.path, '') for path in fs.listTree(project.path) when fs.isFile path)
waitsForPromise ->
project.getFilePaths().done (result) ->
expect(result).toEqual(expectedPaths)
describe ".resolve(path)", ->
it "returns an absolute path based on the project's root", ->
absolutePath = require.resolve('fixtures/dir/a')
expect(project.resolve('a')).toBe absolutePath
expect(project.resolve(absolutePath + '/../a')).toBe absolutePath
expect(project.resolve('a/../a')).toBe absolutePath