pulsar/spec/app/window-spec.coffee

208 lines
7.2 KiB
CoffeeScript
Raw Normal View History

$ = require 'jquery'
fs = require 'fs-utils'
2013-03-04 23:12:20 +04:00
{less} = require 'less'
describe "Window", ->
projectPath = null
beforeEach ->
spyOn(atom, 'getPathToOpen').andReturn(project.getPath())
window.handleWindowEvents()
window.deserializeWindowState()
projectPath = project.getPath()
afterEach ->
window.shutdown()
$(window).off 'beforeunload'
2013-02-06 00:03:56 +04:00
describe "when the window is loaded", ->
it "doesn't have .is-blurred on the body tag", ->
2013-02-06 00:03:56 +04:00
expect($("body")).not.toHaveClass("is-blurred")
2013-02-06 00:59:26 +04:00
describe "when the window is blurred", ->
2013-02-06 00:03:56 +04:00
beforeEach ->
$(window).trigger 'blur'
afterEach ->
$('body').removeClass('is-blurred')
it "adds the .is-blurred class on the body", ->
expect($("body")).toHaveClass("is-blurred")
describe "when the window is focused again", ->
it "removes the .is-blurred class from the body", ->
$(window).trigger 'focus'
expect($("body")).not.toHaveClass("is-blurred")
2013-03-13 02:54:07 +04:00
describe "window:close event", ->
describe "when no pane items are modified", ->
it "calls window.close", ->
spyOn window, 'close'
$(window).trigger 'window:close'
expect(window.close).toHaveBeenCalled()
2013-01-08 01:15:33 +04:00
2013-03-13 02:54:07 +04:00
describe "when pane items are are modified", ->
it "prompts user to save and and calls window.close", ->
spyOn(window, 'close')
2013-03-13 02:54:07 +04:00
spyOn(atom, "confirm").andCallFake (a, b, c, d, e, f, g, noSave) -> noSave()
editSession = rootView.open("sample.js")
editSession.insertText("I look different, I feel different.")
$(window).trigger 'window:close'
2013-03-13 02:54:07 +04:00
expect(window.close).toHaveBeenCalled()
expect(atom.confirm).toHaveBeenCalled()
2013-03-13 02:54:07 +04:00
it "prompts user to save and aborts if dialog is canceled", ->
spyOn(window, 'close')
2013-03-13 02:54:07 +04:00
spyOn(atom, "confirm").andCallFake (a, b, c, d, e, cancel) -> cancel()
editSession = rootView.open("sample.js")
editSession.insertText("I look different, I feel different.")
$(window).trigger 'window:close'
2013-03-13 02:54:07 +04:00
expect(window.close).not.toHaveBeenCalled()
expect(atom.confirm).toHaveBeenCalled()
2013-01-08 01:15:33 +04:00
describe ".reload()", ->
beforeEach ->
spyOn($native, "reload")
it "returns false when no buffers are modified", ->
window.reload()
expect($native.reload).toHaveBeenCalled()
it "shows an alert when a modifed buffer exists", ->
2012-08-31 02:19:38 +04:00
rootView.open('sample.js')
rootView.getActiveView().insertText("hi")
2012-08-30 05:54:39 +04:00
spyOn(atom, "confirm")
window.reload()
expect($native.reload).not.toHaveBeenCalled()
2012-08-30 05:54:39 +04:00
expect(atom.confirm).toHaveBeenCalled()
describe "requireStylesheet(path)", ->
2013-03-06 05:03:28 +04:00
it "synchronously loads css at the given path and installs a style tag for it in the head", ->
cssPath = project.resolve('css.css')
lengthBefore = $('head style').length
2013-03-06 05:03:28 +04:00
requireStylesheet(cssPath)
expect($('head style').length).toBe lengthBefore + 1
2013-03-06 05:03:28 +04:00
element = $('head style[id*="css.css"]')
expect(element.attr('id')).toBe cssPath
expect(element.text()).toBe fs.read(cssPath)
# doesn't append twice
2013-03-06 05:03:28 +04:00
requireStylesheet(cssPath)
expect($('head style').length).toBe lengthBefore + 1
2013-03-06 05:03:28 +04:00
$('head style[id*="css.css"]').remove()
2013-03-04 23:12:20 +04:00
it "synchronously loads and parses less files at the given path and installs a style tag for it in the head", ->
2013-03-06 05:03:28 +04:00
lessPath = project.resolve('sample.less')
lengthBefore = $('head style').length
2013-03-06 05:03:28 +04:00
requireStylesheet(lessPath)
expect($('head style').length).toBe lengthBefore + 1
2013-03-06 05:03:28 +04:00
element = $('head style[id*="sample.less"]')
expect(element.attr('id')).toBe lessPath
expect(element.text()).toBe """
#header {
color: #4d926f;
}
h2 {
color: #4d926f;
}
2013-03-06 05:03:28 +04:00
"""
# doesn't append twice
2013-03-06 05:03:28 +04:00
requireStylesheet(lessPath)
expect($('head style').length).toBe lengthBefore + 1
2013-03-06 05:03:28 +04:00
$('head style[id*="sample.less"]').remove()
2012-12-27 05:35:19 +04:00
describe ".disableStyleSheet(path)", ->
it "removes styling applied by given stylesheet path", ->
cssPath = require.resolve(fs.join("fixtures", "css.css"))
expect($(document.body).css('font-weight')).not.toBe("bold")
requireStylesheet(cssPath)
expect($(document.body).css('font-weight')).toBe("bold")
removeStylesheet(cssPath)
expect($(document.body).css('font-weight')).not.toBe("bold")
describe ".shutdown()", ->
it "saves the serialized state of the window so it can be deserialized after reload", ->
projectPath = project.getPath()
expect(atom.getWindowState()).toEqual {}
# JSON.stringify removes keys with undefined values
rootViewState = JSON.parse(JSON.stringify(rootView.serialize()))
projectState = JSON.parse(JSON.stringify(project.serialize()))
syntaxState = JSON.parse(JSON.stringify(syntax.serialize()))
window.shutdown()
windowState = atom.getWindowState()
expect(windowState.rootView).toEqual rootViewState
expect(windowState.project).toEqual projectState
expect(windowState.syntax).toEqual syntaxState
expect(atom.saveWindowState).toHaveBeenCalled()
it "unsubscribes from all buffers", ->
2012-08-31 02:19:38 +04:00
rootView.open('sample.js')
buffer = rootView.getActivePaneItem().buffer
rootView.getActivePane().splitRight()
expect(window.rootView.find('.editor').length).toBe 2
window.shutdown()
expect(buffer.subscriptionCount()).toBe 0
it "only serializes window state the first time it is called", ->
window.shutdown()
window.shutdown()
expect(atom.saveWindowState.callCount).toBe 1
describe ".installAtomCommand(commandPath)", ->
commandPath = '/tmp/installed-atom-command/atom'
afterEach ->
fs.remove(commandPath) if fs.exists(commandPath)
describe "when the command path doesn't exist", ->
it "copies atom.sh to the specified path", ->
expect(fs.exists(commandPath)).toBeFalsy()
window.installAtomCommand(commandPath)
expect(fs.exists(commandPath)).toBeTruthy()
expect(fs.read(commandPath).length).toBeGreaterThan 1
describe ".deserialize(state)", ->
class Foo
@deserialize: ({name}) -> new Foo(name)
constructor: (@name) ->
beforeEach ->
registerDeserializer(Foo)
afterEach ->
unregisterDeserializer(Foo)
it "calls deserialize on the deserializer for the given state object, or returns undefined if one can't be found", ->
object = deserialize({ deserializer: 'Foo', name: 'Bar' })
expect(object.name).toBe 'Bar'
expect(deserialize({ deserializer: 'Bogus' })).toBeUndefined()
describe "when the deserializer has a version", ->
beforeEach ->
Foo.version = 2
describe "when the deserialized state has a matching version", ->
it "attempts to deserialize the state", ->
object = deserialize({ deserializer: 'Foo', version: 2, name: 'Bar' })
expect(object.name).toBe 'Bar'
describe "when the deserialized state has a non-matching version", ->
it "returns undefined", ->
expect(deserialize({ deserializer: 'Foo', version: 3, name: 'Bar' })).toBeUndefined()
expect(deserialize({ deserializer: 'Foo', version: 1, name: 'Bar' })).toBeUndefined()
expect(deserialize({ deserializer: 'Foo', name: 'Bar' })).toBeUndefined()