Skip to content

Resolving an issue when DetailText receives undefined/null value #14652

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2025
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
2 changes: 1 addition & 1 deletion shell/components/DetailText.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default {

value: {
type: String,
default: null,
default: '',
},

maxLength: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { useI18n } from '@shell/composables/useI18n';
import { useStore } from 'vuex';

export interface Row {
key: string;
value: string;
key?: string;
value?: string;
}

export interface Props {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import DetailText from '@shell/components/DetailText.vue';

export interface Props {
username: string;
password: string;
username?: string;
password?: string;
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import DetailText from '@shell/components/DetailText.vue';

export interface Props {
crt: string;
token: string;
crt?: string;
token?: string;
}
</script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import DetailText from '@shell/components/DetailText.vue';

export interface Props {
username: string;
password: string;
username?: string;
password?: string;
}
</script>

Expand Down
63 changes: 63 additions & 0 deletions shell/composables/resources.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { useResourceIdentifiers, useFetchResourceWithId } from '@shell/composables/resources';

const mockStore: any = {
getters: { 'cluster/schemaFor': jest.fn(() => ({ id: 'test-schema' })) },
dispatch: jest.fn()
};
const mockRoute: any = {
params: {
cluster: 'test-cluster',
namespace: 'test-namespace',
id: 'test-id',
},
};

jest.mock('vuex', () => ({ useStore: () => mockStore }));
jest.mock('vue-router', () => ({ useRoute: () => mockRoute }));

describe('resources', () => {
describe('useResourceIdentifiers', () => {
it('should return the correct value for a route with a namespace', async() => {
const { clusterId, id, schema } = useResourceIdentifiers('test-type');

expect(clusterId).toBe('test-cluster');
expect(id).toBe('test-namespace/test-id');
expect(schema).toStrictEqual({ id: 'test-schema' });
});

it('should return the correct value for a route without a namespace', async() => {
mockRoute.params.namespace = undefined;
const { clusterId, id, schema } = useResourceIdentifiers('test-type');

expect(clusterId).toBe('test-cluster');
expect(id).toBe('test-id');
expect(schema).toStrictEqual({ id: 'test-schema' });
});
});

describe('useFetchResourceWithId', () => {
it('should return the correct value given the correct arguments are passed to the dispatch method', async() => {
mockStore.dispatch.mockImplementation(() => Promise.resolve({ id: 'test-resource' }));
const resource = await useFetchResourceWithId('test-type', 'test-id');

expect(mockStore.dispatch).toHaveBeenCalledWith('cluster/find', { type: 'test-type', id: 'test-id' });
expect(resource).toStrictEqual({ id: 'test-resource' });
});

it('should dispatch a loadingError if a 404 occurs', async() => {
// eslint-disable-next-line prefer-promise-reject-errors
mockStore.dispatch.mockImplementationOnce(() => Promise.reject({ status: 404 }));
await useFetchResourceWithId('test-type', 'test-id');

expect(mockStore.dispatch).toHaveBeenCalledWith('loadingError', new Error('nav.failWhale.resourceIdNotFound-{"resource":"test-type","fqid":"test-id"}'));
});

it('should dispatch a loadingError if a 403 occurs', async() => {
// eslint-disable-next-line prefer-promise-reject-errors
mockStore.dispatch.mockImplementationOnce(() => Promise.reject({ status: 403 }));
await useFetchResourceWithId('test-type', 'test-id');

expect(mockStore.dispatch).toHaveBeenCalledWith('loadingError', new Error('nav.failWhale.resourceIdNotFound-{"resource":"test-type","fqid":"test-id"}'));
});
});
});
10 changes: 9 additions & 1 deletion shell/composables/resources.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useI18n } from '@shell/composables/useI18n';
import { computed, Ref, toValue } from 'vue';
import { useRoute } from 'vue-router';
import { useStore } from 'vuex';
Expand All @@ -22,9 +23,16 @@ export const useResourceIdentifiers = (type: ResourceType) => {

export const useFetchResourceWithId = async(type: ResourceType, id: IdType) => {
const store = useStore();
const i18n = useI18n(store);

const typeValue = toValue(type);
const idValue = toValue(id);

return await store.dispatch('cluster/find', { type: typeValue, id: idValue });
try {
return await store.dispatch('cluster/find', { type: typeValue, id: idValue });
} catch (ex: any) {
if (ex.status === 404 || ex.status === 403) {
store.dispatch('loadingError', new Error(i18n.t('nav.failWhale.resourceIdNotFound', { resource: type, fqid: id }, true)));
}
}
};