如何测试聚合物中元素的存在?

时间:2017-07-10 07:20:11

标签: javascript polymer web-component web-component-tester

我在聚合物组件中面临这个问题,无法找到我面临的问题。以下是我的代码:

<dom-module id="invoice-history">
  <template>
      <template is="dom-if" if="{{!_isHistoryPresent(invoiceListHistory)}}" >
        <div class="no-message">{{noDataMessage}}</div>
      </template>
    <style include="invoice-history-styles"></style>
  </template>
  <script>
    Polymer({
      is: 'invoice-history',

      properties: {
        invoiceListHistory: Array
      },

      _isHistoryPresent: function (history) {
        var hp = true;
        if(history){
            hp = history.every(function (invoice) {
              return invoice.txnStatus == null;
            });
        }else {
            this.message = "history not present";
        }
        return !hp;
      }
    });
  </script>
</dom-module>

以下是我的测试(我已按预期编写了输出):

<test-fixture id="invoice-history-fixture">
  <template>
    <invoice-history></invoice-history>
  </template>
</test-fixture>
var invoiceListHistory = [good values]
before(function () {
   iHistory = fixture('invoice-history-fixture');
});
context('dom manipulation tests', function () {
      it('Should display no data message if there is no data', function (done) {
        iHistory.setAttribute('invoice-list-history', invoiceListHistory);
        iHistory.setAttribute('no-data-message', "It doesn't have data");
        window.setTimeout(function () {
         expect(Polymer.dom(iHistory.root).querySelector('.no-message').textContent).to.be.equal("XYZ"); // expected 'It doesn't have data' to equal 'XYZ'
         expect(Polymer.dom(iHistory.root).querySelector('.no-message')).to.be.visible; // It passes
         expect(qHistoryEl.message).to.be.equal('ABCD'); // expected 'history not present' to equal 'ABCD'
         expect(window.getComputedStyle(Polymer.dom(iHistory.root).querySelector('.no-message'), null).getPropertyValue('visibility')).to.be.equal('jkl');
         // expected 'visible' to equal 'jkl'

         done();
         }, 0);
      });
    });  

First expect(...)的结果是可以理解的。但是第二个期望(...)如何通过? 而这里最重要的事情,可能是根本原因,在第三期望(......)它是在说expected 'history not present' to equal 'ABCD'。这意味着代码会转到 _isHistoryPresent()方法中的 else {} 块。 如何?

如何使用dom-if测试动态创建元素的存在?

1 个答案:

答案 0 :(得分:0)

我远离聚合物专家 - 我现在只是自己学习它。但似乎&#34; setAttribute&#34;不是分配invoiceListHistory.的正确方法您也可以指定有关invoiceListHistory的更多详细信息。像这样的东西:

// properties for your element
properties: {
  "invoiceListHistory": {
    type: Array,
    // I'd do this if you want it to be exposed via attribute, 
    // but that seems weird to me with an array
    reflectToAttribute: true,
    value: []
  },
  "noDataMessage": {
    type: String,
    value: "No data available";
   }
}

// Then in your test
iHistory.invoiceListHistory = invoiceListHistory;

Aaaand我刚刚注意到这个问题是从7月10日开始的,所以我相信你现在已经解决了这个问题。没关系-_-