diff --git a/models/inventory/tests/helpers.ts b/models/inventory/tests/helpers.ts new file mode 100644 index 00000000..39d09489 --- /dev/null +++ b/models/inventory/tests/helpers.ts @@ -0,0 +1,21 @@ +import { Fyo } from 'fyo'; +import { ModelNameEnum } from 'models/types'; +import test from 'tape'; + +export const dummyItems = [ + { + name: 'Ball Pen', + rate: 50, + for: 'Both', + trackItem: true, + }, + { + name: 'Ink Pen', + rate: 700, + for: 'Both', + trackItem: true, + }, +]; + +export function createDummyItems(fyo: Fyo) { +} diff --git a/models/inventory/tests/testInventory.spec.ts b/models/inventory/tests/testInventory.spec.ts new file mode 100644 index 00000000..cd7f1f2b --- /dev/null +++ b/models/inventory/tests/testInventory.spec.ts @@ -0,0 +1,24 @@ +import { ModelNameEnum } from 'models/types'; +import test from 'tape'; +import { closeTestFyo, getTestFyo, setupTestFyo } from 'tests/helpers'; +import { dummyItems } from './helpers'; + +const fyo = getTestFyo(); + +setupTestFyo(fyo, __filename); + +test('create dummy items', async (t) => { + for (const item of dummyItems) { + const doc = fyo.doc.getNewDoc(ModelNameEnum.Item, item); + t.ok(await doc.sync(), `${item.name} created`); + } +}); + +test('check dummy items', async (t) => { + for (const { name } of dummyItems) { + const exists = await fyo.db.exists(ModelNameEnum.Item, name); + t.ok(exists, `${name} exists`); + } +}); + +closeTestFyo(fyo, __filename); diff --git a/src/App.vue b/src/App.vue index 4788737d..eb8a75e7 100644 --- a/src/App.vue +++ b/src/App.vue @@ -41,12 +41,13 @@ import { ModelNameEnum } from 'models/types'; import { computed } from 'vue'; import WindowsTitleBar from './components/WindowsTitleBar.vue'; import { handleErrorWithDialog } from './errorHandling'; -import { fyo, initializeInstance } from './initFyo'; +import { fyo } from './initFyo'; import DatabaseSelector from './pages/DatabaseSelector.vue'; import Desk from './pages/Desk.vue'; import SetupWizard from './pages/SetupWizard/SetupWizard.vue'; import setupInstance from './setup/setupInstance'; import './styles/index.css'; +import { initializeInstance } from './utils/initialization'; import { checkForUpdates } from './utils/ipcCalls'; import { updateConfigFiles } from './utils/misc'; import { Search } from './utils/search'; diff --git a/tests/helpers.ts b/tests/helpers.ts index 951cc0e9..9959cbfd 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -2,7 +2,10 @@ import { DatabaseManager } from 'backend/database/manager'; import { config } from 'dotenv'; import { Fyo } from 'fyo'; import { DummyAuthDemux } from 'fyo/tests/helpers'; +import path from 'path'; +import setupInstance from 'src/setup/setupInstance'; import { SetupWizardOptions } from 'src/setup/types'; +import test from 'tape'; import { getFiscalYear } from 'utils/misc'; export function getTestSetupWizardOptions(): SetupWizardOptions { @@ -25,6 +28,21 @@ export function getTestDbPath(dbPath?: string) { return dbPath ?? process.env.TEST_DB_PATH ?? ':memory:'; } +/** + * Test Boilerplate + * + * The bottom three functions are test boilerplate for when + * an initialized fyo object is to be used. + * + * They are required because top level await is not supported. + * + * Therefore setup and cleanup of the fyo object is wrapped + * in tests which are executed serially (and awaited in order) + * by tape. + * + * If `closeTestFyo` is not called the test process won't exit. + */ + export function getTestFyo(): Fyo { return new Fyo({ DatabaseDemux: DatabaseManager, @@ -33,3 +51,23 @@ export function getTestFyo(): Fyo { isElectron: false, }); } + +const ext = '.spec.ts'; + +export function setupTestFyo(fyo: Fyo, filename: string) { + const testName = path.basename(filename, ext); + + return test(`setup: ${testName}`, async () => { + const options = getTestSetupWizardOptions(); + const dbPath = getTestDbPath(); + await setupInstance(dbPath, options, fyo); + }); +} + +export function closeTestFyo(fyo: Fyo, filename: string) { + const testName = path.basename(filename, ext); + + return test(`cleanup: ${testName}`, async () => { + await fyo.close(); + }); +}