Dart Mocking HTML library Dart Mocking HTML library dart dart

Dart Mocking HTML library


This is not easily doable as dart:html library is not headless (i.e. it needs a browser). I usually try to follow the MVP design pattern to make sure that the code that interacts with the DOM is only in my view class and that all biz logic is in the presenter. That way I unit test the presenter without needing access to DOM API's. A small example is included below.

// view interface has no reference to dart:htmlabstract class View {   hello();}// view impl uses dart:html but hands of all logic to the presenterclass ViewImpl implements View {   View(this._presenter) {      var link = new Element.html("<a href="">a link</a>");      link.on.click.add(_presenter.onClick());      body.nodes.add(link);   }   hello() {      body.nodes.add(new Element.html("<p>Hello from presenter</p>");   }   Presenter _presenter;}// presenter acts on the View interface and can therefor be tested with a mock.class Presenter {  Presenter(this._view);  onClick() => _view.hello();  View _view;}