diff --git a/frontend/packages/console-app/src/components/detect-namespace/__tests__/getValueForNamespace.spec.ts b/frontend/packages/console-app/src/components/detect-namespace/__tests__/getValueForNamespace.spec.ts index 24ddccc6f61..eb7c53dd8e6 100644 --- a/frontend/packages/console-app/src/components/detect-namespace/__tests__/getValueForNamespace.spec.ts +++ b/frontend/packages/console-app/src/components/detect-namespace/__tests__/getValueForNamespace.spec.ts @@ -8,6 +8,7 @@ jest.mock('../checkNamespaceExists', () => ({ const checkNamespaceExistsMock = checkNamespaceExists as jest.Mock; +const activeNamespace: string = 'active-ns'; const preferredNamespace: string = 'preferred-ns'; const lastNamespace: string = 'last-ns'; @@ -16,16 +17,31 @@ describe('getValueForNamespace', () => { jest.resetAllMocks(); }); - it(`should return preferredNamespace if it is defined and exists`, async () => { + it(`should return activeNamespace if it is defined and exists`, async () => { checkNamespaceExistsMock.mockReturnValueOnce(Promise.resolve(true)); + const namespace = await getValueForNamespace( + preferredNamespace, + lastNamespace, + true, + activeNamespace, + ); + + expect(namespace).toEqual(activeNamespace); + }); + + it(`should return preferredNamespace if activeNamespace does not exist and preferredNamespace is defined and exists`, async () => { + checkNamespaceExistsMock + .mockReturnValueOnce(Promise.resolve(false)) + .mockReturnValueOnce(Promise.resolve(true)); const namespace = await getValueForNamespace(preferredNamespace, lastNamespace, true); expect(namespace).toEqual(preferredNamespace); }); - it('should return lastNamespace if preferred namespace does not exist and last namespace is defined and exists', async () => { + it('should return lastNamespace if activeNamespace and preferred namespace does not exist and last namespace is defined and exists', async () => { checkNamespaceExistsMock + .mockReturnValueOnce(Promise.resolve(false)) .mockReturnValueOnce(Promise.resolve(false)) .mockReturnValueOnce(Promise.resolve(true)); @@ -34,8 +50,9 @@ describe('getValueForNamespace', () => { expect(namespace).toEqual(lastNamespace); }); - it(`should return ${ALL_NAMESPACES_KEY} if preferred and last namespace does not exists`, async () => { + it(`should return ${ALL_NAMESPACES_KEY} if activeNamespace, preferred and last namespace does not exists`, async () => { checkNamespaceExistsMock + .mockReturnValueOnce(Promise.resolve(false)) .mockReturnValueOnce(Promise.resolve(false)) .mockReturnValueOnce(Promise.resolve(false)); diff --git a/frontend/packages/console-app/src/components/detect-namespace/getValueForNamespace.ts b/frontend/packages/console-app/src/components/detect-namespace/getValueForNamespace.ts index 60374bb8288..54f520bbe32 100644 --- a/frontend/packages/console-app/src/components/detect-namespace/getValueForNamespace.ts +++ b/frontend/packages/console-app/src/components/detect-namespace/getValueForNamespace.ts @@ -5,7 +5,11 @@ export const getValueForNamespace = async ( preferredNamespace: string, lastNamespace: string, useProjects: boolean, + activeNamespace?: string, ): Promise => { + if (await checkNamespaceExists(activeNamespace, useProjects)) { + return activeNamespace; + } if (await checkNamespaceExists(preferredNamespace, useProjects)) { return preferredNamespace; } diff --git a/frontend/packages/console-app/src/components/detect-namespace/namespace.ts b/frontend/packages/console-app/src/components/detect-namespace/namespace.ts index 9609419e830..89adc00d834 100644 --- a/frontend/packages/console-app/src/components/detect-namespace/namespace.ts +++ b/frontend/packages/console-app/src/components/detect-namespace/namespace.ts @@ -57,7 +57,7 @@ export const useValuesForNamespaceContext: UseValuesForNamespaceContext = () => !flagPending(useProjects) && preferredNamespaceLoaded && lastNamespaceLoaded; React.useEffect(() => { if (!urlNamespace && resourcesLoaded) { - getValueForNamespace(preferredNamespace, lastNamespace, useProjects) + getValueForNamespace(preferredNamespace, lastNamespace, useProjects, activeNamespace) .then((ns: string) => { updateNamespace(ns); })