Get help with testing, discuss unit testing strategies etc.


Post by mjschfr »

Does any one have any ideas on strategies for testing routes and query params inside of Ext JS ViewControllers.

Calling a function which performs a redirectTo doesn't seem to trigger the associated route or action in the routes config of a ViewController. Would have thought you could use a spyOn the action function, but no luck.

Also is there any way to test incoming location.search parameters. Updating location.search seems to break the testing environment. Unless I am just not doing it correctly.

Thanks in advance!

Martin :)

Post by nickolay »

If you can post some simple self-contained example we could come up with the solution.

Updating "location.search" should work. Perhaps it is done in the wrong context (should be done in the test page, not harness).

Post by mjschfr »

The code below shows the two different situations:
1) changing routes using a redirectTo (assertion fails, but no tests actually fail, verified onPagesRoutes does actually get called but toHaveBeenCalledWith fails with # of times call = 0) and
2) updating the location.search to extract and save incoming query params on start up, after updating location.search inside of the test it return null instead of 123 for project
Ext.define('MainViewController', {
   extend: 'Ext.app.ViewController',
   alias: 'controller.main',
   routes: {
      'pages/:id': {
         action: 'onPagesRoute',
         conditions: {
            ':id': '([%a-zA-Z0-9.\\-\\_\\s,]+)'
         }
      }
   },
   init: function(){

      //URL loading this page is something similar to https://localhost:1841/apps/AppName/?project=123
      var hasUrlParameters = this.hasUrlParameters();
   
   },
   hasUrlParameters: function() {

      var me = this;
      var viewModel = me.getViewModel();

      var project = me.getUrlParameter('project');
     
      if (project !== '') {

         viewModel.set('project', project);

         return true;

      }

      return false;

   },
   onPagesRoute: function(pageName) {

      //Load something with the pageName provided during the redirecTo operation

   },
   loadPageView: function(shortClassName) {

      var me = this;

      var redirectToURL = 'pages/' + shortClassName;
      me.redirectTo(redirectToURL, false);

   },
   getUrlParameter: function(name) {

      name = name.replace(/[\[]/, '\\[').replace(/[\]]/, '\\]');
      var regex = new RegExp('[\\?&]' + name + '=([^&#]*)');
      var results = regex.exec(location.search);
      return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' '));

   }
});
startTest(function(t) {

   t.describe("viewController", function(t) {

      var controller = Ext.create('MainViewController');
      var viewModel = Ext.create('Ext.app.ViewModel');
     
      controller.getViewModel = function() {
         return viewModel;
      };      

      t.describe('hasUrlParameters', function(t) {

        t.it('should extract values from the Url if present', function(t) {

            location.search = '?project=123';

            var hasUrlParams = controller.hasUrlParameters();

            //selectedProject comes back as null
            t.expect(viewModel.get('selectedProject')).toBe(123);
           
         });

      });

      t.todo('loadPageView', function(t) {

         t.todo('should take "pageName" and redirect route, expecting onPagesRoute to be called with the value for "pageName"',
            function(t) {

               var spy = t.spyOn(controller, 'onPagesRoute').and.callThrough();

               controller.loadPageView('pageOne');

               t.expect(spy).toHaveBeenCalledWith('pageOne');
               //This show that it fails assertion but doesn't actual fail any tests.

            });

      });   

   })
});

Post by nickolay »

Can you zip the whole setup to make it runnable?

Post by mjschfr »

Unfortunately, these are snippets I extracted and simplified from a much larger project. I don't actually have it running in a simple stand alone project. :(

Post by nickolay »

May be start a new empty project and add only the relevant parts in it?

Post Reply