Path: blob/main/src/vs/base/test/browser/indexedDB.test.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/4import assert from 'assert';5import { IndexedDB } from '../../browser/indexedDB.js';6import { flakySuite } from '../common/testUtils.js';7import { ensureNoDisposablesAreLeakedInTestSuite } from '../common/utils.js';89flakySuite('IndexedDB', () => {1011let indexedDB: IndexedDB;1213setup(async () => {14indexedDB = await IndexedDB.create('vscode-indexeddb-test', 1, ['test-store']);15await indexedDB.runInTransaction('test-store', 'readwrite', store => store.clear());16});1718teardown(() => {19indexedDB?.close();20});2122test('runInTransaction', async () => {23await indexedDB.runInTransaction('test-store', 'readwrite', store => store.add('hello1', 'key1'));24const value = await indexedDB.runInTransaction('test-store', 'readonly', store => store.get('key1'));25assert.deepStrictEqual(value, 'hello1');26});2728test('getKeyValues', async () => {29await indexedDB.runInTransaction('test-store', 'readwrite', store => {30const requests: IDBRequest[] = [];31requests.push(store.add('hello1', 'key1'));32requests.push(store.add('hello2', 'key2'));33requests.push(store.add(true, 'key3'));3435return requests;36});37function isValid(value: unknown): value is string {38return typeof value === 'string';39}40const keyValues = await indexedDB.getKeyValues('test-store', isValid);41assert.strictEqual(keyValues.size, 2);42assert.strictEqual(keyValues.get('key1'), 'hello1');43assert.strictEqual(keyValues.get('key2'), 'hello2');44});4546test('hasPendingTransactions', async () => {47const promise = indexedDB.runInTransaction('test-store', 'readwrite', store => store.add('hello2', 'key2'));48assert.deepStrictEqual(indexedDB.hasPendingTransactions(), true);49await promise;50assert.deepStrictEqual(indexedDB.hasPendingTransactions(), false);51});5253test('close', async () => {54const promise = indexedDB.runInTransaction('test-store', 'readwrite', store => store.add('hello3', 'key3'));55indexedDB.close();56assert.deepStrictEqual(indexedDB.hasPendingTransactions(), false);57try {58await promise;59assert.fail('Transaction should be aborted');60} catch (error) { }61});6263ensureNoDisposablesAreLeakedInTestSuite();64});656667