Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: resetting application deletes all dbs #2090

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
});
}
}