Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Commit

Permalink
#21 - Mock AppSettings properly for external files
Browse files Browse the repository at this point in the history
Signed-off-by: Wright, Christopher R <Christopher.Wright@ca.com>
  • Loading branch information
Wright, Christopher R authored and Wright, Christopher R committed Sep 20, 2018
1 parent f4d0e06 commit 60fcf3e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/security/__tests__/CredentialManagerFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
59 changes: 59 additions & 0 deletions packages/settings/src/__mocks__/AppSettings.ts
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 60fcf3e

Please sign in to comment.