Polymer 2.0:在Chrome上的测试中未呈现的子组件

时间:2017-05-18 14:17:43

标签: javascript google-chrome polymer mocha polymer-2.x

我有一个简单的聚合物组件,使用2个不同的组件。在页面上使用时,一切正常,但是当我运行测试时,它们仅在Chrome上失败。不呈现子组件moment-elementduration-view的内容。

<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../../bower_components/moment-element/moment-element.html">
<link rel="import" href="../duration-view/duration-view.html">

<dom-module id="issue-row">
    <template>
        <li>
            [[issue]]
            <moment-element datetime="[[startDate]]" output-format="[[startDateFormat]]"></moment-element>
            -
            <moment-element datetime="[[endDate]]" output-format="[[endDateFormat]]"></moment-element>
            <duration-view date-from="[[startDate]]" date-to="[[endDate]]"></duration-view>
        </li>
    </template>

    <script>
        class IssueRow extends Polymer.Element {
            static get is() { return 'issue-row'; }
            static get properties() {
                return {
                    issue : String,
                    startDate : String,
                    endDate : String,
                    startDateFormat : {
                        type : String,
                        value : 'D.MM HH:mm'
                    },
                    endDateFormat : {
                        type : String,
                        value : 'HH:mm'
                    }
                };
            }
        }

        window.customElements.define(IssueRow.is, IssueRow);
    </script>
</dom-module>
<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../../bower_components/moment-element/moment-import.html">
<link rel="import" href="moment-duration-format-import.html">

<dom-module id="duration-view">
    <template>
        some static content which is not rendered.
        [[formattedDuration]]
    </template>

    <script>
        class DurationView extends Polymer.Element {
            static get is() { return 'duration-view'; }
            static get properties() {
                return {
                    format : {
                        type : String,
                        value : "hh:mm"
                    },
                    duration : {
                        type : String,
                        value : '00:00:00'
                    },
                    dateFrom : String,
                    dateTo : String,
                    formattedDuration : {
                        type : String,
                        notify : true
                    }
                };
            }
            static get observers() {
                return ['_computeDuration(duration, dateFrom, dateTo, format)']
            }
            _computeDuration(duration, dateFrom, dateTo, format) {
                console.log(duration, dateFrom, dateTo, format);
                var output;
                if (dateFrom && dateTo) {
                    var from = moment(dateFrom);
                    var to = moment(dateTo);
                    if (!from.isValid() || !to.isValid()) {
                        this.set('formattedDuration', 'Invalid date');
                        return;
                    }
                    output = moment.duration(to.diff(from), 'ms');
                } else {
                    output = moment.duration(duration);
                }
                this.set('formattedDuration', output.format(format, {trim: false}));
            }
        }

        window.customElements.define(DurationView.is, DurationView);
    </script>
</dom-module>
  <test-fixture id="BasicTestFixture">
    <template>
      <issue-row issue="PR-493" start-date="2017-05-18 11:00" end-date="2017-05-18 14:30"></issue-row>
    </template>
  </test-fixture>

  <script>
      suite('issue-row', function () {

        test('displays issue number, dates and duration', function () {
          var element = fixture('BasicTestFixture');
          var elementShadowRoot = element.shadowRoot;
          var elementText = elementShadowRoot.textContent;
          expect(elementText).to.have.string("PR-493");
          expect(elementText).to.have.string("18.05");
          expect(elementText).to.have.string("11:00");
          expect(elementText).to.have.string("14:30");
          expect(elementText).to.have.string("3:30");
        });

      });
  </script>

我在第二次expect来电时遇到断言错误:

Error: expected '\n        \n            PR-493\n            \n            -\n            \n            \n        \n    ' to contain '18.05'
  Context.<anonymous> at issue-row_test.html:32
console.log中的

_computeDuration在Chrome上进行测试时仅输出默认值(或为空)。

这适用于Firefox,但在Chrome上失败。测试结果:

chrome 58 (5/0/1)                       firefox 53 (6/0/0)

1 个答案:

答案 0 :(得分:0)

问题是对DOM DOM概念的误解。元素已呈现,但它们未在标准DOM遍历中显示。在这种特殊情况下,textContentinnerHTMLouterHTML属性会忽略阴影元素。它适用于Firefox,因为它缺少影子DOM支持,并且它在标准DOM树中呈现所有内容。

我制作了一个用于测试嵌套组件内容的简单工具:

window.deep = function (element) {
    return {
        textContent : (function () {
            if (element.nodeType === Node.TEXT_NODE) {
                return element.textContent;
            }
            var children;
            if (element.shadowRoot) {
                children = element.shadowRoot.childNodes;
            } else {
                children = element.childNodes;
            }
            var content = [];
            for (var i in children) {
                content.push(deep(children[i]).textContent);
            }
            return content.join('');
        })()
    }
}

如果需要,请随意使用此概念。用法:deep(fixture('fixtureId')).textContent