dimanche 12 juillet 2015

Polymer unit test "ready" callback

I have the following unit test for my custom polymer component:

<head>
  <meta charset="UTF-8">
  <title>survey</title>

  <script src="../bower_components/webcomponentsjs/webcomponents.js"></script>
  <script src="/web-component-tester/browser.js"></script>
  <script src="../bower_components/test-fixture/test-fixture-mocha.js"></script>

  <link rel="import" href="../bower_components/polymer/polymer.html">
  <link rel="import" href="../bower_components/test-fixture/test-fixture.html">
  <link rel="import" href="../bower_components/iron-test-helpers/iron-test-helpers.html">
  <link rel="import" href="../views/components/survey.html">

</head>

<body>
  <test-fixture id="Network">
    <template>
      <survey></survey>
    </template>
  </test-fixture>
  <script>
    describe('<survey>', function() {
      var survey;


      describe('network', function() {
        beforeEach(function(done) {
          survey = fixture('Network');
        })
        it('should work', function() {
          expect(survey.$.dog).to.exist;
        });

      });
    });
  </script>

And the following custom polymer survey component:

<link rel="import" href="../../bower_components/paper-checkbox/paper-checkbox.html">
<link rel="import" href="../../bower_components/paper-button/paper-button.html">
<link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html">

<dom-module id="survey">
<template>
  <h3 class="text-center">Tell us about yourself!</h3>
  <div class="form-group">
    <label>I'm a...</label>
    <array-selector id="imaSelector" items="{{ima}}" selected="{{imaSelected}}" multi toggle></array-selector>
    <template is="dom-repeat" id="imaList" items="{{ima}}">
      <div class="checkbox">
        <paper-checkbox id="{{item.id}}" on-iron-change="toggleIma">{{item.name}}</paper-checkbox>
      </div>
    </template>
  </div>
</template>
</dom-module>
<script>
  Polymer({
    is: 'survey',
    properties: {
      ima: {
        type: Array,
        value: function() {
          return [ {
            name: 'House Cat',
            id: 'houseCat'
          }, {
            name: 'Basic Dog',
            id: 'dog'
          }, {
            name: 'Swimming Fish',
            id: 'fish'
          }];
        }
      },
    
    },
    toggleIma: function(e) {
      var item = this.$.imaList.itemForElement(e.target);
      if (item) {
        this.$.imaSelector.select(item.id);
      }
    }

  })
</script>

This test will fail, because the local dom is not initialized, due to the fact that I'm using a dom-repeat element.

How do I want until the local dom is stamped?

Aucun commentaire:

Enregistrer un commentaire