Skip to content

Commit

Permalink
test: add jest test for OrgList.filterAuthInfo()
Browse files Browse the repository at this point in the history
  • Loading branch information
CristiCanizales committed Aug 22, 2024
1 parent bbfaf36 commit 838ef09
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/salesforcedx-vscode-core/src/orgPicker/orgList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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 = '[email protected]';

const createOrgAuthorization = (
overrides: Partial<OrgAuthorization> = {}
): OrgAuthorization => ({
orgId: '000',
username: '[email protected]',
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: '[email protected]'
} 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: '[email protected]'
} 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: '[email protected]',
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: '[email protected]',
aliases: ['alias1']
});
const orgAuths = [orgAuth];
getAuthFieldsForMock.mockResolvedValueOnce({} as AuthFields);
getDevHubUsernameMock.mockResolvedValueOnce(null);

const result = await orgList.filterAuthInfo(orgAuths);

expect(result).toEqual(['alias1 - [email protected]']);
});

it('should filter out org authorizations with errors', async () => {
const orgAuthWithError = createOrgAuthorization({
username: '[email protected]',
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: '[email protected]'
});
const orgAuths = [validOrgAuth];
getAuthFieldsForMock.mockResolvedValueOnce({} as AuthFields);
getDevHubUsernameMock.mockResolvedValueOnce(dummyDevHubUsername);

const result = await orgList.filterAuthInfo(orgAuths);

expect(result).toEqual(['[email protected]']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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', () => {
Expand Down

0 comments on commit 838ef09

Please sign in to comment.