I started testing my web components and noticed that Dart has a good library for that and it does work well with Dart. But now I also want to test my Polymer component, but
You need proper Polymer initialization code. See how to implement a main function in polymer apps for details.
This package contains a lot of Polymer.dart unit tests you can use as example https://github.com/dart-lang/core-elements/tree/master/test
import "../../../components/medium/bar-chart.dart";
import "package:unittest/unittest.dart";
import 'package:polymer/polymer.dart';
import 'dart:html';
main() {
// old initPolymer().run(() {
initPolymer().then((zone) => zone.run(() {
print("--- Bar Chart Test ---");
group('Bar Chart Test', () {
test(("queryForBarChart"),() {
expect(querySelector('bar-chart'), isNotNull); // PASS
});
test(("testtest"),() {
// This should pass
expect(querySelector('bar-chart').test(), equals(5));
// Test failed: Caught type 'HtmlElement' is not a subtype of type 'BarChartElement' in type cast.
// without Cast: Test failed: Caught Class 'HtmlElement' has no instance method 'test'.
});
});
});
}
Hint: don't forget to add the entry pages for the test to the polymer transformer configuation.