Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ export interface PreferencesStorage {
*/
export class IModelAppUserPreferencesStorage implements PreferencesStorage {
#nameSpace = PROPERTY_GRID_NAMESPACE;
#warnedAboutMissingPreferences = false;
constructor(nameSpace = PROPERTY_GRID_NAMESPACE) {
this.#nameSpace = nameSpace;
}

public async set(key: string, value: string): Promise<void> {
if (!IModelApp.userPreferences) {
Logger.logError(LOGGER_CATEGORY, `Cannot save user preference ${key} because 'IModelApp.userPreferences' not defined.`);
this.logMissingPreferencesWarning(key, "save");
return;
}

Expand All @@ -51,7 +52,7 @@ export class IModelAppUserPreferencesStorage implements PreferencesStorage {

public async get(key: string): Promise<string | undefined> {
if (!IModelApp.userPreferences) {
Logger.logError(LOGGER_CATEGORY, `Cannot get persisted user preference ${key} because 'IModelApp.userPreferences' not defined.`);
this.logMissingPreferencesWarning(key, "get");
return undefined;
}

Expand All @@ -68,4 +69,11 @@ export class IModelAppUserPreferencesStorage implements PreferencesStorage {
}
return undefined;
}

private logMissingPreferencesWarning(key: string, action: string) {
if (!this.#warnedAboutMissingPreferences) {
Logger.logWarning(LOGGER_CATEGORY, `Cannot ${action} persisted user preference ${key} because 'IModelApp.userPreferences' not defined.`);
this.#warnedAboutMissingPreferences = true;
}
}
Comment thread
Copilot marked this conversation as resolved.
Outdated
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ describe("IModelAppUserPreferencesStorage", () => {
};

let userPreferencesStub: ReturnType<typeof vi.spyOn>;
let loggerStub: ReturnType<typeof vi.spyOn>;
let logErrorStub: ReturnType<typeof vi.spyOn>;
let logWarningStub: ReturnType<typeof vi.spyOn>;
let storage: IModelAppUserPreferencesStorage;

beforeEach(() => {
userPreferencesStub = vi.spyOn(IModelApp, "userPreferences", "get");
loggerStub = vi.spyOn(Logger, "logError");
logErrorStub = vi.spyOn(Logger, "logError");
logWarningStub = vi.spyOn(Logger, "logWarning");
userPreferencesStub.mockReturnValue(imodelUserPreferences);
storage = new IModelAppUserPreferencesStorage();
});

afterEach(() => {
userPreferencesStub.mockReset();
loggerStub.mockReset();
logErrorStub.mockReset();
logWarningStub.mockReset();
imodelUserPreferences.get.mockReset();
imodelUserPreferences.save.mockReset();
});
Expand All @@ -46,14 +49,22 @@ describe("IModelAppUserPreferencesStorage", () => {
throw new Error("Invalid Key");
});
await storage.set("test-key", "test-value");
expect(loggerStub).toHaveBeenCalledWith("PropertyGrid", expect.stringContaining("Invalid Key"));
expect(logErrorStub).toHaveBeenCalledWith("PropertyGrid", expect.stringContaining("Invalid Key"));
});

it("logs error if `IModelApp.userPreferences` not defined", async () => {
it("logs warning if `IModelApp.userPreferences` not defined", async () => {
userPreferencesStub.mockReset();
userPreferencesStub.mockReturnValue(undefined);
await storage.set("test-key", "test-value");
expect(loggerStub).toHaveBeenCalledWith("PropertyGrid", expect.stringContaining("'IModelApp.userPreferences' not defined"));
expect(logWarningStub).toHaveBeenCalledWith("PropertyGrid", expect.stringContaining("'IModelApp.userPreferences' not defined"));
});

it("logs warning only once if `IModelApp.userPreferences` not defined", async () => {
userPreferencesStub.mockReset();
userPreferencesStub.mockReturnValue(undefined);
await storage.set("test-key", "test-value");
await storage.set("test-key-2", "test-value-2");
expect(logWarningStub).toHaveBeenCalledTimes(1);
});
});

Expand All @@ -68,14 +79,22 @@ describe("IModelAppUserPreferencesStorage", () => {
throw new Error("Invalid Key");
});
await storage.get("test-key");
expect(loggerStub).toHaveBeenCalledWith("PropertyGrid", expect.stringContaining("Invalid Key"));
expect(logErrorStub).toHaveBeenCalledWith("PropertyGrid", expect.stringContaining("Invalid Key"));
});

it("logs error if `IModelApp.userPreferences` not defined", async () => {
it("logs warning if `IModelApp.userPreferences` not defined", async () => {
userPreferencesStub.mockReset();
userPreferencesStub.mockReturnValue(undefined);
expect(await storage.get("test-key")).toBeUndefined();
expect(loggerStub).toHaveBeenCalledWith("PropertyGrid", expect.stringContaining("'IModelApp.userPreferences' not defined"));
expect(logWarningStub).toHaveBeenCalledWith("PropertyGrid", expect.stringContaining("'IModelApp.userPreferences' not defined"));
});

it("logs warning only once if `IModelApp.userPreferences` not defined", async () => {
userPreferencesStub.mockReset();
userPreferencesStub.mockReturnValue(undefined);
await storage.get("test-key");
await storage.get("test-key-2");
expect(logWarningStub).toHaveBeenCalledTimes(1);
});
});
});