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 {txnAsPromise} from "./utils";
import {QueryTargetWrapper, Store} from "./Store"; 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"; } get databaseName(): string { return "mockdb"; }
addWriteError(error: StorageError, refItem: ILogItem | undefined, operationName: string, keys: IDBKey[] | undefined) {} addWriteError(error: StorageError, refItem: ILogItem | undefined, operationName: string, keys: IDBKey[] | undefined) {}
} }
@ -285,10 +290,12 @@ export function tests() {
} }
async function createTestStore(): Promise<Store<TestEntry>> { 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) => { const db = await createMockDatabase("findExistingKeys", (db: IDBDatabase) => {
db.createObjectStore("test", {keyPath: "key"}); db.createObjectStore("test", {keyPath: "key"});
}, mockImpl); }, idbFactory);
const txn = db.transaction(["test"], "readwrite"); const txn = db.transaction(["test"], "readwrite");
return new Store<TestEntry>(txn.objectStore("test"), mockImpl); 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. 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 {StorageFactory} from "../matrix/storage/idb/StorageFactory";
import {IDOMStorage} from "../matrix/storage/idb/types"; import {IDOMStorage} from "../matrix/storage/idb/types";
import {Storage} from "../matrix/storage/idb/Storage"; import {Storage} from "../matrix/storage/idb/Storage";
import {Instance as nullLogger} from "../logging/NullLogger"; import {Instance as nullLogger} from "../logging/NullLogger";
import {openDatabase, CreateObjectStore} from "../matrix/storage/idb/utils"; import {openDatabase, CreateObjectStore} from "../matrix/storage/idb/utils";
export function createMockStorage(): Promise<Storage> { export async function createMockStorage(): Promise<Storage> {
return new StorageFactory(null as any, new FDBFactory(), FDBKeyRange, new MockLocalStorage()).create("1", nullLogger.item); 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> { // don't import fake-indexeddb until it's safe to assume we're actually in a unit test,
return openDatabase(name, createObjectStore, 1, impl.idbFactory); // 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 { // don't import fake-indexeddb until it's safe to assume we're actually in a unit test,
idbFactory: FDBFactory; // as this is a devDependency
export async function getMockIDBKeyRange(): Promise<typeof IDBKeyRange> {
constructor() { // @ts-ignore
this.idbFactory = new FDBFactory(); return (await import("fake-indexeddb/lib/FDBKeyRange.js")).default;
} }
get IDBKeyRange(): typeof IDBKeyRange { export function createMockDatabase(name: string, createObjectStore: CreateObjectStore, idbFactory: IDBFactory): Promise<IDBDatabase> {
return FDBKeyRange; return openDatabase(name, createObjectStore, 1, idbFactory);
}
} }
class MockLocalStorage implements IDOMStorage { class MockLocalStorage implements IDOMStorage {