How to test Polymer Dart

后端 未结 1 1478
挽巷
挽巷 2021-01-07 09:23

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

相关标签:
1条回答
  • 2021-01-07 10:03

    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.

    0 讨论(0)
提交回复
热议问题