Thursday, January 3, 2013

Testing UI Events in Dart

I have finally moved on to updating the events chapter in Dart for Hipsters. I do not think too much has changed in Dart since I wrote that chapter, but I do need to figure out how to test it.

I think that this chapter will also require browser-based unit tests. For many, if not all, of the tests, I could probably get away with faking the various Element, Event, and EventListenerList classes. Such tests would allow me to verify that the code compiles and runs—I could even use dart_analyzer to ensure a certain amount of propriety. But it is important that I verify that the event API in Dart does not change out from under me, so "real" tests it is.

I already know how to do this to a certain extent. I start with an empty HTML document that will source my test suite:

The question then becomes how do I simulate a click event in Dart and how do I verify the result? Verifying the result seems a simple matter of checking the element's style. It turns out that the Element class supports a click() method for doing just what I need. So I write my test as: