This repository has been archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#21 - Mock AppSettings properly for external files
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
Showing
2 changed files
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |