The syntax looks much better than the current usage of Async.
I prefer 'type()' or 'hasType()' over 'hasName()' or 'ofType()'
because the parameter to the Event constructor is called 'type', and
'ofType("eventType")' reminds me too much of 'instanceOf()'.
I'd also favour calling the 'timeOut()' function 'timeout()' as it
better mirrors the capitalisation of setTimeout().
Do you intend to / need to add a Async.handleEvent()-style method or
will users use a normal 'dispatcher.addEventListener()'?
Are there examples yet for event ordering? Why would enforcement of
ordering be specified in the EventRule constructor? I had a point here
about it being nice from a usage and implementation point of view but
may limit flexibility when mixing ordered and unordered events, then I
realised mixing ordered / unordered could be done with separate
EventRule instances for ordered and unordered events.
cheers,
Drew
I wrote these syntax examples while thinking about this,
http://gist.github.com/633667
In writing those I feel it is more natural to get users to stick with
adding their own event listeners instead of wrapping them with
handleEvent. Doing so uses syntax they are already familiar with, and
keeps the async event expectations separate from whatever event
handling they want to do as part of the test.
Adding .calls() is good, matches Mockolate.
On determining success, defining which event signifies the end of the
test may be a reasonable requirement. See the couple of tests at the
bottom of the gist.
As an alternate example of async in
http://github.com/drewbourne/spectacular I accumulate all calls to
async() and wait until each of their timeouts expire or all the
functions have been called. In this case these calls are only ever
onces. To support other counts and atLeast, atMost I'd lean towards
having a function that must be called to indicate test completion.
Qunit from jQuery uses a stop() / start() workflow to indicate each
test stopping while async calls are made, and then starting again in
the async result handlers. Works well in that context and may be a
pattern to consider.
cheers,
Drew
Much clearer about the goal of this rule now. My reference to using
addEventListener was more with regard validation that this rule may
not provide. With the proposed api though, any validation that needs
to happen on the event should be possible by this rule.
Continuing with the success/ failure cases, I think the EventRule will
have to expose whether it should wait until all the timeouts occurs or
continue as soon as it can. Some scenarios like once(), times(5) could
continue as soon as their expected counts are met. Others like
atLeast(3) and atMost(3) should probably wait for the timeouts to hit.
That distinction seems a bit arbitrary though. Similar to ordered
events EventRule could have a flag to indicate it should continue as
soon as possible or wait for timeout. I'd like to be able to override
it per expected event though.
cheers
Drew