only import fake-indexeddb in tests

as it is a devDependency and can end up in the legacy bundle
under circumstances
This commit is contained in:
Bruno Windels 2021-12-01 17:25:07 +01:00
parent 7fda78ff2f
commit 1a618dd106
2 changed files with 30 additions and 22 deletions

View File

@ -269,13 +269,18 @@ export class QueryTarget<T> {
}
}
import {createMockDatabase, MockIDBImpl} from "../../../mocks/Storage";
import {createMockDatabase, createMockIDBFactory, getMockIDBKeyRange} from "../../../mocks/Storage";
import {txnAsPromise} from "./utils";
import {QueryTargetWrapper, Store} from "./Store";
export function tests() {
export async function tests() {
class MockTransaction extends MockIDBImpl {
class MockTransaction {
constructor(public readonly idbFactory: IDBFactory, readonly idbKeyRangeType: typeof IDBKeyRange) {}
get IDBKeyRange(): typeof IDBKeyRange {
return this.idbKeyRangeType;
}
get databaseName(): string { return "mockdb"; }
addWriteError(error: StorageError, refItem: ILogItem | undefined, operationName: string, keys: IDBKey[] | undefined) {}
}
@ -285,10 +290,12 @@ export function tests() {
}
async function createTestStore(): Promise<Store<TestEntry>> {
const mockImpl = new MockTransaction();
const idbFactory = await createMockIDBFactory();
const idbKeyRangeType = await getMockIDBKeyRange();
const mockImpl = new MockTransaction(idbFactory, idbKeyRangeType);
const db = await createMockDatabase("findExistingKeys", (db: IDBDatabase) => {
db.createObjectStore("test", {keyPath: "key"});
}, mockImpl);
}, idbFactory);
const txn = db.transaction(["test"], "readwrite");
return new Store<TestEntry>(txn.objectStore("test"), mockImpl);
}

View File

@ -14,34 +14,35 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
// @ts-ignore
import FDBFactory from "fake-indexeddb/lib/FDBFactory.js";
// @ts-ignore
import FDBKeyRange from "fake-indexeddb/lib/FDBKeyRange.js";
import {StorageFactory} from "../matrix/storage/idb/StorageFactory";
import {IDOMStorage} from "../matrix/storage/idb/types";
import {Storage} from "../matrix/storage/idb/Storage";
import {Instance as nullLogger} from "../logging/NullLogger";
import {openDatabase, CreateObjectStore} from "../matrix/storage/idb/utils";
export function createMockStorage(): Promise<Storage> {
return new StorageFactory(null as any, new FDBFactory(), FDBKeyRange, new MockLocalStorage()).create("1", nullLogger.item);
export async function createMockStorage(): Promise<Storage> {
const idbFactory = await createMockIDBFactory();
const FDBKeyRange = await getMockIDBKeyRange();
return new StorageFactory(null as any, idbFactory, FDBKeyRange, new MockLocalStorage()).create("1", nullLogger.item);
}
export function createMockDatabase(name: string, createObjectStore: CreateObjectStore, impl: MockIDBImpl): Promise<IDBDatabase> {
return openDatabase(name, createObjectStore, 1, impl.idbFactory);
// don't import fake-indexeddb until it's safe to assume we're actually in a unit test,
// as this is a devDependency
export async function createMockIDBFactory(): Promise<IDBFactory> {
// @ts-ignore
const FDBFactory = (await import("fake-indexeddb/lib/FDBFactory.js")).default;
return new FDBFactory();
}
export class MockIDBImpl {
idbFactory: FDBFactory;
// don't import fake-indexeddb until it's safe to assume we're actually in a unit test,
// as this is a devDependency
export async function getMockIDBKeyRange(): Promise<typeof IDBKeyRange> {
// @ts-ignore
return (await import("fake-indexeddb/lib/FDBKeyRange.js")).default;
}
constructor() {
this.idbFactory = new FDBFactory();
}
get IDBKeyRange(): typeof IDBKeyRange {
return FDBKeyRange;
}
export function createMockDatabase(name: string, createObjectStore: CreateObjectStore, idbFactory: IDBFactory): Promise<IDBDatabase> {
return openDatabase(name, createObjectStore, 1, idbFactory);
}
class MockLocalStorage implements IDOMStorage {