Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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));

Expand All @@ -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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ export const getValueForNamespace = async (
preferredNamespace: string,
lastNamespace: string,
useProjects: boolean,
activeNamespace?: string,
): Promise<string> => {
if (await checkNamespaceExists(activeNamespace, useProjects)) {
return activeNamespace;
}
if (await checkNamespaceExists(preferredNamespace, useProjects)) {
return preferredNamespace;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand Down