|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const assert = require('assert'); |
| 4 | +const clear = require('./clear'); |
| 5 | +const Parse = require('../../node'); |
| 6 | +const sleep = require('./sleep'); |
| 7 | + |
| 8 | +const TestObject = Parse.Object.extend('TestObject'); |
| 9 | + |
| 10 | +describe('Parse EventuallyQueue', () => { |
| 11 | + beforeEach(done => { |
| 12 | + Parse.initialize('integration', null, 'notsosecret'); |
| 13 | + Parse.CoreManager.set('SERVER_URL', 'http://localhost:1337/parse'); |
| 14 | + Parse.Storage._clear(); |
| 15 | + clear().then(() => { |
| 16 | + done(); |
| 17 | + }); |
| 18 | + }); |
| 19 | + |
| 20 | + it('can queue save object', async () => { |
| 21 | + const object = new TestObject({ test: 'test' }); |
| 22 | + await object.save(); |
| 23 | + object.set('foo', 'bar'); |
| 24 | + await Parse.EventuallyQueue.save(object); |
| 25 | + await Parse.EventuallyQueue.sendQueue(); |
| 26 | + |
| 27 | + const query = new Parse.Query(TestObject); |
| 28 | + const result = await query.get(object.id); |
| 29 | + assert.strictEqual(result.get('foo'), 'bar'); |
| 30 | + |
| 31 | + const length = await Parse.EventuallyQueue.length(); |
| 32 | + assert.strictEqual(length, 0); |
| 33 | + }); |
| 34 | + |
| 35 | + it('can queue destroy object', async () => { |
| 36 | + const object = new TestObject({ test: 'test' }); |
| 37 | + await object.save(); |
| 38 | + await Parse.EventuallyQueue.destroy(object); |
| 39 | + await Parse.EventuallyQueue.sendQueue(); |
| 40 | + |
| 41 | + const query = new Parse.Query(TestObject); |
| 42 | + query.equalTo('objectId', object.id); |
| 43 | + const results = await query.find(); |
| 44 | + assert.strictEqual(results.length, 0); |
| 45 | + |
| 46 | + const length = await Parse.EventuallyQueue.length(); |
| 47 | + assert.strictEqual(length, 0); |
| 48 | + }); |
| 49 | + |
| 50 | + it('can queue multiple object', async () => { |
| 51 | + const obj1 = new TestObject({ foo: 'bar' }); |
| 52 | + const obj2 = new TestObject({ foo: 'baz' }); |
| 53 | + const obj3 = new TestObject({ foo: 'bag' }); |
| 54 | + await Parse.EventuallyQueue.save(obj1); |
| 55 | + await Parse.EventuallyQueue.save(obj2); |
| 56 | + await Parse.EventuallyQueue.save(obj3); |
| 57 | + |
| 58 | + let length = await Parse.EventuallyQueue.length(); |
| 59 | + assert.strictEqual(length, 3); |
| 60 | + |
| 61 | + await Parse.EventuallyQueue.sendQueue(); |
| 62 | + |
| 63 | + const query = new Parse.Query(TestObject); |
| 64 | + query.ascending('createdAt'); |
| 65 | + const results = await query.find(); |
| 66 | + assert.strictEqual(results.length, 3); |
| 67 | + assert.strictEqual(results[0].get('foo'), 'bar'); |
| 68 | + assert.strictEqual(results[1].get('foo'), 'baz'); |
| 69 | + assert.strictEqual(results[2].get('foo'), 'bag'); |
| 70 | + |
| 71 | + length = await Parse.EventuallyQueue.length(); |
| 72 | + assert.strictEqual(length, 0); |
| 73 | + |
| 74 | + // TODO: can't use obj1, etc because they don't have an id |
| 75 | + await Parse.EventuallyQueue.destroy(results[0]); |
| 76 | + await Parse.EventuallyQueue.destroy(results[1]); |
| 77 | + await Parse.EventuallyQueue.destroy(results[2]); |
| 78 | + |
| 79 | + length = await Parse.EventuallyQueue.length(); |
| 80 | + assert.strictEqual(length, 3); |
| 81 | + |
| 82 | + await Parse.EventuallyQueue.sendQueue(); |
| 83 | + const objects = await query.find(); |
| 84 | + assert.strictEqual(objects.length, 0); |
| 85 | + }); |
| 86 | + |
| 87 | + it('can queue destroy for object that does not exist', async () => { |
| 88 | + const object = new TestObject({ test: 'test' }); |
| 89 | + await object.save(); |
| 90 | + await object.destroy(); |
| 91 | + await Parse.EventuallyQueue.destroy(object); |
| 92 | + await Parse.EventuallyQueue.sendQueue(); |
| 93 | + |
| 94 | + const length = await Parse.EventuallyQueue.length(); |
| 95 | + assert.strictEqual(length, 0); |
| 96 | + }); |
| 97 | + |
| 98 | + it('can queue destroy then save', async () => { |
| 99 | + const object = new TestObject({ hash: 'test' }); |
| 100 | + await Parse.EventuallyQueue.destroy(object); |
| 101 | + await Parse.EventuallyQueue.save(object); |
| 102 | + await Parse.EventuallyQueue.sendQueue(); |
| 103 | + |
| 104 | + const query = new Parse.Query(TestObject); |
| 105 | + query.equalTo('hash', 'test'); |
| 106 | + const results = await query.find(); |
| 107 | + assert.strictEqual(results.length, 1); |
| 108 | + |
| 109 | + const length = await Parse.EventuallyQueue.length(); |
| 110 | + assert.strictEqual(length, 0); |
| 111 | + }); |
| 112 | + |
| 113 | + it('can queue unsaved object with hash', async () => { |
| 114 | + const hash = 'secret'; |
| 115 | + const object = new TestObject({ test: 'test' }); |
| 116 | + object.set('hash', hash); |
| 117 | + await Parse.EventuallyQueue.save(object); |
| 118 | + await Parse.EventuallyQueue.sendQueue(); |
| 119 | + |
| 120 | + const query = new Parse.Query(TestObject); |
| 121 | + query.equalTo('hash', hash); |
| 122 | + const results = await query.find(); |
| 123 | + assert.strictEqual(results.length, 1); |
| 124 | + }); |
| 125 | + |
| 126 | + it('can queue saved object and unsaved with hash', async () => { |
| 127 | + const hash = 'ransom+salt'; |
| 128 | + const object = new TestObject({ test: 'test' }); |
| 129 | + object.set('hash', hash); |
| 130 | + await Parse.EventuallyQueue.save(object); |
| 131 | + await Parse.EventuallyQueue.sendQueue(); |
| 132 | + |
| 133 | + let query = new Parse.Query(TestObject); |
| 134 | + query.equalTo('hash', hash); |
| 135 | + const results = await query.find(); |
| 136 | + assert.strictEqual(results.length, 1); |
| 137 | + |
| 138 | + const unsaved = new TestObject({ hash, foo: 'bar' }); |
| 139 | + await Parse.EventuallyQueue.save(unsaved); |
| 140 | + await Parse.EventuallyQueue.sendQueue(); |
| 141 | + |
| 142 | + query = new Parse.Query(TestObject); |
| 143 | + query.equalTo('hash', hash); |
| 144 | + const hashes = await query.find(); |
| 145 | + assert.strictEqual(hashes.length, 1); |
| 146 | + assert.strictEqual(hashes[0].get('foo'), 'bar'); |
| 147 | + }); |
| 148 | + |
| 149 | + it('can poll server', async () => { |
| 150 | + const object = new TestObject({ test: 'test' }); |
| 151 | + await object.save(); |
| 152 | + object.set('foo', 'bar'); |
| 153 | + await Parse.EventuallyQueue.save(object); |
| 154 | + Parse.EventuallyQueue.poll(); |
| 155 | + assert.ok(Parse.EventuallyQueue.polling); |
| 156 | + |
| 157 | + await sleep(4000); |
| 158 | + const query = new Parse.Query(TestObject); |
| 159 | + const result = await query.get(object.id); |
| 160 | + assert.strictEqual(result.get('foo'), 'bar'); |
| 161 | + |
| 162 | + const length = await Parse.EventuallyQueue.length(); |
| 163 | + assert.strictEqual(length, 0); |
| 164 | + assert.strictEqual(Parse.EventuallyQueue.polling, undefined); |
| 165 | + }); |
| 166 | + |
| 167 | + it('can clear queue', async () => { |
| 168 | + const object = new TestObject({ test: 'test' }); |
| 169 | + await object.save(); |
| 170 | + await Parse.EventuallyQueue.save(object); |
| 171 | + const q = await Parse.EventuallyQueue.getQueue(); |
| 172 | + assert.strictEqual(q.length, 1); |
| 173 | + |
| 174 | + await Parse.EventuallyQueue.clear(); |
| 175 | + const length = await Parse.EventuallyQueue.length(); |
| 176 | + assert.strictEqual(length, 0); |
| 177 | + }); |
| 178 | +}); |
0 commit comments