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

chore: Convert lib/tests to TypeScript #1345

Merged
merged 10 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
48 changes: 31 additions & 17 deletions lib/__tests__/ProjectLogsManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
// @ts-nocheck
const { ProjectLogsManager } = require('../projects/ProjectLogsManager');
kemmerle marked this conversation as resolved.
Show resolved Hide resolved
const { getProjectConfig, ensureProjectExists } = require('../projects');
const {
fetchProjectComponentsMetadata,
} = require('@hubspot/local-dev-lib/api/projects');
import { ProjectLogsManager } from '../projects/ProjectLogsManager';
import { getProjectConfig, ensureProjectExists } from '../projects';
import { fetchProjectComponentsMetadata } from '@hubspot/local-dev-lib/api/projects';

const SUBCOMPONENT_TYPES = {
APP_ID: 'APP_ID',
kemmerle marked this conversation as resolved.
Show resolved Hide resolved
PACKAGE_LOCK_FILE: 'PACKAGE_LOCK_FILE',
CRM_CARD_V2: 'CRM_CARD_V2',
CARD_V2: 'CARD_V2',
SERVERLESS_PKG: 'SERVERLESS_PKG',
SERVERLESS_ROUTE: 'SERVERLESS_ROUTE',
SERVERLESS_FUNCTION: 'SERVERLESS_FUNCTION',
APP_FUNCTION: 'APP_FUNCTION',
AUTOMATION_ACTION: 'AUTOMATION_ACTION',
REACT_EXTENSION: 'REACT_EXTENSION',
} as const;

jest.mock('../projects');
jest.mock('@hubspot/local-dev-lib/api/projects');
Expand All @@ -26,31 +36,33 @@ describe('lib/projects/ProjectLogsManager', () => {
const function1 = {
componentName: 'function1',
type: {
name: 'APP_FUNCTION',
name: SUBCOMPONENT_TYPES.APP_FUNCTION,
},
deployOutput: {
appId,
appFunctionName: 'function1',
},
};
const functions = [
function1,
{
componentName: 'function2',
type: {
name: 'APP_FUNCTION',
name: SUBCOMPONENT_TYPES.APP_FUNCTION,
},
deployOutput: {
appId,
appFunctionName: 'function2',
},
},
];

beforeEach(() => {
ProjectLogsManager.reset();

getProjectConfig.mockResolvedValue(projectConfig);
ensureProjectExists.mockResolvedValue(projectDetails);
fetchProjectComponentsMetadata.mockResolvedValue({
(getProjectConfig as jest.Mock).mockResolvedValue(projectConfig);
(ensureProjectExists as jest.Mock).mockResolvedValue(projectDetails);
(fetchProjectComponentsMetadata as jest.Mock).mockResolvedValue({
data: {
topLevelComponentMetadata: [
{
Expand Down Expand Up @@ -81,7 +93,7 @@ describe('lib/projects/ProjectLogsManager', () => {
});

it('should throw an error if there is a problem with the config', async () => {
getProjectConfig.mockResolvedValue({});
(getProjectConfig as jest.Mock).mockResolvedValue({});
await expect(async () =>
ProjectLogsManager.init(accountId)
).rejects.toThrow(
Expand All @@ -99,7 +111,7 @@ describe('lib/projects/ProjectLogsManager', () => {
});

it('should throw an error if there is data missing from the project details', async () => {
ensureProjectExists.mockResolvedValue({});
(ensureProjectExists as jest.Mock).mockResolvedValue({});
await expect(async () =>
ProjectLogsManager.init(accountId)
).rejects.toThrow(/There was an error fetching project details/);
Expand Down Expand Up @@ -177,9 +189,11 @@ describe('lib/projects/ProjectLogsManager', () => {
const functionToChoose = {
componentName: 'function1',
type: {
name: 'APP_FUNCTION',
name: SUBCOMPONENT_TYPES.APP_FUNCTION,
},
deployOutput: {
appId: 123,
appFunctionName: 'function1',
endpoint: { path: 'yooooooo', methods: ['GET'] },
},
};
Expand All @@ -202,11 +216,11 @@ describe('lib/projects/ProjectLogsManager', () => {

describe('reset', () => {
it('should reset all the values', async () => {
ProjectLogsManager.someRandomField = 'value';
expect(ProjectLogsManager.someRandomField).toBeDefined();
ProjectLogsManager.projectName = 'value';
expect(ProjectLogsManager.projectName).toBeDefined();

ProjectLogsManager.reset();
expect(ProjectLogsManager.isPublicFunction).toBeUndefined();
expect(ProjectLogsManager.projectName).toBeUndefined();
});
});
});
100 changes: 65 additions & 35 deletions lib/__tests__/commonOpts.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
// @ts-nocheck
const {
import {
CMS_PUBLISH_MODE,
DEFAULT_CMS_PUBLISH_MODE,
} = require('@hubspot/local-dev-lib/constants/files');
const {
} from '@hubspot/local-dev-lib/constants/files';
import {
getAndLoadConfigIfNeeded,
getAccountId,
getAccountConfig,
loadConfigFromEnvironment,
} = require('@hubspot/local-dev-lib/config');
const { getCmsPublishMode } = require('../commonOpts');
} from '@hubspot/local-dev-lib/config';
import { getCmsPublishMode } from '../commonOpts';
import { CmsPublishMode } from '@hubspot/local-dev-lib/types/Files';
import { Arguments } from 'yargs';

type CmsPublishModeArgs = {
cmsPublishMode?: CmsPublishMode;
account?: number | string;
};

function buildArguments(
args: CmsPublishModeArgs
kemmerle marked this conversation as resolved.
Show resolved Hide resolved
): Arguments<CmsPublishModeArgs> {
return {
_: [],
$0: '',
...args,
};
}

jest.mock('@hubspot/local-dev-lib/config');
jest.mock('@hubspot/local-dev-lib/logger');
Expand Down Expand Up @@ -39,62 +55,76 @@ describe('lib/commonOpts', () => {
};

afterEach(() => {
getAndLoadConfigIfNeeded.mockReset();
getAccountId.mockReset();
getAccountConfig.mockReset();
loadConfigFromEnvironment.mockReset();
jest.resetAllMocks();
});

describe('cms publish mode option precedence', () => {
describe('1. --cmsPublishMode', () => {
it('should return the cms publish mode specified by the command option if present.', () => {
getAndLoadConfigIfNeeded.mockReturnValue(
(getAndLoadConfigIfNeeded as jest.Mock).mockReturnValue(
configWithDefaultCmsPublishMode
);
getAccountConfig.mockReturnValue(devAccountConfig);
(getAccountConfig as jest.Mock).mockReturnValue(devAccountConfig);
expect(
getCmsPublishMode({ cmsPublishMode: CMS_PUBLISH_MODE.draft })
getCmsPublishMode(
buildArguments({
cmsPublishMode: CMS_PUBLISH_MODE.draft,
})
)
).toBe(CMS_PUBLISH_MODE.draft);
expect(
getCmsPublishMode({ cmsPublishMode: CMS_PUBLISH_MODE.publish })
getCmsPublishMode(
buildArguments({
cmsPublishMode: CMS_PUBLISH_MODE.publish,
})
)
).toBe(CMS_PUBLISH_MODE.publish);
expect(getCmsPublishMode({ cmsPublishMode: 'undefined-mode' })).toBe(
'undefined-mode'
);
});
});
describe('2. hubspot.config.yml -> config.accounts[x].defaultCmsPublishMode', () => {
it('should return the defaultCmsPublishMode specified by the account specific config if present.', () => {
getAndLoadConfigIfNeeded.mockReturnValue(
(getAndLoadConfigIfNeeded as jest.Mock).mockReturnValue(
configWithDefaultCmsPublishMode
);
getAccountId.mockReturnValue(accounts.DEV);
getAccountConfig.mockReturnValue(devAccountConfig);
loadConfigFromEnvironment.mockReturnValue(undefined);
expect(getCmsPublishMode({ account: accounts.DEV })).toBe(
CMS_PUBLISH_MODE.draft
);
(getAccountId as jest.Mock).mockReturnValue(accounts.DEV);
(getAccountConfig as jest.Mock).mockReturnValue(devAccountConfig);
(loadConfigFromEnvironment as jest.Mock).mockReturnValue(undefined);
expect(
getCmsPublishMode(
buildArguments({
account: accounts.DEV,
})
)
).toBe(CMS_PUBLISH_MODE.draft);
});
});
describe('3. hubspot.config.yml -> config.defaultCmsPublishMode', () => {
it('should return the defaultCmsPublishMode specified by the config if present.', () => {
getAndLoadConfigIfNeeded.mockReturnValue(
(getAndLoadConfigIfNeeded as jest.Mock).mockReturnValue(
configWithDefaultCmsPublishMode
);
getAccountId.mockReturnValue(accounts.PROD);
getAccountConfig.mockReturnValue(prodAccountConfig);
loadConfigFromEnvironment.mockReturnValue(undefined);
expect(getCmsPublishMode({ account: accounts.PROD })).toBe(
CMS_PUBLISH_MODE.draft
);
(getAccountId as jest.Mock).mockReturnValue(accounts.PROD);
(getAccountConfig as jest.Mock).mockReturnValue(prodAccountConfig);
(loadConfigFromEnvironment as jest.Mock).mockReturnValue(undefined);
expect(
kemmerle marked this conversation as resolved.
Show resolved Hide resolved
getCmsPublishMode(
buildArguments({
account: accounts.PROD,
})
)
).toBe(CMS_PUBLISH_MODE.draft);
});
});
describe('4. DEFAULT_CMS_PUBLISH_MODE', () => {
it('should return the defaultCmsPubishMode specified by the config if present.', () => {
loadConfigFromEnvironment.mockReturnValue(undefined);
expect(getCmsPublishMode({ account: 'xxxxx' })).toBe(
DEFAULT_CMS_PUBLISH_MODE
);
(loadConfigFromEnvironment as jest.Mock).mockReturnValue(undefined);
expect(
getCmsPublishMode(
buildArguments({
account: 'xxxxx',
})
)
).toBe(DEFAULT_CMS_PUBLISH_MODE);
});
});
});
Expand Down
Loading
Loading