|
| 1 | +import test from 'ava' |
| 2 | +import { newHocuspocus, newHocuspocusProvider } from '../utils/index.ts' |
| 3 | +import { retryableAssertion } from '../utils/retryableAssertion.ts' |
| 4 | + |
| 5 | +test('beforeSync gets called in proper order', async t => { |
| 6 | + await new Promise(async resolve => { |
| 7 | + const mockContext = { |
| 8 | + user: 123, |
| 9 | + } |
| 10 | + |
| 11 | + const expectedValuesByCallNumber = [ |
| 12 | + undefined, // beforeSync 1 |
| 13 | + 'bar', // beforeSync 2 |
| 14 | + 'bar', // beforeSync 3 |
| 15 | + ] |
| 16 | + let callNumber = 0 |
| 17 | + |
| 18 | + const server = await newHocuspocus({ |
| 19 | + async onConnect() { |
| 20 | + return mockContext |
| 21 | + }, |
| 22 | + async beforeSync({ document, context, payload }) { |
| 23 | + t.deepEqual(context, mockContext) |
| 24 | + |
| 25 | + const value = document.getArray('foo').get(0) |
| 26 | + |
| 27 | + t.is(value, expectedValuesByCallNumber[callNumber]) |
| 28 | + callNumber += 1 |
| 29 | + |
| 30 | + if (callNumber === expectedValuesByCallNumber.length - 1) { |
| 31 | + resolve('done') |
| 32 | + } |
| 33 | + }, |
| 34 | + async onChange({ context, document }) { |
| 35 | + t.deepEqual(context, mockContext) |
| 36 | + |
| 37 | + const value = document.getArray('foo').get(0) |
| 38 | + |
| 39 | + t.is(value, expectedValuesByCallNumber[2]) |
| 40 | + }, |
| 41 | + }) |
| 42 | + |
| 43 | + const provider = newHocuspocusProvider(server, { |
| 44 | + onSynced() { |
| 45 | + provider.document.getArray('foo').insert(0, ['bar']) |
| 46 | + }, |
| 47 | + }) |
| 48 | + }) |
| 49 | +}) |
| 50 | + |
| 51 | +test('beforeSync callback is called for every sync', async t => { |
| 52 | + let onConnectCount = 0 |
| 53 | + let updateCount = 0 |
| 54 | + let syncstep1Count = 0 |
| 55 | + let syncstep2Count = 0 |
| 56 | + |
| 57 | + await new Promise(async resolve => { |
| 58 | + const server = await newHocuspocus({ |
| 59 | + async onConnect() { |
| 60 | + onConnectCount += 1 |
| 61 | + }, |
| 62 | + async beforeSync({ type }) { |
| 63 | + if (type === 0){ |
| 64 | + syncstep1Count += 1 |
| 65 | + } else if (type === 1) { |
| 66 | + syncstep2Count += 1 |
| 67 | + } else if (type === 2) { |
| 68 | + updateCount += 1 |
| 69 | + } |
| 70 | + }, |
| 71 | + }) |
| 72 | + |
| 73 | + await Promise.all([ |
| 74 | + new Promise(done => { |
| 75 | + newHocuspocusProvider(server, { |
| 76 | + onClose() { |
| 77 | + t.fail() |
| 78 | + }, |
| 79 | + onSynced() { |
| 80 | + done('done') |
| 81 | + }, |
| 82 | + }) |
| 83 | + }), |
| 84 | + new Promise(done => { |
| 85 | + newHocuspocusProvider(server, { |
| 86 | + onClose() { |
| 87 | + t.fail() |
| 88 | + }, |
| 89 | + onSynced() { |
| 90 | + done('done') |
| 91 | + }, |
| 92 | + }) |
| 93 | + }), |
| 94 | + ]) |
| 95 | + |
| 96 | + resolve('done') |
| 97 | + }) |
| 98 | + |
| 99 | + await retryableAssertion(t, tt => { |
| 100 | + tt.is(onConnectCount, 2) |
| 101 | + tt.is(syncstep1Count, 2) |
| 102 | + tt.is(syncstep2Count, 2) |
| 103 | + tt.is(updateCount, 0) |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | + |
| 108 | +test('beforeSync callback is called on every update', async t => { |
| 109 | + let onConnectCount = 0 |
| 110 | + let updateCount = 0 |
| 111 | + let syncstep1Count = 0 |
| 112 | + let syncstep2Count = 0 |
| 113 | + |
| 114 | + |
| 115 | + await new Promise(async resolve => { |
| 116 | + const server = await newHocuspocus({ |
| 117 | + async onConnect() { |
| 118 | + onConnectCount += 1 |
| 119 | + }, |
| 120 | + async beforeSync({ type }) { |
| 121 | + if (type === 0){ |
| 122 | + syncstep1Count += 1 |
| 123 | + } else if (type === 1) { |
| 124 | + syncstep2Count += 1 |
| 125 | + } else if (type === 2) { |
| 126 | + updateCount += 1 |
| 127 | + } |
| 128 | + }, |
| 129 | + }) |
| 130 | + |
| 131 | + await Promise.all([ |
| 132 | + new Promise(done => { |
| 133 | + newHocuspocusProvider(server, { |
| 134 | + onClose() { |
| 135 | + t.fail() |
| 136 | + }, |
| 137 | + onSynced() { |
| 138 | + done('done') |
| 139 | + }, |
| 140 | + }) |
| 141 | + }), |
| 142 | + new Promise(done => { |
| 143 | + const provider = newHocuspocusProvider(server, { |
| 144 | + onClose() { |
| 145 | + t.fail() |
| 146 | + }, |
| 147 | + onSynced() { |
| 148 | + provider.document.getArray('foo').insert(0, ['bar']) |
| 149 | + done('done') |
| 150 | + }, |
| 151 | + }) |
| 152 | + }), |
| 153 | + ]) |
| 154 | + |
| 155 | + resolve('done') |
| 156 | + }) |
| 157 | + |
| 158 | + await retryableAssertion(t, tt => { |
| 159 | + tt.is(onConnectCount, 2) |
| 160 | + tt.is(syncstep1Count, 2) |
| 161 | + tt.is(syncstep2Count, 2) |
| 162 | + tt.is(updateCount, 1) |
| 163 | + }) |
| 164 | +}) |
0 commit comments