setting up unit test involving observer

183 views Asked by At

i'm having trouble figuring out how to approach writing a unit test for a few methods involving an observer. I have an Iron-pages element that is set up like this

<iron-pages>
    selected="[[page]]"
    attr-for-selected="name"
    fallback-selection="manhattan-transit-view404"
    role="main">
    <manhattan-transit-home name="home"></manhattan-transit-home>
    <manhattan-transit-map name="map"></manhattan-transit-map>
    <manhattan-transit-route name="route"></manhattan-transit-route>
    <manhattan-transit-report name="report"></manhattan-transit-report>
    <manhattan-transit-view404 name="view404"></manhattan-transit-view404>
</iron-pages>

it contains the names of webpages reachable by clicking on their respected buttons to allow a user to change web pages. There is an observer that is set up like this int the script node

properties: {
    page: {
      type: String,
      reflectToAttribute: true,
      observer: '_pageChanged'
    }
  },

  observers: [
    '_routePageChanged(routeData.page)'
  ],

  _routePageChanged: function(page) {
    this.page = page || 'home';
  },

  _pageChanged: function(page) {
    // Load page import on demand. Show 404 page if fails
    var resolvedPageUrl = this.resolveUrl('manhattan-transit-' + page + '.html');
    this.importHref(resolvedPageUrl, null, this._showPage404, true);
  },

  _showPage404: function() {
    this.page = 'view404';
  }

I am new to testing and I think I need a test for the _routePageChanged, the _pageChanged, and the _show404 functions, and have no idea how to approach testing those. I know i need to make sure the page switches correctly based on the clicked info, and I am probably going to want to simulate the various clicks but for the test, but don't know how to go about doing that.

0

There are 0 answers