-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathuploads.test.ts
146 lines (120 loc) · 4.17 KB
/
uploads.test.ts
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { createBaseLogger, createLogger, WebPowerSyncDatabaseOptions } from '@powersync/web';
import p from 'p-defer';
import { v4 } from 'uuid';
import { beforeAll, describe, expect, it, vi } from 'vitest';
import { DEFAULT_CONNECTED_POWERSYNC_OPTIONS, generateConnectedDatabase } from './utils/generateConnectedDatabase';
// Don't want to actually export the warning string from the package
const PARTIAL_WARNING = 'Potentially previously uploaded CRUD entries are still present';
describe(
'CRUD Uploads - With Web Workers',
{ sequential: true },
describeCrudUploadTests(() => ({
database: {
dbFilename: `${v4()}.db`
},
schema: DEFAULT_CONNECTED_POWERSYNC_OPTIONS.powerSyncOptions.schema,
crudUploadThrottleMs: 1_000,
flags: {
enableMultiTabs: false
}
}))
);
describe(
'CRUD Uploads - Without Web Workers',
{ sequential: true },
describeCrudUploadTests(() => ({
database: {
dbFilename: `crud-uploads-test-${v4()}.db`
},
schema: DEFAULT_CONNECTED_POWERSYNC_OPTIONS.powerSyncOptions.schema,
crudUploadThrottleMs: 1_000,
flags: {
useWebWorker: false
}
}))
);
function describeCrudUploadTests(getDatabaseOptions: () => WebPowerSyncDatabaseOptions) {
return () => {
beforeAll(() => createBaseLogger().useDefaults());
it('should warn for missing upload operations in uploadData', async () => {
const logger = createLogger('crud-logger');
const options = getDatabaseOptions();
const { powersync, uploadSpy } = await generateConnectedDatabase({
powerSyncOptions: {
...options,
logger
}
});
const loggerSpy = vi.spyOn(logger, 'warn');
const deferred = p();
uploadSpy.mockImplementation(async (db) => {
// This upload method does not perform an upload
deferred.resolve();
});
// Create something with CRUD in it.
await powersync.execute('INSERT into users (id, name) VALUES (uuid(), ?)', ['steven']);
// The empty upload handler should have been called
// Timeouts seem to be weird in Vitest Browser mode.
// This makes the check below more stable.
await deferred.promise;
await vi.waitFor(
() => {
expect(loggerSpy.mock.calls.find((logArgs) => logArgs[0].includes(PARTIAL_WARNING))).exist;
},
{
timeout: 500,
interval: 100
}
);
});
it('should immediately upload sequential transactions', async () => {
const logger = createLogger('crud-logger');
const options = getDatabaseOptions();
const { powersync, uploadSpy } = await generateConnectedDatabase({
powerSyncOptions: {
...options,
logger
}
});
const loggerSpy = vi.spyOn(logger, 'warn');
const deferred = p();
uploadSpy.mockImplementation(async (db) => {
const nextTransaction = await db.getNextCrudTransaction();
if (!nextTransaction) {
return;
}
// Mockingly delete the crud op in order to progress through the CRUD queue
for (const op of nextTransaction.crud) {
await db.execute(`DELETE FROM ps_crud WHERE id = ?`, [op.clientId]);
}
deferred.resolve();
});
// Create the first item
await powersync.execute('INSERT into users (id, name) VALUES (uuid(), ?)', ['steven']);
// Modify the first item in a new transaction
await powersync.execute(`
UPDATE
users
SET
name = 'Mugi'
WHERE
name = 'steven'`);
// Create a second item
await powersync.execute('INSERT into users (id, name) VALUES (uuid(), ?)', ['steven2']);
// The empty upload handler should have been called.
// Timeouts seem to be weird in Vitest Browser mode.
// This makes the check below more stable.
await deferred.promise;
await vi.waitFor(
() => {
expect(uploadSpy.mock.calls.length).eq(3);
},
{
timeout: 5_000,
interval: 300
}
);
expect(loggerSpy.mock.calls.find((logArgs) => logArgs[0].includes(PARTIAL_WARNING))).toBeUndefined;
});
};
}