Skip to content
This repository has been archived by the owner on Jan 12, 2018. It is now read-only.

Commit

Permalink
no need to define assertions before each test
Browse files Browse the repository at this point in the history
  • Loading branch information
Burgestrand committed Jun 4, 2014
1 parent 58eee75 commit 4765b39
Showing 1 changed file with 44 additions and 44 deletions.
88 changes: 44 additions & 44 deletions test/spec_helper.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -39,54 +39,54 @@ beforeEach ->
@render = (template, model, controller) =>
@body.appendChild(Serenade.view(template).render(model, controller))

chai.Assertion::element = (selector, options) ->
if options and options.count
actual = @obj.find(selector).length
@assert actual == options.count, "expected #{options.count} of @{selector}, but there were #{actual}", "expected #{selector} not to occur #{options.count} times, but it did"
else
@assert @obj.querySelectorAll(selector).length > 0, "expected #{selector} to occur", "expected #{selector} not to occur, but it did"
@sinon = sinon.sandbox.create()

chai.Assertion::text = (text) ->
actual = @obj.textContent.trim().replace(/\s+/g, ' ')
@assert actual is text, "expected #{actual} to be #{text}"
chai.Assertion::attribute = (name) ->
@assert @obj.hasAttribute(name), "expected #{@obj} to have attribute #{name}"
chai.Assertion::property = (name) ->
@assert @obj[name], "expected #{@obj} to have property #{name}"
chai.Assertion::triggerEvent = (event, options={}) ->
args = null
count = 0
fun = (a...) ->
args = a
count += 1
afterEach ->
@sinon.restore()

event.bind fun
@obj()
event.unbind fun
chai.Assertion::element = (selector, options) ->
if options and options.count
actual = @obj.find(selector).length
@assert actual == options.count, "expected #{options.count} of @{selector}, but there were #{actual}", "expected #{selector} not to occur #{options.count} times, but it did"
else
@assert @obj.querySelectorAll(selector).length > 0, "expected #{selector} to occur", "expected #{selector} not to occur, but it did"

if @negate
@assert count isnt 0, "", "event #{event.name} should not have been triggered (was triggered #{count} times)"
else
expectedCount = options.count ? 1
@assert count is expectedCount, "event #{event.name} was triggered #{count} times, expected #{expectedCount}"
if options.with
@assert compareArrays(args, options.with), "event arguments #{args} do not match expected arguments #{options.with}"
chai.Assertion::become = (value, done) ->
count = 0
test = =>
result = @obj()
if result is value
done()
else if count > 3
done(new Error("expected #{result} to become equal to #{value}"))
else
count += 1
setTimeout(test, 5)
test()
chai.Assertion::text = (text) ->
actual = @obj.textContent.trim().replace(/\s+/g, ' ')
@assert actual is text, "expected #{actual} to be #{text}"
chai.Assertion::attribute = (name) ->
@assert @obj.hasAttribute(name), "expected #{@obj} to have attribute #{name}"
chai.Assertion::property = (name) ->
@assert @obj[name], "expected #{@obj} to have property #{name}"
chai.Assertion::triggerEvent = (event, options={}) ->
args = null
count = 0
fun = (a...) ->
args = a
count += 1

@sinon = sinon.sandbox.create()
event.bind fun
@obj()
event.unbind fun

afterEach ->
@sinon.restore()
if @negate
@assert count isnt 0, "", "event #{event.name} should not have been triggered (was triggered #{count} times)"
else
expectedCount = options.count ? 1
@assert count is expectedCount, "event #{event.name} was triggered #{count} times, expected #{expectedCount}"
if options.with
@assert compareArrays(args, options.with), "event arguments #{args} do not match expected arguments #{options.with}"
chai.Assertion::become = (value, done) ->
count = 0
test = =>
result = @obj()
if result is value
done()
else if count > 3
done(new Error("expected #{result} to become equal to #{value}"))
else
count += 1
setTimeout(test, 5)
test()

root.context = describe

0 comments on commit 4765b39

Please sign in to comment.