Skip to content

Instantly share code, notes, and snippets.

@timruffles
Created April 24, 2012 21:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timruffles/2484149 to your computer and use it in GitHub Desktop.
Save timruffles/2484149 to your computer and use it in GitHub Desktop.
Custom JSTestDriver CSS expectations
(->
assertCssBoolean = (msg, selector, el, boolean) ->
if !el
[selector,el] = [msg, selector]
msg = "Unexpectedly #{if boolean then "didn't find" else "found"} '#{selector}' in #{$(el).html().truncate(400)}"
unless boolean == !!($(el).find(selector).length > 0)
fail msg
assertHasCss = (msg, selector, el) ->
assertCssBoolean msg, selector, el, true
assertHasntCss = (msg, selector, el) ->
assertCssBoolean msg, selector, el, false
assertMatchesSelector = (klass,el) ->
assertTrue "Expected #{el} to match #{klass}", $(el).is(klass)
assertDoesntMatchSelector = (klass,el) ->
assertFalse "Expected #{el} not to match #{klass}", $(el).is(klass)
)()
assertHasCountCss = (msg, selector, el, expected) ->
if !expected
[selector,el,expected] = [msg, selector, el]
count = $(el).find(selector).length
msg = "Found #{count} elements for '#{selector}', expected #{expected} in #{$(el).html().truncate(400)}"
count ||= $(el).find(selector).length
unless count is expected
fail msg
assertHasText = (msg, text, el) ->
if !el
[text, el] = [msg, text]
msg = "Could not find '#{text}' in #{$(el).html().truncate(400)}"
unless new RegExp(text).test $(el).html()
fail msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment