Util.spec.js
4.77 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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});
});
});
});
});