DatasourceRef: keep uid/type ref when using template variables (#41785)

This commit is contained in:
Ryan McKinley
2021-11-16 22:55:12 -08:00
committed by GitHub
parent b4faee786f
commit bce7cb3ebf
6 changed files with 23 additions and 4 deletions

View File

@@ -30,7 +30,7 @@ class TestDataSource {
constructor(public instanceSettings: DataSourceInstanceSettings) {}
}
jest.mock('../plugin_loader', () => ({
jest.mock('./plugin_loader', () => ({
importDataSourcePlugin: (meta: DataSourcePluginMeta) => {
return Promise.resolve(new DataSourcePlugin(TestDataSource as any));
},
@@ -128,13 +128,21 @@ describe('datasource_srv', () => {
describe('when getting instance settings', () => {
it('should work by name or uid', () => {
expect(dataSourceSrv.getInstanceSettings('mmm')).toBe(dataSourceSrv.getInstanceSettings('uid-code-mmm'));
const ds = dataSourceSrv.getInstanceSettings('mmm');
expect(dataSourceSrv.getInstanceSettings('uid-code-mmm')).toBe(ds);
expect(dataSourceSrv.getInstanceSettings({ uid: 'uid-code-mmm' })).toBe(ds);
});
it('should work with variable', () => {
const ds = dataSourceSrv.getInstanceSettings('${datasource}');
expect(ds?.name).toBe('${datasource}');
expect(ds?.uid).toBe('${datasource}');
expect(ds?.rawRef).toMatchInlineSnapshot(`
Object {
"type": "test-db",
"uid": "uid-code-BBB",
}
`);
});
it('should not set isDefault when being fetched via variable', () => {
@@ -146,6 +154,12 @@ describe('datasource_srv', () => {
const ds = dataSourceSrv.getInstanceSettings('${datasourceDefault}');
expect(ds?.name).toBe('${datasourceDefault}');
expect(ds?.uid).toBe('${datasourceDefault}');
expect(ds?.rawRef).toMatchInlineSnapshot(`
Object {
"type": "test-db",
"uid": "uid-code-BBB",
}
`);
});
});

View File

@@ -101,6 +101,7 @@ export class DatasourceSrv implements DataSourceService {
isDefault: false,
name: nameOrUid,
uid: nameOrUid,
rawRef: { type: dsSettings.type, uid: dsSettings.uid },
};
}