testing dom mutation in polymer 2

180 Views Asked by At

I have been trying to unit test a polymer element using WCT for some time. I am having issues when it comes to testing dom mutations. Below is the scenario which I am trying to test:

<div class="menu-wrapper">
            <template id="links" is="dom-if" if="{{isLoggedIn}}">
                <template id="menuList" is="dom-repeat" items={{menu}}>
                    <span id="{{item.name}}"><a class$="secondaryLinkButton [[item.class]]" href="{{_generateURL(rootPath,item.slug)}}">[[item.display]]</a></span>
                </template>
            </template>
</div>

Test looks like this:

<test-fixture id="commonHeader">
  <template>
    <common-header></common-header>
  </template>
</test-fixture>

<script>

  suite('<common-header>', function() {
    var commonHeader;

    var menus =  [
          {
            "display" : "Menu 1",
            "name"    : "Menu 1",
            "slug"    : "Menu 1"
          },
          {
            "display" : "Menu 2",
            "name"    : "Menu 2",
            "slug"    : "Menu 2"                
          },
          {
            "display" : "Menu 3",
            "name"    : "Menu 3",
            "slug"    : "Menu 3"                
          }
        ];

    var isLoggedIn = true;    
    var self = this;

    setup(function(){
      commonHeader = fixture('commonHeader');
    });

    test('header should be login', function(done) {
      commonHeader.set('isLoggedIn', true)
      commonHeader.menu = menus;
      flush(function(){
        var total = commonHeader.shadowRoot.querySelector('.menu-wrapper').querySelectorAll('span');
        console.log('total', total);
        done();
      });
    });



  })
</script>

Assume all of this is inside common-header element. When I am doing the testing I am not getting span tag itself. I get an empty array and when I try to debug in the console. I can see only #document-fragment. Any idea how to handle this?

1

There are 1 best solutions below

4
On

From the documentation:

Whenever possible you should always use Polymer's array mutation methods.

Your problem might be in

commonHeader.menu = menus;

And Polymer not being notified that the menu array property has changed, thus not updating the dom-repeat template. Try pushing the elements like so:

commonHeader.push('menu', menus);