Util.spec.js
3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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});
});
});
});
});