add tests for api urls (#54116)

Create function for generating api urls and add tests
This commit is contained in:
Jeff Levin
2022-08-24 06:51:35 -08:00
committed by GitHub
parent 736d035c65
commit f5b99e5ca7
2 changed files with 27 additions and 4 deletions

View File

@@ -5,6 +5,8 @@ import {
dashboardHasTemplateVariables, dashboardHasTemplateVariables,
generatePublicDashboardUrl, generatePublicDashboardUrl,
publicDashboardPersisted, publicDashboardPersisted,
getPublicDashboardConfigUrl,
savePublicDashboardConfigUrl,
} from './SharePublicDashboardUtils'; } from './SharePublicDashboardUtils';
describe('dashboardHasTemplateVariables', () => { describe('dashboardHasTemplateVariables', () => {
@@ -40,3 +42,15 @@ describe('publicDashboardPersisted', () => {
expect(publicDashboardPersisted(pubdash)).toBe(false); expect(publicDashboardPersisted(pubdash)).toBe(false);
}); });
}); });
describe('getPublicDashboardConfigUrl', () => {
it('builds the correct url', () => {
expect(getPublicDashboardConfigUrl('abc1234')).toEqual('/api/dashboards/uid/abc1234/public-config');
});
});
describe('savePublicDashboardConfigUrl', () => {
it('builds the correct url', () => {
expect(savePublicDashboardConfigUrl('abc1234')).toEqual('/api/dashboards/uid/abc1234/public-config');
});
});

View File

@@ -22,8 +22,7 @@ export const getPublicDashboardConfig = async (
dashboardUid: string, dashboardUid: string,
setPublicDashboard: React.Dispatch<React.SetStateAction<PublicDashboard>> setPublicDashboard: React.Dispatch<React.SetStateAction<PublicDashboard>>
) => { ) => {
const url = `/api/dashboards/uid/${dashboardUid}/public-config`; const pdResp: PublicDashboard = await getBackendSrv().get(getPublicDashboardConfigUrl(dashboardUid));
const pdResp: PublicDashboard = await getBackendSrv().get(url);
setPublicDashboard(pdResp); setPublicDashboard(pdResp);
}; };
@@ -32,8 +31,10 @@ export const savePublicDashboardConfig = async (
publicDashboardConfig: PublicDashboard, publicDashboardConfig: PublicDashboard,
setPublicDashboard: React.Dispatch<React.SetStateAction<PublicDashboard>> setPublicDashboard: React.Dispatch<React.SetStateAction<PublicDashboard>>
) => { ) => {
const url = `/api/dashboards/uid/${dashboardUid}/public-config`; const pdResp: PublicDashboard = await getBackendSrv().post(
const pdResp: PublicDashboard = await getBackendSrv().post(url, publicDashboardConfig); savePublicDashboardConfigUrl(dashboardUid),
publicDashboardConfig
);
// Never allow a user to send the orgId // Never allow a user to send the orgId
// @ts-ignore // @ts-ignore
@@ -43,6 +44,14 @@ export const savePublicDashboardConfig = async (
setPublicDashboard(pdResp); setPublicDashboard(pdResp);
}; };
export const getPublicDashboardConfigUrl = (dashboardUid: string) => {
return `/api/dashboards/uid/${dashboardUid}/public-config`;
};
export const savePublicDashboardConfigUrl = (dashboardUid: string) => {
return `/api/dashboards/uid/${dashboardUid}/public-config`;
};
// Instance methods // Instance methods
export const dashboardHasTemplateVariables = (variables: VariableModel[]): boolean => { export const dashboardHasTemplateVariables = (variables: VariableModel[]): boolean => {
return variables.length > 0; return variables.length > 0;