pulsar/spec/spec-helper.coffee

160 lines
4.8 KiB
CoffeeScript
Raw Normal View History

2011-12-28 03:42:29 +04:00
nakedLoad 'jasmine-jquery'
$ = require 'jquery'
_ = require 'underscore'
2012-03-08 01:45:26 +04:00
Keymap = require 'keymap'
Point = require 'point'
Project = require 'project'
Directory = require 'directory'
2012-06-30 02:08:16 +04:00
File = require 'file'
RootView = require 'root-view'
2012-06-30 02:08:16 +04:00
fs = require 'fs'
require 'window'
2012-06-27 03:38:06 +04:00
$native.showDevTools()
2012-01-28 05:33:02 +04:00
requireStylesheet "jasmine.css"
defaultTitle = document.title
2012-06-30 02:08:16 +04:00
pathsWithSubscriptions = null
2012-01-28 05:33:02 +04:00
beforeEach ->
window.fixturesProject = new Project(require.resolve('fixtures'))
2012-01-28 05:33:02 +04:00
window.resetTimeouts()
2012-06-30 02:08:16 +04:00
pathsWithSubscriptions = []
afterEach ->
delete window.rootView if window.rootView
2012-05-12 03:06:42 +04:00
$('#jasmine-content').empty()
document.title = defaultTitle
2012-06-30 02:08:16 +04:00
ensureNoPathSubscriptions()
2012-04-04 04:20:48 +04:00
window.keymap.bindKeys '*', 'meta-w': 'close'
2012-03-08 01:27:21 +04:00
$(document).on 'close', -> window.close()
# Don't load user configuration in specs, because it's variable
RootView.prototype.loadUserConfiguration = ->
2012-06-30 02:08:16 +04:00
for klass in [Directory, File]
klass.prototype.originalOn = klass.prototype.on
klass.prototype.on = (args...) ->
pathsWithSubscriptions.push(this) if @subscriptionCount() == 0
@originalOn(args...)
2012-06-30 02:08:16 +04:00
ensureNoPathSubscriptions = ->
totalSubscriptionCount = 0
2012-06-30 02:08:16 +04:00
for path in pathsWithSubscriptions
totalSubscriptionCount += path.subscriptionCount()
console.log "Non-zero subscription count on", path if path.subscriptionCount() > 0
if totalSubscriptionCount > 0
2012-06-30 02:08:16 +04:00
throw new Error("Total path subscription count was #{totalSubscriptionCount}, when it should have been 0.\nSee console for details.")
# Use underscore's definition of equality for toEqual assertions
jasmine.Env.prototype.equals_ = _.isEqual
emitObject = jasmine.StringPrettyPrinter.prototype.emitObject
jasmine.StringPrettyPrinter.prototype.emitObject = (obj) ->
if obj.inspect
@append obj.inspect()
else
emitObject.call(this, obj)
window.keyIdentifierForKey = (key) ->
if key.length > 1 # named key
key
else
charCode = key.toUpperCase().charCodeAt(0)
"U+00" + charCode.toString(16)
window.keydownEvent = (key, properties={}) ->
event = $.Event "keydown", _.extend({originalEvent: { keyIdentifier: keyIdentifierForKey(key) }}, properties)
# event.keystroke = (new Keymap).keystrokeStringForEvent(event)
event
2012-01-11 03:30:53 +04:00
window.mouseEvent = (type, properties) ->
if properties.point
{point, editor} = properties
{top, left} = @pagePixelPositionForPoint(editor, point)
properties.pageX = left + 1
properties.pageY = top + 1
2012-02-03 02:57:05 +04:00
properties.originalEvent ?= {detail: 1}
$.Event type, properties
window.clickEvent = (properties={}) ->
window.mouseEvent("click", properties)
window.mousedownEvent = (properties={}) ->
window.mouseEvent('mousedown', properties)
2012-01-29 05:00:59 +04:00
window.mousemoveEvent = (properties={}) ->
window.mouseEvent('mousemove', properties)
2012-01-29 05:00:59 +04:00
window.waitsForPromise = (fn) ->
window.waitsFor (moveOn) ->
promise = fn()
promise.then promise.done(moveOn), promise.fail(moveOn)
2012-01-28 05:33:02 +04:00
window.resetTimeouts = ->
window.now = 0
window.timeoutCount = 0
window.timeouts = []
window.setTimeout = (callback, ms) ->
id = ++window.timeoutCount
window.timeouts.push([id, window.now + ms, callback])
id
window.clearTimeout = (idToClear) ->
window.timeouts = window.timeouts.filter ([id]) -> id != idToClear
window.advanceClock = (delta=1) ->
2012-01-28 05:33:02 +04:00
window.now += delta
window.timeouts = window.timeouts.filter ([id, strikeTime, callback]) ->
if strikeTime <= window.now
callback()
false
else
true
window.pagePixelPositionForPoint = (editor, point) ->
point = Point.fromObject point
top = editor.renderedLines.offset().top + point.row * editor.lineHeight
left = editor.renderedLines.offset().left + point.column * editor.charWidth - editor.renderedLines.scrollLeft()
{ top, left }
window.tokensText = (tokens) ->
_.pluck(tokens, 'value').join('')
window.setEditorWidthInChars = (editor, widthInChars, charWidth=editor.charWidth) ->
editor.width(charWidth * widthInChars + editor.renderedLines.position().left)
window.setEditorHeightInLines = (editor, heightInChars, charHeight=editor.lineHeight) ->
editor.height(charHeight * heightInChars + editor.renderedLines.position().top)
$(window).trigger 'resize' # update editor's on-screen lines
$.fn.resultOfTrigger = (type) ->
event = $.Event(type)
this.trigger(event)
event.result
$.fn.enableKeymap = ->
2012-02-29 23:33:15 +04:00
@on 'keydown', (e) => window.keymap.handleKeyEvent(e)
2012-01-17 07:23:27 +04:00
$.fn.attachToDom = ->
$('#jasmine-content').append(this)
$.fn.simulateDomAttachment = ->
$('<html>').append(this)
$.fn.textInput = (data) ->
this.each ->
event = document.createEvent('TextEvent')
event.initTextEvent('textInput', true, true, window, data)
event = jQuery.event.fix(event)
$(this).trigger(event)
$.fn.simulateDomAttachment = ->
$('<html>').append(this)
unless fs.md5ForPath(require.resolve('fixtures/sample.js')) == "dd38087d0d7e3e4802a6d3f9b9745f2b"
throw "Sample.js is modified"