49158a45 by Adam Heath

Util now has a spec.

1 parent b3e51cf0
...@@ -5,7 +5,7 @@ define(function(require) { ...@@ -5,7 +5,7 @@ define(function(require) {
5 var result = filterFunc(obj); 5 var result = filterFunc(obj);
6 var ptr = startPtr; 6 var ptr = startPtr;
7 while (true) { 7 while (true) {
8 result = _.extend(result, filterFunc(_.result(ptr, fieldName))); 8 result = _.defaults(result, filterFunc(_.result(ptr, fieldName)));
9 if (ptr === endPtr) { 9 if (ptr === endPtr) {
10 break; 10 break;
11 } 11 }
......
1 define(function(require) {
2 'use strict';
3 var _ = require('underscore');
4 var Backbone = require('backbone');
5 var Util = require('solr/model/Util');
6
7 var BaseModel = Backbone.Model.extend({}, {
8 staticProps: function() {
9 return {
10 hardCoded: 74,
11 override: 75,
12 };
13 },
14 staticSets: ['hardCoded', 'override'],
15 });
16 var SubModel = BaseModel.extend({}, {
17 staticProps: {
18 override: 76,
19 newConstant: 77,
20 },
21 staticSets: function() {
22 return ['override', 'newConstant'];
23 },
24 });
25 describe('Util', function() {
26 it('loads', function() {
27 expect(Util).toBeDefined();
28 });
29
30 it('exported items', function() {
31 var keys = {};
32 _.each(Util, function(value, key) {
33 keys[key] = true;
34 });
35 expect(keys).toEqual({
36 getField: true,
37 setField: true,
38 getItemKeyAccessor: true,
39 mergeStaticProps: true,
40 mergeStaticSets: true,
41 });
42 });
43 describe('methods', function() {
44 var pojo, bb, emptyBB;
45 beforeEach(function() {
46 pojo = {a: 1, b: 2, key: 'TheKey'};
47 bb = new BaseModel(pojo);
48 emptyBB = new SubModel();
49 });
50 it('setField only modifies given', function() {
51 var r = Util.setField(pojo, 'a', 100);
52 expect(pojo).toEqual({a: 100, b: 2, key: 'TheKey'});
53 expect(r).toBe(100);
54 });
55 it('setField adds new', function() {
56 var r = Util.setField(pojo, 'c', 42);
57 expect(pojo).toEqual({a: 1, b: 2, c: 42, key: 'TheKey'});
58 expect(r).toBe(42);
59 });
60 it('getField returns value', function() {
61 var r = Util.getField(pojo, 'b');
62 expect(pojo).toEqual({a: 1, b: 2, key: 'TheKey'});
63 expect(r).toBe(2);
64 });
65 it('getField returns undefined', function() {
66 var r = Util.getField(pojo, 'c');
67 expect(pojo).toEqual({a: 1, b: 2, key: 'TheKey'});
68 expect(r).toBe(undefined);
69 });
70 it('itemKeyAccessor returns value', function() {
71 var r = Util.getItemKeyAccessor(bb);
72 expect(r).toBe('TheKey');
73 });
74 it('itemKeyAccessor returns undefined', function() {
75 var r = Util.getItemKeyAccessor(emptyBB);
76 expect(r).toBe(undefined);
77 });
78 it('mergeStaticProps', function() {
79 var r = Util.mergeStaticProps(SubModel, Backbone.Model, {'default': 1}, 'staticProps');
80 expect(r).toEqual({default: 1, hardCoded: 74, override: 76, newConstant: 77});
81 });
82 it('mergeStaticSets', function() {
83 var r = Util.mergeStaticSets(SubModel, Backbone.Model, ['default'], 'staticSets');
84 var keys = {};
85 _.each(r, function(value) {
86 keys[value] = true;
87 });
88 expect(keys).toEqual({default: true, hardCoded: true, override: true, newConstant: true});
89 });
90 });
91 });
92 });