Results 1 to 3 of 3

Thread: InBrowser ViewModel test

  1. #1
    Sencha Premium User
    Join Date
    Oct 2014
    Posts
    17
    Answers
    1

    Default Answered: InBrowser ViewModel test

    I am trying to test some formulas in a viewModel with an inbrowser test.
    It seems I am unable to get the value back out of the view model in the test.

    I've distilled it down to a very simple formula and test

    In the viewModel I have a record being stored as the "activeRecord" and a formula to get the value from one field in the record...something along these lines

    Code:
    Ext.define('MyApp.view.main.MainModel',{
    ...
    data: {
        activeRecord: undefined},
    
    formulas: {
        currentRecordId: function(get){
          return get('activeRecord').get('record_id');
        },
    }...
    }
    Then in the test I have something like this

    Code:
    describe("MainModel", function() {
    var mainModel;
    
    
    beforeEach(function(){
           mainModel = Ext.ComponentQuery.query('mainView')[0].getViewModel(); 
            var theRecord = Ext.create('MyApp.model.SomethingModel',{
               record_id: 'TEST'
            });
            mainModel.set('activeRecord', theRecord);
        });
    
    
        it('Should recognize a new vehicle based on year', function(){
            expect(mainModel.get('currentRecordId')).toEqual('TEST');
        });
    });
    I've tried various levels of nesting and setups, adding waits, etc... But no matter what I do the results of my evaluation of the viewModel formula is always null.

    I'd appreciate any suggestions.

    Thanks,
    Jill

  2. Try calling "notify" on the View Model after setting "activeRecord", for example:

    Code:
    // In-Browser scenario URL: https://examples.sencha.com/extjs/6.7.0/examples/kitchensink/frame-index.html#binding-formulas
    
    describe('View Model', function() {
        var mainModel;
        
        beforeEach(function() {
            mainModel = Ext.ComponentQuery.query('binding-formulas')[0].getViewModel();
            
            mainModel.set('x', 30);
            mainModel.notify();
        });
        
        it('should return correct values from the formulae', function() {
            expect(mainModel.get('x')).toEqual(30);
            expect(mainModel.get('twice')).toEqual(60);  // If "notify" wasn't called above, the formula value here won't have updated
            expect(mainModel.get('quad')).toEqual(120);
        });
    });

  3. #2
    Sencha - Sales Team daniel.gallo's Avatar
    Join Date
    Apr 2009
    Location
    London, England
    Posts
    394
    Answers
    74

    Default

    Try calling "notify" on the View Model after setting "activeRecord", for example:

    Code:
    // In-Browser scenario URL: https://examples.sencha.com/extjs/6.7.0/examples/kitchensink/frame-index.html#binding-formulas
    
    describe('View Model', function() {
        var mainModel;
        
        beforeEach(function() {
            mainModel = Ext.ComponentQuery.query('binding-formulas')[0].getViewModel();
            
            mainModel.set('x', 30);
            mainModel.notify();
        });
        
        it('should return correct values from the formulae', function() {
            expect(mainModel.get('x')).toEqual(30);
            expect(mainModel.get('twice')).toEqual(60);  // If "notify" wasn't called above, the formula value here won't have updated
            expect(mainModel.get('quad')).toEqual(120);
        });
    });
    Daniel Gallo
    Sales Engineer
    Sencha

  4. #3
    Sencha Premium User
    Join Date
    Oct 2014
    Posts
    17
    Answers
    1

    Default

    Thank you. In the mean time I had moved the code to a controller but I will remember to call notify on the viewModel next time. I'm sure that's what I've been missing.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •