From 60fcf3e21242af75432a90a00cd87e588743fbd6 Mon Sep 17 00:00:00 2001 From: "Wright, Christopher R" Date: Thu, 20 Sep 2018 13:10:23 -0400 Subject: [PATCH] #21 - Mock AppSettings properly for external files Signed-off-by: Wright, Christopher R --- .../CredentialManagerFactory.test.ts | 8 +++ .../settings/src/__mocks__/AppSettings.ts | 59 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 packages/settings/src/__mocks__/AppSettings.ts diff --git a/packages/security/__tests__/CredentialManagerFactory.test.ts b/packages/security/__tests__/CredentialManagerFactory.test.ts index 0a115fbec..e3d159559 100644 --- a/packages/security/__tests__/CredentialManagerFactory.test.ts +++ b/packages/security/__tests__/CredentialManagerFactory.test.ts @@ -16,14 +16,22 @@ describe("CredentialManagerFactory", () => { const testClassDir = "CredentialManagerFactory-testClasses"; jest.doMock("../src/DefaultCredentialManager"); + jest.doMock("../../settings/src/AppSettings"); let {CredentialManagerFactory, DefaultCredentialManager} = require(".."); + let { AppSettings } = require("../../settings"); + + beforeEach(() => { + AppSettings.initialize("We are mocked"); + }); afterEach(async () => { // Because initialize can only be called once, we need to reset the module cache everytime and // reload our modules. So we will clear the module registry and import again jest.resetModuleRegistry(); jest.doMock("../src/DefaultCredentialManager"); + jest.doMock("../../settings/src/AppSettings"); ({CredentialManagerFactory, DefaultCredentialManager} = await import("..")); + ({AppSettings} = await import("../../settings")); }); it("should throw an error when getting the manager before init", () => { diff --git a/packages/settings/src/__mocks__/AppSettings.ts b/packages/settings/src/__mocks__/AppSettings.ts new file mode 100644 index 000000000..81053bfaf --- /dev/null +++ b/packages/settings/src/__mocks__/AppSettings.ts @@ -0,0 +1,59 @@ +/* + * This program and the accompanying materials are made available under the terms of the * + * Eclipse Public License v2.0 which accompanies this distribution, and is available at * + * https://www.eclipse.org/legal/epl-v20.html * + * * + * SPDX-License-Identifier: EPL-2.0 * + * * + * Copyright Contributors to the Zowe Project. * + * * + */ + +import { ISettingsFile } from "../doc/ISettingsFile"; + +/* + * This file mocks the AppSettings class and tries to keep some of the logic in tact. + * Almost all methods of app settings are now a mock object so you can spy on the class. + */ + +// Define the constructor of the mock app settings class +function Settings() { + this.setNewOverride = jest.fn(); + + this.settings = { // tslint:disable-line + overrides: { + CredentialManager: false + } + } as ISettingsFile; +} + +// Mock the constructor and have Settings be the instance +const AppSettings: any = jest.fn(Settings); + +// Define the static mInstance private variable +AppSettings.mInstance = null; + +// Define the initialize method mock and implementation +AppSettings.initialize = jest.fn(() => { + if (AppSettings.mInstance != null) { + throw new Error("AppSettings was already initialized. If this has changed please alter the mock logic"); + } + + AppSettings.mInstance = new AppSettings(); + + return AppSettings.mInstance; +}); + +// Define the instance getter property and implementation +Object.defineProperty(AppSettings, "instance", { + get: () => { + if (AppSettings.mInstance == null) { + throw new Error("AppSettings should be initialized first. If this has changed please alter the mock logic"); + } + + return AppSettings.mInstance; + } +}); + +// Export the mocked settings object :) +exports.AppSettings = AppSettings;