Skip to content

Commit

Permalink
chore: core API to access the sfdx project namespace. (#5253)
Browse files Browse the repository at this point in the history
* chore: add get sfdx namespace function in workspacecontext
  • Loading branch information
floralan authored Dec 5, 2023
1 parent 2f76a15 commit 1ac6bda
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from '@salesforce/salesforcedx-utils-vscode';
import * as vscode from 'vscode';
import { decorators } from '../decorators';
import { SfdxProjectConfig } from '../sfdxProject';
import { workspaceContextUtils } from '.';

/**
Expand Down Expand Up @@ -43,6 +44,11 @@ export class WorkspaceContext {
return await WorkspaceContextUtil.getInstance().getConnection();
}

public async getSfdxNamespace(): Promise<string | undefined> {
const namespace = await SfdxProjectConfig.getValue('namespace');
return typeof namespace === 'string' ? namespace : undefined;
}

protected async handleCliConfigChange(orgInfo: OrgUserInfo) {
await workspaceContextUtils
.setupWorkspaceOrgType(orgInfo.username)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { WorkspaceContextUtil } from '@salesforce/salesforcedx-utils-vscode';
import { WorkspaceContext, workspaceContextUtils } from '../../../src/context';
import { decorators } from '../../../src/decorators';
import { SfdxProjectConfig } from '../../../src/sfdxProject';

describe('workspaceContext', () => {
describe('handleCliConfigChange', () => {
Expand Down Expand Up @@ -63,4 +64,29 @@ describe('workspaceContext', () => {
expect(orgId).not.toBeNull();
});
});

describe('getSfdxNamespace', () => {
it('should get the namespace from SfdxProjectConfig', async () => {
const dummyNamespace = 'dummyNamespace';
const getValueMock = jest
.spyOn(SfdxProjectConfig, 'getValue')
.mockResolvedValue(dummyNamespace);

const namespace = await WorkspaceContext.getInstance().getSfdxNamespace();

expect(getValueMock).toHaveBeenCalledWith('namespace');
expect(namespace).toEqual(dummyNamespace);
});

it('should return undefined if namespace is not a string', async () => {
const getValueMock = jest
.spyOn(SfdxProjectConfig, 'getValue')
.mockResolvedValue(undefined);

const namespace = await WorkspaceContext.getInstance().getSfdxNamespace();

expect(getValueMock).toHaveBeenCalledWith('namespace');
expect(namespace).toBeUndefined();
});
});
});

0 comments on commit 1ac6bda

Please sign in to comment.