Util.spec.js 3.33 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({
                getField: true,
                setField: true,
                getItemKeyAccessor: true,
                mergeStaticProps: true,
                mergeStaticSets: true,
            });
        });
        describe('methods', function() {
            var pojo, bb, emptyBB;
            beforeEach(function() {
                pojo = {a: 1, b: 2, key: 'TheKey'};
                bb = new BaseModel(pojo);
                emptyBB = new SubModel();
            });
            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});
            });
        });
    });
});