Created
October 18, 2010 15:27
-
-
Save netzpirat/632397 to your computer and use it in GitHub Desktop.
My Jasmine matchers taken from JSpec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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