2

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

<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(...) 的结果是可以理解的。但是第二个 expect(...) 如何通过?
这里最重要的事情,可能是根本原因,是在 Third expect(...) 它说expected 'history not present' to equal 'ABCD'。这意味着代码转到_isHistoryPresent()方法内的else{}块。如何?

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

4

1 回答 1

0

我远不是聚合物专家 - 我现在自己也在学习它。但似乎“setAttribute”不是分配的正确方法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 日开始的,所以我相信你现在已经解决了这个问题。没关系 -_-

于 2017-10-11T19:16:28.147 回答