-
Notifications
You must be signed in to change notification settings - Fork 0
/
safe-update-tests.js
84 lines (70 loc) · 2.43 KB
/
safe-update-tests.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
79
80
81
82
83
84
var Test = new Mongo.Collection('Test');
var currentCount = function () {
return (Test.findOne('counter') || {}).count;
};
var updateCounter = function (options) {
options = options || {};
var modifier = {},
setter = {count: options.inc ? 10 : currentCount() + 1},
updateOptions = _.pick(options, 'replace');
if (options.set) {
modifier.$set = setter;
} else if (options.inc) {
modifier.$inc = setter;
} else {
modifier = setter;
}
return Test.update('counter', modifier, updateOptions);
};
if (Meteor.isServer) {
if (!Test.findOne('counter')) {
Test.insert({_id: 'counter', count: 0, another: 'field'});
} else {
Test.update('counter', {$set: {another: 'field'}});
}
Meteor.methods({
updateCounter: updateCounter
});
}
var basicTests = function () {
Tinytest.add('throws if modifier doesn’t contain any $-operators', function (test) {
test.throws(function () {
updateCounter();
});
});
Tinytest.add('doesn’t throws with replace:true', function (test) {
test.equal(updateCounter({replace: true}), 1, 'one doc should be updated');
});
Tinytest.add('update works normally if modifier does contain some $-operators', function (test) {
var count = currentCount();
test.equal(updateCounter({set: true}), 1, 'one doc should be updated');
test.equal(count + 1, currentCount(), '+1 to counter');
test.equal(updateCounter({inc: true}), 1, 'one doc should be updated');
test.equal((count + 1) + 10, currentCount(), '+10 to counter');
});
};
if (Meteor.isServer) {
// server
basicTests();
}
if (Meteor.isClient) {
// client
Tracker.autorun(function (c) {
if (typeof currentCount() !== 'undefined') {
// subscription is ready
basicTests();
c.stop();
}
});
testAsyncMulti('update via methods calls behave identically', [
function (test, expect) {
Meteor.call('updateCounter', expect(function (error, result) {
test.instanceOf(error, Meteor.Error, 'error should be here');
}));
Meteor.call('updateCounter', {set: true}, expect(function (error, result) {
test.isUndefined(error, 'error should be undifined');
test.equal(result, 1, 'one doc should be updated');
}));
}
]);
}