UNPKG

@itwin/presentation-testing

Version:

Testing utilities for iTwin.js Presentation library

52 lines 2.09 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module IModel */ import { SnapshotDb } from "@itwin/core-backend"; import { IModelConnection } from "@itwin/core-frontend"; import { createFileNameFromString, setupOutputFileLocation } from "./FilenameUtils.js"; import { TestIModelBuilderImpl } from "./IModelBuilderImpl.js"; export async function buildTestIModel(nameParam, cb) { const name = typeof nameParam === "string" ? nameParam : createFileNameFromString(nameParam.test.fullTitle()); const outputFile = setupOutputFileLocation(`${name}.bim`); const db = SnapshotDb.createEmpty(outputFile, { rootSubject: { name } }); const builder = new TestIModelBuilderImpl(db); try { await cb(builder); } finally { db.saveChanges("Created test IModel"); db.close(); } return TestIModelConnection.openFile(outputFile); } /** * Implementation of `IModelConnection` that allows opening local files in tests. * @beta */ /* c8 ignore start */ export class TestIModelConnection extends IModelConnection { _db; // This was added based on this: https://github.com/iTwin/itwinjs-core/pull/7171/files#diff-9d26b04e7ae074b911fb87be3425360d7bd55a7c9f947f5aed1ba36d359f01eb constructor(_db) { // eslint-disable-next-line @itwin/no-internal super(_db.getConnectionProps()); this._db = _db; IModelConnection.onOpen.raiseEvent(this); } get isClosed() { // eslint-disable-next-line @itwin/no-internal return !this._db.isOpen; } async close() { this._db.close(); } static openFile(filePath) { return new TestIModelConnection(SnapshotDb.openFile(filePath)); } } /* c8 ignore end */ //# sourceMappingURL=IModelUtilities.js.map