Skip to content

Commit

Permalink
fix: resetting application deletes all dbs and works without login (#…
Browse files Browse the repository at this point in the history
…2090)

* reset from support component deletes all dbs
* prevent error when viewing support component while not logged in

---------

Co-authored-by: Sebastian Leidig <sebastian@aam-digital.com>
  • Loading branch information
TheSlimvReal and sleidig authored Nov 21, 2023
1 parent 7b0d434 commit bda2d9e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/app/core/support/support/support.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SupportComponent>;
Expand All @@ -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;

Expand Down Expand Up @@ -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(() => {
Expand Down
17 changes: 16 additions & 1 deletion src/app/core/support/support/support.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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());
Expand All @@ -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;
});
}
}

0 comments on commit bda2d9e

Please sign in to comment.