我在我的聚合物组件中面临这个问题,无法找出我面临的问题。以下是我的代码:
<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 测试动态创建的元素的存在?