Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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