Using Ext Spec

Ext Spec can be used to test an Ext JS class instance, its constructor, its static methods and callback.

This example class is tested in each of the following scenarios:
Ext.define(
    'App.namespace.Class',
    {
        instanceMethod: function () {
            var item = 'item ' + this.instanceList.length;
            this.instanceList.push(item);

            return item;
        },

        constructor: function () {
            this.callParent(arguments);

            this.instanceList = [];
        }

        statics: {
            staticMethod: function () {
                return 'something static';
            }
        }
    },
    function () {
        Ext.ClassManager.setNamespace('App.staticMethod', this.staticMethod);
    }
);

Testing an Instance

describe('App.namespace.Class', function () {
    var instance;

    beforeEach(function () {
        instance = ExtSpec.create('App.namespace.Class', function () {
            this.callParent = jasmine.createSpy('callParent');
        });
    });

    describe('instanceMethod', function () {
        it('should do something', function () {
            var something = instance.instanceMethod();

            expect(something).toHaveHappened();
        })
    });
});

Testing a Constructor

describe('App.namespace.Class', function () {
    describe('constructor', function () {
        var constructor = ExtSpec.ClassManager.construct('App.namespace.Class');

        it('should do something', function () {
            var something = jasmine.createSpyObj('something', ['callParent']);

            constructor.call(something);

            expect(something).toHaveHappened();
        });
    });
});

Testing Static Methods

describe('App.namespace.Class', function () {
    describe('statics', function () {
        var constructor = ExtSpec.ClassManager.construct('App.namespace.Class');

        describe('staticMethod', function () {
            it('should do something', function () {
                var something = constructor.staticMethod();

                expect(something).toHaveHappened();
            });
        })
    });
});

Testing a Callback

describe('App.namespace.Class', function () {
    describe('callback', function () {
        var constructor = ExtSpec.ClassManager.construct('App.namespace.Class'),
            callback = ExtSpec.ClassManager.callback('App.namespace.Class');

        it('should do something', function () {
            callback.call(constructor, constructor);

            expect('something').toHaveHappened();
        });
    });
});

Last edited Jun 21, 2013 at 10:32 AM by Qube, version 4

Comments

No comments yet.