Skip to content

Instantly share code, notes, and snippets.

@jbaudanza
Created May 14, 2011 21:12
Show Gist options
  • Save jbaudanza/972643 to your computer and use it in GitHub Desktop.
Save jbaudanza/972643 to your computer and use it in GitHub Desktop.
jQuery plugin to do sanity checks on your selectors
# I live in constant fear of velociraptors, hipsters, and the risk that my jQuery
# selectors won't match the DOM at runtime.
#
# I've been using this pattern lately to assert that a jQuery object contains the
# elements that I expect. BeatLab.warn is a wrapper around the hoptoad notifier
# that sends me a warning message with a stacktrace.
#
# This is of course not a replacement for good unit tests, for which I highly
# recommend jasmine.
#
# Example:
# $('#please_be_there').assert().show()
# $('.a_few_things').assert(3).hide()
#
jQuery.fn.assert = (length) ->
if length?
if length != @length
BeatLab.warn("Expected '#{@selector}' to match #{length} elements")
else
if @length == 0
BeatLab.warn("Expected '#{@selector}' to match some elements")
this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment