Updating context test, some minor adjustments based on getting the test working again.

This commit is contained in:
mike12345567 2022-11-14 18:29:15 +00:00
parent c6d31a856a
commit 380ee221b1
4 changed files with 17 additions and 29 deletions

View File

@ -5,6 +5,7 @@ import {
getDevelopmentAppID,
getProdAppID,
baseGlobalDBName,
getDB,
PouchLike,
} from "../db"
import { Context } from "./localStorage"
@ -185,7 +186,10 @@ export function updateAppId(appId: string) {
export function getGlobalDB(): PouchLike {
const context = Context.get()
return new PouchLike(baseGlobalDBName(context?.tenantId))
if (!context || (env.MULTI_TENANCY && !context.tenantId)) {
throw new Error("Global DB not found")
}
return getDB(baseGlobalDBName(context?.tenantId))
}
/**
@ -194,7 +198,7 @@ export function getGlobalDB(): PouchLike {
*/
export function getAppDB(opts?: any): PouchLike {
const appId = getAppId()
return new PouchLike(appId, opts)
return getDB(appId, opts)
}
/**
@ -206,7 +210,7 @@ export function getProdAppDB(opts?: any): PouchLike {
if (!appId) {
throw new Error("Unable to retrieve prod DB - no app ID.")
}
return new PouchLike(getProdAppID(appId), opts)
return getDB(getProdAppID(appId), opts)
}
/**
@ -218,5 +222,5 @@ export function getDevAppDB(opts?: any): PouchLike {
if (!appId) {
throw new Error("Unable to retrieve dev DB - no app ID.")
}
return new PouchLike(getDevelopmentAppID(appId), opts)
return getDB(getDevelopmentAppID(appId), opts)
}

View File

@ -1,18 +1,10 @@
import "../../../tests/utilities/TestConfiguration"
import * as context from ".."
import { DEFAULT_TENANT_ID } from "../../constants"
import env from "../../environment"
// must use require to spy index file exports due to known issue in jest
const dbUtils = require("../../db")
jest.spyOn(dbUtils, "closePouchDB")
jest.spyOn(dbUtils, "getDB")
require("../../../tests/utilities/TestConfiguration")
const context = require("../")
const { DEFAULT_TENANT_ID } = require("../../constants")
const env = require("../../environment")
const dbCore = require("../../db")
describe("context", () => {
beforeEach(() => {
jest.clearAllMocks()
})
describe("doInTenant", () => {
describe("single-tenancy", () => {
it("defaults to the default tenant", () => {
@ -25,8 +17,6 @@ describe("context", () => {
const db = context.getGlobalDB()
expect(db.name).toBe("global-db")
})
expect(dbUtils.getDB).toHaveBeenCalledTimes(1)
expect(dbUtils.closePouchDB).toHaveBeenCalledTimes(1)
})
})
@ -40,7 +30,7 @@ describe("context", () => {
let error
try {
context.getTenantId()
} catch (e: any) {
} catch (e) {
error = e
}
expect(error.message).toBe("Tenant id not found")
@ -59,7 +49,7 @@ describe("context", () => {
let error
try {
context.getGlobalDB()
} catch (e: any) {
} catch (e) {
error = e
}
expect(error.message).toBe("Global DB not found")
@ -85,8 +75,6 @@ describe("context", () => {
const db = context.getGlobalDB()
expect(db.name).toBe("test_global-db")
})
expect(dbUtils.getDB).toHaveBeenCalledTimes(1)
expect(dbUtils.closePouchDB).toHaveBeenCalledTimes(1)
})
it("sets the tenant id when nested with same tenant id", async () => {
@ -121,10 +109,6 @@ describe("context", () => {
})
})
})
// only 1 db is opened and closed
expect(dbUtils.getDB).toHaveBeenCalledTimes(1)
expect(dbUtils.closePouchDB).toHaveBeenCalledTimes(1)
})
it("sets different tenant id inside another context", () => {

View File

@ -1,4 +1,4 @@
export * from "./connections"
export * from "./pouchLike"
export * from "./utils"
export { init, getPouch, getPouchDB } from "./pouchDB"
export { init, getPouch, getPouchDB, closePouchDB } from "./pouchDB"

View File

@ -11,7 +11,7 @@ const checkInitialised = () => {
}
}
export function getDB(dbName: string, opts?: any): PouchLike {
export function getDB(dbName?: string, opts?: any): PouchLike {
if (env.isTest()) {
dbList.add(dbName)
}