diff --git a/src/app/core/support/support/support.component.spec.ts b/src/app/core/support/support/support.component.spec.ts index c1e14b2221..c94c26f7cc 100644 --- a/src/app/core/support/support/support.component.spec.ts +++ b/src/app/core/support/support/support.component.spec.ts @@ -24,6 +24,13 @@ import { DownloadService } from "../../export/download-service/download.service" import { AuthService } from "../../session/auth/auth.service"; import { TEST_USER } from "../../../utils/mock-local-session"; +class MockDeleteRequest { + onsuccess: () => {}; + constructor() { + setTimeout(() => this.onsuccess()); + } +} + describe("SupportComponent", () => { let component: SupportComponent; let fixture: ComponentFixture; @@ -36,6 +43,12 @@ describe("SupportComponent", () => { userAgent: "mock user agent", serviceWorker: { getRegistrations: () => [], ready: Promise.resolve() }, }, + indexedDB: { + databases: jasmine.createSpy(), + deleteDatabase: jasmine + .createSpy() + .and.callFake(() => new MockDeleteRequest()), + }, }; let mockLocation: any; @@ -112,13 +125,18 @@ describe("SupportComponent", () => { mockWindow.navigator.serviceWorker.getRegistrations = () => [ { unregister: unregisterSpy }, ]; + mockWindow.indexedDB.databases.and.resolveTo([ + { name: "db1" }, + { name: "db2" }, + ]); await component.resetApplication(); - expect(mockDB.destroy).toHaveBeenCalled(); expect(unregisterSpy).toHaveBeenCalled(); expect(localStorage.getItem("someItem")).toBeNull(); expect(mockLocation.pathname).toBe(""); + expect(mockWindow.indexedDB.deleteDatabase).toHaveBeenCalledWith("db1"); + expect(mockWindow.indexedDB.deleteDatabase).toHaveBeenCalledWith("db2"); }); it("should display the service worker logs after they are available", fakeAsync(() => { diff --git a/src/app/core/support/support/support.component.ts b/src/app/core/support/support/support.component.ts index 8bb95d6f15..0f5743f03d 100644 --- a/src/app/core/support/support/support.component.ts +++ b/src/app/core/support/support/support.component.ts @@ -108,6 +108,11 @@ export class SupportComponent implements OnInit { } private initDbInfo() { + if (!this.database || !this.database.getPouchDB()) { + this.dbInfo = "db not initialized"; + return; + } + return this.database .getPouchDB() .info() @@ -154,7 +159,9 @@ export class SupportComponent implements OnInit { return; } - await this.database.destroy(); + const dbs = await this.window.indexedDB.databases(); + await Promise.all(dbs.map(({ name }) => this.destroyDatabase(name))); + const registrations = await this.window.navigator.serviceWorker.getRegistrations(); const unregisterPromises = registrations.map((reg) => reg.unregister()); @@ -171,4 +178,12 @@ export class SupportComponent implements OnInit { "aamdigital_data_" + new Date().toISOString(), ); } + + private destroyDatabase(name: string) { + return new Promise((resolve, reject) => { + const del = this.window.indexedDB.deleteDatabase(name); + del.onsuccess = resolve; + del.onerror = reject; + }); + } }