Skip to content

Instantly share code, notes, and snippets.

@doitian
Created March 7, 2012 17:47
Show Gist options
  • Save doitian/1994659 to your computer and use it in GitHub Desktop.
Save doitian/1994659 to your computer and use it in GitHub Desktop.
jasmine spec_helper sample
describe 'Render Helpers', ->
describeFunction prettyNumber, 'prettyNumber',
'23123456, {abbreviateBy: "M"}': '23M'
'123456, {abbreviateBy: "M"}': '0M'
'123123, {abbreviateBy: "k"}': '123k'
'123123, {abbreviateBy: "x"}': '123123'
'79296886': '79,296,886'
'7929.6886': '7,929.6886'
'"1.0000"': '1.0000'
'"1234.120"': '1,234.120'
'12': '12'
'12, {decimals:2}': '12.00'
describe 'DataSource', ->
beforeEach ->
class @klass extends DataSource
@object = new @klass
describe '::fetch', ->
describe 'when DataSource.urlRoot is "/test", and original data is "pending"', ->
beforeEach ->
DataSource.urlRoot = '/test'
@object.data = "pending"
@stubAjax()
@abortSpy = @spy()
@ajax.returns abort: @abortSpy
@triggerSpy = @spy(@object, 'trigger')
describe 'when source name is "clients", url is "/clients" and original data is "pending"', ->
beforeEach ->
@klass.configure 'clients', '/clients'
describe 'when is invoked with no params', ->
beforeEach ->
@object.fetch()
it 'sends an ajax request', ->
expect(@ajax).toHaveBeenCalledOnce()
it 'triggers dataSourceFetch', ->
expect(@triggerSpy).toHaveBeenCalledWith('dataSourceFetch')
it 'sends request to /test/clients', ->
expect(@ajaxOption('url')).toBe('/test/clients/')
it 'sends description to "fetch clients"', ->
expect(@ajaxOption('description')).toBe('fetch clients')
it 'sends empty data', ->
expect(@ajaxOption('data')).toEqual({})
jasmine.Spec::fakeServer = ->
@server ||= sinon.fakeServer.create()
jasmine.Spec::respondWith = (args...) ->
@fakeServer().respondWith args...
jasmine.Spec::respond = ->
@server?.respond()
jasmine.Spec::spy = (args...) ->
@sinon.spy args...
# gets event object from a callback spy
jasmine.Spec::getEvent = (spy, counter = 0) ->
spy.getCall(counter).args[0]
jasmine.Spec::stub = (args...) ->
@sinon.stub args...
beforeEach ->
@sinon = sinon.sandbox.create()
afterEach ->
@server?.restore()
@sinon.restore()
jasmine.Spec::stubAjax = ->
@ajax = @stub($, 'ajax')
@ajaxOption = (name, call = 0) -> @ajax.getCall(call).args[0][name]
@ajax
matchers =
toHaveCss: (name, value) -> @actual.css(name) == value
root = global ? window
beforeEach ->
@addMatchers matchers
root.describeFunction = (func, name, testcases) ->
describe name, ->
for arg, expectation of testcases
do (arg, expectation) ->
describe "with arguments (#{arg})", ->
it "returns #{expectation}", ->
eval("var actual = func(#{arg});")
expect(actual).toEqual(expectation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment