Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests for config/environment and OAuth2Manager #62

Merged
merged 3 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
119 changes: 118 additions & 1 deletion config/__tests__/CLIConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,130 @@
import { ENVIRONMENTS } from '../../constants/environments';
import config from '../CLIConfiguration';

// TODO write tests for CLIConfiguration.ts
describe('config/CLIConfiguration', () => {
afterAll(() => {
config.setActive(false);
});

describe('constructor()', () => {
it('initializes correctly', () => {
expect(config).toBeDefined();
expect(config.options).toBeDefined();
expect(config.useEnvConfig).toBe(false);
expect(config.config).toBe(null);
expect(config.active).toBe(false);
});
});

describe('isActive()', () => {
it('returns true when the class is being used', () => {
expect(config.isActive()).toBe(false);
config.setActive(true);
expect(config.isActive()).toBe(true);
});
});

describe('getAccount()', () => {
it('returns null when no config is loaded', () => {
expect(config.getAccount('account-name')).toBe(null);
});
});

describe('isConfigFlagEnabled()', () => {
it('returns default value when no config is loaded', () => {
expect(config.isConfigFlagEnabled('allowUsageTracking', false)).toBe(
false
);
});
});

describe('getAccountId()', () => {
it('returns null when it cannot find the account in the config', () => {
expect(config.getAccountId('account-name')).toBe(null);
});
});

describe('getDefaultAccount()', () => {
it('returns null when no config is loaded', () => {
expect(config.getDefaultAccount()).toBe(null);
});
});

describe('getConfigAccountIndex()', () => {
it('returns -1 when no config is loaded', () => {
expect(config.getConfigAccountIndex(123)).toBe(-1);
});
});

describe('isAccountInConfig()', () => {
it('returns false when no config is loaded', () => {
expect(config.isAccountInConfig(123)).toBe(false);
});
});

describe('getConfigForAccount()', () => {
it('returns null when no config is loaded', () => {
expect(config.getConfigForAccount(123)).toBe(null);
});
});

describe('getEnv()', () => {
it('returns PROD when no config is loaded', () => {
expect(config.getEnv(123)).toBe(ENVIRONMENTS.PROD);
});
});

describe('updateDefaultAccount()', () => {
it('throws when no config is loaded', () => {
expect(() => {
config.updateDefaultAccount('account-name');
}).toThrow();
});
});

describe('renameAccount()', () => {
it('throws when no config is loaded', () => {
expect(() => {
config.renameAccount('account-name', 'new-account-name');
}).toThrow();
});
});

describe('removeAccountFromConfig()', () => {
it('throws when no config is loaded', () => {
expect(() => {
config.removeAccountFromConfig('account-name');
}).toThrow();
});
});

describe('updateDefaultMode()', () => {
it('throws when no config is loaded', () => {
expect(() => {
config.updateDefaultMode('newMode');
}).toThrow();
});
});

describe('updateHttpTimeout()', () => {
it('throws when no config is loaded', () => {
expect(() => {
config.updateHttpTimeout('1000');
}).toThrow();
});
});

describe('updateAllowUsageTracking()', () => {
it('throws when no config is loaded', () => {
expect(() => {
config.updateAllowUsageTracking(true);
}).toThrow();
});
});

describe('isTrackingAllowed()', () => {
it('returns true when no config is loaded', () => {
expect(config.isTrackingAllowed()).toBe(true);
});
});
});
50 changes: 43 additions & 7 deletions config/__tests__/environment.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
import { getValidEnv } from '../../lib/environment';
import { ENVIRONMENTS } from '../../constants/environments';
import { loadConfigFromEnvironment } from '../environment';
import { ENVIRONMENT_VARIABLES } from '../../constants/environments';
import { PERSONAL_ACCESS_KEY_AUTH_METHOD } from '../../constants/auth';

// TODO write tests for environment.ts
describe('config/environment', () => {
describe('getValidEnv()', () => {
it('defaults to prod when no args are passed', () => {
const env = getValidEnv();
describe('loadConfigFromEnvironment()', () => {
const INITIAL_ENV = process.env;

expect(env).toBe(ENVIRONMENTS.PROD);
beforeEach(() => {
jest.resetModules();
process.env = { ...INITIAL_ENV };
});

afterAll(() => {
process.env = INITIAL_ENV;
});

it('returns null when no accountId exists', () => {
const config = loadConfigFromEnvironment();
expect(config).toBe(null);
});

it('returns null when no env exists', () => {
process.env[ENVIRONMENT_VARIABLES.HUBSPOT_ACCOUNT_ID] = '1234';

const config = loadConfigFromEnvironment();
expect(config).toBe(null);
});

it('generates a personal access key config from the env', () => {
process.env[ENVIRONMENT_VARIABLES.HUBSPOT_ACCOUNT_ID] = '1234';
process.env[ENVIRONMENT_VARIABLES.HUBSPOT_ENVIRONMENT] = 'qa';
process.env[ENVIRONMENT_VARIABLES.HUBSPOT_PERSONAL_ACCESS_KEY] =
'personal-access-key';

const config = loadConfigFromEnvironment();
expect(config).toMatchObject({
accounts: [
{
authType: PERSONAL_ACCESS_KEY_AUTH_METHOD.value,
accountId: 1234,
env: 'qa',
personalAccessKey: 'personal-access-key',
},
],
});
});
});
});
28 changes: 28 additions & 0 deletions errors/__tests__/fileSystemErrors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { throwFileSystemError } from '../fileSystemErrors';
import { BaseError } from '../../types/Error';

export const newError = (overrides = {}): BaseError => {
return {
name: 'Error',
message: 'An error ocurred',
errno: 1,
code: 'error_code',
syscall: 'error_syscall',
errors: [],
...overrides,
};
};

const fileSystemErrorContext = {
filepath: 'some/path',
};

describe('errors/fileSystemErrors', () => {
describe('throwFileSystemError()', () => {
it('throws a fileSystemError', () => {
expect(() => {
throwFileSystemError(newError(), fileSystemErrorContext);
}).toThrow();
});
});
});
3 changes: 3 additions & 0 deletions errors/fileSystemErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { BaseError, FileSystemErrorContext } from '../types/Error';

const i18nKey = 'errors.fileSystemErrors';

/**
* @throws
*/
export function throwFileSystemError(
error: BaseError,
context: FileSystemErrorContext
Expand Down
146 changes: 146 additions & 0 deletions lib/__tests__/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import chalk from 'chalk';
import {
Styles,
stylize,
setLogLevel,
getLogLevel,
LOG_LEVEL,
logger,
shouldLog,
} from '../logging/logger';

describe('lib/logging/logger', () => {
afterEach(() => {
setLogLevel(LOG_LEVEL.LOG);
});

describe('stylize()', () => {
it('stylizes input', () => {
const res = stylize('[ERROR]', Styles.error, ['test']);

expect(res[0]).toEqual(`${chalk.reset.red('[ERROR]')} test`);
});
});

describe('setLogLevel()', () => {
it('sets the log level for the logger', () => {
setLogLevel(LOG_LEVEL.DEBUG);
expect(getLogLevel()).toBe(LOG_LEVEL.DEBUG);

setLogLevel(LOG_LEVEL.WARN);
expect(getLogLevel()).toBe(LOG_LEVEL.WARN);

setLogLevel(LOG_LEVEL.NONE);
expect(getLogLevel()).toBe(LOG_LEVEL.NONE);
});
});

describe('shouldLog()', () => {
it('returns false for all logs when the currentLogLevel is NONE', () => {
setLogLevel(LOG_LEVEL.NONE);
expect(shouldLog(LOG_LEVEL.DEBUG)).toBeFalsy();
expect(shouldLog(LOG_LEVEL.LOG)).toBeFalsy();
expect(shouldLog(LOG_LEVEL.WARN)).toBeFalsy();
expect(shouldLog(LOG_LEVEL.ERROR)).toBeFalsy();
});

it('returns true for all logs when the currentLogLevel is DEBUG', () => {
setLogLevel(LOG_LEVEL.DEBUG);
expect(shouldLog(LOG_LEVEL.DEBUG)).toBeTruthy();
expect(shouldLog(LOG_LEVEL.LOG)).toBeTruthy();
expect(shouldLog(LOG_LEVEL.WARN)).toBeTruthy();
expect(shouldLog(LOG_LEVEL.ERROR)).toBeTruthy();
});

it('returns false for debugs when the currentLogLevel is LOG', () => {
setLogLevel(LOG_LEVEL.LOG);
expect(shouldLog(LOG_LEVEL.DEBUG)).toBeFalsy();
expect(shouldLog(LOG_LEVEL.LOG)).toBeTruthy();
expect(shouldLog(LOG_LEVEL.WARN)).toBeTruthy();
expect(shouldLog(LOG_LEVEL.ERROR)).toBeTruthy();
});

it('returns false for debugs and logs when the currentLogLevel is WARN', () => {
setLogLevel(LOG_LEVEL.WARN);
expect(shouldLog(LOG_LEVEL.DEBUG)).toBeFalsy();
expect(shouldLog(LOG_LEVEL.LOG)).toBeFalsy();
expect(shouldLog(LOG_LEVEL.WARN)).toBeTruthy();
expect(shouldLog(LOG_LEVEL.ERROR)).toBeTruthy();
});

it('returns false for debugs, logs, and warns when the currentLogLevel is ERROR', () => {
setLogLevel(LOG_LEVEL.ERROR);
expect(shouldLog(LOG_LEVEL.DEBUG)).toBeFalsy();
expect(shouldLog(LOG_LEVEL.LOG)).toBeFalsy();
expect(shouldLog(LOG_LEVEL.WARN)).toBeFalsy();
expect(shouldLog(LOG_LEVEL.ERROR)).toBeTruthy();
});
});

describe('logger()', () => {
let warnSpy: jest.SpyInstance;
let logSpy: jest.SpyInstance;
let debugSpy: jest.SpyInstance;
let infoSpy: jest.SpyInstance;
let groupSpy: jest.SpyInstance;
let groupEndSpy: jest.SpyInstance;

beforeEach(() => {
setLogLevel(LOG_LEVEL.LOG);
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => null);
logSpy = jest.spyOn(console, 'log').mockImplementation(() => null);
debugSpy = jest.spyOn(console, 'debug').mockImplementation(() => null);
infoSpy = jest.spyOn(console, 'info').mockImplementation(() => null);
groupSpy = jest.spyOn(console, 'group').mockImplementation(() => null);
groupEndSpy = jest
.spyOn(console, 'groupEnd')
.mockImplementation(() => null);
});

afterAll(() => {
warnSpy.mockReset();
logSpy.mockReset();
debugSpy.mockReset();
infoSpy.mockReset();
groupSpy.mockReset();
groupEndSpy.mockReset();
});

it('handles warnings', () => {
logger.log('test log');
expect(warnSpy).not.toHaveBeenCalled();

logger.warn('test log');
expect(warnSpy).toHaveBeenCalled();
});

it('handles logs', () => {
logger.debug('test log');
expect(debugSpy).not.toHaveBeenCalled();

logger.log('test log');
expect(logSpy).toHaveBeenCalled();

setLogLevel(LOG_LEVEL.DEBUG);
logger.debug('test log');
expect(debugSpy).toHaveBeenCalled();
});

it('handles info', () => {
logger.info('test log');
expect(infoSpy).toHaveBeenCalled();
});

it('handles success', () => {
logger.success('test log');
expect(logSpy).toHaveBeenCalled();
});

it('handles group and groupEnd', () => {
logger.group('test log');
logger.groupEnd();
expect(groupSpy).toHaveBeenCalled();
expect(groupEndSpy).toHaveBeenCalled();
});
});
});
23 changes: 23 additions & 0 deletions lib/__tests__/text.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { commaSeparatedValues } from '../text';

describe('lib/text', () => {
describe('commaSeparatedValues()', () => {
it('returns a string with comma separated values', () => {
const res = commaSeparatedValues(['first', 'second', 'third']);

expect(res).toBe('first, second, and third');
});

it('supports a custom conjuction', async () => {
const res = commaSeparatedValues(['first', 'second', 'third'], 'custom');

expect(res).toBe('first, second, custom third');
});

it('supports a custom if empty case', async () => {
const res = commaSeparatedValues([], null, 'input is empty');

expect(res).toBe('input is empty');
});
});
});
Loading
Loading