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 cms/templates and api/fileMapper #58

Merged
merged 2 commits into from
Nov 6, 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
23 changes: 23 additions & 0 deletions api/__tests__/fileMapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fileStreamResponse from './fixtures/fileStreamResponse.json';
import { createFileMapperNodeFromStreamResponse } from '../fileMapper';

describe('api/fileMapper', () => {
describe('createFileMapperNodeFromStreamResponse()', () => {
const src = '1234/test.html';
it('should return request#tranform to create a FileMapperNode from the octet-stream response', () => {
const node = createFileMapperNodeFromStreamResponse(
src,
fileStreamResponse
);
expect(node).toEqual({
source: null,
path: '/1234/test.html',
createdAt: 0,
updatedAt: 1565214001268,
name: 'test.html',
folder: false,
children: [],
});
});
});
});
17 changes: 17 additions & 0 deletions api/__tests__/fixtures/fileStreamResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"status": 200,
"data": "<!--\n templateType: page\n isAvailableForNewContent: false\n-->\n<div>\n\thello new stuff\n\thello new stuff\n\thello new stuff\n</div>\n",
"headers": {
"content-type": "application/octet-stream",
"content-length": "132",
"content-disposition": "form-data; name=\"file\"; filename=\"test.html\"; creation-date=\"0\"; modification-date=\"1565214001268\""
},
"request": {
"method": "GET",
"headers": {
"content-type": "application/octet-stream",
"accept": "application/octet-stream"
}
},
"statusText": "OK"
}
6 changes: 3 additions & 3 deletions api/fileMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { FileMapperNode, FileMapperOptions, FileTree } from '../types/Files';

export const FILE_MAPPER_API_PATH = 'content/filemapper/v1';

function createFileMapperNodeFromStreamResponse(
export function createFileMapperNodeFromStreamResponse(
filePath: string,
response: AxiosResponse
response: Partial<AxiosResponse>
): FileMapperNode {
if (filePath[0] !== '/') {
filePath = `/${filePath}`;
Expand Down Expand Up @@ -68,7 +68,7 @@ export async function fetchModule(
});
}

//Fetch a file by file path.
// Fetch a file by file path.
export async function fetchFileStream(
accountId: number,
filePath: string,
Expand Down
2 changes: 1 addition & 1 deletion config/__tests__/CLIConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import config from '../CLIConfiguration';

// TODO write tests for CLIConfiguration.ts
describe('config/CLIConfiguration', () => {
describe('constructor', () => {
describe('constructor()', () => {
it('initializes correctly', () => {
expect(config).toBeDefined();
expect(config.options).toBeDefined();
Expand Down
20 changes: 10 additions & 10 deletions config/__tests__/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ function getAccountByAuthType(
return getAccounts(config).filter(portal => portal.authType === authType)[0];
}

describe('lib/config', () => {
describe('setConfig method', () => {
describe('config/config', () => {
describe('setConfig()', () => {
beforeEach(() => {
setConfig(CONFIG);
});
Expand All @@ -111,7 +111,7 @@ describe('lib/config', () => {
});
});

describe('getAccountId method', () => {
describe('getAccountId()', () => {
beforeEach(() => {
process.env = {};
setConfig({
Expand Down Expand Up @@ -159,7 +159,7 @@ describe('lib/config', () => {
});
});

describe('updateDefaultAccount method', () => {
describe('updateDefaultAccount()', () => {
const myPortalName = 'Foo';

beforeEach(() => {
Expand All @@ -172,7 +172,7 @@ describe('lib/config', () => {
});
});

describe('deleteEmptyConfigFile method', () => {
describe('deleteEmptyConfigFile()', () => {
it('does not delete config file if there are contents', () => {
jest
.spyOn(fs, 'readFileSync')
Expand All @@ -194,7 +194,7 @@ describe('lib/config', () => {
});
});

describe('updateAccountConfig method', () => {
describe('updateAccountConfig()', () => {
const CONFIG = {
defaultPortal: PORTALS[0].name,
portals: PORTALS,
Expand Down Expand Up @@ -342,7 +342,7 @@ describe('lib/config', () => {
});
});

describe('validateConfig method', () => {
describe('validateConfig()', () => {
const DEFAULT_PORTAL = PORTALS[0].name;

it('allows valid config', () => {
Expand Down Expand Up @@ -406,7 +406,7 @@ describe('lib/config', () => {
});
});

describe('getAndLoadConfigIfNeeded method', () => {
describe('getAndLoadConfigIfNeeded()', () => {
beforeEach(() => {
setConfig(undefined);
process.env = {};
Expand Down Expand Up @@ -551,7 +551,7 @@ describe('lib/config', () => {
});
});

describe('getConfigPath method', () => {
describe('getConfigPath()', () => {
beforeAll(() => {
setConfigPath(CONFIG_PATHS.default);
});
Expand Down Expand Up @@ -610,7 +610,7 @@ describe('lib/config', () => {
});
});

describe('createEmptyConfigFile method', () => {
describe('createEmptyConfigFile()', () => {
describe('when no config is present', () => {
let fsExistsSyncSpy: jest.SpyInstance;

Expand Down
16 changes: 8 additions & 8 deletions config/__tests__/configFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const CONFIG = {
} as CLIConfig_NEW;

describe('config/configFile', () => {
describe('getConfigFilePath method', () => {
describe('getConfigFilePath()', () => {
it('returns the config file path', () => {
const configFilePath = getConfigFilePath();
const homeDir = os.homedir();
Expand All @@ -51,7 +51,7 @@ describe('config/configFile', () => {
});
});

describe('configFileExists method', () => {
describe('configFileExists()', () => {
it('returns true if config file exists', () => {
existsSyncSpy.mockImplementation(() => true);
const exists = configFileExists();
Expand All @@ -61,7 +61,7 @@ describe('config/configFile', () => {
});
});

describe('configFileIsBlank method', () => {
describe('configFileIsBlank()', () => {
it('returns true if config file is blank', () => {
readFileSyncSpy.mockImplementation(() => Buffer.from(''));
const isBlank = configFileIsBlank();
Expand All @@ -78,7 +78,7 @@ describe('config/configFile', () => {
});
});

describe('deleteConfigFile method', () => {
describe('deleteConfigFile()', () => {
it('deletes a file', () => {
unlinkSyncSpy.mockImplementation(() => null);
deleteConfigFile();
Expand All @@ -87,7 +87,7 @@ describe('config/configFile', () => {
});
});

describe('readConfigFile method', () => {
describe('readConfigFile()', () => {
it('reads the config file', () => {
readFileSyncSpy.mockImplementation(() => Buffer.from('content'));
const result = readConfigFile('path/to/config/file');
Expand All @@ -103,7 +103,7 @@ describe('config/configFile', () => {
});
});

describe('parseConfig method', () => {
describe('parseConfig()', () => {
it('parses the config file', () => {
loadSpy.mockImplementation(() => ({}));
const result = parseConfig('config-source');
Expand All @@ -119,7 +119,7 @@ describe('config/configFile', () => {
});
});

describe('loadConfigFromFile method', () => {
describe('loadConfigFromFile()', () => {
it('loads the config from file', () => {
readFileSyncSpy.mockImplementation(() => Buffer.from('content'));
loadSpy.mockImplementation(() => ({}));
Expand All @@ -136,7 +136,7 @@ describe('config/configFile', () => {
});
});

describe('writeConfigToFile method', () => {
describe('writeConfigToFile()', () => {
it('writes the config to a file', () => {
ensureFileSyncSpy.mockImplementation(() => null);
writeFileSyncSpy.mockImplementation(() => null);
Expand Down
6 changes: 3 additions & 3 deletions config/__tests__/configUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const CONFIG: CLIConfig = {
};

describe('config/configUtils', () => {
describe('getOrderedAccount method', () => {
describe('getOrderedAccount()', () => {
it('returns an ordered account', () => {
const orderedAccount = getOrderedAccount(PAK_ACCOUNT);
const keys = Object.keys(orderedAccount);
Expand All @@ -63,7 +63,7 @@ describe('config/configUtils', () => {
});
});

describe('getOrderedConfig method', () => {
describe('getOrderedConfig()', () => {
it('returns an ordered config', () => {
const orderedConfig = getOrderedConfig(CONFIG);
const keys = Object.keys(orderedConfig);
Expand All @@ -80,7 +80,7 @@ describe('config/configUtils', () => {
});
});

describe('generateConfig method', () => {
describe('generateConfig()', () => {
it('returns a personal access key auth account', () => {
const pakConfig = generateConfig('personalaccesskey', {
accountId: 111,
Expand Down
2 changes: 1 addition & 1 deletion config/__tests__/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ENVIRONMENTS } from '../../constants/environments';

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

Expand Down
20 changes: 10 additions & 10 deletions errors/__tests__/apiErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export const newStatutsCodeError = (overrides = {}): GenericError => {
};
};

describe('apiErrors', () => {
describe('isApiStatusCodeError', () => {
describe('errors/apiErrors', () => {
describe('isApiStatusCodeError()', () => {
it('returns true for api status code errors', () => {
const error1 = newError({ status: 100 });
const error2 = newError({ status: 599 });
Expand All @@ -57,7 +57,7 @@ describe('apiErrors', () => {
});
});

describe('isMissingScopeError', () => {
describe('isMissingScopeError()', () => {
it('returns true for missing scope errors', () => {
const error1 = newStatutsCodeError({
status: 403,
Expand All @@ -77,7 +77,7 @@ describe('apiErrors', () => {
});
});

describe('isGatingError', () => {
describe('isGatingError()', () => {
it('returns true for gating errors', () => {
const error1 = newStatutsCodeError({
status: 403,
Expand All @@ -97,7 +97,7 @@ describe('apiErrors', () => {
});
});

describe('isApiUploadValidationError', () => {
describe('isApiUploadValidationError()', () => {
it('returns true for api upload validation errors', () => {
const error1 = newStatutsCodeError({
status: 400,
Expand All @@ -122,7 +122,7 @@ describe('apiErrors', () => {
});
});

describe('isSpecifiedHubSpotAuthError', () => {
describe('isSpecifiedHubSpotAuthError()', () => {
it('returns true for matching HubSpot auth errors', () => {
const error1 = newError({ name: 'HubSpotAuthError', status: 123 });
expect(isSpecifiedHubSpotAuthError(error1, { status: 123 })).toBe(true);
Expand All @@ -134,28 +134,28 @@ describe('apiErrors', () => {
});
});

describe('throwStatusCodeError', () => {
describe('throwStatusCodeError()', () => {
it('throws status code error', () => {
const error = newStatutsCodeError() as StatusCodeError;
expect(() => throwStatusCodeError(error)).toThrow();
});
});

describe('throwApiStatusCodeError', () => {
describe('throwApiStatusCodeError()', () => {
it('throws api status code error', () => {
const error = newStatutsCodeError() as StatusCodeError;
expect(() => throwApiStatusCodeError(error)).toThrow();
});
});

describe('throwApiError', () => {
describe('throwApiError()', () => {
it('throws api error', () => {
const error = newStatutsCodeError() as StatusCodeError;
expect(() => throwApiError(error)).toThrow();
});
});

describe('throwApiUploadError', () => {
describe('throwApiUploadError()', () => {
it('throws api upload error', () => {
const error = newStatutsCodeError() as StatusCodeError;
expect(() => throwApiUploadError(error)).toThrow();
Expand Down
14 changes: 7 additions & 7 deletions errors/__tests__/standardErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export const newError = (overrides = {}): BaseError => {
};
};

describe('standardErrors', () => {
describe('isSystemError', () => {
describe('errors/standardErrors', () => {
describe('isSystemError()', () => {
it('returns true for system errors', () => {
const error = newError();
expect(isSystemError(error)).toBe(true);
Expand All @@ -38,7 +38,7 @@ describe('standardErrors', () => {
});
});

describe('isFatalError', () => {
describe('isFatalError()', () => {
it('returns true for fatal errors', () => {
const cause = newError() as StatusCodeError;
const error = new HubSpotAuthError('A fatal auth error', { cause });
Expand All @@ -51,28 +51,28 @@ describe('standardErrors', () => {
});
});

describe('throwErrorWithMessage', () => {
describe('throwErrorWithMessage()', () => {
it('throws error with message', () => {
const error = newError();
expect(() => throwErrorWithMessage('', {}, error)).toThrow();
});
});

describe('throwTypeErrorWithMessage', () => {
describe('throwTypeErrorWithMessage()', () => {
it('throws type error with message', () => {
const error = newError();
expect(() => throwTypeErrorWithMessage('', {}, error)).toThrow();
});
});

describe('throwAuthErrorWithMessage', () => {
describe('throwAuthErrorWithMessage()', () => {
it('throws auth error with message', () => {
const error = newError() as StatusCodeError;
expect(() => throwAuthErrorWithMessage('', {}, error)).toThrow();
});
});

describe('throwError', () => {
describe('throwError()', () => {
it('throws error', () => {
const error = newError();
expect(() => throwError(error)).toThrow();
Expand Down
Loading
Loading