-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add jest test for OrgList.filterAuthInfo()
- Loading branch information
1 parent
bbfaf36
commit 838ef09
Showing
3 changed files
with
145 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
packages/salesforcedx-vscode-core/test/jest/orgPicker/orgPicker.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters