Skip to content

Instantly share code, notes, and snippets.

@netzpirat
Created October 18, 2010 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netzpirat/632397 to your computer and use it in GitHub Desktop.
Save netzpirat/632397 to your computer and use it in GitHub Desktop.
My Jasmine matchers taken from JSpec
this.addMatchers({
toBe: (selector) -> $(@actual).is(selector)
toBeA: (clazz) -> @actual.constructor == clazz
toBeAn: (clazz) -> @actual.constructor == clazz
toBeAnimated: -> $(@actual).queue.length > 0
toBeAnInstanceOf: (instance) -> @actual instanceof instance
toBeChecked: -> $(@actual).is ':checked'
toBeDisabled: -> $(@actual).attr 'disabled'
toBeElement: (element) -> $(@actual).get(0).tagName == element.toUpperCase()
toBeEmpty: -> @actual == '' || @actual.length == 0
toBeEnabled: -> not $(@actual).attr 'disabled'
toBeHidden: -> $(@actual).is ':hidden'
toBeNull: -> @actual == null
toBeSelected: -> $(@actual).is ':selected'
toBeUndefined: -> typeof @actual == 'undefined'
toBeVisible: -> $(@actual).is ':visible'
toContain: (selector) -> $(@actual).find(selector).size() > 0
toExist: -> @actual.size() > 0
toHaveAttr: (attr, value) -> $(@actual).attr(attr) == value
toHaveChild: (child) -> $(@actual).children(child).length == 1
toHaveChildren: (children) -> $(@actual).children(children).length > 1
toHaveClass: (clazz) -> $(@actual).hasClass clazz
toHaveLength: (length) -> @actual.length == length
toHaveName: (name) -> $(@actual).attr 'name' == name
toHaveTag: (tag) -> $(tag, @actual).length == 1
toHaveTags: (tags) -> $(tags, @actual).length > 1
toHaveText: (text) -> $(@actual).text() == text
toHaveType: (type) -> $(@actual).attr 'type' == type
toHaveProperty: (property, value) ->
actualValue = @actual[property]
actualType = typeof actualValue
return false if actualType == 'function' || actualType == 'undefined'
return typeof value == 'undefined' || value == actualValue
toHaveValue: (value) -> $(@actual).val() == value
toInclude: (objects...) ->
for obj in objects
switch @actual.constructor
when String, Number, RegExp, Function
included = @actual.toString().indexOf(obj) != -1
when Object
included = _.keys(@actual).indexOf(obj) != -1
when Array
included = @actual.indexOf(obj) != -1
return false if not included
true
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment