Skip to content

Commit

Permalink
chore: porting some lib tests to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
brandenrodgers committed Jan 15, 2025
1 parent 48e9d58 commit 77c37a8
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 64 deletions.
130 changes: 76 additions & 54 deletions lib/__tests__/validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,69 @@
// @ts-nocheck
const { getAccountConfig } = require('@hubspot/local-dev-lib/config');
const { getOauthManager } = require('@hubspot/local-dev-lib/oauth');
const {
accessTokenForPersonalAccessKey,
} = require('@hubspot/local-dev-lib/personalAccessKey');
const { getAccountId } = require('../commonOpts');
const { validateAccount } = require('../validation');
import { getAccountConfig } from '@hubspot/local-dev-lib/config';
import { getOauthManager } from '@hubspot/local-dev-lib/oauth';
import { accessTokenForPersonalAccessKey } from '@hubspot/local-dev-lib/personalAccessKey';
import { getAccountId } from '../commonOpts';
import { validateAccount } from '../validation';
import { Arguments } from 'yargs';

jest.mock('@hubspot/local-dev-lib/config');
jest.mock('@hubspot/local-dev-lib/logger');
jest.mock('@hubspot/local-dev-lib/oauth');
jest.mock('@hubspot/local-dev-lib/personalAccessKey');
jest.mock('../commonOpts');

const yargsOption = (option: { [key: string]: string }): Arguments => ({
$0: '',
_: [''],
...option,
});

describe('lib/validation', () => {
describe('validateAccount', () => {
it('returns false if an account is missing', async () => {
getAccountId.mockReturnValueOnce(null);
expect(await validateAccount({ account: 123 })).toBe(false);
(getAccountId as jest.Mock).mockReturnValueOnce(null);
expect(await validateAccount(yargsOption({ account: '123' }))).toBe(
false
);
});
it('returns false if an account config is missing', async () => {
getAccountId.mockReturnValueOnce(123);
getAccountConfig.mockReturnValueOnce(undefined);
expect(await validateAccount({ account: 123 })).toBe(false);
(getAccountId as jest.Mock).mockReturnValueOnce('123');
(getAccountConfig as jest.Mock).mockReturnValueOnce(undefined);
expect(await validateAccount(yargsOption({ account: '123' }))).toBe(
false
);
});
it('returns false for oauth2 authType if auth is missing', async () => {
getAccountId.mockReturnValueOnce(123);
getAccountConfig.mockReturnValueOnce({
accountId: 123,
(getAccountId as jest.Mock).mockReturnValueOnce('123');
(getAccountConfig as jest.Mock).mockReturnValueOnce({
accountId: '123',
authType: 'oauth2',
});
expect(await validateAccount({ account: 123 })).toBe(false);
expect(await validateAccount(yargsOption({ account: '123' }))).toBe(
false
);
});
it('returns false if OAuth is missing configuration', async () => {
getAccountId.mockReturnValueOnce(123);
getAccountConfig.mockReturnValueOnce({
accountId: 123,
(getAccountId as jest.Mock).mockReturnValueOnce('123');
(getAccountConfig as jest.Mock).mockReturnValueOnce({
accountId: '123',
authType: 'oauth2',
auth: {
clientId: 'foo',
},
});
expect(await validateAccount({ account: 123 })).toBe(false);
expect(await validateAccount(yargsOption({ account: '123' }))).toBe(
false
);
});
it('returns false if an access token was not retrieved', async () => {
getAccountId.mockReturnValueOnce(123);
getOauthManager.mockReturnValueOnce({
(getAccountId as jest.Mock).mockReturnValueOnce('123');
(getOauthManager as jest.Mock).mockReturnValueOnce({
accessToken() {
return null;
},
});
getAccountConfig.mockReturnValueOnce({
accountId: 123,
(getAccountConfig as jest.Mock).mockReturnValueOnce({
accountId: '123',
authType: 'oauth2',
auth: {
clientId: 'foo',
Expand All @@ -61,17 +73,19 @@ describe('lib/validation', () => {
},
},
});
expect(await validateAccount({ account: 123 })).toBe(false);
expect(await validateAccount(yargsOption({ account: '123' }))).toBe(
false
);
});
it('returns false if an getting an access token throws', async () => {
getAccountId.mockReturnValueOnce(123);
getOauthManager.mockReturnValueOnce({
(getAccountId as jest.Mock).mockReturnValueOnce('123');
(getOauthManager as jest.Mock).mockReturnValueOnce({
accessToken() {
throw new Error('It failed');
},
});
getAccountConfig.mockReturnValueOnce({
accountId: 123,
(getAccountConfig as jest.Mock).mockReturnValueOnce({
accountId: '123',
authType: 'oauth2',
auth: {
clientId: 'foo',
Expand All @@ -81,17 +95,19 @@ describe('lib/validation', () => {
},
},
});
expect(await validateAccount({ account: 123 })).toBe(false);
expect(await validateAccount(yargsOption({ account: '123' }))).toBe(
false
);
});
it('returns true if OAuth is configured and an access token is received', async () => {
getAccountId.mockReturnValueOnce(123);
getOauthManager.mockReturnValueOnce({
(getAccountId as jest.Mock).mockReturnValueOnce('123');
(getOauthManager as jest.Mock).mockReturnValueOnce({
accessToken() {
return 'yep';
},
});
getAccountConfig.mockReturnValueOnce({
accountId: 123,
(getAccountConfig as jest.Mock).mockReturnValueOnce({
accountId: '123',
authType: 'oauth2',
auth: {
clientId: 'foo',
Expand All @@ -101,40 +117,46 @@ describe('lib/validation', () => {
},
},
});
expect(await validateAccount({ account: 123 })).toBe(true);
expect(await validateAccount(yargsOption({ account: '123' }))).toBe(true);
});
it('returns false if "personalaccesskey" configured and getting an access token throws', async () => {
getAccountId.mockReturnValueOnce(123);
accessTokenForPersonalAccessKey.mockImplementationOnce(() => {
throw new Error('It failed');
});
getAccountConfig.mockReturnValueOnce({
accountId: 123,
(getAccountId as jest.Mock).mockReturnValueOnce('123');
(accessTokenForPersonalAccessKey as jest.Mock).mockImplementationOnce(
() => {
throw new Error('It failed');
}
);
(getAccountConfig as jest.Mock).mockReturnValueOnce({
accountId: '123',
authType: 'personalaccesskey',
personalAccessKey: 'foo',
});
expect(await validateAccount({ account: 123 })).toBe(false);
expect(await validateAccount(yargsOption({ account: '123' }))).toBe(
false
);
});
it('returns true if "personalaccesskey" configured and an access token is received', async () => {
getAccountId.mockReturnValueOnce(123);
accessTokenForPersonalAccessKey.mockImplementationOnce(() => {
return 'secret-stuff';
});
getAccountConfig.mockReturnValueOnce({
accountId: 123,
(getAccountId as jest.Mock).mockReturnValueOnce('123');
(accessTokenForPersonalAccessKey as jest.Mock).mockImplementationOnce(
() => {
return 'secret-stuff';
}
);
(getAccountConfig as jest.Mock).mockReturnValueOnce({
accountId: '123',
authType: 'personalaccesskey',
personalAccessKey: 'foo',
});
expect(await validateAccount({ account: 123 })).toBe(true);
expect(await validateAccount(yargsOption({ account: '123' }))).toBe(true);
});
it('returns true if apiKey is configured and present', async () => {
getAccountId.mockReturnValueOnce(123);
getAccountConfig.mockReturnValueOnce({
accountId: 123,
(getAccountId as jest.Mock).mockReturnValueOnce('123');
(getAccountConfig as jest.Mock).mockReturnValueOnce({
accountId: '123',
authType: 'apikey',
apiKey: 'my-secret-key',
});
expect(await validateAccount({ account: 123 })).toBe(true);
expect(await validateAccount(yargsOption({ account: '123' }))).toBe(true);
});
});
});
7 changes: 3 additions & 4 deletions lib/prompts/__tests__/downloadProjectPrompt.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @ts-nocheck
const { getAccountId } = require('@hubspot/local-dev-lib/config');
const { fetchProjects } = require('@hubspot/local-dev-lib/api/projects');
const { downloadProjectPrompt } = require('../downloadProjectPrompt');
import { getAccountId } from '@hubspot/local-dev-lib/config';
import { fetchProjects } from '@hubspot/local-dev-lib/api/projects';
import { downloadProjectPrompt } from '../downloadProjectPrompt';

jest.mock('../promptUtils');
jest.mock('@hubspot/local-dev-lib/api/projects', () => ({
Expand Down
11 changes: 5 additions & 6 deletions lib/prompts/__tests__/projectsLogsPrompt.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// @ts-nocheck
const { projectLogsPrompt } = require('../projectsLogsPrompt');
const { promptUser } = require('../promptUtils');
const chalk = require('chalk');
import { projectLogsPrompt } from '../projectsLogsPrompt';
import { promptUser } from '../promptUtils';
import chalk from 'chalk';

jest.mock('../promptUtils');

describe('lib/prompts/projectsLogsPrompt', () => {
it('should return undefined functionName when functionChoices is nullable', async () => {
const actual = await projectLogsPrompt({ functionChoices: null });
it('should return undefined functionName when functionChoices is undefined', async () => {
const actual = await projectLogsPrompt({ functionChoices: undefined });
expect(actual).toEqual({});
expect(promptUser).not.toHaveBeenCalled();
});
Expand Down

0 comments on commit 77c37a8

Please sign in to comment.