Skip to content

Instantly share code, notes, and snippets.

View searls's full-sized avatar
💚

Justin Searls searls

💚
View GitHub Profile
describe "CallListView", ->
beforeEach ->
$template = inject('<script id="call-list-template" type="text/html"></script>')
$template.html('HTML <b>for</b> <%= foo %>')
@model = jasmine.createSpyObj('CallList',['bind','moveUp','toJSON'])
@subject = new CallListView model: @model
describe "events", ->
it "binds to up-arrow clicks", ->
window.blog = function(msg) {
lines.push({time: new Date(), message: msg});
$(function() {
var $flash = $('#flash-warning').css('font-family','Courier New').show().find('.message');
$flash.html('<a class="blah">reset</a> <a class="flush">flush</a><br/>'+_(lines).reduce(function(m,l) {
return m+'['+l.time+'] '+l.message+'<br/>';
},''));
});
};
$('.blah').live('click',function(e) { e.preventDefault(); lines = []; $('#flash-warning .message').html(''); });
@searls
searls / form_view.coffee
Created November 7, 2011 21:12
A little view that synchronizes a form in the DOM and its backing model via the view object.
class FormView extends Backbone.View
events: ->
'change :input': 'propogateChangesToModel'
render: ->
@syncForm(@$('form'),@model)
save: (e) ->
e.preventDefault()
e.stopPropagation()
#looking for some code that won't work? you're in luck!
window.newb = (Constructor) ->
args = Array.prototype.slice.call(arguments, 1)
return ->
Temp = ->
Temp.prototype = Constructor.prototype
inst = new Temp
ret = Constructor.apply(inst, args)
@searls
searls / makes_sandwiches.coffee
Created November 16, 2011 04:20
jasmine-given example
class MakesSandwiches
make: -> cheese: "swiss", bread: "sourdough"
$.fn.tooltip = $.fn.tooltip || ->
window.App = {}
App.addValidationSupportTo = (obj) ->
if obj instanceof Backbone.Model
support =
isValid: ->
result = _(@validations).all (validation,attr) =>
validation.test.call(@,@get(attr))
if result then true else false
@searls
searls / date.js
Created November 17, 2011 21:16
Faking dates in jasmine
/* Faking dates
* when I need to fake a date, my preferred way is to introduce a new
* production method that returns the date I need (usually I need the
* current date, but it could just as well forward arguments). Once
* we have it, we can spy on it just like we could anything else.
* **/
window.currentDate = function() { return new Date(); };
window.newAllyMcbealTonight = function(){
return currentDate().getFullYear() == 1998;
@searls
searls / example-gists.md
Created November 17, 2011 23:57
Some Jasmine fun!

some tricky test moments in Jasmine

If you know RSpec, Jasmine is easy! But some common uses of JavaScript may not be as immediately obvious.

Try to use Jasmine (you could use your own setup or just tryjasmine.com) to write some specs that cover cases like these. The links will load up an example.

mainWidget =
show: -> "Pretend this shows main widget"
login = ->
$.ajax
success: (response) ->
mainWidget.show() unless response == "fail"
#1
doStuff = -> alert("Stuff!")
$('.do-stuff').live 'click', (e) ->
doStuff()
#2
clickeyDo = (selector, callback) ->
$(selector).live 'click', (e) ->
e.preventDefault()
callback(e)