-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.js
78 lines (61 loc) · 2.05 KB
/
index.test.js
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
var mongoose = require('mongoose');
var transformProps = require('.');
var SubDocSchema = new mongoose.Schema({
foo: String
});
var ModelSchema = new mongoose.Schema({
subDoc: SubDocSchema
});
var Model = mongoose.model('Model', ModelSchema);
describe('transformProps', function() {
var castToString = function(prop) {
return String(prop);
};
var doc;
beforeEach(function() {
doc = new Model({
subDoc: {foo: 'bar'}
}).toObject();
});
it('throws a TypeError when passed a bad @object', function() {
var badArgsSet = new Set([
['notAnObject', castToString, '_id'],
[doc, 'notAFunction', '_id'],
[doc, castToString, function() {
return 'bad @propKeys';
}]
]);
badArgsSet.forEach(function(badArgs) {
// "Note: You must wrap the code in a function, otherwise the error will not be caught and the assertion will fail."
// https://facebook.github.io/jest/docs/en/expect.html#tothrowerror
var act = function() {
transformProps.apply(null, badArgs);
};
expect(act).toThrow(TypeError);
});
});
it('can convert `_id`s to string', function() {
['_id', ['_id']].forEach(function(propKeysArg) { // Test that @propKeys of type str and single-item array are ok.
transformProps(doc, castToString, propKeysArg);
expect(typeof doc._id).toBe('string');
expect(typeof doc.subDoc._id).toBe('string');
});
});
it('can use multiple transformers', function() {
var appendHello = function(str) {
return str + 'Hello!';
};
transformProps(doc, [castToString, appendHello], '_id');
expect(doc._id.substr(-6)).toBe('Hello!');
expect(doc.subDoc._id.substr(-6)).toBe('Hello!');
});
it('can transform multiple props', function() {
var appendHello = function(str) {
return str + 'Hello!';
};
transformProps(doc, [castToString, appendHello], ['_id', 'foo']);
expect(doc._id.substr(-6)).toBe('Hello!');
expect(doc.subDoc._id.substr(-6)).toBe('Hello!');
expect(doc.subDoc.foo).toBe('barHello!');
});
});