Util.spec.js 4.77 KB
define(function(require) {
    'use strict';
    var _ = require('underscore');
    var Backbone = require('backbone');
    var Util = require('solr/model/Util');

    var BaseModel = Backbone.Model.extend({}, {
        staticProps: function() {
            return {
                hardCoded: 74,
                override: 75,
            };
        },
        staticSets: ['hardCoded', 'override'],
    });
    var SubModel = BaseModel.extend({}, {
        staticProps: {
            override: 76,
            newConstant: 77,
        },
        staticSets: function() {
            return ['override', 'newConstant'];
        },
    });
    describe('Util', function() {
        it('loads', function() {
            expect(Util).toBeDefined();
        });

        it('exported items', function() {
            var keys = {};
            _.each(Util, function(value, key) {
                keys[key] = true;
            });
            expect(keys).toEqual({
                preventDefault: true,
                stepFalse: true,
                getField: true,
                setField: true,
                getItemKeyAccessor: true,
                mergeStaticProps: true,
                mergeStaticSets: true,
            });
        });
        describe('methods', function() {
            var e, pojo, bb, emptyBB;
            beforeEach(function() {
                pojo = {a: 1, b: 2, key: 'TheKey'};
                bb = new BaseModel(pojo);
                emptyBB = new SubModel();
                e = {preventDefault: jasmine.createSpy('preventDefault')};
            });
            it('preventDefault(null)', function() {
                var r = Util.preventDefault(null);
                expect(r).toBe(undefined);
                expect(e.preventDefault).not.toHaveBeenCalled();
            });
            it('preventDefault(undefined)', function() {
                var r = Util.preventDefault(undefined);
                expect(r).toBe(undefined);
                expect(e.preventDefault).not.toHaveBeenCalled();
            });
            it('preventDefault(e)', function() {
                var r = Util.preventDefault(e);
                expect(r).toBe(undefined);
                expect(e.preventDefault).toHaveBeenCalled();
            });
            it('stepFalse(null)', function() {
                var r = Util.stepFalse(null);
                expect(r).toBe(false);
                expect(e.preventDefault).not.toHaveBeenCalled();
            });
            it('stepFalse(undefined)', function() {
                var r = Util.stepFalse(undefined);
                expect(r).toBe(false);
                expect(e.preventDefault).not.toHaveBeenCalled();
            });
            it('preventDefault(e)', function() {
                var r = Util.stepFalse(e);
                expect(r).toBe(false);
                expect(e.preventDefault).toHaveBeenCalled();
            });
            it('setField only modifies given', function() {
                var r = Util.setField(pojo, 'a', 100);
                expect(pojo).toEqual({a: 100, b: 2, key: 'TheKey'});
                expect(r).toBe(100);
            });
            it('setField adds new', function() {
                var r = Util.setField(pojo, 'c', 42);
                expect(pojo).toEqual({a: 1, b: 2, c: 42, key: 'TheKey'});
                expect(r).toBe(42);
            });
            it('getField returns value', function() {
                var r = Util.getField(pojo, 'b');
                expect(pojo).toEqual({a: 1, b: 2, key: 'TheKey'});
                expect(r).toBe(2);
            });
            it('getField returns undefined', function() {
                var r = Util.getField(pojo, 'c');
                expect(pojo).toEqual({a: 1, b: 2, key: 'TheKey'});
                expect(r).toBe(undefined);
            });
            it('itemKeyAccessor returns value', function() {
                var r = Util.getItemKeyAccessor(bb);
                expect(r).toBe('TheKey');
            });
            it('itemKeyAccessor returns undefined', function() {
                var r = Util.getItemKeyAccessor(emptyBB);
                expect(r).toBe(undefined);
            });
            it('mergeStaticProps', function() {
                var r = Util.mergeStaticProps(SubModel, Backbone.Model, {'default': 1}, 'staticProps');
                expect(r).toEqual({default: 1, hardCoded: 74, override: 76, newConstant: 77});
            });
            it('mergeStaticSets', function() {
                var r = Util.mergeStaticSets(SubModel, Backbone.Model, ['default'], 'staticSets');
                var keys = {};
                _.each(r, function(value) {
                    keys[value] = true;
                });
                expect(keys).toEqual({default: true, hardCoded: true, override: true, newConstant: true});
            });
        });
    });
});