From 838ef09a3b3b69ed7019ebe37fdc023594d942fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristina=20Isabel=20Ca=C3=B1izales?= Date: Thu, 22 Aug 2024 19:37:21 -0300 Subject: [PATCH] test: add jest test for OrgList.filterAuthInfo() --- .../src/orgPicker/orgList.ts | 2 +- .../test/jest/orgPicker/orgPicker.test.ts | 144 ++++++++++++++++++ .../orgPicker/orgList.test.ts | 2 - 3 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 packages/salesforcedx-vscode-core/test/jest/orgPicker/orgPicker.test.ts diff --git a/packages/salesforcedx-vscode-core/src/orgPicker/orgList.ts b/packages/salesforcedx-vscode-core/src/orgPicker/orgList.ts index 258024fa6b..e5a9d2ee68 100644 --- a/packages/salesforcedx-vscode-core/src/orgPicker/orgList.ts +++ b/packages/salesforcedx-vscode-core/src/orgPicker/orgList.ts @@ -97,7 +97,7 @@ export class OrgList implements vscode.Disposable { } const aliases = orgAuth.aliases || []; const authListItem = - aliases?.length > 0 + aliases.length > 0 ? `${aliases.join(',')} - ${orgAuth.username}` : orgAuth.username; diff --git a/packages/salesforcedx-vscode-core/test/jest/orgPicker/orgPicker.test.ts b/packages/salesforcedx-vscode-core/test/jest/orgPicker/orgPicker.test.ts new file mode 100644 index 0000000000..4f652cdc86 --- /dev/null +++ b/packages/salesforcedx-vscode-core/test/jest/orgPicker/orgPicker.test.ts @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2024, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ +import { AuthFields, OrgAuthorization } from '@salesforce/core-bundle'; +import * as vscode from 'vscode'; +import { OrgList } from '../../../src/orgPicker'; +import { OrgAuthInfo } from '../../../src/util'; + +describe('OrgList - filterAuthInfo', () => { + let orgList: OrgList; + let createFileSystemWatcherMock: jest.SpyInstance; + let getDevHubUsernameMock: jest.SpyInstance; + let getAuthFieldsForMock: jest.SpyInstance; + let mockWatcher: any; + + const dummyDevHubUsername = 'test-devhub@example.com'; + + const createOrgAuthorization = ( + overrides: Partial = {} + ): OrgAuthorization => ({ + orgId: '000', + username: 'test-username@example.com', + oauthMethod: 'unknown', + aliases: [], + configs: [], + isScratchOrg: undefined, + isDevHub: undefined, + isSandbox: undefined, + instanceUrl: undefined, + accessToken: undefined, + error: undefined, + isExpired: false, + ...overrides + }); + + beforeEach(() => { + mockWatcher = { + onDidChange: jest.fn(), + onDidCreate: jest.fn(), + onDidDelete: jest.fn() + }; + createFileSystemWatcherMock = ( + vscode.workspace.createFileSystemWatcher as any + ).mockReturnValue(mockWatcher); + (vscode.window.createStatusBarItem as jest.Mock).mockReturnValue({ + command: '', + text: '', + tooltip: '', + show: jest.fn(), + dispose: jest.fn() + }); + orgList = new OrgList(); + getAuthFieldsForMock = jest.spyOn(OrgList.prototype, 'getAuthFieldsFor'); + getDevHubUsernameMock = jest.spyOn(OrgAuthInfo, 'getDevHubUsername'); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should filter out orgs with the scratchAdminUsername field', async () => { + const orgAuth = createOrgAuthorization(); + const orgAuths = [orgAuth]; + getAuthFieldsForMock.mockResolvedValueOnce({ + scratchAdminUsername: 'admin@example.com' + } as AuthFields); + getDevHubUsernameMock.mockResolvedValueOnce(null); + + const result = await orgList.filterAuthInfo(orgAuths); + + expect(result).toEqual([]); + }); + + it('should filter out scratch orgs parented by non-default Dev Hubs', async () => { + const orgAuth = createOrgAuthorization({ isScratchOrg: true }); + const orgAuths = [orgAuth]; + getAuthFieldsForMock.mockResolvedValueOnce({ + devHubUsername: 'other-devhub@example.com' + } as AuthFields); + getDevHubUsernameMock.mockResolvedValueOnce(dummyDevHubUsername); + + const result = await orgList.filterAuthInfo(orgAuths); + + expect(result).toEqual([]); + }); + + it('should filter out expired orgs', async () => { + const expiredOrgAuth = createOrgAuthorization({ + username: 'expired-org@example.com', + isExpired: true + }); + const orgAuths = [expiredOrgAuth]; + getAuthFieldsForMock.mockResolvedValueOnce({} as AuthFields); + getDevHubUsernameMock.mockResolvedValueOnce(dummyDevHubUsername); + + const result = await orgList.filterAuthInfo(orgAuths); + + expect(result).toEqual([]); + }); + + it('should include aliases in the result if available', async () => { + const orgAuth = createOrgAuthorization({ + username: 'test-username@example.com', + aliases: ['alias1'] + }); + const orgAuths = [orgAuth]; + getAuthFieldsForMock.mockResolvedValueOnce({} as AuthFields); + getDevHubUsernameMock.mockResolvedValueOnce(null); + + const result = await orgList.filterAuthInfo(orgAuths); + + expect(result).toEqual(['alias1 - test-username@example.com']); + }); + + it('should filter out org authorizations with errors', async () => { + const orgAuthWithError = createOrgAuthorization({ + username: 'error-org@example.com', + error: 'Some error' + }); + const orgAuths = [orgAuthWithError]; + getAuthFieldsForMock.mockResolvedValueOnce({} as AuthFields); + getDevHubUsernameMock.mockResolvedValueOnce(null); + + const result = await orgList.filterAuthInfo(orgAuths); + + expect(result).toEqual([]); + }); + + it('should return a list of valid org authorizations', async () => { + const validOrgAuth = createOrgAuthorization({ + username: 'valid-org@example.com' + }); + const orgAuths = [validOrgAuth]; + getAuthFieldsForMock.mockResolvedValueOnce({} as AuthFields); + getDevHubUsernameMock.mockResolvedValueOnce(dummyDevHubUsername); + + const result = await orgList.filterAuthInfo(orgAuths); + + expect(result).toEqual(['valid-org@example.com']); + }); +}); diff --git a/packages/salesforcedx-vscode-core/test/vscode-integration/orgPicker/orgList.test.ts b/packages/salesforcedx-vscode-core/test/vscode-integration/orgPicker/orgList.test.ts index 589d912bff..fa7ed98811 100644 --- a/packages/salesforcedx-vscode-core/test/vscode-integration/orgPicker/orgList.test.ts +++ b/packages/salesforcedx-vscode-core/test/vscode-integration/orgPicker/orgList.test.ts @@ -9,7 +9,6 @@ import { OrgAuthorization, StateAggregator } from '@salesforce/core-bundle'; -import { ConfigUtil } from '@salesforce/salesforcedx-utils-vscode'; import { expect } from 'chai'; import { createSandbox, SinonStub } from 'sinon'; import * as vscode from 'vscode'; @@ -18,7 +17,6 @@ import { OrgList } from '../../../src/orgPicker'; import * as util from '../../../src/util'; import { OrgAuthInfo } from '../../../src/util'; -const AN_ALIAS = 'anAlias'; const sandbox = createSandbox(); describe('orgList Tests', () => {