1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-17 16:53:34 +00:00

[PM-6658] Migrate disableFavicon to Domain Settings service and remove Settings service (#8333)

* add showFavicons to domain settings

* replace usages of disableFavicon with showFavicons via the domain settings service and remove/replace settings service

* create migration for disableFavicon

* cleanup
This commit is contained in:
Jonathan Prusik
2024-03-19 06:14:49 -04:00
committed by GitHub
parent b95dfd9d30
commit 13e1672c69
25 changed files with 237 additions and 199 deletions

View File

@@ -0,0 +1,108 @@
import { MockProxy } from "jest-mock-extended";
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
import { mockMigrationHelper } from "../migration-helper.spec";
import { EnableFaviconMigrator } from "./42-move-enable-favicon-to-domain-settings-state-provider";
function exampleJSON() {
return {
global: {
otherStuff: "otherStuff1",
disableFavicon: true,
},
authenticatedAccounts: ["user-1", "user-2"],
"user-1": {
settings: {
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
},
"user-2": {
settings: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
function rollbackJSON() {
return {
global_domainSettings_showFavicons: false,
global: {
otherStuff: "otherStuff1",
},
authenticatedAccounts: ["user-1", "user-2"],
"user-1": {
settings: {
otherStuff: "otherStuff2",
},
otherStuff: "otherStuff3",
},
"user-2": {
settings: {
otherStuff: "otherStuff4",
},
otherStuff: "otherStuff5",
},
};
}
const showFaviconsKeyDefinition: KeyDefinitionLike = {
stateDefinition: {
name: "domainSettings",
},
key: "showFavicons",
};
describe("EnableFaviconMigrator", () => {
let helper: MockProxy<MigrationHelper>;
let sut: EnableFaviconMigrator;
describe("migrate", () => {
beforeEach(() => {
helper = mockMigrationHelper(exampleJSON(), 41);
sut = new EnableFaviconMigrator(41, 42);
});
it("should remove global disableFavicon", async () => {
await sut.migrate(helper);
expect(helper.set).toHaveBeenCalledTimes(1);
expect(helper.set).toHaveBeenCalledWith("global", {
otherStuff: "otherStuff1",
});
});
it("should set global showFavicons", async () => {
await sut.migrate(helper);
expect(helper.setToGlobal).toHaveBeenCalledTimes(1);
expect(helper.setToGlobal).toHaveBeenCalledWith(showFaviconsKeyDefinition, false);
});
});
describe("rollback", () => {
beforeEach(() => {
helper = mockMigrationHelper(rollbackJSON(), 42);
sut = new EnableFaviconMigrator(41, 42);
});
it("should null global showFavicons", async () => {
await sut.rollback(helper);
expect(helper.setToGlobal).toHaveBeenCalledTimes(1);
expect(helper.setToGlobal).toHaveBeenCalledWith(showFaviconsKeyDefinition, null);
});
it("should add global disableFavicon back", async () => {
await sut.rollback(helper);
expect(helper.set).toHaveBeenCalledTimes(1);
expect(helper.set).toHaveBeenCalledWith("global", {
disableFavicon: true,
otherStuff: "otherStuff1",
});
});
});
});

View File

@@ -0,0 +1,45 @@
import { KeyDefinitionLike, MigrationHelper } from "../migration-helper";
import { Migrator } from "../migrator";
type ExpectedGlobalState = {
disableFavicon?: boolean;
};
const ShowFaviconDefinition: KeyDefinitionLike = {
stateDefinition: {
name: "domainSettings",
},
key: "showFavicons",
};
export class EnableFaviconMigrator extends Migrator<41, 42> {
async migrate(helper: MigrationHelper): Promise<void> {
// global state ("disableFavicon" -> "showFavicons")
const globalState = await helper.get<ExpectedGlobalState>("global");
if (globalState?.disableFavicon != null) {
await helper.setToGlobal(ShowFaviconDefinition, !globalState.disableFavicon);
// delete `disableFavicon` from state global
delete globalState.disableFavicon;
await helper.set<ExpectedGlobalState>("global", globalState);
}
}
async rollback(helper: MigrationHelper): Promise<void> {
// global state ("showFavicons" -> "disableFavicon")
const globalState = (await helper.get<ExpectedGlobalState>("global")) || {};
const showFavicons: boolean = await helper.getFromGlobal(ShowFaviconDefinition);
if (showFavicons != null) {
await helper.set<ExpectedGlobalState>("global", {
...globalState,
disableFavicon: !showFavicons,
});
// remove the global state provider framework key for `showFavicons`
await helper.setToGlobal(ShowFaviconDefinition, null);
}
}
}