From 77c37a8cf6d4b13e08e17db85135e3e629b3dcb1 Mon Sep 17 00:00:00 2001 From: Branden Rodgers Date: Wed, 15 Jan 2025 13:03:38 -0500 Subject: [PATCH] chore: porting some lib tests to TS --- lib/__tests__/validation.test.ts | 130 ++++++++++-------- .../__tests__/downloadProjectPrompt.test.ts | 7 +- .../__tests__/projectsLogsPrompt.test.ts | 11 +- 3 files changed, 84 insertions(+), 64 deletions(-) diff --git a/lib/__tests__/validation.test.ts b/lib/__tests__/validation.test.ts index ba44922a6..913048735 100644 --- a/lib/__tests__/validation.test.ts +++ b/lib/__tests__/validation.test.ts @@ -1,11 +1,9 @@ -// @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'); @@ -13,45 +11,59 @@ 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', @@ -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', @@ -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', @@ -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); }); }); }); diff --git a/lib/prompts/__tests__/downloadProjectPrompt.test.ts b/lib/prompts/__tests__/downloadProjectPrompt.test.ts index b5ae8e6d9..3e893971b 100644 --- a/lib/prompts/__tests__/downloadProjectPrompt.test.ts +++ b/lib/prompts/__tests__/downloadProjectPrompt.test.ts @@ -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', () => ({ diff --git a/lib/prompts/__tests__/projectsLogsPrompt.test.ts b/lib/prompts/__tests__/projectsLogsPrompt.test.ts index 7acfddef0..ead609007 100644 --- a/lib/prompts/__tests__/projectsLogsPrompt.test.ts +++ b/lib/prompts/__tests__/projectsLogsPrompt.test.ts @@ -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(); });