2011-12-16 02:13:34 +04:00
|
|
|
Editor = require 'editor'
|
|
|
|
$ = require 'jquery'
|
|
|
|
ck = require 'coffeekup'
|
2011-12-17 02:42:38 +04:00
|
|
|
fs = require 'fs'
|
2011-12-16 02:13:34 +04:00
|
|
|
|
|
|
|
describe "Editor", ->
|
2011-12-17 04:25:33 +04:00
|
|
|
mainDiv = null
|
|
|
|
editor = null
|
|
|
|
filePath = null
|
|
|
|
tempFilePath = null
|
2011-12-16 02:13:34 +04:00
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
filePath = require.resolve 'fixtures/sample.txt'
|
2011-12-17 02:42:38 +04:00
|
|
|
tempFilePath = '/tmp/temp.txt'
|
2011-12-16 02:13:34 +04:00
|
|
|
mainDiv = $("<div id='main'>")
|
|
|
|
$("#jasmine-content").append(mainDiv)
|
2011-12-17 04:25:33 +04:00
|
|
|
spyOn(Editor.prototype, 'open').andCallThrough()
|
|
|
|
editor = new Editor
|
2011-12-16 02:13:34 +04:00
|
|
|
|
|
|
|
afterEach ->
|
2011-12-17 02:42:38 +04:00
|
|
|
fs.remove tempFilePath
|
2011-12-16 02:13:34 +04:00
|
|
|
editor.destroy()
|
|
|
|
|
|
|
|
describe "constructor", ->
|
2011-12-17 04:25:33 +04:00
|
|
|
it "attaches itself to the #main element and opens the given url", ->
|
2011-12-16 03:06:34 +04:00
|
|
|
expect(mainDiv.children('.editor').html()).not.toBe ''
|
2011-12-17 04:25:33 +04:00
|
|
|
expect(Editor.prototype.open).toHaveBeenCalled()
|
2011-12-16 02:13:34 +04:00
|
|
|
|
|
|
|
describe 'destroy', ->
|
|
|
|
it 'destroys the ace editor and removes #editor from the dom.', ->
|
2011-12-17 04:25:33 +04:00
|
|
|
spyOn(editor.aceEditor, 'destroy').andCallThrough()
|
2011-12-16 02:13:34 +04:00
|
|
|
|
|
|
|
editor.destroy()
|
|
|
|
expect(editor.aceEditor.destroy).toHaveBeenCalled()
|
2011-12-16 03:06:34 +04:00
|
|
|
expect(mainDiv.children('.editor').length).toBe 0
|
2011-12-16 02:13:34 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
describe "open(url)", ->
|
|
|
|
describe "when called with a url", ->
|
|
|
|
it "loads a buffer for the given url into the editor", ->
|
|
|
|
editor.open(filePath)
|
|
|
|
fileContents = fs.read(filePath)
|
2011-12-23 20:48:11 +04:00
|
|
|
expect(editor.getAceSession().getValue()).toBe fileContents
|
2011-12-17 04:25:33 +04:00
|
|
|
expect(editor.buffer.url).toBe(filePath)
|
|
|
|
expect(editor.buffer.getText()).toEqual fileContents
|
|
|
|
|
2011-12-23 20:48:11 +04:00
|
|
|
it "sets the mode on the session based on the file extension", ->
|
|
|
|
editor.open('something.js')
|
|
|
|
expect(editor.getAceSession().getMode().name).toBe 'javascript'
|
|
|
|
|
|
|
|
editor.open('something.text')
|
|
|
|
expect(editor.getAceSession().getMode().name).toBe 'text'
|
|
|
|
|
|
|
|
it "assigns the url on the $atomController global", ->
|
|
|
|
expect($atomController.url).toBeNull()
|
|
|
|
editor.open(filePath)
|
|
|
|
expect($atomController.url.toString()).toEqual(filePath)
|
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
describe "when called with null", ->
|
|
|
|
it "loads an empty buffer with no url", ->
|
|
|
|
editor.open()
|
2011-12-23 20:48:11 +04:00
|
|
|
expect(editor.getAceSession().getValue()).toBe ""
|
2011-12-17 04:25:33 +04:00
|
|
|
expect(editor.buffer.url).toBeUndefined()
|
|
|
|
expect(editor.buffer.getText()).toEqual ""
|
|
|
|
|
2011-12-16 02:13:34 +04:00
|
|
|
describe "when the text is changed via the ace editor", ->
|
|
|
|
it "updates the buffer text", ->
|
2011-12-17 04:25:33 +04:00
|
|
|
editor.open(filePath)
|
2011-12-16 03:06:34 +04:00
|
|
|
expect(editor.buffer.getText()).not.toMatch /^.ooo/
|
2011-12-23 20:48:11 +04:00
|
|
|
editor.getAceSession().insert {row: 0, column: 1}, 'ooo'
|
2011-12-16 03:06:34 +04:00
|
|
|
expect(editor.buffer.getText()).toMatch /^.ooo/
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
describe "save", ->
|
|
|
|
describe "when the current buffer has a url", ->
|
|
|
|
beforeEach ->
|
|
|
|
editor.open tempFilePath
|
|
|
|
expect(editor.buffer.url).toBe tempFilePath
|
|
|
|
|
|
|
|
it "saves the current buffer to disk", ->
|
|
|
|
editor.buffer.setText 'Edited buffer!'
|
|
|
|
expect(fs.exists(tempFilePath)).toBeFalsy()
|
|
|
|
|
|
|
|
editor.save()
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
expect(fs.exists(tempFilePath)).toBeTruthy()
|
|
|
|
expect(fs.read(tempFilePath)).toBe 'Edited buffer!'
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
describe "when the current buffer has no url", ->
|
|
|
|
selectedFilePath = null
|
2011-12-17 02:42:38 +04:00
|
|
|
beforeEach ->
|
2011-12-17 04:25:33 +04:00
|
|
|
expect(editor.buffer.url).toBeUndefined()
|
|
|
|
editor.buffer.setText 'Save me to a new url'
|
|
|
|
spyOn(atom.native, 'savePanel').andCallFake -> selectedFilePath
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
it "presents a 'save as' dialog", ->
|
|
|
|
editor.save()
|
|
|
|
expect(atom.native.savePanel).toHaveBeenCalled()
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
describe "when a url is chosen", ->
|
|
|
|
it "saves the buffer to the chosen url", ->
|
|
|
|
selectedFilePath = '/tmp/temp.txt'
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
editor.save()
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
expect(fs.exists(selectedFilePath)).toBeTruthy()
|
|
|
|
expect(fs.read(selectedFilePath)).toBe 'Save me to a new url'
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
describe "when dialog is cancelled", ->
|
|
|
|
it "does not save the buffer", ->
|
|
|
|
selectedFilePath = null
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
editor.save()
|
2011-12-17 02:42:38 +04:00
|
|
|
|
2011-12-17 04:25:33 +04:00
|
|
|
expect(fs.exists(selectedFilePath)).toBeFalsy()
|
2011-12-17 02:42:38 +04:00
|
|
|
|