Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/browser/indexedDB.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import assert from 'assert';
6
import { IndexedDB } from '../../browser/indexedDB.js';
7
import { flakySuite } from '../common/testUtils.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from '../common/utils.js';
9
10
flakySuite('IndexedDB', () => {
11
12
let indexedDB: IndexedDB;
13
14
setup(async () => {
15
indexedDB = await IndexedDB.create('vscode-indexeddb-test', 1, ['test-store']);
16
await indexedDB.runInTransaction('test-store', 'readwrite', store => store.clear());
17
});
18
19
teardown(() => {
20
indexedDB?.close();
21
});
22
23
test('runInTransaction', async () => {
24
await indexedDB.runInTransaction('test-store', 'readwrite', store => store.add('hello1', 'key1'));
25
const value = await indexedDB.runInTransaction('test-store', 'readonly', store => store.get('key1'));
26
assert.deepStrictEqual(value, 'hello1');
27
});
28
29
test('getKeyValues', async () => {
30
await indexedDB.runInTransaction('test-store', 'readwrite', store => {
31
const requests: IDBRequest[] = [];
32
requests.push(store.add('hello1', 'key1'));
33
requests.push(store.add('hello2', 'key2'));
34
requests.push(store.add(true, 'key3'));
35
36
return requests;
37
});
38
function isValid(value: unknown): value is string {
39
return typeof value === 'string';
40
}
41
const keyValues = await indexedDB.getKeyValues('test-store', isValid);
42
assert.strictEqual(keyValues.size, 2);
43
assert.strictEqual(keyValues.get('key1'), 'hello1');
44
assert.strictEqual(keyValues.get('key2'), 'hello2');
45
});
46
47
test('hasPendingTransactions', async () => {
48
const promise = indexedDB.runInTransaction('test-store', 'readwrite', store => store.add('hello2', 'key2'));
49
assert.deepStrictEqual(indexedDB.hasPendingTransactions(), true);
50
await promise;
51
assert.deepStrictEqual(indexedDB.hasPendingTransactions(), false);
52
});
53
54
test('close', async () => {
55
const promise = indexedDB.runInTransaction('test-store', 'readwrite', store => store.add('hello3', 'key3'));
56
indexedDB.close();
57
assert.deepStrictEqual(indexedDB.hasPendingTransactions(), false);
58
try {
59
await promise;
60
assert.fail('Transaction should be aborted');
61
} catch (error) { }
62
});
63
64
ensureNoDisposablesAreLeakedInTestSuite();
65
});
66
67