|
| 1 | +// These tests are the same as those in minimongo_tests.js, just that after every batch of database modify queries |
| 2 | +// we sleep a bit to allow for changes to propagate back to Meteor and run any observes and invalidate any autoruns. |
| 3 | + |
| 4 | +Tinytest.add("reactive-mongo - reactive stop", function (test) { |
| 5 | + var coll = new LocalCollection(); |
| 6 | + coll.insert({_id: 'A'}); |
| 7 | + coll.insert({_id: 'B'}); |
| 8 | + coll.insert({_id: 'C'}); |
| 9 | + Meteor._sleepForMs(100); |
| 10 | + |
| 11 | + var addBefore = function (str, newChar, before) { |
| 12 | + var idx = str.indexOf(before); |
| 13 | + if (idx === -1) |
| 14 | + return str + newChar; |
| 15 | + return str.slice(0, idx) + newChar + str.slice(idx); |
| 16 | + }; |
| 17 | + |
| 18 | + var x, y; |
| 19 | + var sortOrder = ReactiveVar(1); |
| 20 | + |
| 21 | + var c = Tracker.autorun(function () { |
| 22 | + var q = coll.find({}, {sort: {_id: sortOrder.get()}}); |
| 23 | + x = ""; |
| 24 | + q.observe({ addedAt: function (doc, atIndex, before) { |
| 25 | + x = addBefore(x, doc._id, before); |
| 26 | + }}); |
| 27 | + y = ""; |
| 28 | + q.observeChanges({ addedBefore: function (id, fields, before) { |
| 29 | + y = addBefore(y, id, before); |
| 30 | + }}); |
| 31 | + }); |
| 32 | + |
| 33 | + test.equal(x, "ABC"); |
| 34 | + test.equal(y, "ABC"); |
| 35 | + |
| 36 | + sortOrder.set(-1); |
| 37 | + test.equal(x, "ABC"); |
| 38 | + test.equal(y, "ABC"); |
| 39 | + Tracker.flush(); |
| 40 | + test.equal(x, "CBA"); |
| 41 | + test.equal(y, "CBA"); |
| 42 | + |
| 43 | + coll.insert({_id: 'D'}); |
| 44 | + coll.insert({_id: 'E'}); |
| 45 | + Meteor._sleepForMs(100); |
| 46 | + test.equal(x, "EDCBA"); |
| 47 | + test.equal(y, "EDCBA"); |
| 48 | + |
| 49 | + c.stop(); |
| 50 | + // stopping kills the observes immediately |
| 51 | + coll.insert({_id: 'F'}); |
| 52 | + Meteor._sleepForMs(100); |
| 53 | + test.equal(x, "EDCBA"); |
| 54 | + test.equal(y, "EDCBA"); |
| 55 | +}); |
| 56 | + |
| 57 | +Tinytest.add("reactive-mongo - fetch in observe", function (test) { |
| 58 | + var coll = new LocalCollection; |
| 59 | + var callbackInvoked = false; |
| 60 | + var observe = coll.find().observeChanges({ |
| 61 | + added: function (id, fields) { |
| 62 | + callbackInvoked = true; |
| 63 | + test.equal(fields, {foo: 1}); |
| 64 | + var doc = coll.findOne({foo: 1}); |
| 65 | + test.isTrue(doc); |
| 66 | + test.equal(doc.foo, 1); |
| 67 | + } |
| 68 | + }); |
| 69 | + test.isFalse(callbackInvoked); |
| 70 | + var computation = Tracker.autorun(function (computation) { |
| 71 | + if (computation.firstRun) { |
| 72 | + coll.insert({foo: 1}); |
| 73 | + Meteor._sleepForMs(100); |
| 74 | + } |
| 75 | + }); |
| 76 | + test.isTrue(callbackInvoked); |
| 77 | + observe.stop(); |
| 78 | + computation.stop(); |
| 79 | +}); |
| 80 | + |
| 81 | +Tinytest.add("reactive-mongo - count on cursor with limit", function(test){ |
| 82 | + var coll = new LocalCollection(), count; |
| 83 | + |
| 84 | + coll.insert({_id: 'A'}); |
| 85 | + coll.insert({_id: 'B'}); |
| 86 | + coll.insert({_id: 'C'}); |
| 87 | + coll.insert({_id: 'D'}); |
| 88 | + Meteor._sleepForMs(100); |
| 89 | + |
| 90 | + var c = Tracker.autorun(function (c) { |
| 91 | + var cursor = coll.find({_id: {$exists: true}}, {sort: {_id: 1}, limit: 3}); |
| 92 | + count = cursor.count(); |
| 93 | + }); |
| 94 | + |
| 95 | + test.equal(count, 3); |
| 96 | + |
| 97 | + coll.remove('A'); // still 3 in the collection |
| 98 | + Meteor._sleepForMs(100); |
| 99 | + Tracker.flush(); |
| 100 | + test.equal(count, 3); |
| 101 | + |
| 102 | + coll.remove('B'); // expect count now 2 |
| 103 | + Meteor._sleepForMs(100); |
| 104 | + Tracker.flush(); |
| 105 | + test.equal(count, 2); |
| 106 | + |
| 107 | + |
| 108 | + coll.insert({_id: 'A'}); // now 3 again |
| 109 | + Meteor._sleepForMs(100); |
| 110 | + Tracker.flush(); |
| 111 | + test.equal(count, 3); |
| 112 | + |
| 113 | + coll.insert({_id: 'B'}); // now 4 entries, but count should be 3 still |
| 114 | + Meteor._sleepForMs(100); |
| 115 | + Tracker.flush(); |
| 116 | + test.equal(count, 3); |
| 117 | + |
| 118 | + c.stop(); |
| 119 | +}); |
| 120 | + |
| 121 | +Tinytest.add("reactive-mongo - fine-grained reactivity of query with fields projection", function (test) { |
| 122 | + var X = new LocalCollection; |
| 123 | + var id = "asdf"; |
| 124 | + X.insert({_id: id, foo: {bar: 123}}); |
| 125 | + |
| 126 | + var callbackInvoked = false; |
| 127 | + var computation = Tracker.autorun(function () { |
| 128 | + callbackInvoked = true; |
| 129 | + return X.findOne(id, { fields: { 'foo.bar': 1 } }); |
| 130 | + }); |
| 131 | + test.isTrue(callbackInvoked); |
| 132 | + callbackInvoked = false; |
| 133 | + X.update(id, {$set: {'foo.baz': 456}}); |
| 134 | + Meteor._sleepForMs(100); |
| 135 | + test.isFalse(callbackInvoked); |
| 136 | + X.update(id, {$set: {'foo.bar': 124}}); |
| 137 | + Meteor._sleepForMs(100); |
| 138 | + Tracker.flush(); |
| 139 | + test.isTrue(callbackInvoked); |
| 140 | + |
| 141 | + computation.stop(); |
| 142 | +}); |
0 commit comments